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', () => {