mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +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
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user