Skip to content

Commit 4b8ae55

Browse files
committed
fix: throw explicit error when using unknown encrypter
1 parent 5e226bd commit 4b8ae55

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/encryption_manager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export class EncryptionManager<KnownEncrypters extends Record<string, Encryption
5858
return cachedEncryption
5959
}
6060

61+
if (!Object.hasOwn(this.#config.list, encrypterToUse)) {
62+
throw new RuntimeException(
63+
`Cannot create encryption instance. Encryption "${String(encrypterToUse)}" is not defined in the config`
64+
)
65+
}
66+
6167
const encrypterConfig = this.#config.list[encrypterToUse]
6268

6369
/**

tests/encryption_manager.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ test.group('Encryption manager', () => {
7171
)
7272
})
7373

74+
test('fail when encrypter is not configured', ({ assert }) => {
75+
const manager = new EncryptionManager({
76+
default: 'legacy',
77+
list: {
78+
legacy: chacha20poly1305({ id: 'nova', keys: [SECRET] }),
79+
},
80+
})
81+
82+
assert.throws(
83+
// @ts-expect-error testing runtime guard
84+
() => manager.use('missing'),
85+
'Cannot create encryption instance. Encryption "missing" is not defined in the config'
86+
)
87+
})
88+
7489
test('use message verifier', ({ assert }) => {
7590
const manager = new EncryptionManager({
7691
default: 'legacy',

0 commit comments

Comments
 (0)