mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
Handlers that resolve an anonymized repository, pull request or gist now put its public ID on the log context, so request, job and error records carry repoId, pullRequestId or gistId. The signed-in user's internal _id (userId) is recorded on account routes. On routes that serve anonymized content, userId is kept only when the user owns, co-authors or administers the project; otherwise the record carries viewer "visitor" or "anonymous" and no userId. The collector, the Redis mirror and the admin log APIs apply the same rule. Jobs carry repoId and the owner's userId stamped at enqueue time. The admin Errors page links repoId to the admin repository search and userId to the admin user page, which now accepts a user _id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
163 lines
11 KiB
JavaScript
163 lines
11 KiB
JavaScript
const { setTimeout } = require("node:timers");
|
|
const { expect } = require('chai');
|
|
const { randomUUID } = require('node:crypto');
|
|
const { spawn, spawnSync } = require('node:child_process');
|
|
require('ts-node/register/transpile-only');
|
|
const { logRecord } = require('../src/core/log-record');
|
|
const { logContext, jobTrace } = require('../src/core/log-context');
|
|
const { loggedJob } = require('../src/queue/log-job');
|
|
|
|
describe('structured logging privacy and context', function () {
|
|
this.timeout(15000); // Child processes compile TypeScript before exercising pipe failures.
|
|
it('keeps the production summary compatible with JSON and legacy records', () => {
|
|
const { logSummary, observeLine } = require('../scripts/check-production-logs');
|
|
const summary = logSummary();
|
|
observeLine(summary, JSON.stringify(logRecord('info', 'requests', ['request', {ms: 1, status: 503, outcome: 'completed'}])));
|
|
observeLine(summary, JSON.stringify(logRecord('error', 'process', ['process fatal error'])));
|
|
observeLine(summary, '2026-01-01T00:00:00Z WARN [requests] request {"ms":2,"status":429,"outcome":"completed"}');
|
|
expect(summary).to.include({completed: 2, completed5xx: 1, fatalErrors: 1, errors: 1, warnings: 1, rateLimited: 1});
|
|
});
|
|
it('omits arbitrary text, private identities, request bodies and nested secrets', () => {
|
|
const secret = 'private-owner/private-repository?state=secret';
|
|
const record = logRecord('error', 'upstream', [secret, {
|
|
message: secret, url: secret, headers: { authorization: secret }, repoId: secret,
|
|
requestBody: secret, status: 502, code: 'ETIMEDOUT', stages: { upstream: 42, [secret]: 1 },
|
|
}]);
|
|
expect(JSON.stringify(record)).not.to.include(secret);
|
|
expect(record).to.include({ status: 502, errorCode: 'ETIMEDOUT', stage_upstream_ms: 42 });
|
|
expect(record).to.have.keys('timestamp', 'severity', 'service', 'instance', 'release', 'event', 'message', 'module', 'status', 'errorCode', 'stage_upstream_ms');
|
|
});
|
|
it('keeps concurrent request contexts separate and propagates an opaque job trace', async () => {
|
|
const ids = [randomUUID(), randomUUID()];
|
|
const rows = await Promise.all(ids.map((requestId, i) => logContext.run({ requestId }, async () => {
|
|
await new Promise(resolve => setTimeout(resolve, 10 - i));
|
|
const trace = jobTrace();
|
|
expect(trace.requestId).to.equal(requestId);
|
|
expect(trace.logJobId).not.to.equal(requestId);
|
|
return logRecord('info', 'fixture', ['request']);
|
|
})));
|
|
expect(rows.map(row => row.requestId)).to.deep.equal(ids);
|
|
expect(logRecord('info', 'fixture', ['request'])).not.to.have.property('requestId');
|
|
});
|
|
it('sets sandbox job context and preserves success/failure semantics', async () => {
|
|
const requestId = randomUUID(), jobId = randomUUID();
|
|
const processor = loggedJob('download', async () => {
|
|
expect(logContext.getStore()).to.deep.equal({ service: 'worker', requestId, jobId, attempt: 3, repoId: 'private-id' });
|
|
throw new Error('fixture failure');
|
|
});
|
|
try { await processor({ data: { repoId: 'private-id', requestId, logJobId: jobId }, attemptsMade: 2 }); }
|
|
catch (error) { expect(error.message).to.equal('fixture failure'); return; }
|
|
throw new Error('job failure was swallowed');
|
|
});
|
|
it('ignores cyclic objects and hostile getters without losing safe fields', () => {
|
|
const value = {status: 502}; value.self = value;
|
|
Object.defineProperty(value, 'message', {get() { throw new Error('getter invoked'); }});
|
|
value.method = {toString() { throw new Error('toString invoked'); }};
|
|
const record = logRecord('error', 'route', ['__proto__', value]);
|
|
expect(record).to.include({event: 'application_log', status: 502});
|
|
const failure = new Error('fixture'); failure.cause = failure;
|
|
expect(require('../src/core/logger').serializeError(failure).cause.message).to.equal('error_details_truncated');
|
|
});
|
|
it('rejects injected correlation IDs and non-finite measurements', () => {
|
|
const row = logRecord('warn', 'fixture', ['request', { requestId: 'bad\nsecret', jobId: 'private', ms: Infinity, status: NaN }]);
|
|
expect(row).not.to.have.any.keys('requestId', 'jobId', 'ms', 'status');
|
|
});
|
|
it('survives a reader closing stdout', async function () {
|
|
this.timeout(10000);
|
|
const child = spawn(process.execPath, ['-r', 'ts-node/register/transpile-only', '-e', `
|
|
const { createLogger, getDroppedOutput } = require('./src/core/logger');
|
|
const logger = createLogger('process');
|
|
process.on('message', () => {
|
|
logger.info('process heartbeat');
|
|
setTimeout(() => { logger.info('process heartbeat'); process.send(getDroppedOutput()); process.disconnect(); }, 50);
|
|
});
|
|
process.send('ready');
|
|
`], { env: { ...process.env, LOG_REDIS_MIRROR: 'off' }, stdio: ['ignore', 'pipe', 'pipe', 'ipc'] });
|
|
let drops, fallback = "";
|
|
child.stderr.on("data", chunk => { fallback += chunk; });
|
|
child.on('message', message => {
|
|
if (message === 'ready') { child.stdout.destroy(); child.send('write'); }
|
|
else drops = message;
|
|
});
|
|
const code = await new Promise((resolve, reject) => { child.on('exit', resolve); child.on('error', reject); });
|
|
expect(code).to.equal(0);
|
|
expect(drops).to.be.greaterThan(0);
|
|
expect(fallback).to.include('"event":"process_heartbeat"');
|
|
});
|
|
it('preserves admin error codes and expected-error ring suppression', () => {
|
|
const child = spawnSync(process.execPath, ['-r', 'ts-node/register/transpile-only', '-e', `
|
|
const redis = require('redis');
|
|
let pushes = 0, last; const fields = [];
|
|
const tx = {lPush(key, value) {pushes++; last = JSON.parse(value); return tx;}, lTrim() {return tx;},
|
|
hIncrBy(key, field) { fields.push(field); return tx; }, expire() {return tx;}, exec() {return Promise.resolve([]);} };
|
|
redis.createClient = () => ({isReady:true, isOpen:true, on() {}, connect() {return Promise.resolve();}, multi() {return tx;}});
|
|
require('./src/core/logger').createLogger('route').warn('repository_not_ready', {message:'repository_not_ready', httpStatus:409});
|
|
if (pushes !== 0 || !fields.includes('cb:warn:repository_not_ready')) process.exitCode = 1;
|
|
require('./src/core/logger').createLogger('route').warn('invalid_request', {message:'invalid_request', httpStatus:400});
|
|
if (pushes !== 1 || last.raw[0].message !== 'invalid_request' || last.message !== 'invalid_request') process.exitCode = 1;
|
|
`], {env: {...process.env, LOG_REDIS_MIRROR: 'on'}, encoding: 'utf8', timeout: 10000});
|
|
expect(child.status, child.stderr).to.equal(0);
|
|
});
|
|
it('keeps exact reproduction and a frame when Redis payload clamping reduces diagnostics', () => {
|
|
const child = spawnSync(process.execPath, ['-r','ts-node/register/transpile-only','-e', `
|
|
const assert=require('node:assert/strict'),redis=require('redis');let payload;
|
|
const tx={lPush(key,value){payload=value;return tx;},lTrim(){return tx;},hIncrBy(){return tx;},expire(){return tx;},exec(){return Promise.resolve([]);}};
|
|
redis.createClient=()=>({isReady:true,isOpen:true,on(){},connect(){return Promise.resolve();},multi(){return tx;}});
|
|
const requestPath='/api/repo/demo-123/file/'+ 'a'.repeat(1950);
|
|
const frame='at <anonymous> (src/core/model/owner-alert-lifecycle.schema.ts:123456:12345)';
|
|
const stack=Array(8).fill(frame).join('\\n');
|
|
require('./src/core/logger').createLogger('route').error('failure',{...Object.fromEntries(['ms','firstByteMs','responseMs','bytes','bytesPerSecond','attempt','rss','memoryLimitBytes','memoryRatio','uptime','activeRequests','waiting','active','delayed','failed','oldestWaitingMs','droppedLogs','loopP95Ms','upstreamStatus'].map(key=>[key,Number.MAX_VALUE])),requestPath,method:'GET',errorType:'TypeError',errorStack:stack,causeType:'Error',causeStack:stack,httpStatus:500});
|
|
assert(payload.length<=4096);const row=JSON.parse(payload).raw[0];assert.equal(row.requestPath,requestPath);assert.equal(row.repoId,'demo-123');assert.equal(row.errorStack,frame);
|
|
`],{env:{...process.env,LOG_REDIS_MIRROR:'on'},encoding:'utf8',timeout:10000});
|
|
expect(child.status,child.stderr).equal(0);
|
|
});
|
|
it('reports interval drops once and retries a dropped report without resetting history', () => {
|
|
const child=spawnSync(process.execPath,['-r','ts-node/register/transpile-only','-e',`
|
|
const assert=require('node:assert/strict');
|
|
const {createLogger,reportOutputDrops}=require('./src/core/logger');
|
|
const logger=createLogger('process');let blocked=true;
|
|
Object.defineProperty(process.stdout,'writableLength',{get:()=>blocked?300000:0});
|
|
logger.info('request');logger.info('request');
|
|
reportOutputDrops(fields=>logger.info('process heartbeat',fields));
|
|
blocked=false;
|
|
reportOutputDrops(fields=>{assert.equal(fields.droppedLogsInterval,3);logger.info('process heartbeat',fields);});
|
|
setImmediate(()=>reportOutputDrops(fields=>{assert.equal(fields.droppedLogsInterval,0);assert.equal(fields.droppedLogs,3);logger.info('process heartbeat',fields);}));
|
|
`],{env:{...process.env,LOG_REDIS_MIRROR:'off'},encoding:'utf8',timeout:10000});
|
|
expect(child.status,child.stderr).equal(0);
|
|
const rows=child.stdout.trim().split('\n').map(JSON.parse);
|
|
expect(rows.map(r=>r.droppedLogsInterval)).deep.equal([3,0]);
|
|
});
|
|
it('retains earlier losses when a report write fails asynchronously', () => {
|
|
const child=spawnSync(process.execPath,['-r','ts-node/register/transpile-only','-e',`
|
|
const assert=require('node:assert/strict');
|
|
const {createLogger,reportOutputDrops}=require('./src/core/logger');
|
|
const logger=createLogger('process');let blocked=true;
|
|
Object.defineProperty(process.stdout,'writableLength',{get:()=>blocked?300000:0});
|
|
logger.info('request');logger.info('request');blocked=false;
|
|
process.stdout.write=(line,callback)=>{setImmediate(()=>callback(new Error('EPIPE')));return true;};
|
|
reportOutputDrops(fields=>logger.info('process heartbeat',fields));
|
|
setImmediate(()=>{
|
|
reportOutputDrops(fields=>{assert.equal(fields.droppedLogsInterval,3);logger.info('process heartbeat',fields);});
|
|
setImmediate(()=>reportOutputDrops(fields=>{assert.equal(fields.droppedLogsInterval,0);logger.info('process heartbeat',fields);}));
|
|
});
|
|
`],{env:{...process.env,LOG_REDIS_MIRROR:'off'},encoding:'utf8',timeout:10000});
|
|
expect(child.status,child.stderr).equal(0);
|
|
expect(child.stderr.trim().split('\n').filter(line=>line.startsWith('{')).map(JSON.parse).map(r=>r.droppedLogsInterval)).deep.equal([3,0]);
|
|
});
|
|
it('writes JSON without Redis and bounds a blocked stdout pipe', () => {
|
|
const child = spawnSync(process.execPath, ['-r', 'ts-node/register/transpile-only', '-e', `
|
|
const { createLogger, getDroppedOutput } = require('./src/core/logger');
|
|
const logger = createLogger('fixture');
|
|
logger.info('request', {status: 200, requestBody: 'secret-fixture'});
|
|
Object.defineProperty(process.stdout, 'writableLength', {value: 300000});
|
|
for (let i = 0; i < 10000; i++) logger.warn('request', {status: 429});
|
|
if (getDroppedOutput() !== 10000) process.exitCode = 1;
|
|
`], { cwd: process.cwd(), env: { ...process.env, LOG_REDIS_MIRROR: 'off' }, encoding: 'utf8', timeout: 10000 });
|
|
expect(child.status, child.stderr).to.equal(0);
|
|
const lines = child.stdout.trim().split('\n');
|
|
expect(lines).to.have.length(1);
|
|
expect(JSON.parse(lines[0])).to.include({ event: 'request_completed', status: 200 });
|
|
expect(child.stdout).not.to.include('secret-fixture');
|
|
});
|
|
});
|