zero gravitySOLUTIONS
← All insightsAI & AUTOMATION / SECURITY TEST GUIDE

Test tenant isolation in vector retrieval

QUICK ANSWER

Tenant isolation requires an authorization boundary before evidence reaches the model, independent of vector similarity. Derive tenant scope from authenticated application context, enforce it on every retrieval path, and test against deliberately tempting cross-tenant matches using the actual production database role.

Inside this article 5 sections

A vector search result can be an excellent semantic match and still belong to the wrong customer. In a multi-tenant product, relevance must operate inside an authorized tenant scope. Asking the model to ignore other customers' documents after retrieval is too late if those documents already entered its context.

The useful test is adversarial: make another tenant's document the closest possible match and verify that it cannot appear in candidates, reranker input, model context, caches, or diagnostic views available to the requester.

Derive tenant scope outside the model

Resolve the tenant from authenticated application context and verify the user's membership. A tenant ID written in the chat message or generated tool arguments is not authority. The retrieval tool should receive its trusted scope from the server and reject attempts to override it.

Keep the tenant key in every source, chunk, embedding record, and derived summary. Use a stable document identity within that scope. If an ingestion job can write a chunk without a tenant key, the retrieval boundary begins with incomplete data and becomes harder to enforce consistently.

Apply the same scope to lexical retrieval, vector retrieval, parent-document expansion, citation lookup, and reranking. A filtered vector query followed by an unrestricted fetch-by-document-ID can reintroduce the leak. Treat each stage that loads content as an authorization boundary.

Use query constraints and database policy deliberately

For a PostgreSQL-backed design, a parameterized query can express the tenant condition explicitly. The following SQL is a query shape, assuming a vector column and a server-supplied tenant parameter; it is not a complete deployment policy.

SELECT id, source_version, content
FROM knowledge_chunks
WHERE tenant_id = $1
  AND deleted_at IS NULL
ORDER BY embedding <=> $2::vector
LIMIT $3;

Row-level security can provide another enforcement layer, but it must be enabled and tested under the actual application role. Table owners and privileged roles can behave differently from ordinary roles. A passing test under a restricted development account does not establish safety for a production connection using broader privileges.

If tenant context is stored in a database session setting, ensure it is set from trusted server state and cannot bleed between pooled requests. Transaction-local configuration and explicit cleanup strategies need testing under connection reuse. Do not let arbitrary SQL clients set an authorization value and thereby impersonate another tenant.

Build fixtures that reward a broken filter

CROSS-TENANT FIXTURES / TEMPTING WRONG MATCHES
Search as tenant A using an exact phrase from tenant B
FixtureTenantPurposeAllowed result
Ordinary export guidanceAPermitted relevant evidenceMay be returned
Exact secret phrase matchBStrongest lexical and semantic lureNever enters A's context
Same document ID suffixBTests ambiguous lookup keysCannot be fetched through A's citation path
Deleted historical chunkATests lifecycle filteringExcluded despite correct tenant
The correct result can be an empty list. Low recall is a quality problem; broadening the tenant scope is not an acceptable repair.

Use distinctive synthetic marker strings so the harness can detect a leak at every intermediate boundary. Do not use real secrets as test markers. Capture candidate identifiers and tenant labels in a protected test trace, then assert scope before content is sent to any external model.

def assert_allowed(results, trusted_tenant):
    assert all(row["tenant_id"] == trusted_tenant for row in results)
    assert all(not row["deleted"] for row in results)

allowed = [{"tenant_id": "A", "deleted": False}]
assert_allowed(allowed, "A")
try:
    assert_allowed(allowed + [{"tenant_id": "B", "deleted": False}], "A")
except AssertionError:
    pass
else:
    raise AssertionError("Harness failed to detect cross-tenant evidence")

This small Python check verifies the harness assertion itself. It does not test a database or prove isolation. Connect the same assertions to real query results, parent expansion, and model-context assembly in the integration suite, including empty results and missing-tenant rows.

Separate authorization from approximate-search recall

Approximate nearest-neighbor indexes can interact with filters in ways that reduce the number of permitted matches returned. In pgvector, filtering after an approximate index scan can leave fewer results than requested. Iterative scans, index tuning, partitioning, or separate tables may improve retrieval within the permitted scope.

A shared approximate index also lets other tenants' vector populations affect recall and performance. Partitioning or separate indexes can reduce that coupling, at the cost of more operational objects and different capacity tradeoffs. Choose based on tenant count, size distribution, workload, and isolation requirements.

Never respond to poor recall by dropping the tenant filter or searching globally and trusting a final text filter. Evaluate retrieval quality using an exact permitted baseline where practical. Measure authorization correctness independently: a configuration with excellent recall and one cross-tenant result fails the security boundary.

Exercise every route that can bypass the happy path

Test missing tenant context, forged tenant arguments, pooled connection reuse, admin-role misconfiguration, shared answer caches, fallback search paths, and citation fetches. Include a user who belongs to two tenants and switches active workspace; membership in both does not necessarily mean every request should combine both data sets.

Run negative tests after connector changes, index migrations, and retrieval optimizations. Inspect the model input, not only its final response. A generator that declines to repeat leaked text does not repair an upstream access failure.

The permission-aware assistant guide covers the broader access model, and tenant-scoped idempotency covers action identity. Our AI integration services help connect these boundaries across retrieval and execution.

FROM INSIGHT TO ACTIONAI-native implementation

Make tenant isolation a tested retrieval property.

We help design authorization-aware queries and regression harnesses that inspect the evidence boundary before model generation.

Explore AI implementationDiscuss your AI workflow

Start with your goals.
We’ll work through the next step together.