fix(logging): retain safe ZIP and worker failure reasons

This commit is contained in:
Thomas Durieux committed 2026-11-19 15:04:03 +00:00
1 parent 7841201ad1
commit b284bfb4f9
12 files changed
+145 -15

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
const { expect } = require("chai");
require("ts-node/register/transpile-only");
const { anonymizationErrorCode } = require("../src/core/anonymization-errors");
const { memoryBreakdown } = require("../src/core/log-memory");
const { logRecord } = require("../src/core/log-record");
describe("bounded anonymization failure evidence", () => {
it("retains recognized failure causes and never forwards source text or paths", () => {
for (const [error, expected] of [
[new Error("anonymized_output_too_large"), "anonymized_output_too_large"],
[Object.assign(new Error("private file"), {code:"ENOSPC"}), "anonymization_storage_full"],
[Object.assign(new Error("private pattern"), {code:"ERR_SCRIPT_EXECUTION_TIMEOUT"}), "anonymization_deadline_exceeded"],
[new Error("/private/source/token"), "anonymization_worker_failed"],
[Object.defineProperty({}, "message", {get(){throw new Error("getter invoked");}}), "anonymization_worker_failed"],
]) {
const code = anonymizationErrorCode(error);
expect(code).to.equal(expected);
expect(logRecord("error", "zip-stream", [new Error(code)]).errorCode).to.equal(code);
}
});
it("retains a queue timeout and unsafe ZIP code separately from cancellation", () => {
for (const code of ["anonymization_queue_timeout", "anonymization_cancelled", "zip_unsafe_entry_path"]) {
expect(logRecord("error", "zip-stream", [new Error(code)]).errorCode).to.equal(code);
}
});
it("preserves a bounded cause through an actual worker without exposing a private filesystem path", async function () {
this.timeout(15000);
const { Worker } = require("worker_threads");
const { once } = require("events");
const worker = new Worker(require("path").resolve("src/core/anonymization.worker.ts"), {
execArgv:["-r",require.resolve("ts-node/register/transpile-only")],
});
try {
const received = once(worker, "message");
worker.postMessage({input:"/nonexistent/private-repository/secret.txt",output:"/nonexistent/output",options:{terms:[]},maxOutput:100,context:{mask:"XXXX",hostname:"test.invalid"}});
const [result] = await received;
expect(result).to.deep.equal({error:"anonymization_io_failed"});
} finally { await worker.terminate(); }
});
it("separates reclaimable cache from working set without hiding total pressure", () => {
expect(memoryBreakdown(750, 768, "file 450\ninactive_file 400\n")).to.deep.equal({
memoryFileCacheBytes:450,memoryWorkingSetBytes:350,memoryWorkingSetRatio:350/768,
});
expect(memoryBreakdown(750,768,"total_cache 450\ntotal_inactive_file 400")).to.deep.equal(memoryBreakdown(750,768,"file 450\ninactive_file 400"));
expect(memoryBreakdown(750,768,"")).to.deep.equal({});
});
});
+23
View File
@@ -58,6 +58,29 @@ describe("ZIP stream errors", function () {
});
}
it("rejects backslash archive entries with a specific safe error, without emitting their contents", async function () {
const input = await fixture();
// Replace both ZIP filename headers without changing their byte lengths.
const name = Buffer.from("repo/README.md");
for (let at = input.indexOf(name); at !== -1; at = input.indexOf(name, at + name.length)) input[at + 5] = 92;
const previous = got.stream;
const response = new PassThrough();
const chunks = [], errors = [];
response.on("data", chunk => chunks.push(chunk));
response.on("error", error => errors.push(error));
const closed = new Promise(resolve => response.once("close", resolve));
try {
got.stream = () => Readable.from([input]);
await streamAnonymizedZip({
repoId:"test", organization:"owner", repoName:"public", commit:"abc",
getToken:()=>"", anonymizerOptions:{terms:["private"]},
}, response);
await closed;
expect(errors.map(error => error.message)).to.deep.equal(["zip_unsafe_entry_path"]);
expect(Buffer.concat(chunks).includes(Buffer.from("private source content"))).to.equal(false);
} finally { got.stream = previous; response.destroy(); }
});
it("aborts a download on an asynchronous anonymization timeout without crashing", async function () {
const input = await fixture();
const previous = { stream: got.stream, zip: GitHubDownload.prototype.getZipUrl, flush: AnonymizeTransformer.prototype._flush };