Przeglądaj źródła

docs: an encrypted collection REFUSES new vector inserts, and my test missed it

The database found the case my measurement did not cover. On 2.17.0 and
2.17.1 as released, an encrypted collection refuses every new insert carrying
a vector - "Invalid JSON: type must be number, but is string" - because the
handler encrypts the whole body including _vector before the store extracts
the vector. An encrypted RAG collection is frozen for new embeddings. Fixed
on their main as 2.17.2, which is not released and not in apt, so the
constraint stands for anything deployable.

Rewritten rather than appended, per the rule added an hour ago: the claim was
wrong, so it reads as wrong now instead of sitting beside a newer one.

How my test missed it is the part worth keeping. I flipped a populated
collection and verified existing rows search at 1.0 before, after and across
the seal, that the export was byte-identical with the same content_sha256,
and that a re-import retrieved correctly. All still true - pre-flip vectors
live in the vector sub-db, not the body. But I never inserted a NEW row after
the flip, and my re-import went into a fresh collection that our own route
creates encrypted=false, so it could not have caught it either. Then I wrote
"safe for search and for backup", which was broader than what I had measured.

A check that answers a narrower question than the one asked, committed by the
person who spent the day pointing that out to others. The general form is now
recorded: when testing whether a state change is safe, test WRITES INTO the
new state, not only reads of what was there before it.

Unaffected: our credential collections are not vector collections, and every
user collection is created encrypted=false, so no tenant can reach this
without someone deliberately flipping one.
fszontagh 1 miesiąc temu
rodzic
commit
fbe62cd9a8
1 zmienionych plików z 4 dodań i 5 usunięć
  1. 4 5
      CLAUDE.md

+ 4 - 5
CLAUDE.md

