From 28fc2c6cab3b22c4a3b206d1d0ae926c14689013 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 10:13:53 -0600 Subject: [PATCH] fix(projector): validate GITHUB_APP_ID in appJwt like GITHUB_APP_INSTALL_ID (#4139) --- pm-stack/projector/src/github.ts | 26 +++++++++++++++--------- pm-stack/projector/test/github.test.ts | 28 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/pm-stack/projector/src/github.ts b/pm-stack/projector/src/github.ts index f880f8fbe9..1a1782b337 100644 --- a/pm-stack/projector/src/github.ts +++ b/pm-stack/projector/src/github.ts @@ -67,10 +67,26 @@ async function importRsaKey(der: Uint8Array): Promise { } } +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): Promise { 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); @@ -91,15 +107,7 @@ const discoveredInstallations = new Map(); 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"); } /** diff --git a/pm-stack/projector/test/github.test.ts b/pm-stack/projector/test/github.test.ts index 4bf6bd1f61..5c819934d7 100644 --- a/pm-stack/projector/test/github.test.ts +++ b/pm-stack/projector/test/github.test.ts @@ -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[] = [];