|
| 1 | +import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | + |
| 5 | +import { safeDelete } from '@socketsecurity/lib-stable/fs/safe' |
| 6 | +import { afterAll, afterEach, expect, test, vi } from 'vitest' |
| 7 | + |
| 8 | +import { logger } from '../../lib/logger.ts' |
| 9 | +import { |
| 10 | + auditLogPath, |
| 11 | + emitAuditEvent, |
| 12 | + extractResources, |
| 13 | + maskArgs, |
| 14 | + newRequestId, |
| 15 | + tokenIdentity, |
| 16 | +} from '../../lib/tool-audit.ts' |
| 17 | +import type { AuditEntry } from '../../lib/tool-audit.ts' |
| 18 | + |
| 19 | +const scratchDir = mkdtempSync(path.join(os.tmpdir(), 'socket-audit-test-')) |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + vi.unstubAllEnvs() |
| 23 | + vi.restoreAllMocks() |
| 24 | +}) |
| 25 | + |
| 26 | +afterAll(async () => { |
| 27 | + await safeDelete(scratchDir) |
| 28 | +}) |
| 29 | + |
| 30 | +test('uses the operator directory when no audit path is configured', () => { |
| 31 | + vi.stubEnv('SOCKET_MCP_AUDIT_LOG', undefined) |
| 32 | + expect(auditLogPath()).toBe( |
| 33 | + path.join(os.homedir(), '.socket', 'mcp-audit.jsonl'), |
| 34 | + ) |
| 35 | +}) |
| 36 | + |
| 37 | +test('creates parent directories and preserves prior audit entries', () => { |
| 38 | + const file = path.join(scratchDir, 'nested', 'events.jsonl') |
| 39 | + vi.stubEnv('SOCKET_MCP_AUDIT_LOG', file) |
| 40 | + const entry: AuditEntry = { |
| 41 | + timestamp: '2026-01-01T00:00:00.000Z', |
| 42 | + identity: 'operator', |
| 43 | + requestId: newRequestId(), |
| 44 | + tool: 'organizations', |
| 45 | + status: 'success', |
| 46 | + resources: ['org:example-org'], |
| 47 | + args: {}, |
| 48 | + } |
| 49 | + emitAuditEvent(entry) |
| 50 | + const denied = { |
| 51 | + ...entry, |
| 52 | + requestId: newRequestId(), |
| 53 | + status: 'denied' as const, |
| 54 | + } |
| 55 | + emitAuditEvent(denied) |
| 56 | + expect( |
| 57 | + readFileSync(file, 'utf8') |
| 58 | + .trim() |
| 59 | + .split(/\r?\n/) |
| 60 | + .map(line => JSON.parse(line)), |
| 61 | + ).toEqual([entry, denied]) |
| 62 | +}) |
| 63 | + |
| 64 | +test('logs an audit write failure without failing the tool call', () => { |
| 65 | + const parentFile = path.join(scratchDir, 'parent-file') |
| 66 | + writeFileSync(parentFile, 'example') |
| 67 | + vi.stubEnv('SOCKET_MCP_AUDIT_LOG', path.join(parentFile, 'events.jsonl')) |
| 68 | + const report = vi.spyOn(logger, 'error').mockImplementation(() => logger) |
| 69 | + expect(() => |
| 70 | + emitAuditEvent({ |
| 71 | + timestamp: '2026-01-01T00:00:00.000Z', |
| 72 | + identity: 'operator', |
| 73 | + requestId: newRequestId(), |
| 74 | + tool: 'organizations', |
| 75 | + status: 'failure', |
| 76 | + resources: [], |
| 77 | + args: {}, |
| 78 | + }), |
| 79 | + ).not.toThrow() |
| 80 | + expect(report).toHaveBeenCalledTimes(1) |
| 81 | +}) |
| 82 | + |
| 83 | +test.each([ |
| 84 | + [{}, []], |
| 85 | + [{ org: 'example-org', organization: 'other-org' }, ['org:example-org']], |
| 86 | + [{ organization: 'example-org' }, ['org:example-org']], |
| 87 | + [{ org: 42, ecosystem: 42, name: 'example-package', purl: '' }, []], |
| 88 | + [ |
| 89 | + { ecosystem: 'npm', depname: 'example-package', version: '1.0.0' }, |
| 90 | + ['pkg:npm/example-package@1.0.0'], |
| 91 | + ], |
| 92 | + [ |
| 93 | + { ecosystem: 'npm', name: 'example-package', version: 42 }, |
| 94 | + ['pkg:npm/example-package'], |
| 95 | + ], |
| 96 | + [ |
| 97 | + { purl: 'pkg:npm/example-package@1.0.0' }, |
| 98 | + ['purl:pkg:npm/example-package@1.0.0'], |
| 99 | + ], |
| 100 | +])('extracts available resource identifiers', (args, resources) => { |
| 101 | + expect(extractResources(args)).toEqual(resources) |
| 102 | +}) |
| 103 | + |
| 104 | +test('redacts nested sensitive keys without mutating caller arguments', () => { |
| 105 | + const args = { |
| 106 | + Authorization: 'test_fake_token', |
| 107 | + metadata: { API_KEY: 'test_fake_key', name: 'example-package' }, |
| 108 | + count: 2, |
| 109 | + } |
| 110 | + expect(maskArgs(args)).toEqual({ |
| 111 | + Authorization: '***REDACTED***', |
| 112 | + metadata: { API_KEY: '***REDACTED***', name: 'example-package' }, |
| 113 | + count: 2, |
| 114 | + }) |
| 115 | + expect(args.metadata.API_KEY).toBe('test_fake_key') |
| 116 | +}) |
| 117 | + |
| 118 | +test('uses operator identity without a bearer token', () => { |
| 119 | + expect(tokenIdentity(undefined)).toBe('operator') |
| 120 | + expect(tokenIdentity('')).toBe('operator') |
| 121 | +}) |
| 122 | + |
| 123 | +test('produces stable distinct pseudonymous token identities', () => { |
| 124 | + const identity = tokenIdentity('test_fake_token') |
| 125 | + expect(identity).toMatch(/^sha256:[a-f0-9]{16}$/u) |
| 126 | + expect(tokenIdentity('test_fake_token')).toBe(identity) |
| 127 | + expect(tokenIdentity('test_fake_other_token')).not.toBe(identity) |
| 128 | +}) |
0 commit comments