mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-11-17 17:44:09 +01:00
Merge pull request #955 from tdurieux/fix/remaining-production-findings
fix: stop quota retry loops, narrow folder queries and diagnose upstream failures
This commit is contained in:
15 files changed
+312
-21
No files matched your search
@@ -0,0 +1,67 @@
|
||||
const { expect } = require("chai");
|
||||
const { createHmac } = require("crypto");
|
||||
require("ts-node/register/transpile-only");
|
||||
const { handleError } = require("../src/server/routes/route-utils");
|
||||
const { getAllRepositories } = require("../src/server/routes/user");
|
||||
const { githubAppWebhook } = require("../src/server/routes/github-app");
|
||||
const config = require("../src/config").default;
|
||||
const Installation = require("../src/core/model/github-installation").default;
|
||||
const AnonymousError = require("../src/core/AnonymousError").default;
|
||||
|
||||
function response() {
|
||||
return { headersSent: false, status(code) { this.code = code; return this; },
|
||||
json(body) { this.body = body; return this; }, getHeader() { return "request-id"; } };
|
||||
}
|
||||
|
||||
describe("credential and webhook failure diagnostics", () => {
|
||||
const restores = [];
|
||||
function stub(object, key, value) { const original = object[key]; object[key] = value; restores.push(() => { object[key] = original; }); }
|
||||
afterEach(() => restores.splice(0).reverse().forEach(restore => restore()));
|
||||
it("logs generic upstream 401 as warning while retaining server errors", () => {
|
||||
const warnings = [], errors = [];
|
||||
stub(console, "warn", (...args) => warnings.push(args));
|
||||
stub(console, "error", (...args) => errors.push(args));
|
||||
handleError(Object.assign(new Error("denied"), { status: 401 }), response());
|
||||
handleError(Object.assign(new Error("failed"), { response: { statusCode: 503 } }), response());
|
||||
expect(warnings).to.have.length(1); expect(errors).to.have.length(1);
|
||||
expect(errors.flat().join(" ")).to.include('"httpStatus":503');
|
||||
});
|
||||
it("logs got HTTP401 as warning with its actual status", () => {
|
||||
const warnings = []; stub(console, "warn", (...args) => warnings.push(args));
|
||||
const failure = Object.assign(new Error("Unauthorized"), { response: { statusCode: 401 } });
|
||||
Object.setPrototypeOf(failure, require("got").HTTPError.prototype);
|
||||
const res = response(); handleError(failure, res);
|
||||
expect(warnings).to.have.length(1); expect(res.code).to.equal(401);
|
||||
});
|
||||
for (const status of [401, 403, 429, 503]) it(`normalizes only confirmed discovery 401 (${status})`, async () => {
|
||||
const failure = Object.assign(new Error("private provider details"), { status });
|
||||
let received;
|
||||
try { await getAllRepositories({ getGitHubRepositories: async () => { throw failure; } }, true); }
|
||||
catch (error) { received = error; }
|
||||
if (status === 401) {
|
||||
expect(received).to.be.instanceOf(AnonymousError);
|
||||
expect(received.message).to.equal("token_expired"); expect(received.httpStatus).to.equal(401);
|
||||
} else expect(received).to.equal(failure);
|
||||
});
|
||||
it("preserves App-specific reconnect errors", async () => {
|
||||
const failure = new AnonymousError("github_app_reconnect_required", { httpStatus: 401 });
|
||||
try { await getAllRepositories({ getGitHubRepositories: async () => { throw failure; } }, true); throw Error("expected denial"); }
|
||||
catch (error) { expect(error).to.equal(failure); }
|
||||
});
|
||||
it("logs signed webhook persistence failures without secrets and still returns 503", async () => {
|
||||
stub(config, "GITHUB_APP_ENABLED", true); stub(config, "GITHUB_APP_ID", "123");
|
||||
stub(config, "GITHUB_APP_WEBHOOK_SECRET", "test-secret");
|
||||
const messages = []; stub(console, "error", (...args) => messages.push(args));
|
||||
stub(Installation, "updateOne", async () => { throw new Error("secret-token https://private/?token=secret"); });
|
||||
const body = Buffer.from(JSON.stringify({ action: "added", installation: { id: 4, app_id: 123 }, private: "private-body" }));
|
||||
const req = { body, headers: { "x-github-event": "installation_repositories", "x-github-delivery": "abcd-1234",
|
||||
"x-hub-signature-256": "sha256=" + createHmac("sha256", "test-secret").update(body).digest("hex") } };
|
||||
const handler = githubAppWebhook.stack.find(layer => layer.route).route.stack.at(-1).handle;
|
||||
const res = response(); await handler(req, res);
|
||||
expect(res.code).to.equal(503); expect(res.body.error).to.equal("webhook_processing_failed");
|
||||
const text = JSON.stringify(messages);
|
||||
expect(text).to.include("persist_installation_block").and.include("abcd-1234");
|
||||
expect(text).not.to.include("secret-token"); expect(text).not.to.include("private-body"); expect(text).not.to.include("https://");
|
||||
expect(text).not.to.include(req.headers["x-hub-signature-256"]);
|
||||
});
|
||||
});
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
/* global require, module */
|
||||
require("ts-node/register/transpile-only");
|
||||
const db = require("../../src/server/database");
|
||||
const gh = require("../../src/core/GitHubUtils");
|
||||
const notifications = require("../../src/core/owner-notifications");
|
||||
const AnonymousError = require("../../src/core/AnonymousError").default;
|
||||
const processDownload = require("../../src/queue/processes/downloadRepository").default;
|
||||
db.connect = async () => {};
|
||||
gh.getToken = async () => "test-token";
|
||||
gh.getRedisGateResetAt = async () => 0;
|
||||
notifications.notifyOwnerAccessProblem = async () => {};
|
||||
module.exports = async job => {
|
||||
db.getRepository = async () => ({
|
||||
status: "preparing", owner: { id: "owner" }, model: {},
|
||||
async updateStatus(status) { this.status = status; },
|
||||
async resetSate() {},
|
||||
async anonymize() { throw new AnonymousError(job.data.failure, { httpStatus: job.data.failure === "quota_exceeded" ? 402 : 502 }); },
|
||||
});
|
||||
return processDownload(job);
|
||||
};
|
||||
@@ -51,13 +51,18 @@ describe("folder file endpoint disclosure", function () {
|
||||
} finally { await close(server); }
|
||||
});
|
||||
it("blocks a real HTTP redirect to a GitHub tree page before fetching its HTML", async () => {
|
||||
const server = await listen(http.createServer((_req, res) => { res.writeHead(302, { location: "https://github.com/example/private-repo/tree/main/folder" }); res.end(); }));
|
||||
const warnings = [];
|
||||
stub(console, "warn", (...args) => warnings.push(args.join(" ")));
|
||||
const server = await listen(http.createServer((_req, res) => { res.writeHead(302, { location: "https://github.com/example/private-repo/tree/main/folder?token=secret-query" }); res.end(); }));
|
||||
const original = got.stream;
|
||||
stub(got, "stream", (_url, options) => original(`http://127.0.0.1:${server.address().port}/raw`, options));
|
||||
const source = new GitHubStream({ repoId: "test", organization: "example", repoName: "repo", commit: "main", getToken: () => "" });
|
||||
try {
|
||||
const error = await collect(await source.downloadWithFallback("", "tree-sha", "folder")).catch(error => error);
|
||||
expect(error.message).to.equal("upstream_error");
|
||||
expect(warnings.join(" ")).to.include("github.com").and.include("host");
|
||||
expect(warnings.join(" ")).not.to.include("private-repo");
|
||||
expect(warnings.join(" ")).not.to.include("secret-query");
|
||||
} finally { await close(server); }
|
||||
});
|
||||
it("rejects a successful web-page response before emitting any bytes", async () => {
|
||||
|
||||
@@ -451,6 +451,43 @@ const uri = process.env.PERFORMANCE_MONGO_URI;
|
||||
expect(files[0]).not.to.be.instanceOf(mongoose.Document);
|
||||
});
|
||||
|
||||
it("bounds a direct directory listing to its immediate children in the path index", async () => {
|
||||
const repo = new Repository(await repository({ status: "ready", treeGeneration: "old" }));
|
||||
await rows(12000, i => ({ path: `src/deep/${i}` }));
|
||||
await rows(9, i => ({ path: "src", name: `direct${i}.txt` }));
|
||||
const find = File.find; let query;
|
||||
File.find = function (...args) { query = find.apply(this, args); return query; };
|
||||
try {
|
||||
const files = await repo.files({ recursive: false, path: "src" });
|
||||
expect(files).to.have.length(9);
|
||||
const plan = await query.clone().explain("executionStats");
|
||||
expect(plan.executionStats.totalKeysExamined).to.be.at.most(10);
|
||||
expect(plan.executionStats.totalDocsExamined).to.equal(9);
|
||||
} finally { File.find = find; }
|
||||
});
|
||||
|
||||
for (const generation of ["old", undefined]) {
|
||||
it(`uses literal case-sensitive directory paths for ${generation || "legacy"} listings`, async () => {
|
||||
const repo = new Repository(await repository({ status: "ready", treeGeneration: generation }));
|
||||
const paths = ["", "猫.[x] +$", "Src", "src", "src\n", "src/deep"];
|
||||
await File.collection.insertMany(paths.flatMap((directory, i) => [
|
||||
{ repoId: "large", ...(generation ? { treeGeneration: generation } : {}),
|
||||
path: directory, name: `wanted${i}`, size: 1, sha: "b".repeat(40) },
|
||||
{ repoId: "large", treeGeneration: "staged", path: directory, name: "hidden", size: 1 },
|
||||
]));
|
||||
// Explicit null is not a legacy snapshot, even though Mongo indexes
|
||||
// missing and null fields alike.
|
||||
await File.collection.insertOne({ repoId: "large", treeGeneration: null, path: "src", name: "null-generation", size: 1 });
|
||||
for (const [i, directory] of paths.entries()) {
|
||||
const files = await repo.files({ recursive: false, path: directory });
|
||||
expect(files.map(file => file.name)).to.deep.equal([`wanted${i}`]);
|
||||
}
|
||||
expect((await repo.files({ recursive: false })).map(file => file.name)).to.deep.equal(["wanted0"]);
|
||||
expect((await repo.files({ recursive: true, path: "src" })).map(file => file.name).sort())
|
||||
.to.deep.equal(["wanted3", "wanted4", "wanted5"]);
|
||||
});
|
||||
}
|
||||
|
||||
it("preserves rows that stop matching the deletion filter after selection", async () => {
|
||||
await rows(2);
|
||||
let checks = 0;
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
const { expect } = require("chai");
|
||||
require("ts-node/register/transpile-only");
|
||||
const db = require("../src/server/database");
|
||||
const gh = require("../src/core/GitHubUtils");
|
||||
const notifications = require("../src/core/owner-notifications");
|
||||
const utils = require("../src/server/routes/route-utils");
|
||||
const queue = require("../src/queue");
|
||||
const AnonymousError = require("../src/core/AnonymousError").default;
|
||||
const { repositoryFailureStatus } = require("../src/core/repository-failure-status");
|
||||
const { UnrecoverableError } = require("bullmq");
|
||||
|
||||
describe("quota job failures", () => {
|
||||
const restores = [];
|
||||
function stub(object, key, value) {
|
||||
const previous = object[key]; object[key] = value; restores.push(() => { object[key] = previous; });
|
||||
}
|
||||
afterEach(() => { while (restores.length) restores.pop()(); });
|
||||
function repository() {
|
||||
return { repoId: "quota-test", status: "error", statusActivityDate: new Date(0),
|
||||
owner: { id: "owner" }, options: {}, model: { owner: "owner", statusMessage: "quota_exceeded" },
|
||||
assertNotArchived() {},
|
||||
async updateStatus(status, message) { this.status = status; this.model.statusMessage = message; },
|
||||
async resetSate() {},
|
||||
};
|
||||
}
|
||||
it("maps stored quota errors to 402, leaving unknown failures as 500", () => {
|
||||
expect(repositoryFailureStatus("quota_exceeded")).to.equal(402);
|
||||
expect(repositoryFailureStatus("unexpected")).to.equal(500);
|
||||
});
|
||||
it("does not let public reads retry an old quota denial", async () => {
|
||||
const repo = repository(); let enqueues = 0; let writes = 0;
|
||||
stub(utils, "getRepo", async () => repo);
|
||||
stub(utils, "getUser", async () => { throw Error("not_connected"); });
|
||||
stub(queue, "downloadQueue", { add: async () => { enqueues++; } });
|
||||
stub(repo, "updateStatus", async () => { writes++; });
|
||||
const route = require("../src/server/routes/repository-public").default.stack.find(l => l.route?.path === "/:repoId/options").route.stack[0].handle;
|
||||
for (let i = 0; i < 2; i++) {
|
||||
let status, body;
|
||||
await route({}, { header() {}, status(value) { status = value; return this; }, json(value) { body = value; } });
|
||||
expect(status).to.equal(402); expect(body).to.deep.equal({ error: "quota_exceeded" });
|
||||
}
|
||||
expect(enqueues).to.equal(0); expect(writes).to.equal(0);
|
||||
});
|
||||
for (const code of ["quota_exceeded", "github_unavailable"]) {
|
||||
it(`classifies ${code} at the worker boundary and preserves its status message`, async () => {
|
||||
const repo = repository(); const failure = new AnonymousError(code, { httpStatus: code === "quota_exceeded" ? 402 : 502 });
|
||||
stub(db, "connect", async () => {}); stub(db, "getRepository", async () => repo);
|
||||
stub(gh, "getToken", async () => "test-token"); stub(gh, "getRedisGateResetAt", async () => 0);
|
||||
stub(notifications, "notifyOwnerAccessProblem", async () => {});
|
||||
repo.anonymize = async () => { throw failure; };
|
||||
const error = await require("../src/queue/processes/downloadRepository").default({ data: { repoId: repo.repoId }, updateProgress() {} }).catch(error => error);
|
||||
expect(repo.status).to.equal("error"); expect(repo.model.statusMessage).to.equal(code);
|
||||
if (code === "quota_exceeded") expect(error).to.be.instanceOf(UnrecoverableError);
|
||||
else expect(error).to.equal(failure);
|
||||
});
|
||||
}
|
||||
it("allows an authenticated owner to explicitly retry quota admission", async () => {
|
||||
const repo = repository(); let refreshes = 0;
|
||||
repo.refresh = async () => { refreshes++; };
|
||||
stub(utils, "getRepo", async () => repo);
|
||||
stub(utils, "getUser", async () => ({ id: "owner", model: { id: "owner" } }));
|
||||
stub(notifications, "resetOwnerAccessAlerts", async () => {});
|
||||
const route = require("../src/server/routes/repository-private").default.stack.find(l => l.route?.path === "/:repoId/refresh").route.stack[0].handle;
|
||||
let body;
|
||||
await route({}, { json(value) { body = value; } });
|
||||
expect(refreshes).to.equal(1); expect(body).to.deep.equal({ status: "error" });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
const { expect } = require("chai");
|
||||
const { Queue, QueueEvents, Worker } = require("bullmq");
|
||||
const { randomUUID } = require("node:crypto");
|
||||
const path = require("node:path");
|
||||
const port = Number(process.env.PERFORMANCE_REDIS_PORT);
|
||||
|
||||
(port ? describe : describe.skip)("sandboxed quota retry policy", function () {
|
||||
this.timeout(30000);
|
||||
let queue, events, worker;
|
||||
before(async () => {
|
||||
const name = `quota-test-${randomUUID()}`;
|
||||
const connection = { host: "127.0.0.1", port };
|
||||
queue = new Queue(name, { connection });
|
||||
events = new QueueEvents(name, { connection });
|
||||
await events.waitUntilReady();
|
||||
worker = new Worker(name, path.resolve(__dirname, "fixtures/quota-download.worker.cjs"), { connection });
|
||||
await worker.waitUntilReady();
|
||||
});
|
||||
after(async () => {
|
||||
await worker?.close();
|
||||
if (queue) await queue.obliterate({ force: true });
|
||||
await events?.close(); await queue?.close();
|
||||
});
|
||||
for (const [failure, attempts] of [["quota_exceeded", 1], ["github_unavailable", 3]]) {
|
||||
it(`${failure} executes ${attempts} time(s) across the sandbox boundary`, async () => {
|
||||
let executions = 0;
|
||||
const active = () => { executions++; };
|
||||
worker.on("active", active);
|
||||
try {
|
||||
const job = await queue.add("download", { repoId: failure, failure }, { attempts: 3 });
|
||||
const error = await job.waitUntilFinished(events, 20000).catch(error => error);
|
||||
expect(error).to.be.instanceOf(Error); expect(error.message).to.equal(failure);
|
||||
const failed = await queue.getJob(job.id);
|
||||
expect(await failed.getState()).to.equal("failed");
|
||||
expect(failed.attemptsMade).to.equal(attempts);
|
||||
expect(executions).to.equal(attempts);
|
||||
} finally { worker.off("active", active); }
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user