From 5ff2338b489a4768768b87fefdb12853e4e0d385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=85=E8=86=80?= Date: Fri, 23 Oct 2026 23:22:43 +0800 Subject: [PATCH] fix(github): resolve immutable member ids A login can be renamed and reclaimed before its first sign-in. Resolve the current immutable user id when membership is granted so a future holder of the login cannot inherit access. --- server/lib/github/oauth.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/server/lib/github/oauth.ts b/server/lib/github/oauth.ts index 1305f1f..69a37a7 100644 --- a/server/lib/github/oauth.ts +++ b/server/lib/github/oauth.ts @@ -177,6 +177,31 @@ function mapGitHubError(err: unknown): Error { return err instanceof Error ? err : new Error(String(err)); } +/** + * The immutable user id GitHub has for `login` right now, or null when it is + * unknown (no such user, no answer). Looked up with the caller's own token. + */ +export async function resolveGitHubUserId(token: string, login: string): Promise { + try { + const res = await fetch(`https://api.github.com/users/${encodeURIComponent(login)}`, { + headers: { + Authorization: `Bearer ${token}`, + Accept: "application/vnd.github+json", + "User-Agent": "WebHooker", + }, + redirect: "error", + signal: AbortSignal.timeout(5000), + }); + if (!res.ok) return null; + const data = (await res.json()) as { id?: unknown; login?: unknown }; + const sameLogin = + typeof data.login === "string" && data.login.toLowerCase() === login.toLowerCase(); + return sameLogin && typeof data.id === "number" ? String(data.id) : null; + } catch { + return null; + } +} + async function requireOctokit(kv: KVNamespace, githubUserId: string): Promise { const octokit = await getUserOctokit(githubUserId, kv); if (!octokit) throw new Error("GITHUB_TOKEN_EXPIRED");