import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { describe, expect, it } from 'vitest'; import { isRpcMethod, redactForAccess, rpcAccessError, rpcAuditLine, type WebAuthContext, WRITE_RPC_METHODS, } from './rpc-access'; const handlersSource = readFileSync( join(__dirname, '../routes/api.rpc.tsx'), 'utf8' ); const reader: WebAuthContext = { mode: 'oidc', access: 'read', subject: 'u1' }; const writer: WebAuthContext = { ...reader, access: 'write' }; describe('rpcAccessError', () => { it('refuses every write method to read access', () => { for (const method of WRITE_RPC_METHODS) { expect(rpcAccessError(method, reader)).toContain(method); expect(rpcAccessError(method, writer)).toBeUndefined(); } }); it('allows reads to read access, and everything without a decision', () => { expect(rpcAccessError('fetchRuns', reader)).toBeUndefined(); expect(rpcAccessError('getEncryptionKeyForRun', reader)).toBeUndefined(); expect(rpcAccessError('cancelRun', undefined)).toBeUndefined(); }); it('names only methods the RPC route has', () => { for (const method of WRITE_RPC_METHODS) { expect(handlersSource).toContain(` ${method}: (`); } }); }); describe('rpcAuditLine', () => { it('attributes authenticated writes', () => { expect( rpcAuditLine( 'cancelRun', { runId: 'wrun_1' }, { ...writer, email: 'a@b.c' } ) ).toBe('[workflow-web] u1 called cancelRun runId=wrun_1'); expect(rpcAuditLine('bulkCancelRuns', { runIds: ['a', 'b'] }, writer)).toBe( '[workflow-web] u1 called bulkCancelRuns runs=2' ); }); it('skips reads and unauthenticated modes', () => { expect(rpcAuditLine('fetchRuns', {}, writer)).toBeUndefined(); expect( rpcAuditLine('cancelRun', {}, { mode: 'local', access: 'write' }) ).toBeUndefined(); }); }); describe('isRpcMethod', () => { const handlers = { cancelRun: () => {}, fetchRuns: () => {} }; it('accepts only own string keys', () => { expect(isRpcMethod('cancelRun', handlers)).toBe(true); // An array coerces to 'cancelRun' with `in` and as a property key. expect(isRpcMethod(['cancelRun'], handlers)).toBe(false); expect(isRpcMethod('constructor', handlers)).toBe(false); expect(isRpcMethod('toString', handlers)).toBe(false); expect(isRpcMethod(undefined, handlers)).toBe(false); }); }); describe('redactForAccess', () => { const bytes = new Uint8Array([1, 2]); const createdAt = new Date(0); const result = { success: true, data: { hookId: 'hook_1', token: 'secret', createdAt, events: [ { eventType: 'hook_created', eventData: { token: 'secret', metadata: bytes }, }, ], }, }; it('drops every token for read access, keeping bytes and dates', () => { const redacted = redactForAccess(result, reader); expect(JSON.stringify(redacted)).not.toContain('secret'); expect(redacted.data.events[0].eventData.metadata).toBe(bytes); expect(redacted.data.createdAt).toBe(createdAt); expect(redacted.data.hookId).toBe('hook_1'); }); it('returns the value unchanged for write access and without a decision', () => { expect(redactForAccess(result, writer)).toBe(result); expect(redactForAccess(result, undefined)).toBe(result); }); });