diff --git a/server/lib/github/oauth.ts b/server/lib/github/oauth.ts index e66c729..1305f1f 100644 --- a/server/lib/github/oauth.ts +++ b/server/lib/github/oauth.ts @@ -2,7 +2,8 @@ import { Octokit } from "octokit"; import { saveToken, getToken } from "./store"; export function getOAuthURL(clientId: string, state: string): string { - return `https://github.com/login/oauth/authorize?client_id=${clientId}&scope=repo&state=${state}`; + // `read:org` lets the install flow check organization admin rights. + return `https://github.com/login/oauth/authorize?client_id=${clientId}&scope=${encodeURIComponent("repo read:org")}&state=${state}`; } function b64url(buf: ArrayBuffer | string): string { @@ -40,31 +41,82 @@ export async function createAppJwt(appId: string, privateKey: string): Promise { + if (!appId || !privateKey) return null; + try { + const jwt = await createAppJwt(appId, privateKey); + const res = await fetch(`https://api.github.com/app/installations/${installationId}`, { + headers: { ...API_HEADERS, Authorization: `Bearer ${jwt}` }, + }); + if (!res.ok) return null; + const data = (await res.json()) as { + account?: { login?: string; id?: number; type?: string }; + }; + if (!data.account?.login) return null; + // An account without an id can still be named, but never matches a user. + return { login: data.account.login, id: data.account.id ?? -1, type: data.account.type ?? "" }; + } catch { + return null; + } +} + +/** The login of the account owning an installation (null when unknown). */ export async function getInstallationAccount( appId: string, privateKey: string, installationId: number, ): Promise { - if (!appId || !privateKey) return null; + return (await getInstallationDetails(appId, privateKey, installationId))?.login ?? null; +} + +/** + * Whether a GitHub user may administer an App installation. The Setup URL's + * `installation_id` is attacker-controlled, so binding it to a group requires + * proof from the user's own token: the user is the installation's account, or + * an active admin of its organization. Merely having access to it (being + * listed in `/user/installations` as a collaborator) is not enough. + */ +export async function userManagesInstallation( + token: string, + userId: string, + _installationId: number, + account: InstallationAccount | null, +): Promise { + const headers = { ...API_HEADERS, Authorization: `Bearer ${token}` }; try { - const jwt = await createAppJwt(appId, privateKey); - const res = await fetch(`https://api.github.com/app/installations/${installationId}`, { - headers: { - Authorization: `Bearer ${jwt}`, - Accept: "application/vnd.github+json", - "X-GitHub-Api-Version": "2022-11-28", - }, - }); - if (!res.ok) return null; - const data = (await res.json()) as { account?: { login?: string } }; - return data.account?.login ?? null; + if (!account) return false; + if (account.type === "User") return String(account.id) === userId; + const res = await fetch( + `https://api.github.com/user/memberships/orgs/${encodeURIComponent(account.login)}`, + { headers }, + ); + if (!res.ok) return false; + const membership = (await res.json()) as { state?: string; role?: string }; + return membership.state === "active" && membership.role === "admin"; } catch { - return null; + return false; } }