Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function verifyGitHubSignature(rawBody: string, signatureHeader: st
export function timingSafeEqualHex(left: string, right: string): boolean {
const leftBytes = hexToBytes(left);
const rightBytes = hexToBytes(right);
if (leftBytes === null || rightBytes === null) return false;
if (leftBytes.length !== rightBytes.length) return false;
let result = 0;
for (let index = 0; index < leftBytes.length; index += 1) {
Expand All @@ -32,11 +33,13 @@ export function timingSafeEqualHex(left: string, right: string): boolean {
return result === 0;
}

function hexToBytes(hex: string): Uint8Array {
if (!/^[0-9a-f]+$/i.test(hex) || hex.length % 2 !== 0) return new Uint8Array();
function hexToBytes(hex: string): Uint8Array | null {
if (hex.length === 0 || hex.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(hex)) return null;
const bytes = new Uint8Array(hex.length / 2);
for (let index = 0; index < bytes.length; index += 1) {
bytes[index] = Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16);
const byte = Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16);
if (Number.isNaN(byte)) return null;
bytes[index] = byte;
}
return bytes;
}
Expand Down
36 changes: 35 additions & 1 deletion test/unit/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
import { describe, expect, it } from "vitest";

Check notice on line 1 in test/unit/crypto.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Issue discovery is disabled for this repo

This repo is configured for direct contribution review rather than issue-discovery flow.

Check notice on line 1 in test/unit/crypto.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Open PR queue is busy

This repo has a busy open PR queue in the local Gittensory cache.
import { createOpaqueToken, hashToken, timingSafeEqual } from "../../src/auth/security";
import { verifyGitHubSignature } from "../../src/utils/crypto";
import { base64UrlEncode, sha256Hex, timingSafeEqualHex, verifyGitHubSignature } from "../../src/utils/crypto";

describe("crypto helpers", () => {
it("hashes input with sha256Hex", async () => {
const digest = await sha256Hex("gittensory");
expect(digest).toMatch(/^[0-9a-f]{64}$/);
expect(digest).toBe(await sha256Hex("gittensory"));
expect(digest).not.toBe(await sha256Hex("gittensory-x"));
});

it("compares hex strings in constant time and rejects malformed input", () => {
expect(timingSafeEqualHex("ab12", "ab12")).toBe(true);
expect(timingSafeEqualHex("AB12", "ab12")).toBe(true);
expect(timingSafeEqualHex("ab12", "ab13")).toBe(false);
expect(timingSafeEqualHex("ab12", "ab1234")).toBe(false);
expect(timingSafeEqualHex("zz", "00")).toBe(false);
expect(timingSafeEqualHex("gg", "gg")).toBe(false);
expect(timingSafeEqualHex("abc", "def")).toBe(false);
expect(timingSafeEqualHex("", "00")).toBe(false);
expect(timingSafeEqualHex("00", "")).toBe(false);
expect(timingSafeEqualHex("0x12", "0012")).toBe(false);
expect(timingSafeEqualHex("12gh", "12gh")).toBe(false);
});

it("base64url-encodes strings and byte arrays without padding", () => {
expect(base64UrlEncode("hello")).toBe("aGVsbG8");
expect(base64UrlEncode(new Uint8Array([255, 254]))).toBe("__4");
expect(base64UrlEncode("subjects?_d")).toBe(base64UrlEncode("subjects?_d"));
});
});

describe("webhook signature verification", () => {
it("accepts valid GitHub HMAC signatures and rejects tampering", async () => {
Expand All @@ -17,6 +46,11 @@
await expect(verifyGitHubSignature(body, null, secret)).resolves.toBe(false);
await expect(verifyGitHubSignature(body, "bad-prefix", secret)).resolves.toBe(false);
await expect(verifyGitHubSignature(body, `sha256=${signature}`, "")).resolves.toBe(false);
await expect(verifyGitHubSignature(body, "sha256=abc", secret)).resolves.toBe(false);
await expect(verifyGitHubSignature(body, "sha256=zz", secret)).resolves.toBe(false);
await expect(
verifyGitHubSignature(body, `sha256=${"a".repeat(63)}`, secret),
).resolves.toBe(false);
});

it("uses timing-safe token comparisons and one-way token hashes", async () => {
Expand Down
Loading