fix(healthcare-clinical): SHA-256 query fingerprint in audit entries

The audit log's queryHash used a 32-bit rolling hash, so distinct
queries could share one fingerprint. Use SHA-256 from node:crypto.
Re-applies the remaining hunk of #1436; the backend JSON parsing
hunks already landed in e50df6722 (#1558).

Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
ruv committed 2026-11-06 12:24:50 -05:00
1 parent 09bd1bf304
commit d4e995a84a
2 files changed
+18 -7

No files matched your search

@@ -9,6 +9,7 @@
* - ontology-navigate: Navigate medical ontology hierarchies
*/
import { createHash } from 'node:crypto';
import type {
MCPTool,
MCPToolResult,
@@ -89,13 +90,9 @@ async function logAudit(
}
function hashInput(input: unknown): string {
const str = JSON.stringify(input);
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash) + str.charCodeAt(i);
hash = hash & hash;
}
return Math.abs(hash).toString(16);
// SHA-256: a 32-bit rolling hash collides trivially, which lets two
// different queries share one audit fingerprint.
return createHash('sha256').update(JSON.stringify(input)).digest('hex');
}
// ============================================================================
@@ -245,6 +245,20 @@ describe('Healthcare Clinical MCP Tools', () => {
})
);
});
it('should record a collision-resistant SHA-256 query hash in audit entries', async () => {
const auditLogger = { log: vi.fn().mockResolvedValue(undefined) };
const context = createMockContext({ auditLogger });
const input = { patientFeatures: { diagnoses: ['E11.9'] } };
await patientSimilarityTool.handler(input, context);
const { createHash } = await import('node:crypto');
const expected = createHash('sha256').update(JSON.stringify(input)).digest('hex');
const entry = auditLogger.log.mock.calls[0][0];
expect(entry.queryHash).toMatch(/^[0-9a-f]{64}$/);
expect(entry.queryHash).toBe(expected);
});
});
describe('healthcare/drug-interactions', () => {