Repository navigation
feat(recipher-js): encrypt a VcUuid as the engine's UUID #1996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/recipher-js-records
Are you sure you want to change the base?
Changes from all commits
56a10b0
eba70fb
03eb7a7
9133b31
a868956
fc94103
aef3c32
7b9d19b
40b4516
d9447bc
4d11d0c
306b59f
a2d1fe0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { | |
| passthrough, | ||
| RcDate, | ||
| RcDecimal, | ||
| RcUuid, | ||
| record, | ||
| } from '../../src/index' | ||
| import { testCipher } from '../../src/testing' | ||
|
|
@@ -128,6 +129,129 @@ describe('a record', () => { | |
| }) | ||
| }) | ||
|
|
||
| describe('a UUID field', () => { | ||
| const device = record('devices', { | ||
| id: encrypt(kinds.uuid, { index: ['equality'] }), | ||
| name: encrypt(kinds.string), | ||
| }) | ||
| const text = '123e4567-e89b-12d3-a456-426614174000' | ||
|
|
||
| it('is stored, found by equality, and read back as an RcUuid', async () => { | ||
| const c = await cipher() | ||
| const keyset = c.defaultKeyset() | ||
| const row = { id: new RcUuid(text), name: 'sensor' } | ||
| const stored = (await keyset.encrypt(device, row)).unwrap() | ||
| expect(Object.keys(stored.id).sort()).toEqual(['ciphertext', 'equality']) | ||
|
|
||
| // A query term for the same UUID matches the stored term. A query term | ||
| // for another UUID does not. | ||
| const same = ( | ||
| await keyset.encryptField(device, 'id', new RcUuid(text)) | ||
| ).unwrap() | ||
| const other = ( | ||
| await keyset.encryptField( | ||
| device, | ||
| 'id', | ||
| new RcUuid('123e4567-e89b-12d3-a456-426614174001'), | ||
| ) | ||
| ).unwrap() | ||
| expect(Buffer.from(same.equality)).toEqual(Buffer.from(stored.id.equality)) | ||
| expect(Buffer.from(other.equality)).not.toEqual( | ||
| Buffer.from(stored.id.equality), | ||
| ) | ||
|
|
||
| const opened = (await c.decrypt(device, stored)).unwrap() | ||
| expect(opened.id).toBeInstanceOf(RcUuid) | ||
| expect(opened.id.value).toBe(text) | ||
| expect(opened.name).toBe('sensor') | ||
| }) | ||
|
|
||
| it('refuses UUID text that is not hyphenated hex, naming the field', async () => { | ||
| const keyset = (await cipher()).defaultKeyset() | ||
| for (const bad of [ | ||
| '123e4567-e89b-12d3-a456-42661417400', | ||
| '{123e4567-e89b-12d3-a456-426614174000}', | ||
| '123e4567-e89b-12d3-a456-42661417400g', | ||
| ]) { | ||
| const result = await keyset.encrypt(device, { | ||
| id: new RcUuid(bad), | ||
| name: 'sensor', | ||
| }) | ||
| expect(result.isErr(), bad).toBe(true) | ||
| expect(result.err()?.code, bad).toBe('ERR_INVALID_UUID') | ||
| expect(result.err()?.field, bad).toBe('id') | ||
| expect(result.err()?.record, bad).toBe('devices') | ||
| } | ||
| }) | ||
|
|
||
| it('stores and decrypts a null under each null setting, and refuses one by default', async () => { | ||
| const nullable = record('nullable_devices', { | ||
| sealed: encrypt(kinds.uuid, { index: ['equality'], null: 'sealed' }), | ||
| sql: encrypt(kinds.uuid, { null: 'sql' }), | ||
| required: encrypt(kinds.uuid), | ||
| }) | ||
| const c = await cipher() | ||
| const keyset = c.defaultKeyset() | ||
| const id = new RcUuid(text) | ||
|
|
||
| const empty = { sealed: null, sql: null, required: id } | ||
| const stored = (await keyset.encrypt(nullable, empty)).unwrap() | ||
| // A sealed null is stored like a value, padded to a value's length. | ||
| // A SQL null is stored as null. | ||
| const full = ( | ||
| await keyset.encrypt(nullable, { sealed: id, sql: id, required: id }) | ||
| ).unwrap() | ||
| expect(stored.sealed).not.toBeNull() | ||
| expect(stored.sealed.ciphertext.length).toBe(full.sealed.ciphertext.length) | ||
| expect(Buffer.from(stored.sealed.equality)).not.toEqual( | ||
| Buffer.from(full.sealed.equality), | ||
| ) | ||
| expect(stored.sql).toBeNull() | ||
|
|
||
| const opened = (await c.decrypt(nullable, stored)).unwrap() | ||
| expect(opened.sealed).toBeNull() | ||
| expect(opened.sql).toBeNull() | ||
| expect(opened.required.value).toBe(text) | ||
|
|
||
| // A field with the default null setting accepts no null. | ||
| const row = { sealed: id, sql: id, required: null } | ||
| // @ts-expect-error: `required` has no null setting, so it accepts no null. | ||
| const refused = await keyset.encrypt(nullable, row) | ||
| expect(refused.err()?.code).toBe('stack_encrypt::dynamic_source') | ||
| expect(refused.err()?.field).toBe('required') | ||
| expect(refused.err()?.record).toBe('nullable_devices') | ||
| }) | ||
|
|
||
| it('refuses the UUID as a string', async () => { | ||
| const keyset = (await cipher()).defaultKeyset() | ||
| const result = await keyset.encrypt(device, { | ||
| // @ts-expect-error: a UUID field accepts an RcUuid, not its text. | ||
| id: text, | ||
| name: 'sensor', | ||
| }) | ||
| expect(result.isErr()).toBe(true) | ||
| expect(result.err()?.code).toBe('recipher_js::field_type') | ||
| expect(result.err()?.field).toBe('id') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor This test checks only Source: code-review, mattpocock-skills:code-review · claude-opus-5-5 Generated by Claude Code
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in a868956. The test now also asserts the code |
||
| expect(result.err()?.record).toBe('devices') | ||
| }) | ||
|
|
||
| it('refuses an order index when the declaration is checked', async () => { | ||
| for (const index of ['ore', 'ope'] as const) { | ||
| const ordered = record('devices', { | ||
| id: encrypt(kinds.uuid, { index: [index] }), | ||
| }) | ||
| const checked = checkDeclarations([ordered]) | ||
| expect(checked.err()?.code).toBe('stack_encrypt::dynamic_plan') | ||
| expect(checked.err()?.fields.reason).toBe('index_not_admitted') | ||
| expect(checked.err()?.field).toBe('id') | ||
| expect(checked.err()?.record).toBe('devices') | ||
|
|
||
| const result = await Cipher.create({ records: [ordered] }) | ||
| expect(result.err()?.fields.reason).toBe('index_not_admitted') | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| describe('one field', () => { | ||
| it('encrypts alone to the form it has in a whole row', async () => { | ||
| const c = await cipher() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,8 @@ async function encryptOne( | |
| | Date | ||
| | RcDate | ||
| | RcTimestamp | ||
| | RcDecimal, | ||
| | RcDecimal | ||
| | RcUuid, | ||
| ) { | ||
| return (await c.defaultKeyset().encrypt(value)).unwrap() | ||
| } | ||
|
|
@@ -104,6 +105,22 @@ describe('every value in the mapping round-trips', () => { | |
| expect(price).toBeInstanceOf(RcDecimal) | ||
| expect((price as RcDecimal).value).toBe('1.50') | ||
| }) | ||
|
|
||
| it('a UUID, as an RcUuid', async () => { | ||
| const id = await roundTrip( | ||
| new RcUuid('123e4567-e89b-12d3-a456-426614174000'), | ||
| ) | ||
| expect(id).toBeInstanceOf(RcUuid) | ||
| expect((id as RcUuid).value).toBe('123e4567-e89b-12d3-a456-426614174000') | ||
| }) | ||
|
|
||
| it('a UUID built in uppercase, which decrypts as lowercase', async () => { | ||
| const id = await roundTrip( | ||
| new RcUuid('123E4567-E89B-12D3-A456-426614174ABC'), | ||
| ) | ||
| expect(id).toBeInstanceOf(RcUuid) | ||
| expect((id as RcUuid).value).toBe('123e4567-e89b-12d3-a456-426614174abc') | ||
| }) | ||
| }) | ||
|
|
||
| describe('a wrapper class', () => { | ||
|
|
@@ -635,6 +652,23 @@ describe('a ciphertext read back with a kind', () => { | |
| expect(opened).toBe(42n) | ||
| }) | ||
|
|
||
| it('opens a UUID with kinds.uuid, and refuses its text', async () => { | ||
| const c = await cipher() | ||
| const keyset = c.defaultKeyset() | ||
| const text = '123e4567-e89b-12d3-a456-426614174000' | ||
| const sealed = (await keyset.encrypt(new RcUuid(text))).unwrap() | ||
| const stored = Ciphertext.fromBytes(sealed.toBytes(), kinds.uuid).unwrap() | ||
| const opened: RcUuid = (await c.decrypt(stored)).unwrap() | ||
| expect(opened.value).toBe(text) | ||
|
|
||
| // The same UUID as a string is text, not a UUID. | ||
| const asText = (await keyset.encrypt(text)).unwrap() | ||
| const refused = await c.decrypt( | ||
| Ciphertext.fromBytes(asText.toBytes(), kinds.uuid).unwrap(), | ||
| ) | ||
| expect(refused.err()?.code).toBe('recipher_js::opened_type') | ||
| }) | ||
|
|
||
| it('refuses a value sealed as another type', async () => { | ||
| const c = await cipher() | ||
| const sealed = (await c.defaultKeyset().encrypt('text')).unwrap() | ||
|
|
@@ -715,7 +749,12 @@ describe('a declared kind', () => { | |
| // refuses it. When it merges, this test fails: change it to check that | ||
| // the null encrypts and decrypts as null. | ||
| const keyset = (await cipher()).defaultKeyset() | ||
| for (const kind of [kinds.int64, kinds.boolean, kinds.timestamp]) { | ||
| for (const kind of [ | ||
| kinds.int64, | ||
| kinds.boolean, | ||
| kinds.timestamp, | ||
| kinds.uuid, | ||
| ]) { | ||
| const result = await keyset.encrypt(null, { kind }) | ||
| expect(result.err()?.code).toBe('recipher_js::null_value') | ||
| } | ||
|
|
@@ -747,21 +786,33 @@ describe('a declared kind', () => { | |
| } | ||
| // The type checker takes any `{ name }` as a kind, so this check is | ||
| // made when the call runs. | ||
| const unknown = await keyset.encrypt(34, { kind: { name: 'uuid' } }) | ||
| const unknown = await keyset.encrypt(34, { kind: { name: 'text' } }) | ||
| expect(unknown.err()?.code).toBe('recipher_js::not_a_kind') | ||
| }) | ||
| }) | ||
|
|
||
| describe('refusals', () => { | ||
|
coderdan marked this conversation as resolved.
Resolved
|
||
| it('a UUID: this version of the SDK does not encrypt one yet', async () => { | ||
| it('UUID text that is not hyphenated hex, as an Err', async () => { | ||
| const keyset = (await cipher()).defaultKeyset() | ||
| const uuid = new RcUuid('123e4567-e89b-12d3-a456-426614174000') | ||
| // @ts-expect-error: a UUID is not a `Scalar` in this version of the SDK. | ||
| const result = await keyset.encrypt(uuid) | ||
| expect(result.err()?.code).toBe('recipher_js::uuid') | ||
| expect(result.err()?.message).toContain( | ||
| 'this version of the SDK does not encrypt a UUID yet', | ||
| ) | ||
| for (const text of [ | ||
| // One digit short. | ||
| '123e4567-e89b-12d3-a456-42661417400', | ||
| // In braces. | ||
| '{123e4567-e89b-12d3-a456-426614174000}', | ||
| // A digit that is not hex. | ||
| '123e4567-e89b-12d3-a456-42661417400g', | ||
| ]) { | ||
| const pending = keyset.encrypt(new RcUuid(text)) | ||
| // A synchronous throw would fail the line above. This line fails if | ||
| // `encrypt` returns a settled `Result` instead of a promise. `await` | ||
| // passes a value that is not a promise through unchanged, so the | ||
| // assertions below would still pass. | ||
| expect(pending, text).toBeInstanceOf(Promise) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor Add a comment to say why this assertion is here. It catches Source: mattpocock-skills:code-review · claude-opus-5-5 Generated by Claude Code
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in a868956. A comment above the assertion says it catches |
||
| const result = await pending | ||
| expect(result.isErr(), text).toBe(true) | ||
| expect(result.err()?.code, text).toBe('ERR_INVALID_UUID') | ||
| expect(result.err()?.message, text).not.toContain(text) | ||
| } | ||
| }) | ||
|
|
||
| it('a bigint outside 128 bits, with the code ERR_INTEGER_RANGE', async () => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.