mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 09:03:37 +01:00
fix: stop falling back to the shared GitHub token
When a repository, gist or pull request owner had no usable GitHub credential, content was fetched with the operator's GITHUB_TOKEN. That token shares one rate limit with its account, so these fallbacks exhausted it and made unrelated requests wait and time out. Owners now get token_expired (401), which already asks them to renew their connection, and the server no longer reads GITHUB_TOKEN. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
62c3bf1daf
commit
341b0cc973
7 files changed
+38
-15
No files matched your search
@@ -103,7 +103,6 @@ npm i
|
||||
**2. Configure GitHub access** — create a `.env` file:
|
||||
|
||||
```env
|
||||
GITHUB_TOKEN=<GITHUB_TOKEN>
|
||||
CLIENT_ID=<CLIENT_ID>
|
||||
CLIENT_SECRET=<CLIENT_SECRET>
|
||||
CREDENTIAL_KEYS='{"2026-09":"<base64-encoded 32-byte random key>"}'
|
||||
@@ -115,7 +114,7 @@ DB_PASSWORD=
|
||||
AUTH_CALLBACK=http://localhost:5000/github/auth
|
||||
```
|
||||
|
||||
- `GITHUB_TOKEN` — create one at <https://github.com/settings/tokens/new> 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 <https://github.com/settings/applications/new>.
|
||||
- The App's callback must be `https://<host>/github/auth` (matching `AUTH_CALLBACK`).
|
||||
|
||||
@@ -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
|
||||
|
||||
+4
-3
@@ -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() {
|
||||
|
||||
+10
-4
@@ -316,6 +316,11 @@ export async function checkToken(token: string) {
|
||||
|
||||
const checkedRepositoryTokens = new WeakMap<Repository, string>();
|
||||
|
||||
/** 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;
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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" } }));
|
||||
|
||||
Reference in new issue
Block a user