Skip to content

Commit 770da03

Browse files
committed
fix(drivers): reject empty or dotted encrypter ids
1 parent 4f660f7 commit 770da03

7 files changed

Lines changed: 77 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const encryption = new Encryption(
4545
)
4646
```
4747

48+
`id` must be a non-empty string and cannot contain `.`.
49+
4850
### 2. Encrypt & Decrypt
4951

5052
```typescript

src/drivers/base_driver.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export abstract class BaseDriver {
2727

2828
protected constructor(config: BaseConfig) {
2929
const key = this.#validateAndGetSecret(config.key)
30+
this.#validateId(config.id)
3031
this.cryptoKey = createHash('sha256').update(key).digest()
3132

3233
const rawBlindIndexKey = hkdfSync(
@@ -57,6 +58,19 @@ export abstract class BaseDriver {
5758
return revealedSecret
5859
}
5960

61+
/**
62+
* Validates encrypter id format when provided.
63+
*/
64+
#validateId(id?: string) {
65+
if (typeof id !== 'string') {
66+
return
67+
}
68+
69+
if (id.trim().length === 0 || id.includes(this.separator)) {
70+
throw new errors.E_INVALID_ENCRYPTER_ID()
71+
}
72+
}
73+
6074
protected computeReturns(values: string[]) {
6175
return values.join(this.separator) as CypherText
6276
}

src/exceptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export const E_MISSING_ENCRYPTER_ID = createError(
2222
'E_MISSING_ENCRYPTER_ID'
2323
)
2424

25+
export const E_INVALID_ENCRYPTER_ID = createError(
26+
'Invalid id. The id must be a non-empty string and cannot contain "."',
27+
'E_INVALID_ENCRYPTER_ID'
28+
)
29+
2530
export const E_DETERMINISTIC_DRIVER_EXPIRES_IN_NOT_SUPPORTED = createError(
2631
'Deterministic encryption does not support expiresIn',
2732
'E_DETERMINISTIC_DRIVER_EXPIRES_IN_NOT_SUPPORTED'

tests/drivers/aes_256_cbc.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ test.group('AES-256-CBC', () => {
3434
)
3535
})
3636

37+
test('fail when id contains separator', ({ assert }) => {
38+
assert.throws(
39+
() => new AES256CBC({ id: 'lan.z', key: SECRET }),
40+
'Invalid id. The id must be a non-empty string and cannot contain "."'
41+
)
42+
})
43+
44+
test('fail when id is empty', ({ assert }) => {
45+
assert.throws(
46+
() => new AES256CBC({ id: '', key: SECRET }),
47+
'Invalid id. The id must be a non-empty string and cannot contain "."'
48+
)
49+
})
50+
3751
test('encrypt value', ({ assert }) => {
3852
const encryption = new AES256CBC({ id: 'lanz', key: SECRET })
3953
assert.notEqual(encryption.encrypt('hello-world'), 'hello-world')

tests/drivers/aes_256_gcm.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ test.group('AES-256-GCM', () => {
3434
)
3535
})
3636

37+
test('fail when id contains separator', ({ assert }) => {
38+
assert.throws(
39+
() => new AES256GCM({ id: 'lan.z', key: SECRET }),
40+
'Invalid id. The id must be a non-empty string and cannot contain "."'
41+
)
42+
})
43+
44+
test('fail when id is empty', ({ assert }) => {
45+
assert.throws(
46+
() => new AES256GCM({ id: '', key: SECRET }),
47+
'Invalid id. The id must be a non-empty string and cannot contain "."'
48+
)
49+
})
50+
3751
test('encrypt value', ({ assert }) => {
3852
const driver = new AES256GCM({ id: 'lanz', key: SECRET })
3953

tests/drivers/aes_siv.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ test.group('AES-SIV', () => {
3434
)
3535
})
3636

37+
test('fail when id contains separator', ({ assert }) => {
38+
assert.throws(
39+
() => new AESSIV({ id: 'lan.z', key: SECRET }),
40+
'Invalid id. The id must be a non-empty string and cannot contain "."'
41+
)
42+
})
43+
44+
test('fail when id is empty', ({ assert }) => {
45+
assert.throws(
46+
() => new AESSIV({ id: '', key: SECRET }),
47+
'Invalid id. The id must be a non-empty string and cannot contain "."'
48+
)
49+
})
50+
3751
test('accept single key in deterministic driver config', ({ assert }) => {
3852
const config = aessiv({ id: 'lanz', key: SECRET })
3953
assert.equal(config.keys.length, 1)

tests/drivers/chacha20_poly1305.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ test.group('ChaCha20-Poly1305', () => {
3434
)
3535
})
3636

37+
test('fail when id contains separator', ({ assert }) => {
38+
assert.throws(
39+
() => new ChaCha20Poly1305({ id: 'lan.z', key: SECRET }),
40+
'Invalid id. The id must be a non-empty string and cannot contain "."'
41+
)
42+
})
43+
44+
test('fail when id is empty', ({ assert }) => {
45+
assert.throws(
46+
() => new ChaCha20Poly1305({ id: '', key: SECRET }),
47+
'Invalid id. The id must be a non-empty string and cannot contain "."'
48+
)
49+
})
50+
3751
test('encrypt value', ({ assert }) => {
3852
const driver = new ChaCha20Poly1305({ id: 'lanz', key: SECRET })
3953

0 commit comments

Comments
 (0)