fix: retain actual anonymous request paths across outcomes

This commit is contained in:
Thomas Durieux committed 2026-11-15 00:01:21 +00:00
1 parent b54cf72bf8
commit 953767b2cd
8 files changed
+57 -22

No files matched your search

+15 -2
View File
@@ -13,15 +13,28 @@ const secret='PRIVATE_TOKEN_owner_filename';
const frame='at printError (src/server/routes/route-utils.ts:200:7)';
function failure(){ const error=new TypeError('https://user:'+secret+'@github.com/private/repo?token='+secret); error.stack=error.message+'\n at printError ('+path.resolve('src/server/routes/route-utils.ts')+':200:7)\n at '+secret+' ('+path.resolve('src/server/routes/route-utils.ts')+':201:9)\n at secret (/home/private/'+secret+':1:2)\n at fn (https://private/'+secret+':1:2)';return error; }
describe('safe actionable exception diagnostics',()=>{
it('keeps bounded anonymous file request context for failures without queries or unsafe paths',()=>{
it('keeps bounded anonymous request context without queries or unsafe paths',()=>{
const requestPath='/api/repo/demo-123/file/docs/reproduce%20issue.md';
expect(requestReproduction(requestPath+'?token=SECRET')).deep.equal({requestPath,repoId:'demo-123'});
for(const value of ['/api/repo/owner/name/readme','https://example.test'+requestPath,requestPath+'?token=SECRET','/api/repo/demo-123/file/%0aSECRET','/api/repo/demo-123/file/../secret','/api/repo/demo-123/file/%252e%252e/secret','/api/repo/demo-123/file/https%3a%2f%2fuser:token@example.test','/api/repo/demo-123/file/'+ 'x'.repeat(2048)]) expect(safeReproductionPath(value),value).equal(undefined);
expect(logContext.run({requestPath},()=>logRecord('info','route',['request',{status:200}]))).not.have.property('requestPath');
expect(logContext.run({requestPath},()=>logRecord('info','route',['request',{status:200}]))).have.property('requestPath',requestPath);
const record=logContext.run({requestPath},()=>logRecord('error','route',['failure',{httpStatus:500}]));
expect(record).include({requestPath,repoId:'demo-123'});
expect(safeRedisLog({ts:record.timestamp,level:'error',module:'route',raw:[record]}).raw[0]).include({requestPath,repoId:'demo-123'});
});
it('preserves anonymous endpoints at all outcomes through both API projections',async()=>{
for(const suffix of ['', '/', '/zip', '/files', '/files/docs/nested%20folder/', '/files/counts', '/files/search', '/options', '/refresh', '/coauthors', '/extend', '/file/README.md']){
const requestPath='/api/repo/demo-123'+suffix;
expect(requestReproduction(requestPath+'?token=SECRET')).deep.equal({requestPath,repoId:'demo-123'});
for(const status of [200,304,404,500]){
const row=logContext.run({requestPath},()=>logRecord('info','requests',['request',{status,outcome:'interrupted'}]));
expect(row).include({requestPath,repoId:'demo-123'});
expect(safeRedisLog({ts:row.timestamp,level:'info',module:'requests',raw:[row]}).raw[0]).include({requestPath,repoId:'demo-123'});
}
}
for(const suffix of ['/files/../secret','/files/%2fsecret','/files/%0aSECRET','/files/%252e%252e/secret','/zip?token=SECRET'])
expect(safeReproductionPath('/api/repo/demo-123'+suffix)).equal(undefined);
});
it('preserves dotted allowlisted source files and anonymous/named async frames',()=>{
const error=new Error(secret);
error.stack='Error: '+secret+'\n at async '+path.resolve('src/server/routes/repository-public.ts')+':224:23\n at async printError ('+path.resolve('src/server/routes/route-utils.ts')+':200:7)\n at '+path.resolve('src/core/model/owner-alert-lifecycle.schema.ts')+':12:3\n at async https://'+secret+':1:2';
+14 -3
View File
@@ -47,6 +47,14 @@ describe("bounded request monitoring", function () {
expect(metrics.drain().every(row=>!JSON.stringify(row).includes('demo-123'))).equal(true);
});
it("records the actual listing path on successful requests while keeping metrics templated", async function () {
app.get('/api/repo/:repoId/files/*path',(_req,res)=>res.json([]));
await listen(); await fetch(server,'/api/repo/demo-123/files/docs/nested%20folder/?token=SECRET');
expect(logs[0][1]).include({status:200,requestPath:'/api/repo/demo-123/files/docs/nested%20folder/',repoId:'demo-123'});
expect(JSON.stringify(logs)).not.include('SECRET');
expect(JSON.stringify(metrics.drain())).not.include('demo-123');
});
it("keeps labels bounded and removes source identities and query values", function () {
expect(requestRoute("/api/repo/private-name/file/secret.txt?token=abc", "api")).to.equal("/api/repo/:repoId/file/:path");
expect(requestRoute("/api/repo/secret-owner/secret-repo/readme?branch=secret", "api")).to.equal("/api/repo/:owner/:repo/readme");
@@ -94,7 +102,8 @@ describe("bounded request monitoring", function () {
expect(callbacks).to.equal(gzip ? 0 : body.length / 16384);
const row = metrics.drain().find(row => row.data.metric === "request").data;
expect(row.bytes).to.equal(result.body.length); expect(row.aborted).to.equal(0); expect(row.count).to.equal(1);
expect(JSON.stringify(logs)).not.to.include("private.txt"); expect(JSON.stringify(logs)).not.to.include("secret");
expect(logs[0][1].requestPath).to.equal("/api/repo/private/file/private.txt");
expect(JSON.stringify(row)).not.to.include("private.txt"); expect(JSON.stringify(logs)).not.to.include("secret");
expect(logs[0][1].requestId).to.equal(result.headers["x-request-id"]);
expect(activeRequestCount()).to.equal(0);
});
@@ -124,12 +133,14 @@ describe("bounded request monitoring", function () {
it(`records a disconnect ${headersSent ? "after" : "before"} headers exactly once`, async function () {
let arrived; const ready = new Promise(resolve => { arrived = resolve; });
let finished; const terminal = new Promise(resolve => { finished = resolve; });
app.get("/", (_req, res) => { if (headersSent) { res.type("text/plain"); res.write("partial"); } arrived(); res.on("close", finished); });
app.get("/api/repo/:repoId/zip", (_req, res) => { if (headersSent) { res.type("text/plain"); res.write("partial"); } arrived(); res.on("close", finished); });
await listen();
const client = http.get({ host: "127.0.0.1", port: server.address().port, path: "/" }); client.on("error", () => {});
const client = http.get({ host: "127.0.0.1", port: server.address().port, path: "/api/repo/demo-123/zip?token=SECRET" }); client.on("error", () => {});
await ready; client.destroy(); await terminal;
expect(logs).to.have.length(1); expect(logs[0][0]).to.equal("request interrupted");
expect(logs[0][1].outcome).to.equal("interrupted");
expect(logs[0][1]).include({requestPath:"/api/repo/demo-123/zip",repoId:"demo-123"});
expect(JSON.stringify(logs)).not.include("SECRET");
const row = metrics.drain().find(row => row.data.metric === "request").data;
expect(row.count).to.equal(1); expect(row.aborted).to.equal(1); expect(activeRequestCount()).to.equal(0);
if (!headersSent) {