Skip to content

Commit 6a1b4c5

Browse files
committed
fix(db): reject seal-account-tokens payloads without a boolean dryRun
1 parent 24d6fba commit 6a1b4c5

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

‎apps/sim/background/seal-account-tokens.test.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
*/
44

55
import { sealingKeyId } from '@sim/security/encryption'
6-
import { databaseMockFns } from '@sim/testing'
7-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
6+
import { databaseMockFns } from '@sim/testing/mocks/database.mock'
7+
import { beforeEach, describe, expect, it, vi } from 'vitest'
88

99
const { mockPostgres } = vi.hoisted(() => ({ mockPostgres: vi.fn() }))
1010

1111
vi.mock('postgres', () => ({ default: mockPostgres }))
1212
/** The pass derives each column's sealing context from the real column definitions. */
1313
vi.unmock('@sim/db/schema')
1414

15-
import { runSealAccountTokens } from '@/background/seal-account-tokens'
15+
import {
16+
runSealAccountTokens,
17+
type SealAccountTokensPayload,
18+
} from '@/background/seal-account-tokens'
1619

1720
const ENCRYPTION_KEY = 'ab'.repeat(32)
1821
const WORKER_KEY_ID = sealingKeyId(Buffer.from(ENCRYPTION_KEY, 'hex'))
@@ -31,17 +34,20 @@ describe('runSealAccountTokens', () => {
3134
mockPostgres.mockImplementation(emptyAccountTable)
3235
})
3336

34-
afterEach(() => {
35-
vi.unstubAllEnvs()
36-
})
37-
3837
it('refuses a real seal under a key id other than the worker’s, before connecting', async () => {
3938
await expect(
4039
runSealAccountTokens({ mode: 'seal', dryRun: false, expectedKeyId: 'deadbeef' })
4140
).rejects.toThrow(`ENCRYPTION_KEY has key id ${WORKER_KEY_ID}, not the expected deadbeef`)
4241
expect(mockPostgres).not.toHaveBeenCalled()
4342
})
4443

44+
it('refuses a payload without dryRun instead of treating it as a real run', async () => {
45+
const payload = { mode: 'seal', expectedKeyId: WORKER_KEY_ID } as SealAccountTokensPayload
46+
47+
await expect(runSealAccountTokens(payload)).rejects.toThrow('dryRun must be a boolean')
48+
expect(mockPostgres).not.toHaveBeenCalled()
49+
})
50+
4551
it('refuses to seal while this worker writes plaintext tokens, before connecting', async () => {
4652
vi.stubEnv('ENCRYPT_ACCOUNT_TOKENS', 'false')
4753

‎apps/sim/background/seal-account-tokens.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ function assertIntegerInRange(name: string, value: number | undefined, min: numb
4141
export async function runSealAccountTokens(payload: SealAccountTokensPayload) {
4242
const { mode, dryRun, expectedKeyId, batchSize, pauseMs } = payload
4343
if (mode !== 'seal' && mode !== 'unseal') throw new Error(`Unknown mode ${String(mode)}`)
44+
if (typeof dryRun !== 'boolean') throw new Error('dryRun must be a boolean')
45+
if (typeof expectedKeyId !== 'string') throw new Error('expectedKeyId must be a string')
4446
assertIntegerInRange('batchSize', batchSize, 1, MAX_BATCH_SIZE)
4547
assertIntegerInRange('pauseMs', pauseMs, 0, MAX_PAUSE_MS)
4648

0 commit comments

Comments
 (0)