Files
anonymous_github/test/log-diagnostics.test.js
T

117 lines
9.6 KiB
JavaScript

const {expect}=require('chai');
const path=require('path');
const {Response}=globalThis;
require('ts-node/register/transpile-only');
const {errorDiagnostics,safeDiagnosticFields}=require('../src/core/log-diagnostics');
const {serializeError}=require('../src/core/logger');
const {logRecord}=require('../src/core/log-record');
const {logContext}=require('../src/core/log-context');
const {requestReproduction,safeReproductionPath}=require('../src/core/log-reproduction');
const {requestRoute}=require('../src/core/request-monitoring');
const {safeRedisLog,investigateLogs,logOptions}=require('../src/server/admin-log-investigation');
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 request context without queries or unsafe paths',()=>{
const method='GET', requestPath='/api/repo/demo-123/file/docs/reproduce%20issue.md';
expect(requestReproduction(requestPath+'?token=SECRET',method)).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,"GET"),value).equal(undefined);
expect(logContext.run({requestPath,method},()=>logRecord('info','route',['request',{status:200}]))).have.property('requestPath',requestPath);
const record=logContext.run({requestPath,method},()=>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/counts', '/files/search', '/options', '/refresh', '/coauthors', '/extend', '/file/README.md']){
const method=['/refresh','/extend','/coauthors'].includes(suffix)?'POST':'GET';
const requestPath='/api/repo/demo-123'+suffix;
expect(requestReproduction(requestPath+'?token=SECRET',method)).deep.equal({requestPath,repoId:'demo-123'});
for(const status of [200,304,404,500]){
const row=logContext.run({requestPath,method},()=>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,'GET')).equal(undefined);
});
it('rejects upstream lookups for repositories named files at every logging boundary',()=>{
for(const action of ['readme','branches','tree','file']){
const requestPath='/api/repo/private-owner/files/'+action;
for(const suffix of ['', '/', '?token=SECRET']){
expect(requestReproduction(requestPath+suffix,"GET")).deep.equal({});
for(const status of [200,403,500]){
const row=logContext.run({requestPath:requestPath+suffix,method:"GET"},()=>logRecord('error','route',['failure',{httpStatus:status}]));
expect(row).not.have.property('requestPath');expect(row).not.have.property('repoId');
const redis=safeRedisLog({ts:row.timestamp,level:'error',module:'route',raw:[{...row,requestPath:requestPath+suffix,repoId:'private-owner'}]});
expect(redis.raw[0]).not.have.property('requestPath');expect(redis.raw[0]).not.have.property('repoId');
}
}
}
});
it('requires the anonymous action method and omits ambiguous GET source metadata',()=>{
for(const action of ['refresh','extend','coauthors']){
for(const suffix of ['', '/']){
const requestPath='/api/repo/private-owner/'+action+suffix;
for(const method of ['GET','HEAD',undefined,'OPTIONS']){
expect(safeReproductionPath(requestPath,method)).equal(undefined);
const row=logContext.run({requestPath,method},()=>logRecord('error','route',['failure',{httpStatus:403}]));
expect(row).not.have.property('requestPath');
const redis=safeRedisLog({ts:row.timestamp,level:'error',module:'route',raw:[{...row,requestPath,repoId:'private-owner'}]});
expect(redis.raw[0]).not.have.property('requestPath');
}
expect(requestReproduction(requestPath,'POST')).deep.equal({requestPath,repoId:'private-owner'});
}
}
});
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';
const record=logRecord('error','route',['',serializeError(error)]);
expect(record.errorStack).include('at <anonymous> (src/server/routes/repository-public.ts:224:23)').and.include(frame).and.include('at <anonymous> (src/core/model/owner-alert-lifecycle.schema.ts:12:3)');
const redis=safeRedisLog({ts:record.timestamp,level:'error',module:'route',raw:[record]});expect(redis.raw[0].errorStack).equal(record.errorStack);
expect(safeDiagnosticFields({errorStack:'at <anonymous> (src/core/model/not-allowlisted.schema.ts:12:3)'})).deep.equal({});
expect(JSON.stringify(record)).not.include(secret);
error.stack='Error: '+secret+'\n at external ('+path.resolve('node_modules/dependency/index.js')+':1:2)\n at async '+path.resolve('src/server/routes/repository-public.ts')+':224:23';
expect(errorDiagnostics(error).errorStack).equal('at <anonymous> (src/server/routes/repository-public.ts:224:23)');
});
it('retains only recognized source frames and types, never messages, URLs or roots',()=>{
const result=errorDiagnostics(failure());
expect(result.errorType).equal('TypeError'); expect(result.errorStack).include(frame).and.include('at <anonymous>');
expect(JSON.stringify(result)).not.include(secret).and.not.include(process.cwd()).and.not.include('https:');
const payload=serializeError(failure()); expect(logRecord('error','route',['failure',payload]).errorStack).include(frame);
});
it('bounds nested causes and frames, tolerates cycles/getters and rejects forged diagnostics',()=>{
const error=failure();error.cause=failure();error.cause.cause=error;
const result=errorDiagnostics(error);expect(result.causeType).equal('TypeError');expect(result.causeStack).include(frame);
error.cause.cause={name:secret,cause:error};
expect(errorDiagnostics(error)).include({causeType:'UnknownError'}).and.not.have.property('causeStack');
error.stack='Error\n'+Array(40).fill(' at printError ('+path.resolve('src/server/routes/route-utils.ts')+':200:7)').join('\n');
expect(errorDiagnostics(error).errorStack.split('\n')).length(8);
const hostile={};Object.defineProperty(hostile,'stack',{get(){throw Error('must not run')}});
expect(errorDiagnostics(hostile)).deep.equal({errorType:'UnknownError'});
expect(safeDiagnosticFields({errorType:secret,errorStack:frame+'\n'+secret+'\r\n at unknown (src/private.ts:1:2)',causeStack:'x'.repeat(5000)})).deep.equal({errorStack:frame});
});
it('carries normalized method/route through route errors and safe Redis projection',()=>{
const route=requestRoute('/api/repo/'+secret+'/file/private.txt?token='+secret,'api');
expect(route).equal('/api/repo/:repoId/file/:path');
const record=logContext.run({method:'GET',route},()=>logRecord('error','route',['failure',{...serializeError(failure()),httpStatus:500}]));
expect(record).include({route,method:'GET',httpStatus:500,errorType:'TypeError'});
const row=safeRedisLog({ts:new Date().toISOString(),level:'error',module:'route',raw:[record]});
expect(row.raw[0]).include({route,method:'GET',httpStatus:500,errorType:'TypeError',errorStack:record.errorStack});
expect(JSON.stringify(row)).not.include(secret);
expect(logRecord('error','route',['',{route:'/private/'+secret,method:'GET\n'+secret}])).not.have.property('route');
});
it('preserves the same diagnostics in correlated central events and rejects unsafe fields',async()=>{
const oldFetch=global.fetch, oldUrl=process.env.ADMIN_LOG_URL;
try{
process.env.ADMIN_LOG_URL='http://logs.test:9428';
global.fetch=async()=>new Response(JSON.stringify({_time:new Date().toISOString(),service:'api',event:'application_log',module:'route',severity:'error',route:'/api/repo/:repoId',method:'GET',errorType:'TypeError',errorStack:frame,causeType:secret,stack:secret,url:secret,httpStatus:500}));
const result=await investigateLogs(logOptions({kind:'requestId',id:'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'},true),true);
expect(result.events[0]).include({route:'/api/repo/:repoId',method:'GET',errorType:'TypeError',errorStack:frame,httpStatus:500});
expect(JSON.stringify(result)).not.include(secret);
}finally{global.fetch=oldFetch;if(oldUrl===undefined)delete process.env.ADMIN_LOG_URL;else process.env.ADMIN_LOG_URL=oldUrl;}
});
});