Skip to content
Merged
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
26 changes: 17 additions & 9 deletions pm-stack/projector/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,26 @@ async function importRsaKey(der: Uint8Array): Promise<CryptoKey> {
}
}

function parsePositiveDecimalId(raw: string, envName: string): number {
if (!/^[0-9]+$/.test(raw)) {
throw new Error(`Invalid ${envName} ${JSON.stringify(raw)}; expected a positive decimal integer`);
}
const value = Number(raw);
if (!Number.isSafeInteger(value) || value <= 0) {
throw new Error(`Invalid ${envName} ${JSON.stringify(raw)}; expected a positive decimal integer`);
}
return value;
}

export async function appJwt(env: Record<string, string | undefined>): Promise<string> {
const appId = env.GITHUB_APP_ID;
const rawKey = env.GITHUB_APP_PRIVATE_KEY;
if (!appId || !rawKey) throw new Error("github_app_credentials_missing");
// Validate the app id's shape before signing: a mistyped id otherwise
// produces a correctly signed JWT that GitHub rejects downstream, and the
// failure surfaces as a misleading credentials problem. Same rule as
// GITHUB_APP_INSTALL_ID (parseConfiguredInstallId).
parsePositiveDecimalId(appId, "GITHUB_APP_ID");
const b64 = decodePem(rawKey);
const der = Uint8Array.from(Buffer.from(b64, "base64"));
const key = await importRsaKey(der);
Expand All @@ -91,15 +107,7 @@ const discoveredInstallations = new Map<string, number>();

function parseConfiguredInstallId(raw: string | undefined): number | undefined {
if (raw === undefined || raw === "") return undefined;

if (!/^[0-9]+$/.test(raw)) {
throw new Error(`Invalid GITHUB_APP_INSTALL_ID ${JSON.stringify(raw)}; expected a positive decimal integer`);
}
const installId = Number(raw);
if (!Number.isSafeInteger(installId) || installId <= 0) {
throw new Error(`Invalid GITHUB_APP_INSTALL_ID ${JSON.stringify(raw)}; expected a positive decimal integer`);
}
return installId;
return parsePositiveDecimalId(raw, "GITHUB_APP_INSTALL_ID");
}

/**
Expand Down
28 changes: 28 additions & 0 deletions pm-stack/projector/test/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ describe("appJwt key formats", () => {
});
});

describe("appJwt GITHUB_APP_ID validation", () => {
test("rejects app ids that are not positive decimal integers", async () => {
for (const raw of ["abc", "0", "-1", "12.5", " 123 ", "9007199254740992", "123456789012345678901234"]) {
const result = await appJwt({ GITHUB_APP_ID: raw, GITHUB_APP_PRIVATE_KEY: testKeyB64("pkcs8") }).catch(
(error: unknown) => error,
);
expect(result).toBeInstanceOf(Error);
expect((result as Error).message).toContain("GITHUB_APP_ID");
expect((result as Error).message).toContain(JSON.stringify(raw));
}
});

test("a valid app id keeps issuing a JWT with the identical iss claim", async () => {
const jwt = await appJwt({ GITHUB_APP_ID: "123", GITHUB_APP_PRIVATE_KEY: testKeyB64("pkcs8") });
const payload = JSON.parse(Buffer.from(jwt.split(".")[1], "base64url").toString("utf8")) as { iss?: string };
expect(payload.iss).toBe("123");
});

test("missing or empty credentials keep github_app_credentials_missing", async () => {
const key = testKeyB64("pkcs8");
for (const env of [{}, { GITHUB_APP_ID: "", GITHUB_APP_PRIVATE_KEY: key }, { GITHUB_APP_ID: "123" }]) {
const result = await appJwt(env).catch((error: unknown) => error);
expect(result).toBeInstanceOf(Error);
expect((result as Error).message).toBe("github_app_credentials_missing");
}
});
});

describe("listResource pagination", () => {
test("follows the Link header's absolute next URL as-is", async () => {
const calls: string[] = [];
Expand Down