-
Notifications
You must be signed in to change notification settings - Fork 6
feat(stack): EQL v3 JSON support — types.Json + containment #621
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
50c0a9c
feat(stack): add EQL v3 JSON columns (types.Json, ste_vec) — model + …
coderdan d84a573
feat(stack): EQL v3 JSON containment via Drizzle @> operator
coderdan 82f2e69
docs(skills): document EQL v3 types.Json + JSON containment
coderdan df5a532
fix(stack): correct v3 JSON payload/type shapes surfaced by CI
coderdan bf526c4
fix(test-kit): document fully-deferred families instead of throwing
coderdan 05e0cf8
test(stack): update supabase introspect for the now-modelled json domain
coderdan 58b9ef8
fix(stack): address v3 JSON review findings (guards, type, tests, doc)
coderdan e27ce9d
refactor(stack): review wording/hygiene for v3 JSON (no behaviour cha…
coderdan f4d45dd
Merge remote-tracking branch 'origin/feat/eql-v3-text-search-schema' …
coderdan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| 'stash': patch | ||
| --- | ||
|
|
||
| Document EQL v3 JSON columns in the bundled skills: `types.Json` in the | ||
| `stash-encryption` typed-schema catalog (capability suffix, family, and an | ||
| encrypted-JSONB query section), and `contains(col, subObject)` JSON containment | ||
| on the v3 Drizzle operators in `stash-drizzle`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --- | ||
| '@cipherstash/stack': minor | ||
| --- | ||
|
|
||
| Add EQL v3 JSON columns. `types.Json('col')` declares a `public.eql_v3_json` | ||
| column that encrypts a JSON document to an ste_vec `SteVecDocument` and | ||
| round-trips it losslessly through `encrypt`/`decrypt` and the model path. A new | ||
| `searchableJson` query capability emits the ste_vec index; the index uses | ||
| `mode: 'compat'`, which eql-3.0.0's `eql_v3_json` requires (it orders ste_vec | ||
| entries by the CLLW-OPE `op` term, so v2's `'standard'`/CLLW-`oc` terms are | ||
| rejected). | ||
|
|
||
| The Drizzle integration's `contains(col, subObject)` now answers encrypted-JSONB | ||
| containment on a `types.Json` column, emitting the `@>` operator with a | ||
| `query_jsonb` needle (from `encryptQuery`). The ste_vec index indexes array | ||
| elements by identity but not position, so containment is a true subset test | ||
| (`{ roles: ['x'] }` matches any document whose `roles` array contains `x`, | ||
| regardless of index). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/stack/integration/drizzle-v3/json-contains.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /** | ||
|
coderdan marked this conversation as resolved.
|
||
| * Live JSON containment for the v3 `types.Json()` column through the Drizzle | ||
| * operators. Seeds encrypted JSONB documents and queries them with | ||
| * `ops.contains(col, subObject)` — the same operator text search uses, but on a | ||
| * `json` column it emits the `@>` operator over the encrypted JSONB document | ||
| * (json has no `eql_v3.contains` overload) with a `query_jsonb` needle from | ||
| * `encryptQuery` — asserting it returns exactly the rows whose document contains | ||
| * the sub-object (jsonb `@>` semantics), and excludes the rest. | ||
| */ | ||
| import { databaseUrl, unwrapResult } from '@cipherstash/test-kit' | ||
| import { and, asc, eq, type SQL } from 'drizzle-orm' | ||
| import { integer, pgTable, text } from 'drizzle-orm/pg-core' | ||
| import { drizzle } from 'drizzle-orm/postgres-js' | ||
| import postgres from 'postgres' | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
| import { EncryptionV3 } from '@/encryption/v3' | ||
| import type { JsonDocument } from '@/eql/v3' | ||
| import { | ||
| createEncryptionOperatorsV3, | ||
| extractEncryptionSchemaV3, | ||
| types, | ||
| } from '@/eql/v3/drizzle' | ||
|
|
||
| const sqlClient = postgres(databaseUrl(), { prepare: false }) | ||
|
|
||
| const TABLE_NAME = 'protect_ci_v3_json_contains' | ||
| const RUN = `run-${Date.now()}-${Math.random().toString(36).slice(2, 8)}` | ||
|
|
||
| const docTable = pgTable(TABLE_NAME, { | ||
| id: integer('id').primaryKey().generatedAlwaysAsIdentity(), | ||
| rowKey: text('row_key').notNull(), | ||
| testRunId: text('test_run_id').notNull(), | ||
| doc: types.Json('doc'), | ||
| }) | ||
|
|
||
| const schema = extractEncryptionSchemaV3(docTable) | ||
|
|
||
| // Distinct documents so each containment query has a definite expected set. | ||
| const DOCS: Record<string, JsonDocument> = { | ||
| ada: { user: 'ada@example.com', roles: ['admin', 'eng'], active: true }, | ||
| grace: { user: 'grace@example.com', roles: ['eng'] }, | ||
| zoe: { user: 'zoe@example.com', roles: ['ops'], active: false }, | ||
| } | ||
|
|
||
| type SelectRow = { rowKey: string } | ||
| let client: Awaited<ReturnType<typeof EncryptionV3>> | ||
| let ops: ReturnType<typeof createEncryptionOperatorsV3> | ||
| let db: ReturnType<typeof drizzle> | ||
|
|
||
| async function matching(condition: SQL): Promise<string[]> { | ||
| const rows = (await db | ||
| .select({ rowKey: docTable.rowKey }) | ||
| .from(docTable) | ||
| .where(and(eq(docTable.testRunId, RUN), condition)) | ||
| .orderBy(asc(docTable.rowKey))) as SelectRow[] | ||
| return rows.map((row) => row.rowKey) | ||
| } | ||
|
|
||
| beforeAll(async () => { | ||
| // EQL v3 is installed once per run by `global-setup.ts`. | ||
| client = await EncryptionV3({ schemas: [schema] }) | ||
| ops = createEncryptionOperatorsV3(client) | ||
| db = drizzle({ client: sqlClient }) | ||
|
|
||
| await sqlClient.unsafe(`DROP TABLE IF EXISTS ${TABLE_NAME}`) | ||
| await sqlClient.unsafe(` | ||
| CREATE TABLE ${TABLE_NAME} ( | ||
| id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY, | ||
| row_key TEXT NOT NULL, | ||
| test_run_id TEXT NOT NULL, | ||
| doc public.eql_v3_json NOT NULL | ||
| ) | ||
| `) | ||
|
|
||
| const rows = Object.entries(DOCS).map(([rowKey, doc]) => ({ | ||
| rowKey, | ||
| testRunId: RUN, | ||
| doc, | ||
| })) | ||
| const encrypted = unwrapResult( | ||
| await client.bulkEncryptModels(rows, schema), | ||
| ) as Array<Record<string, unknown>> | ||
| await db.insert(docTable).values(encrypted as never) | ||
| }, 120000) | ||
|
|
||
| afterAll(async () => { | ||
| await sqlClient`DELETE FROM ${sqlClient(TABLE_NAME)} WHERE test_run_id = ${RUN}` | ||
| await sqlClient.end() | ||
| }, 30000) | ||
|
|
||
| describe('v3 drizzle JSON containment (live pg)', () => { | ||
| it('matches every document containing a sub-object (array element containment)', async () => { | ||
| // `{ roles: ['eng'] }` ⊆ ada (admin,eng) and grace (eng), not zoe (ops). | ||
| const condition = await ops.contains(docTable.doc, { roles: ['eng'] }) | ||
| expect(await matching(condition)).toEqual(['ada', 'grace']) | ||
| }, 30000) | ||
|
|
||
| it('matches on a scalar field', async () => { | ||
| const condition = await ops.contains(docTable.doc, { | ||
| user: 'zoe@example.com', | ||
| }) | ||
| expect(await matching(condition)).toEqual(['zoe']) | ||
| }, 30000) | ||
|
|
||
| it('matches on a nested boolean', async () => { | ||
| const condition = await ops.contains(docTable.doc, { active: true }) | ||
| expect(await matching(condition)).toEqual(['ada']) | ||
| }, 30000) | ||
|
|
||
| it('returns nothing for a sub-object no document contains', async () => { | ||
|
coderdan marked this conversation as resolved.
|
||
| const condition = await ops.contains(docTable.doc, { roles: ['nope'] }) | ||
| expect(await matching(condition)).toEqual([]) | ||
| }, 30000) | ||
|
|
||
| // `doc @> '{}'` holds for every row (jsonb `{} ⊆ anything`), so an empty-object | ||
| // needle would silently return the whole table — the same whole-table footgun | ||
| // the bloom path rejects. The operator must refuse it before querying, rather | ||
| // than leak every row. | ||
| it('rejects an empty-object needle before querying', async () => { | ||
| await expect(ops.contains(docTable.doc, {})).rejects.toThrow( | ||
| /matches every row/, | ||
| ) | ||
| }, 30000) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.