Skip to content

Commit 8380200

Browse files
authored
fix: support decrypting strings larger than 4 MiB (#123)
The `cloakedStringRegex` regex fails on ciphertexts larger than 4 MiB. The new `parseCloakedString` in [`@47ng/[email protected]`][1] doesn't have this limitation. [1]: https://github.com/47ng/cloak/releases/tag/v1.2.0 Fixes: #122
1 parent c17354c commit 8380200

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"migrate": "ts-node ./src/tests/migrate.ts"
4646
},
4747
"dependencies": {
48-
"@47ng/cloak": "^1.1.0",
48+
"@47ng/cloak": "^1.2.0",
4949
"@prisma/generator-helper": "^5.9.1",
5050
"debug": "^4.3.4",
5151
"immer": "^10.0.3",

Diff for: src/encryption.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
2-
cloakedStringRegex,
32
CloakKeychain,
43
decryptStringSync,
54
encryptStringSync,
65
findKeyForMessage,
76
makeKeychainSync,
87
ParsedCloakKey,
8+
parseCloakedString,
99
parseKeySync
1010
} from '@47ng/cloak'
1111
import { Draft, produce } from 'immer'
@@ -176,7 +176,7 @@ export function decryptOnRead<Models extends string, Actions extends string>(
176176
field
177177
}) {
178178
try {
179-
if (!cloakedStringRegex.test(cipherText)) {
179+
if (!parseCloakedString(cipherText)) {
180180
return
181181
}
182182
const decryptionKey = findKeyForMessage(cipherText, keys.keychain)

Diff for: src/tests/integration.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { cloakedStringRegex } from '@47ng/cloak'
2+
import { createHash } from 'node:crypto'
23
import fs from 'node:fs/promises'
34
import path from 'node:path'
45
import { errors } from '../errors'
@@ -453,4 +454,36 @@ describe.each(clients)('integration ($type)', ({ client }) => {
453454
expect(received!.name).toEqual(' François') // clear text in returned value
454455
expect(received!.email).toEqual(normalizeTestEmail)
455456
})
457+
458+
test('query field with 4 MiB+ data', async () => {
459+
const longNameUser = {
460+
name: 'a'.repeat(4_194_304),
461+
462+
} as const
463+
await client.user.upsert({
464+
where: {
465+
email: longNameUser.email
466+
},
467+
create: longNameUser,
468+
update: longNameUser
469+
})
470+
const savedUser = await client.user.findUniqueOrThrow({
471+
where: {
472+
email: longNameUser.email
473+
}
474+
})
475+
expect(savedUser.email).toStrictEqual(longNameUser.email)
476+
// The encrypted field is larger than the unencrypted field, so just comparing
477+
// the lengths is fine.
478+
expect(savedUser.name?.length).toStrictEqual(longNameUser.name.length)
479+
// Don't test for equality, otherwise we'd fill up the jest log with
480+
// a massive error message if something goes wrong.
481+
expect(
482+
createHash('sha256')
483+
.update(savedUser.name ?? '')
484+
.digest('hex')
485+
).toStrictEqual(
486+
createHash('sha256').update(longNameUser.name).digest('hex')
487+
)
488+
}, 15_000) // storing 4 MiB into the DB is a bit slow
456489
})

Diff for: yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# yarn lockfile v1
33

44

5-
"@47ng/cloak@^1.1.0":
6-
version "1.1.0"
7-
resolved "https://registry.yarnpkg.com/@47ng/cloak/-/cloak-1.1.0.tgz#4172ea63287d3c28aef3ac361fbe313d55647204"
8-
integrity sha512-47dVSPgjTiH3Fgt2CATudxJAU7Fv1WjFIo2e4tXG2UcQitqmIGf9GCgv1MnGv8ctNGPAy9gsjHnNujzuZ1bOow==
5+
"@47ng/cloak@^1.2.0":
6+
version "1.2.0"
7+
resolved "https://registry.yarnpkg.com/@47ng/cloak/-/cloak-1.2.0.tgz#7d811527b1530ac55196c7e41fc3bb3eb0c0ac82"
8+
integrity sha512-kKufIDIfW7+YdW+m/0PIFR9zsoIan04tCiAoWsvd2e/MsIh4HtU//n/xDk8eNTTl9cN/rI78qd1r2zCP0QB7hw==
99
dependencies:
1010
"@47ng/codec" "^1.0.1"
1111
"@stablelib/base64" "^1.0.1"

0 commit comments

Comments
 (0)