Adding console.log(request) during debugging can copy much more than the operation ID you need. This exercise creates an audit event from selected fields and tests the actual serialized result.
It runs locally with Node.js 22 or newer, uses synthetic values, and makes no network or filesystem writes. The function demonstrates a serialization boundary; it does not authenticate callers or provide an audit storage service.
1. Build an event from allowed fields
Save the program as audit-events.mjs. In an application, the actor and operation IDs must come from trusted authentication and job state. Validating their shape does not establish their authenticity.
The serializer intentionally ignores extra fields. This differs from validating an execution request, where unexpected arguments should often cause rejection. Here, an event is constructed from a fixed projection of already-known facts.
import assert from 'node:assert/strict';
function auditEvent(input, now) {
assert(input && typeof input === 'object' && !Array.isArray(input), 'invalid event');
for (const key of ['operationId', 'actorId']) {
assert(typeof input[key] === 'string' && /^[a-z0-9-]{1,40}$/.test(input[key]), 'invalid identifier');
}
assert(['read_document', 'send_reply'].includes(input.action), 'invalid action');
assert(['allowed', 'denied', 'unknown'].includes(input.outcome), 'invalid outcome');
assert(Number.isSafeInteger(now) && now >= 0, 'invalid timestamp');
return {
operationId: input.operationId,
actorId: input.actorId,
action: input.action,
outcome: input.outcome,
recordedAt: new Date(now).toISOString(),
};
}
const now = Date.parse('2026-10-26T02:00:00Z');
const source = {
operationId: 'op-17', actorId: 'worker-2', action: 'send_reply', outcome: 'denied',
requestBody: 'FAKE_PRIVATE_DOCUMENT', credential: 'FAKE_SECRET_FOR_TEST',
error: 'Untrusted provider error containing private material',
};
const event = auditEvent(source, now);
assert.deepEqual(Object.keys(event).sort(), ['action', 'actorId', 'operationId', 'outcome', 'recordedAt']);
assert.equal(event.recordedAt, '2026-10-26T02:00:00.000Z');
assert.equal(event.outcome, 'denied');
const stored = JSON.stringify(event);
assert(!stored.includes('FAKE_') && !stored.includes('Untrusted'));
assert.equal(JSON.parse(stored).operationId, 'op-17');
assert.throws(() => auditEvent({ ...source, actorId: 'worker\nforged-event' }, now), /invalid identifier/);
assert.throws(() => auditEvent({ ...source, operationId: 'x'.repeat(41) }, now), /invalid identifier/);
assert.throws(() => auditEvent({ ...source, outcome: 'probably fine' }, now), /invalid outcome/);
assert.throws(() => auditEvent({ ...source, action: 'arbitrary command' }, now), /invalid action/);
assert.throws(() => auditEvent(null, now), /invalid event/);
assert.throws(() => auditEvent(source, NaN), /invalid timestamp/);
console.log('PASS: useful audit fields retained; private extras and malformed events excluded.');
2. Execute the checks
node audit-events.mjs
Expect a single PASS: line. The tests inspect the serialized JSON and confirm that the operation ID survives while the fake document, credential, and raw error do not. Rejections throw before the program emits an event.
When adapting the pattern, catch validation errors at the application boundary and record a small, fixed error code. Logging the rejected input inside the catch block would undo the protection.
3. Introduce a deliberate regression
In a temporary copy, add ...input, immediately after return {. Run the program again. The exact-field assertion must fail because private properties now enter the event. Remove the spread afterward.
This check protects against a convenient refactor that preserves all normal fields while silently adding confidential ones. Testing only whether operationId exists would miss it.
4. Extend only with a reason
An identifier such as worker-2 is still information about your system. Access controls, retention, and protection from tampering remain necessary. A fixed event shape does not make a log public or anonymous.
If an investigation needs a destination, decide whether a resource ID or classification is sufficient before adding the full address. Review the same choice for every new field. The audit logging guide covers storage, provenance, and failure behavior that this local serializer leaves outside its scope.
