From 2891e1f33cbb424e6dd8a9624326f564d4874ca1 Mon Sep 17 00:00:00 2001 From: RhenCloud Date: Sat, 26 Sep 2026 16:02:27 +0800 Subject: [PATCH] fix(github): send User-Agent header on GitHub API requests GitHub rejects API requests without a User-Agent (403), and Workers' fetch does not set one by default, so workflow job enrichment and check-suite build-log lookups silently failed in production. --- server/lib/github/app-token.ts | 2 ++ server/lib/github/check-run.ts | 3 ++- server/lib/github/workflow-jobs.ts | 3 ++- tests/github-workflow-jobs.test.ts | 23 +++++++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/server/lib/github/app-token.ts b/server/lib/github/app-token.ts index f473ad2..fc1ccd7 100644 --- a/server/lib/github/app-token.ts +++ b/server/lib/github/app-token.ts @@ -1,6 +1,7 @@ import { createAppJwt } from "./oauth"; export const GITHUB_API_VERSION = "2022-11-28"; +export const GITHUB_USER_AGENT = "WebHooker (https://github.com/ReCloudStudio/WebHooker)"; /** * Mint a short-lived installation access token for a GitHub App installation. @@ -23,6 +24,7 @@ export async function getInstallationToken( Authorization: `Bearer ${jwt}`, Accept: "application/vnd.github+json", "X-GitHub-Api-Version": GITHUB_API_VERSION, + "User-Agent": GITHUB_USER_AGENT, }, }, ); diff --git a/server/lib/github/check-run.ts b/server/lib/github/check-run.ts index 8b1cdba..bca6210 100644 --- a/server/lib/github/check-run.ts +++ b/server/lib/github/check-run.ts @@ -1,4 +1,4 @@ -import { getInstallationToken, GITHUB_API_VERSION } from "./app-token"; +import { getInstallationToken, GITHUB_API_VERSION, GITHUB_USER_AGENT } from "./app-token"; const CLOUDFLARE_SLUG = "cloudflare-workers-and-pages"; @@ -28,6 +28,7 @@ export async function getCheckSuiteBuildLogUrl( Authorization: `Bearer ${token}`, Accept: "application/vnd.github+json", "X-GitHub-Api-Version": GITHUB_API_VERSION, + "User-Agent": GITHUB_USER_AGENT, }, }); if (!runsRes.ok) return undefined; diff --git a/server/lib/github/workflow-jobs.ts b/server/lib/github/workflow-jobs.ts index 87a01ad..a1f4ba3 100644 --- a/server/lib/github/workflow-jobs.ts +++ b/server/lib/github/workflow-jobs.ts @@ -1,4 +1,4 @@ -import { getInstallationToken, GITHUB_API_VERSION } from "./app-token"; +import { getInstallationToken, GITHUB_API_VERSION, GITHUB_USER_AGENT } from "./app-token"; export interface WorkflowJob { name?: string; @@ -32,6 +32,7 @@ export async function getWorkflowRunJobs( const headers: Record = { Accept: "application/vnd.github+json", "X-GitHub-Api-Version": GITHUB_API_VERSION, + "User-Agent": GITHUB_USER_AGENT, }; if (token) headers.Authorization = `Bearer ${token}`; const res = await fetch(url.toString(), { headers }); diff --git a/tests/github-workflow-jobs.test.ts b/tests/github-workflow-jobs.test.ts index d80bcf3..69a5e99 100644 --- a/tests/github-workflow-jobs.test.ts +++ b/tests/github-workflow-jobs.test.ts @@ -99,4 +99,27 @@ describe("getWorkflowRunJobs", () => { globalThis.fetch = (() => Promise.reject(new Error("network"))) as typeof fetch; expect(await getWorkflowRunJobs(JOBS_URL, undefined, undefined, undefined)).toBeUndefined(); }); + + it("always sends a User-Agent header (GitHub rejects requests without one)", async () => { + let sawUA: string | undefined; + let tokenUA: string | undefined; + const { appId, pem } = await makeAppKeyPair(); + mockFetch((url, init) => { + const headers = (init?.headers ?? {}) as Record; + if (url.includes("/access_tokens")) tokenUA = headers["User-Agent"]; + else sawUA = headers["User-Agent"]; + return new Response( + JSON.stringify( + url.includes("/access_tokens") + ? { token: "ghs_test" } + : { jobs: [{ name: "build" }] }, + ), + { status: 200 }, + ); + }); + + expect(await getWorkflowRunJobs(JOBS_URL, appId, pem, 555)).toBeDefined(); + expect(tokenUA).toBeTruthy(); + expect(sawUA).toBeTruthy(); + }); });