openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

CortexDB per-user scoping: enforce the tenant boundary (the direct wire bypasses it) and prove isolation via the TinyMemory conformance suite

Open Al629176 opened this issue 1 month ago • 5 comments

Description

Make per-user scoping on CortexDB enforced and proven, so a user can reach only their own memory, and verify it end to end through TinyMemory.

The scoping design already exists and is good. What does not exist is proof that the composed path holds, and there is one code path that bypasses the boundary entirely. Both are below, verified against the repos today.

Why this matters more than a normal scoping ticket: CortexDB does not enforce isolation itself. cortexdb-saas says so in its own README — "It is also the only boundary. There is no defence in depth behind it: a bug here is a cross-user read or write."

What the engine does not protect

cortexdb-saas records these probes against a live CortexDB v0.9.9 on cloud_shared_saas, with actors holding none of scope.read.holistic, scope.read.descend or scope.write.about_other:

Probe against a scope the caller does not belong to Engine's answer
POST /v1/recall 403 POLICY_DENIED
POST /v1/forget, /v1/forget/preview 403 POLICY_DENIED
GET /v1/events?scope=<other> 200 — returns the other scope's plaintext
POST /v1/experience into <other> 202 — accepted and stored
POST /v1/recall at a shared ancestor, view=descend 200 — returns every user beneath it

The vendor's position is that this is the application's job: isolation is "enforced through your application logic, not database-level guarantees." So three of five probes are cross-user reads or writes at the engine, by design.

What already protects it

cortexdb-saas is that application layer, and the approach is sound — relocation, not validation. Every scope a tenant sends is rewritten under oc:<tenant>/ on the way in and stripped on the way out. A tenant naming a shared ancestor, or another user outright, lands in its own subtree. Its README makes the right argument for why this is stronger than checking: "There is no string it can send that escapes, so the property does not rest on having enumerated the ways to try." Each tenant root is top-level, so view=holistic climbing up and view=descend fanning down both terminate inside the tenant.

Also already in place: a route allowlist (anything unlisted is 403, so a route a future engine version adds stays unavailable until someone adds it deliberately), opt-in layers each checked against v0.9.9 with two tenants, a tenant registry storing only SHA-256 key digests, and a billing gate that fails closed.

Intended call path:

