diff --git a/README.md b/README.md index c2cb3af..cc46488 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,6 @@ npm i **2. Configure GitHub access** — create a `.env` file: ```env -GITHUB_TOKEN= CLIENT_ID= CLIENT_SECRET= CREDENTIAL_KEYS='{"2026-09":""}' @@ -115,7 +114,7 @@ DB_PASSWORD= AUTH_CALLBACK=http://localhost:5000/github/auth ``` -- `GITHUB_TOKEN` — create one at with the `repo` scope. +- Content is always fetched with the repository owner's own GitHub connection; the server has no shared GitHub token. - `CREDENTIAL_KEYS` / `CREDENTIAL_ACTIVE_KEY_ID` — generate a key with `openssl rand -base64 32`. Existing installations must follow the [credential migration guide](docs/credential-encryption.md) before starting this release. - `CLIENT_ID` / `CLIENT_SECRET` — from an OAuth App at . - The App's callback must be `https:///github/auth` (matching `AUTH_CALLBACK`). diff --git a/src/config.ts b/src/config.ts index 2efef69..0bbc00c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -67,7 +67,6 @@ interface Config { REDIS_HOSTNAME: string; CLIENT_ID: string; CLIENT_SECRET: string; - GITHUB_TOKEN: string; DEFAULT_QUOTA: number; MAX_FILE_FOLDER: number; MAX_FILE_SIZE: number; @@ -181,7 +180,6 @@ const config: Config = { SESSION_SECRET: "", CLIENT_ID: "CLIENT_ID", CLIENT_SECRET: "CLIENT_SECRET", - GITHUB_TOKEN: "", DEFAULT_QUOTA: 2 * 1024 * 1024 * 1024 * 8, MAX_FILE_FOLDER: 1000, MAX_FILE_SIZE: 100 * 1024 * 1024, // in b, 100MB diff --git a/src/core/Gist.ts b/src/core/Gist.ts index e638dc7..064aec9 100644 --- a/src/core/Gist.ts +++ b/src/core/Gist.ts @@ -14,7 +14,7 @@ import AnonymousError from "./AnonymousError"; import { IAnonymizedGistDocument } from "./model/anonymizedGists/anonymizedGists.types"; import AnonymizedGistModel from "./model/anonymizedGists/anonymizedGists.model"; import config from "../config"; -import { octokit } from "./GitHubUtils"; +import { octokit, ownerGitHubAccessRequired } from "./GitHubUtils"; import { ContentAnonimizer } from "./anonymize-utils"; import { createLogger } from "./logger"; @@ -59,8 +59,9 @@ export default class Gist { registerGitHubToken(token, { quotaKey: `app-user:${ownerId}`, renew: () => appUserToken(ownerId) }); return { token, connection: "github-app" }; } - return { token: (await getCredentialToken(this.owner.id, "github", { collection: "anonymizedgists", id: this._model._id })) || config.GITHUB_TOKEN, - connection: "oauth" }; + const token = await getCredentialToken(this.owner.id, "github", { collection: "anonymizedgists", id: this._model._id }); + if (!token) throw ownerGitHubAccessRequired(); + return { token, connection: "oauth" }; } async getToken() { diff --git a/src/core/GitHubUtils.ts b/src/core/GitHubUtils.ts index 8f63839..b4df516 100644 --- a/src/core/GitHubUtils.ts +++ b/src/core/GitHubUtils.ts @@ -316,6 +316,11 @@ export async function checkToken(token: string) { const checkedRepositoryTokens = new WeakMap(); +/** Content is fetched only with its owner's own GitHub access, never a shared operator token. */ +export function ownerGitHubAccessRequired() { + return new AnonymousError("token_expired", { httpStatus: 401 }); +} + export async function getToken(repository: Repository) { try { return await measureStage("credential_resolution", () => resolveRepositoryToken(repository)); @@ -360,10 +365,11 @@ async function resolveRepositoryToken(repository: Repository) { checkedRepositoryTokens.set(repository, ownerAccessToken); return ownerAccessToken; } - void notifyOwnerAccessProblem(repository.owner.id, "token_expired", { kind: "repository", id: String(repository.model._id) }); - return config.GITHUB_TOKEN; + throw ownerGitHubAccessRequired(); } - return (await getCredentialToken(repository.owner.id, "github", { + const token = await getCredentialToken(repository.owner.id, "github", { collection: "anonymizedrepositories", id: repository.model._id, - })) || config.GITHUB_TOKEN; + }); + if (!token) throw ownerGitHubAccessRequired(); + return token; } diff --git a/src/core/PullRequest.ts b/src/core/PullRequest.ts index 3662479..2c64c34 100644 --- a/src/core/PullRequest.ts +++ b/src/core/PullRequest.ts @@ -11,8 +11,7 @@ import ConferenceModel from "./model/conference/conferences.model"; import AnonymousError from "./AnonymousError"; import { IAnonymizedPullRequestDocument } from "./model/anonymizedPullRequests/anonymizedPullRequests.types"; import AnonymizedPullRequestModel from "./model/anonymizedPullRequests/anonymizedPullRequests.model"; -import config from "../config"; -import { octokit } from "./GitHubUtils"; +import { octokit, ownerGitHubAccessRequired } from "./GitHubUtils"; import { ContentAnonimizer } from "./anonymize-utils"; import { createLogger } from "./logger"; @@ -30,7 +29,9 @@ export default class PullRequest { async getToken() { if (this._model.githubAccess?.kind === "github-app") return boundAppToken(this.owner.id, this._model.githubAccess, this._model.source.repositoryFullName); - return (await getCredentialToken(this.owner.id, "github", { collection: "anonymizedpullrequests", id: this._model._id })) || config.GITHUB_TOKEN; + const token = await getCredentialToken(this.owner.id, "github", { collection: "anonymizedpullrequests", id: this._model._id }); + if (!token) throw ownerGitHubAccessRequired(); + return token; } async download() { diff --git a/src/core/legacy-token-refresh.ts b/src/core/legacy-token-refresh.ts index b2721c9..4e89d9a 100644 --- a/src/core/legacy-token-refresh.ts +++ b/src/core/legacy-token-refresh.ts @@ -32,7 +32,7 @@ export async function refreshLegacyToken(ownerId: string, previous: string): Pro const body = await res.json().catch(() => null) as { token?: unknown } | null; if (typeof body?.token === "string" && body.token.length) { if (await replaceCredential(ownerId, previous, body.token)) return body.token; - return (await getCredentialToken(ownerId)) || config.GITHUB_TOKEN; + return getCredentialToken(ownerId); } } else { // Release the response connection even when only the status matters. diff --git a/test/credential-migration.test.js b/test/credential-migration.test.js index 9947af6..4cf9810 100644 --- a/test/credential-migration.test.js +++ b/test/credential-migration.test.js @@ -173,6 +173,24 @@ describeMongo("credential access (MongoDB)", function () { expect((await GistModel.collection.findOne({ _id: gist.model._id })).source.accessToken).to.equal(undefined); expect((await PullModel.collection.findOne({ _id: pull.model._id })).source.accessToken).to.equal(undefined); }); + it("never borrows a shared operator token when the owner has no GitHub access", async () => { + const Gist = require("../src/core/Gist").default; + const PullRequest = require("../src/core/PullRequest").default; + const Repository = require("../src/core/Repository").default; + process.env.GITHUB_TOKEN = "operator-token"; + try { + const resources = [ + new Gist(await GistModel.create({ owner: owner._id, gistId: "no-access-gist", source: { gistId: "no-access" } })), + new PullRequest(await PullModel.create({ owner: owner._id, pullRequestId: "no-access-pr", source: { pullRequestId: "2" } })), + new Repository(await RepoModel.create({ owner: owner._id, repoId: "no-access-repo", source: { type: "GitHubStream" } })), + ]; + for (const resource of resources) { + const error = await resource.getToken().then(() => null, e => e); + expect(error?.message).to.equal("token_expired"); + expect(error.httpStatus).to.equal(401); + } + } finally { delete process.env.GITHUB_TOKEN; } + }); it("refreshes a repository credential without copying it onto the repository", async () => { const Repository = require("../src/core/Repository").default; const repo = new Repository(await RepoModel.create({ owner: owner._id, repoId: "test-repo", source: { type: "GitHubStream" } }));