@@ -161,11 +161,10 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
 - **Client ABI pin:** the server deb is compiled against `DB_CLIENT_VERSION` (`packaging/Dockerfile.base`). Every 2.x client shares one SONAME, so a deb built against a newer client installs fine and only segfaults on the next restart - keep the pin equal to the deploy target, and ship a DB upgrade and a vectorapi rebuild together.
 - **Client ABI pin:** the server deb is compiled against `DB_CLIENT_VERSION` (`packaging/Dockerfile.base`). Every 2.x client shares one SONAME, so a deb built against a newer client installs fine and only segfaults on the next restart - keep the pin equal to the deploy target, and ship a DB upgrade and a vectorapi rebuild together.
 - **The apt repo now indexes EVERY published version, so a rollback is an ordinary apt command.** It used to list only the newest, which made `pkg=<old version>` uninstallable the moment anything newer was published - and that was never retention, only indexing: `dpkg-scanpackages` emits one version per package unless given `-m`, while the pool had kept every `.deb` all along (a publish reported "3 added, 309 unchanged"). Fixed 2026-10-08 in the deb-repo tooling; the trixie index went from 33 stanzas to 1191. **A downgrade needs `--allow-downgrades`** (`apt-get install --allow-downgrades pkg=0.7.1-1 ...`), and downgrade the three app packages TOGETHER or apt resolves a split version set. A plain `apt install pkg` still picks the newest, so ordinary installs are unaffected. The backup step still stages the installed `.deb`s out of `/var/cache/apt/archives/`, which stays the right belt-and-braces for a rollback when the repo itself is unreachable.
 - **The apt repo now indexes EVERY published version, so a rollback is an ordinary apt command.** It used to list only the newest, which made `pkg=<old version>` uninstallable the moment anything newer was published - and that was never retention, only indexing: `dpkg-scanpackages` emits one version per package unless given `-m`, while the pool had kept every `.deb` all along (a publish reported "3 added, 309 unchanged"). Fixed 2026-10-08 in the deb-repo tooling; the trixie index went from 33 stanzas to 1191. **A downgrade needs `--allow-downgrades`** (`apt-get install --allow-downgrades pkg=0.7.1-1 ...`), and downgrade the three app packages TOGETHER or apt resolves a split version set. A plain `apt install pkg` still picks the newest, so ordinary installs are unaffected. The backup step still stages the installed `.deb`s out of `/var/cache/apt/archives/`, which stays the right belt-and-braces for a rollback when the repo itself is unreachable.
 - **"The dev host cannot build the C++" was a misdiagnosis - it was a stale `protoc`.** The host builds everything natively; use `scripts/host-build.sh`. The old symptom was real: generated protos wanted `google/protobuf/map_field_inl.h`, a header protobuf 35 removed. But nothing in the database's tree includes that header - an **old protoc emits the include**. The host had libprotobuf 35.1 and gRPC 1.83 from the distro, and a hand-dropped static **protoc 25.2 in `~/.local/bin`** shadowing everything, while the distro's compiler package (`protobuf-35.1`, separate from `protobuf-devel`, which ships no protoc) was never installed. `find_program(PROTOC protoc REQUIRED)` took the 25.2 binary from PATH, generated code for a protobuf 25 runtime and compiled it against 35 headers. With a matching protoc the client and all of vectorapi compile and link natively. `host-build.sh` therefore picks protoc **by version rather than PATH order** and refuses to run on a mismatch, because the failure otherwise surfaces as a missing header deep in generated code rather than as a version complaint.
 - **"The dev host cannot build the C++" was a misdiagnosis - it was a stale `protoc`.** The host builds everything natively; use `scripts/host-build.sh`. The old symptom was real: generated protos wanted `google/protobuf/map_field_inl.h`, a header protobuf 35 removed. But nothing in the database's tree includes that header - an **old protoc emits the include**. The host had libprotobuf 35.1 and gRPC 1.83 from the distro, and a hand-dropped static **protoc 25.2 in `~/.local/bin`** shadowing everything, while the distro's compiler package (`protobuf-35.1`, separate from `protobuf-devel`, which ships no protoc) was never installed. `find_program(PROTOC protoc REQUIRED)` took the 25.2 binary from PATH, generated code for a protobuf 25 runtime and compiled it against 35 headers. With a matching protoc the client and all of vectorapi compile and link natively. `host-build.sh` therefore picks protoc **by version rather than PATH order** and refuses to run on a mismatch, because the failure otherwise surfaces as a missing header deep in generated code rather than as a version complaint.
