|
| 1 | +// Defense-in-depth tests for the runtime null short-circuits restored |
| 2 | +// across the encryption operation classes. These hit the operation |
| 3 | +// constructors directly rather than going through `Encryption()` so they |
| 4 | +// don't need credentials or a network — the guard short-circuits before |
| 5 | +// any FFI call. See `fix(stack): restore runtime null guards in |
| 6 | +// encryption operations` for context. |
| 7 | + |
| 8 | +import { BatchEncryptQueryOperation } from '@/encryption/operations/batch-encrypt-query' |
| 9 | +import { BulkDecryptOperation } from '@/encryption/operations/bulk-decrypt' |
| 10 | +import { BulkEncryptOperation } from '@/encryption/operations/bulk-encrypt' |
| 11 | +import { DecryptOperation } from '@/encryption/operations/decrypt' |
| 12 | +import { EncryptQueryOperation } from '@/encryption/operations/encrypt-query' |
| 13 | +import { EncryptOperation } from '@/encryption/operations/encrypt' |
| 14 | +import { encryptedColumn, encryptedTable } from '@/schema' |
| 15 | +import { describe, expect, it } from 'vitest' |
| 16 | + |
| 17 | +const table = encryptedTable('null-guards-test', { |
| 18 | + metadata: encryptedColumn('metadata').searchableJson(), |
| 19 | +}) |
| 20 | + |
| 21 | +// Any truthy stand-in — the guard returns before the client is touched. |
| 22 | +const stubClient = {} as any |
| 23 | + |
| 24 | +describe('runtime null guards (defense in depth)', () => { |
| 25 | + it('encrypt(null) short-circuits without an FFI call', async () => { |
| 26 | + const op = new EncryptOperation(stubClient, null, { |
| 27 | + column: table.metadata, |
| 28 | + table, |
| 29 | + }) |
| 30 | + const result = await op.execute() |
| 31 | + if (result.failure) throw new Error(result.failure.message) |
| 32 | + expect(result.data).toBeNull() |
| 33 | + }) |
| 34 | + |
| 35 | + it('decrypt(null) short-circuits without an FFI call', async () => { |
| 36 | + const op = new DecryptOperation(stubClient, null) |
| 37 | + const result = await op.execute() |
| 38 | + if (result.failure) throw new Error(result.failure.message) |
| 39 | + expect(result.data).toBeNull() |
| 40 | + }) |
| 41 | + |
| 42 | + it('bulkEncrypt preserves null positions in mixed arrays', async () => { |
| 43 | + // Single null-only input — no FFI call should happen, the all-null |
| 44 | + // fast path returns a {data: null} placeholder per element. |
| 45 | + const op = new BulkEncryptOperation( |
| 46 | + stubClient, |
| 47 | + [{ id: 'a', plaintext: null }], |
| 48 | + { column: table.metadata, table }, |
| 49 | + ) |
| 50 | + const result = await op.execute() |
| 51 | + if (result.failure) throw new Error(result.failure.message) |
| 52 | + expect(result.data).toEqual([{ id: 'a', data: null }]) |
| 53 | + }) |
| 54 | + |
| 55 | + it('bulkDecrypt preserves null positions in mixed arrays', async () => { |
| 56 | + const op = new BulkDecryptOperation(stubClient, [{ id: 'a', data: null }]) |
| 57 | + const result = await op.execute() |
| 58 | + if (result.failure) throw new Error(result.failure.message) |
| 59 | + expect(result.data).toEqual([{ id: 'a', data: null }]) |
| 60 | + }) |
| 61 | + |
| 62 | + it('encryptQuery(null) short-circuits to { data: null }', async () => { |
| 63 | + const op = new EncryptQueryOperation(stubClient, null, { |
| 64 | + column: table.metadata, |
| 65 | + table, |
| 66 | + queryType: 'steVecSelector', |
| 67 | + returnType: 'composite-literal', |
| 68 | + } as any) |
| 69 | + const result = await op.execute() |
| 70 | + if (result.failure) throw new Error(result.failure.message) |
| 71 | + expect(result.data).toBeNull() |
| 72 | + }) |
| 73 | + |
| 74 | + it('encryptQuery(undefined) short-circuits to { data: null }', async () => { |
| 75 | + const op = new EncryptQueryOperation(stubClient, undefined, { |
| 76 | + column: table.metadata, |
| 77 | + table, |
| 78 | + queryType: 'steVecSelector', |
| 79 | + returnType: 'composite-literal', |
| 80 | + } as any) |
| 81 | + const result = await op.execute() |
| 82 | + if (result.failure) throw new Error(result.failure.message) |
| 83 | + expect(result.data).toBeNull() |
| 84 | + }) |
| 85 | + |
| 86 | + it('batchEncryptQuery preserves null/undefined slots position-stably', async () => { |
| 87 | + // All-null/undefined batch — no FFI call needed. |
| 88 | + const op = new BatchEncryptQueryOperation(stubClient, [ |
| 89 | + { |
| 90 | + column: table.metadata, |
| 91 | + table, |
| 92 | + queryType: 'steVecSelector', |
| 93 | + returnType: 'composite-literal', |
| 94 | + value: null, |
| 95 | + } as any, |
| 96 | + { |
| 97 | + column: table.metadata, |
| 98 | + table, |
| 99 | + queryType: 'steVecSelector', |
| 100 | + returnType: 'composite-literal', |
| 101 | + value: undefined, |
| 102 | + } as any, |
| 103 | + ]) |
| 104 | + const result = await op.execute() |
| 105 | + if (result.failure) throw new Error(result.failure.message) |
| 106 | + expect(result.data).toEqual([null, null]) |
| 107 | + }) |
| 108 | +}) |
0 commit comments