user ──JWT/API key──▶ backend /memory/* ──service token + X-Memory-Tenant: u-<userId>──▶ memory-api ──PASETO──▶ cortexdb

Gap 1 — the direct wire bypasses the boundary

TinyMemory's CortexEngine (tinymemory-integrations/src/cortex) offers two wires:

Engine id Constructor Wire Auth Default endpoint
cortexdb CortexEngine::direct /v1/*, bare JSON API key https://api-v1.cortexdb.ai
tinyhumans CortexEngine::tinyhumans /memory/*, envelopes BearerSource, per request https://api.tinyhumans.ai

tinyhumans is the safe path — it goes through the backend, so it picks up tenant relocation.

direct talks to CortexDB's /v1/* with an API key and never passes through memory-api. On that wire there is no relocation, no allowlist, and no tenant pin — so the three engine behaviours in the table above are reachable as-is. A shared API key on the direct wire in any multi-user deployment means no isolation at all.

This needs a deliberate decision, not a patch: either the direct wire is restricted to genuinely single-user contexts (local, self-hosted, one key per person) and that is enforced in config rather than documented, or it is removed from multi-user builds.

Gap 2 — the backend sets the tenant, and nothing here tests that

cortexdb-saas is safe given a correct X-Memory-Tenant. The backend derives that header from the authenticated user. So the backend's derivation is as load-bearing as the relocation logic, and it lives outside the service that documents and tests the boundary. If any request path lets a client influence the tenant — a parameter, a header passthrough, an admin impersonation route, a cached value on a reused connection — the whole boundary is void.

Gap 3 — the conformance suite tests reach, not isolation

TinyMemory's conformance suite has suite/namespaces.rs, but it checks within-tenant reach: it "stores one item per node and checks what each reach reads back" across a root, two sibling agents and one sub-agent. That is agent-level scoping inside one user.

There is no cross-user check — nothing asserts that user A's recall, events read, write and descend cannot touch user B. That is exactly what this issue asks for, and the suite is the right place for it because every engine must pass it.

Gap 4 — two NetworkPolicies are load-bearing and unverified here

Per cortexdb-saas: the engine is its own pod on a ClusterIP, so what stops anything else in the cluster from reaching it is a NetworkPolicy admitting only memory-api to cortexdb:3141, plus a second admitting only the backend to memory-api. "Without them, anything in the cluster can reach the engine and bypass the proxy." They live in tinyhumansai/k8s. Their presence on each environment needs asserting, not assuming — see k8s#81, where /memory returned 503 with the memory bundle unapplied and MEMORY_* secrets unset, which shows environments do drift.

Related

  • #6718 — validate hosted CortexDB recall, integrate into TinyMemory, retire embedded tinycortex. This issue is its security half; the engine choice there determines what gets scoped here.
  • tinysweeper#147 — CortexDB answers 408 on /v1/experience/bulk for very large conversations.
  • #7001 — memory-migration credits. Importing other people's exports into a shared engine makes this issue a prerequisite, not a parallel track.
  • k8s#81 — staging /memory 503, memory bundle unapplied.

Priority note

Filed P1 because the mitigation exists on the hosted path — this is a single-layer, unproven boundary rather than a confirmed live leak. Escalate to P0 if verification finds the direct wire in use on any multi-user deployment, either NetworkPolicy missing on a live environment, or any request path where a client can influence X-Memory-Tenant.

Acceptance Criteria

Verify what is true today

  • [ ] Every caller of CortexEngine is enumerated, with the wire each uses recorded here; any multi-user use of direct is named as a finding
  • [ ] The backend's X-Memory-Tenant derivation is reviewed and documented — a client must have no way to influence it, including via admin or impersonation routes
  • [ ] Both NetworkPolicies are confirmed present on every environment that runs the engine, with evidence per environment

Enforce

  • [ ] The direct wire is either config-gated to single-tenant use or unavailable in multi-user builds — enforced in code, not documentation
  • [ ] A deployment missing a tenant pin fails closed at startup rather than serving unscoped traffic
  • [ ] Route allowlist and relocation are confirmed to cover every route TinyMemory actually calls, the opt-in layers included

Prove, through TinyMemory

  • [ ] A cross-user isolation check is added to the TinyMemory conformance suite, so every engine must pass it — not a one-off test
  • [ ] With two users on one engine, all five engine behaviours above are re-probed through TinyMemory and each is confined: recall, events read, events-by-id, write, and view=descend at a shared ancestor
  • [ ] forget is covered: user A cannot delete or preview-delete user B's items
  • [ ] Blob routes are covered: another tenant's blob id is a 404, and a write referencing it does not extract its content
  • [ ] Namespace-to-scope mapping is tested for escape attempts — a namespace naming another user, a shared ancestor, .., an absolute scope, and an oc:-prefixed string all land inside the caller's own subtree
  • [ ] The check runs in CI against a live two-tenant engine, not only against doubles
  • [ ] Results are recorded in this issue, including which probes were run and what each returned

Al629176 avatar Oct 05 '26 08:10 Al629176

Per-user scoping on CortexDB is enforced only by the hosted backend path; the direct wire bypasses tenant relocation and allowlist, and no cross-user isolation tests exist. Needs enforcement in code plus conformance-suite proof.

Labelled priority: p1.

tinysweeper avatar Oct 05 '26 08:10 tinysweeper

Progress: direct-wire enforcement + isolation proof (tinyhumansai/tinymemory#198)

Fixed in tinyhumansai/tinymemory#198 (CI green, including the live CortexDB job):

  • The direct wire now fails closed. CortexEngine::direct requires a CortexTenancy: single_user (one person's key, existing layout) or a tenant pin in CortexDB scope form, e.g. org:acme/user:alice. A cortexdb engine with no tenancy is refused with Error::Config at startup. The hosted (tinyhumans) engine refuses one, since the backend pins the tenant.
  • A pinned engine never leaves its pin. Every scope is <pin>/app:tinymemory/.... Scopes the server reports are accepted only if they start with the engine's root. The unscoped view: "descend" recall starts at the pin, not at a shared app:tinymemory.
  • Escape attempts were tested: a namespace naming another user, a shared ancestor, .., an absolute path, oc:- and org:-prefixed strings. All either fail to parse or land inside the caller's own subtree.
  • Conformance suite: conformance::run_isolation(a, b). User B probes user A's items through list, get, fetch, recall (the descend case), the namespace facet, forget by id and by filter, a store of A's exact item, and a store at a namespace naming A. All must miss. It runs on the reference engine (and catches one store passed twice), on the direct double, and live in CI against CortexDB v0.10.4 (also run by hand on v0.9.9):
Live probe, one server and one key Result
two sibling pins, both directions isolated
a pin at the shared ancestor vs a user beneath it (descend) isolated
two single_user engines on one key (the pre-fix situation) leak caught

A pin isolates hosts that go through TinyMemory. It is not a credential: a holder of a shared key can still call /v1/* directly. So users who must be kept apart from each other's clients need a key each, or the hosted engine.

Follow-up when tinymemory is bumped in openhuman: resolve_cortexdb (crates/openhuman-core/src/memory/engine.rs) must set tenancy: Some(CortexTenancy::SingleUser), or a user's own-key CortexDB engine reports a config error.

Still open on this issue:

  • [ ] Review the backend's X-Memory-Tenant derivation (including admin and impersonation routes).
  • [ ] Verify the memory network policies on each environment. Verification is pending and tracked privately. The boundary-check script was also found to false-pass when its probes never ran; fix in the infra repo.
  • [ ] Confirm the memory-api route allowlist covers every route TinyMemory calls.
  • [ ] Run the isolation check through the hosted wire (needs two real user credentials).
  • [ ] Blob routes: not applicable to TinyMemory (it calls none); still relevant to memory-api.

CodeGhost21 avatar Oct 05 '26 14:10 CodeGhost21

Update: the boundary is enforced and tested in cortexdb-saas; tinyhumansai/tinymemory#198 is closed

Correction to my previous comment. The client-side tenant pin in tinyhumansai/tinymemory#198 was the wrong layer, and that PR is closed. The tenant boundary belongs to the service in front of CortexDB, which relocates every scope under the caller's tenant. TinyMemory reaches it through the backend's /memory/* (the tinyhumans engine) and needs no change.

tinyhumansai/cortexdb-saas#12 (draft, CI green):

  • Fix: an event read by id. GET /v1/events/{id} is the one allowlisted route with no scope to pin, and the outbound rewrite only blanked a foreign event's scope field. So a tenant holding another tenant's event id received that event's content. It now returns the event only if its scope lies under the caller's root. Otherwise it answers 404, identical to an unknown id.
  • Cross-tenant probes against a mock engine (12 tests, run on every cargo test). The event-by-id cases fail against the previous code.
  • The same probes against the real engine (v0.9.9), in a new CI job that brings up the engine and service for every PR. Production deploys run the same workflow, so a boundary regression blocks a deploy.

Probes run against the real engine, two tenants, all made by B against A:

Probe Result
events list at A's scope or A's full root nothing of A's
event by id (A's id) 404, nothing of A's
write naming A's root lands in B's own subtree; A's scope unchanged
recall at tm:notes, A's root and A's tenant root, view=descend and holistic nothing of A's
forget of A's id under each scope B can name A's event intact
forget/preview 403, not exposed
service token without a tenant, or a tenant like u-a/../u-b 400
a tenant credential that isn't valid 401

Escape attempts (another tenant's name, /-prefixed, oc:-prefixed) are relocated under the caller's own root. Malformed scopes (.., x/../.., %2e%2e) are refused with 422 and never forwarded. Blob routes (another tenant's blob id is a 404, and a write referencing it is not extracted) were already covered against the mock engine.

Verified by code review, no change needed:

  • X-Memory-Tenant derivation (backend). It is set in exactly one place, u-<userId>, and never read from the request. Every /memory/* route uses the authenticated user. JWT, API key and attestation all resolve to the credential's own owner. Attested hosted instances lack the memory scope and are refused on /memory. There is no impersonation route, and path ids are URI-encoded.
  • Route allowlist vs TinyMemory. Every route the tinyhumans engine sends maps to an allowlisted memory-api route. memory/beliefs/build is mapped but never sent on the hosted wire.
  • Missing tenant. It already fails closed: the service token without X-Memory-Tenant gets 400 TENANT_REQUIRED.

Still open:

  • [ ] Merge tinyhumansai/cortexdb-saas#12.
  • [ ] Confirm the memory network policies on each environment that runs the engine. Pending someone with access.
  • [ ] The direct cortexdb engine (raw CortexDB with an API key) is only safe with one person's own key. Confirm no deployment shares one across users.

CodeGhost21 avatar Oct 05 '26 17:10 CodeGhost21

Update: the fix is live in production, and the network policies are verified

tinyhumansai/cortexdb-saas#12 is merged and deployed. The event-by-id fix (a foreign event now answers 404 instead of returning its content) is running in production. The new isolation CI job ran the cross-tenant probes against the real engine as part of that deploy, and it now gates every future deploy.

Network policies verified in production (the item that would have escalated this to P0):

  • All six ApplicationNetworkPolicy objects are present in the memory namespace: cortexdb-ingress, cortexdb-egress, memory-api-ingress, memory-api-egress, tika-ingress, tika-egress.
  • The applied specs match tinyhumansai/k8s: CortexDB admits only memory-api on 3141; memory-api admits only tinyhumans-backend / tinyhumans-backend-worker pods in app on 3140, with namespace and pod selectors required together.
  • Behaviour, checked with throwaway probe pods. A "blocked" result counts only if the probe provably ran and curl timed out or was refused.
From To Result
pod labelled memory-api CortexDB reached (200)
other pod, memory ns CortexDB blocked
backend-labelled pod, app ns CortexDB blocked (must go through memory-api)
backend-labelled pod, app ns memory-api reached (200)
other pod, app ns memory-api blocked
pod in another namespace CortexDB blocked

The two "reached" probes show the probes ran and the network is up, so the four "blocked" results are real refusals.

One process note. scripts/verify-network-boundary.sh in the k8s repo counts any failure as "refused", including a probe pod it wasn't allowed to create. Treat its output with care until that's fixed.

Status of the acceptance criteria:

  • [x] Cross-user isolation check in a conformance suite, run in CI against a live two-tenant engine
  • [x] Recall, events, events by id, write and descend all confined; forget and preview-forget covered; blob routes covered
  • [x] Escape attempts land in the caller's own subtree or are refused
  • [x] A missing tenant fails closed
  • [x] Route allowlist covers every route TinyMemory calls
  • [x] Backend X-Memory-Tenant derivation reviewed; no client can influence it
  • [x] Both network policies confirmed present and enforced in production
  • [ ] The direct cortexdb engine (raw CortexDB key) is used only with one person's own key. Confirm no deployment shares a key across users.

CodeGhost21 avatar Oct 05 '26 19:10 CodeGhost21

Closing: all acceptance criteria met

Last item: no deployment shares one raw CortexDB key across users. Searched every place in the org that configures a CortexDB key or endpoint:

Where Who shares the memory Result
OpenHuman desktop / self-hosted, cortexdb engine one person The key is read only from that install's own credential store, entered by its user; there is no environment-variable fallback. A self-hosted deployment is one instance with one workspace.
Hosted OpenHuman users many users Through the backend and memory-api (tinyhumans engine), the boundary verified in the previous comment.
OpenCompany hosted tenants, both runtimes many tenants Each tenant gets either its own engine (loopback-only, with a key minted per tenant and revoked on re-provision) or the shared memory service, which is cortexdb-saas with a per-tenant key. Neither shares a raw key.
tinysweeper (review bot) many repositories in one org, one key Every read and write is scoped owner:<o>/repo:<r>/…. Nothing is read above repository level or with descend, and nothing is written at owner level. The repository comes from signed GitHub webhooks, or from admin-token routes limited to one org or an allowlist.

Final status:

  • [x] Cross-user isolation check in a conformance suite, run in CI against a live two-tenant engine (cortexdb-saas isolation job, which also gates production deploys)
  • [x] Recall, events, events by id, write and descend all confined; forget and preview-forget covered; blob routes covered
  • [x] Event read by id no longer returns another tenant's content (tinyhumansai/cortexdb-saas#12, live in production)
  • [x] Escape attempts land in the caller's own subtree or are refused
  • [x] A missing tenant fails closed
  • [x] Route allowlist covers every route TinyMemory calls
  • [x] Backend X-Memory-Tenant derivation reviewed; no client can influence it
  • [x] Both network policies confirmed present and enforced in production
  • [x] The direct wire is used only with one person's own key; no multi-user deployment shares one

Not covered here:

  • Production is the only environment where the network policies were verified.
  • tinysweeper's per-repository scoping is enforced by its own code and is correct today, but no test pins it. A cross-repository isolation test there would be worthwhile hardening; it's outside this issue.

CodeGhost21 avatar Oct 05 '26 19:10 CodeGhost21