-- **ENCRYPTING A VECTOR COLLECTION IS SAFE FOR SEARCH AND FOR BACKUP. MEASURED, not reasoned.** The question arose because `encryptJsonFieldEx` reads "in a whole-document collection every other non-null scalar becomes `$ENCJ$`", walking arrays - which on its face turns a 4096-float vector into 4096 ciphertext strings, and because the body copy and the extracted vector index are separate things. The feared shape was the worst this estate collects: search keeps working off the index while the BODY copy becomes ciphertext, so the damage is invisible until a restore. Measured on a throwaway 2.17.1 instance (never the shared dev database), with a dim-4 collection, three known vectors, `createCollection(existing, encrypted=true)` flipping the flag on populated data:
-  - **Search is unaffected.** Each row is its own nearest neighbour at 1.0 before the flip, after it, and after the seal restart.
-  - **The export is BYTE-IDENTICAL**, same `content_sha256`, vectors as plain arrays and `label` in plaintext, because reads DECRYPT TRANSPARENTLY. Encryption is at rest only; nothing above the storage layer sees ciphertext.
-  - **Re-import retrieves correctly**, which is the load-bearing half a consumer insisted on: every restored row is its own nearest neighbour at 1.000000, and one fixed `query_vector` scores the rows identically to eight decimals in source and restored (`b=0.80000001, a=0.60000002, c=0.00000000` in both).
-  - **The cost is FILTERS, and it is total for that collection.** `label:eq:row-a` is refused: "cannot filter on a field stored encrypted ... its stored form is ciphertext, so any comparison would be silently wrong". So encrypting a collection trades every filter, sort, index and unique constraint on it. For a consumer that was decisive on three grounds: it removes the retrieval model on one collection, and on another it would silently convert a UNIQUE INDEX underpinning retry idempotency into a duplicate-write hazard.
+- **DO NOT SET `encrypted=true` ON A COLLECTION THAT STILL RECEIVES VECTORS. On database 2.17.0 and 2.17.1 as released, an encrypted collection REFUSES every new insert carrying a vector**, failing with `Invalid JSON: type must be number, but is string` - the handler encrypts the whole body, `_vector` included, before the store extracts the vector. So an encrypted RAG collection is FROZEN for new embeddings. Not silent loss, since the write is refused, but broken. Fixed on their `main` as 2.17.2 (`_vector` is never encrypted, everything else still is); **2.17.2 is NOT released and is not in apt**, so the constraint stands for anything we can actually deploy.
+  **MY OWN MEASUREMENT MISSED THIS, AND THE WAY IT MISSED IT IS THE POINT.** I tested a populated collection flipped to `encrypted=true` and verified: every row still its own nearest neighbour at 1.0 before, after and across the seal restart; the export BYTE-IDENTICAL with the same `content_sha256`; and a re-import retrieving correctly at 1.000000 with a fixed `query_vector` scoring identically to eight decimals. All of that is true and still holds - vectors stored BEFORE the flip live in the vector sub-db rather than the body, so they are untouched. **What I never did was insert a NEW row after the flip**, and my re-import went into a fresh collection which our own route creates `encrypted=false`, so it could not have caught it either. I then wrote "safe for search and for backup", which was broader than what I had measured. Same shape as everything else in this file: a check that answers a narrower question than the one being asked, committed by the person who had spent the day pointing that out to others. **When testing whether a state change is safe, test WRITES INTO the new state, not only reads of what was there before it.**
+  **What this does NOT affect:** our three credential collections are not vector collections, so encrypting them is unaffected; and `handlers/collections.cpp` creates every user collection `encrypted=false`, so a tenant cannot reach this unless someone deliberately flips one.
+  **The cost that remains true either way, and is usually decisive: FILTERS.** A filter on an encrypted field is refused outright ("its stored form is ciphertext, so any comparison would be silently wrong"), so encrypting a collection trades every filter, sort, index and unique constraint on it. For a consumer that was decisive on three grounds without needing the write freeze: it removes the retrieval model on one collection, and on another it would silently convert a UNIQUE INDEX underpinning retry idempotency into a duplicate-write hazard.
 - **`encryption-verify` REPORTS CLEAN WHILE THE PLAINTEXT IS STILL ON DISK.** Measured in the same run: immediately after the online upgrade, `grep -F` found the plaintext label in BOTH `wal/wal-current.log` and the project's `data.mdb`, while verify printed `vecenc:vecs [encrypted] ... plaintext-required 0` and `CLEAN`. The only contrary signal is a separate line, `at-rest migration sealed: NO (restart pending)` - easy to read past. After a restart the plaintext was gone and the line read `sealed: yes`. **So CLEAN means "everything that must be ciphertext is ciphertext in the live store", NOT "no plaintext remains anywhere". Read the `sealed` line, and until it says yes the old copies are still readable.** Same family as the flag that recorded an intention: a check that answers confidently about a narrower question than the one being asked.
 - **`encryption-verify` REPORTS CLEAN WHILE THE PLAINTEXT IS STILL ON DISK.** Measured in the same run: immediately after the online upgrade, `grep -F` found the plaintext label in BOTH `wal/wal-current.log` and the project's `data.mdb`, while verify printed `vecenc:vecs [encrypted] ... plaintext-required 0` and `CLEAN`. The only contrary signal is a separate line, `at-rest migration sealed: NO (restart pending)` - easy to read past. After a restart the plaintext was gone and the line read `sealed: yes`. **So CLEAN means "everything that must be ciphertext is ciphertext in the live store", NOT "no plaintext remains anywhere". Read the `sealed` line, and until it says yes the old copies are still readable.** Same family as the flag that recorded an intention: a check that answers confidently about a narrower question than the one being asked.
 - **`encrypted_field` (422) is its own slug, not `validation`.** Database 2.17.0 encrypts any field named `password`, `secret`, `token`, `api_key`, `apiKey` or `credentials` at ANY depth in EVERY collection, and then REFUSES a filter, sort, index, unique constraint, relation or increment on one. It needs its own slug because the remedy is unlike every other validation failure: the request is well formed, the field name is correct, and no correction of the body helps - the caller must stop querying that field, storing a non-sensitive duplicate (a hash, or a boolean "set") or renaming it if it was never a credential. Folding it into `validation` would send them hunting a typo. Note what it REPLACES is worse than itself: before 2.17.0 a top-level field of those names was plaintext and filterable, and a NESTED one compared ciphertext and **silently matched nothing** - so the refusal turns a silently empty result into a visible error. **Our `/documents` filter grammar, `?sort=` and the index route all accept ANY field name with no validation, so a TENANT can trigger this; we cannot.** Our own tests use `password` only as a `ctx.mail` option key, never as a stored field being queried.
 - **`encrypted_field` (422) is its own slug, not `validation`.** Database 2.17.0 encrypts any field named `password`, `secret`, `token`, `api_key`, `apiKey` or `credentials` at ANY depth in EVERY collection, and then REFUSES a filter, sort, index, unique constraint, relation or increment on one. It needs its own slug because the remedy is unlike every other validation failure: the request is well formed, the field name is correct, and no correction of the body helps - the caller must stop querying that field, storing a non-sensitive duplicate (a hash, or a boolean "set") or renaming it if it was never a credential. Folding it into `validation` would send them hunting a typo. Note what it REPLACES is worse than itself: before 2.17.0 a top-level field of those names was plaintext and filterable, and a NESTED one compared ciphertext and **silently matched nothing** - so the refusal turns a silently empty result into a visible error. **Our `/documents` filter grammar, `?sort=` and the index route all accept ANY field name with no validation, so a TENANT can trigger this; we cannot.** Our own tests use `password` only as a `ctx.mail` option key, never as a stored field being queried.
   **IT IS DORMANT UNTIL THE CLIENT MOVES TO 2.17.x, AND I TOLD A CONSUMER IT WAS LIVE.** Measured against a 2.17.1 server with our shipped 2.14.0 client: a filter on an encrypted field comes back as slug **`validation`**, not `encrypted_field`, even though the server's message arrives intact. The reason is the client, not our mapping - carrying the server's error reason on READ calls (`find`, `similaritySearch`) is a 2.17.0 client change, so on 2.14.0 the reason is simply absent and `translate()` falls through to `validation`. So the slug shipped in 0.12.0 cannot fire on prod today. It starts working when `DB_CLIENT_VERSION` moves, which is the same dependency as the index-route mapping, and it covers the FILTER path too rather than only indexes.
   **IT IS DORMANT UNTIL THE CLIENT MOVES TO 2.17.x, AND I TOLD A CONSUMER IT WAS LIVE.** Measured against a 2.17.1 server with our shipped 2.14.0 client: a filter on an encrypted field comes back as slug **`validation`**, not `encrypted_field`, even though the server's message arrives intact. The reason is the client, not our mapping - carrying the server's error reason on READ calls (`find`, `similaritySearch`) is a 2.17.0 client change, so on 2.14.0 the reason is simply absent and `translate()` falls through to `validation`. So the slug shipped in 0.12.0 cannot fire on prod today. It starts working when `DB_CLIENT_VERSION` moves, which is the same dependency as the index-route mapping, and it covers the FILTER path too rather than only indexes.