From d4e995a84affeb4bacf782941fbf3e6c69a5842c Mon Sep 17 00:00:00 2001 From: ruv Date: Fri, 6 Nov 2026 12:24:50 -0500 Subject: [PATCH] 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 --- v3/plugins/healthcare-clinical/src/mcp-tools.ts | 11 ++++------- .../healthcare-clinical/tests/mcp-tools.test.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/v3/plugins/healthcare-clinical/src/mcp-tools.ts b/v3/plugins/healthcare-clinical/src/mcp-tools.ts index 1756050ceb..689a70507d 100644 --- a/v3/plugins/healthcare-clinical/src/mcp-tools.ts +++ b/v3/plugins/healthcare-clinical/src/mcp-tools.ts @@ -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'); } // ============================================================================ diff --git a/v3/plugins/healthcare-clinical/tests/mcp-tools.test.ts b/v3/plugins/healthcare-clinical/tests/mcp-tools.test.ts index 7129f0ce34..5250eaa4bc 100644 --- a/v3/plugins/healthcare-clinical/tests/mcp-tools.test.ts +++ b/v3/plugins/healthcare-clinical/tests/mcp-tools.test.ts @@ -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', () => {