|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { readFileSync, readdirSync } from 'fs'; |
| 3 | +import { join, extname, dirname } from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | +import { validateMessagePayload } from '../lib/validateMessagePayload.js'; |
| 6 | +import { SendMessageSchema } from '../schemas/message.schemas.js'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Security regression checks (#388). |
| 10 | + * |
| 11 | + * These tests fail CI the moment any code path starts accepting a |
| 12 | + * plaintext-only message, or any schema/route grows a field that could |
| 13 | + * carry a raw private key or Signal session-state blob. They are a guard |
| 14 | + * against regressions, not a substitute for the crypto design itself. |
| 15 | + */ |
| 16 | + |
| 17 | +const FORBIDDEN_FIELD_NAMES = [ |
| 18 | + 'plaintext', |
| 19 | + 'plainText', |
| 20 | + 'privateKey', |
| 21 | + 'private_key', |
| 22 | + 'sessionState', |
| 23 | + 'session_state', |
| 24 | + 'signalSession', |
| 25 | + 'identityPrivateKey', |
| 26 | + 'preKeyPrivate', |
| 27 | +]; |
| 28 | + |
| 29 | +const SRC_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..'); |
| 30 | + |
| 31 | +function listSourceFiles(dir: string): string[] { |
| 32 | + const entries = readdirSync(dir, { withFileTypes: true }); |
| 33 | + const files: string[] = []; |
| 34 | + for (const entry of entries) { |
| 35 | + if (entry.name === '__tests__' || entry.name === 'node_modules') continue; |
| 36 | + const full = join(dir, entry.name); |
| 37 | + if (entry.isDirectory()) { |
| 38 | + files.push(...listSourceFiles(full)); |
| 39 | + } else if (extname(entry.name) === '.ts') { |
| 40 | + files.push(full); |
| 41 | + } |
| 42 | + } |
| 43 | + return files; |
| 44 | +} |
| 45 | + |
| 46 | +describe('security regression: ciphertext-only guard', () => { |
| 47 | + it('rejects a text message with plaintext content and no envelopes', () => { |
| 48 | + const result = validateMessagePayload({ |
| 49 | + contentType: 'text', |
| 50 | + // @ts-expect-error - deliberately probing for a plaintext field the type doesn't allow |
| 51 | + plaintext: 'hello in the clear', |
| 52 | + }); |
| 53 | + expect(result.ok).toBe(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it('rejects a text message that supplies ciphertext but no per-device envelopes', () => { |
| 57 | + const result = validateMessagePayload({ |
| 58 | + contentType: 'text', |
| 59 | + ciphertext: 'some-ciphertext', |
| 60 | + }); |
| 61 | + expect(result.ok).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it('accepts a text message only when envelopes carry the encrypted key', () => { |
| 65 | + const result = validateMessagePayload({ |
| 66 | + contentType: 'text', |
| 67 | + envelopes: [{ recipientDeviceId: 'device-1', ciphertext: 'enc-key' }], |
| 68 | + }); |
| 69 | + expect(result.ok).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it('REST SendMessageSchema has no plaintext field', () => { |
| 73 | + const shape = SendMessageSchema.shape as Record<string, unknown>; |
| 74 | + expect(Object.keys(shape)).not.toContain('plaintext'); |
| 75 | + expect(Object.keys(shape)).not.toContain('plainText'); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('security regression: no private-key/session-state field is ever accepted', () => { |
| 80 | + const sourceFiles = listSourceFiles(SRC_ROOT); |
| 81 | + |
| 82 | + it('scanned at least one route/schema file', () => { |
| 83 | + expect(sourceFiles.length).toBeGreaterThan(0); |
| 84 | + }); |
| 85 | + |
| 86 | + it.each(FORBIDDEN_FIELD_NAMES)('no source file declares a "%s" field', (fieldName) => { |
| 87 | + const offenders: string[] = []; |
| 88 | + // Matches z.object key declarations and TS interface/type field declarations, |
| 89 | + // e.g. `privateKey:` — not matched inside comments-only prose or unrelated words. |
| 90 | + const pattern = new RegExp(`(^|[^A-Za-z0-9_])${fieldName}\\s*[:?]\\s*[^,]`, 'm'); |
| 91 | + |
| 92 | + for (const file of sourceFiles) { |
| 93 | + const content = readFileSync(file, 'utf-8'); |
| 94 | + if (pattern.test(content)) { |
| 95 | + offenders.push(file); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + expect(offenders).toEqual([]); |
| 100 | + }); |
| 101 | +}); |
0 commit comments