Dev.to Security 🔐 Cybersecurity 👁 0 📖 8 min read

Legal Contract Review Retrieval Architecture: Enforcing Tenant Isolation at Every Boundary

The page says retrieval_tenant_mismatch > 0. On-call can see a request ID, the authenticated tenant, the tenant attached to each retrieved chunk, and the stage that rejected it. No contract text reached generation. That

The page says retrieval_tenant_mismatch > 0. On-call can see a request ID, the authenticated tenant, the tenant attached to each retrieved chunk, and the stage that rejected it. No contract text reached generation. That is the outcome the system needs.

Short answer: enforce tenant isolation before search, inside search, and again before generation; carry a server-derived tenant identity through the whole retrieval path, fail closed on any mismatch, and treat a rejected cross-tenant candidate as a security signal rather than a harmless empty result.

A legal contract review workspace cannot repair isolation after an answer is generated. Retrieval-augmented generation deliberately combines a model with external, indexed knowledge [1], so the retrieval boundary becomes part of the authorization boundary. Tenant identity must therefore constrain candidate selection, not merely decorate logs or post-filter an answer.

How should a legal contract review workspace enforce tenant isolation in retrieval architecture?

Use more than one boundary. A shared index with a tenant filter can be appropriate when contracts have the same retention, encryption, residency, and operational policy, but only if the filter is injected by trusted server code and cannot be supplied or removed by the caller. A per-tenant index gives a smaller logical blast radius and clearer deletion boundaries. Separate storage and compute provide the strongest boundary, with correspondingly higher provisioning and operational cost.

The decision is less about vector search quality than about the failure you are willing to make possible. A workspace that aggregates contract listings from several sources has at least four identities in motion: the human principal, the workspace tenant, the source connection, and the contract record. Flattening those into one free-form metadata map makes review convenient until a reconnect, re-index, or permission change leaves two fields disagreeing. Keep them distinct, validate their relationship, and make the effective tenant immutable after authentication.

Boundary Isolation mechanism Operational trade-off Not suitable when
Shared index Mandatory tenant predicate plus result validation Fewer index lifecycles; every query path must preserve the predicate Policies require independent storage or a wider shared blast radius is unacceptable
Index per tenant Tenant-selected index plus result validation Clearer deletion and restore scope; more index lifecycle work Tenant count or index churn overwhelms the control plane
Separate stack Independent storage and retrieval resources Strongest resource boundary; highest deployment and observability overhead The team cannot operate many isolated stacks reliably

The catch is straightforward: no row in that table wins everywhere. Stick with a shared index when policy permits it and the team can prove filter coverage across every query mode. Move to per-tenant indexes when deletion, restore, or policy boundaries need independent handling. Choose separate stacks when contractual or regulatory requirements demand resource isolation. I'm not sure which boundary your contracts require without the actual data-processing terms; that review, plus a failure-injection test, resolves the uncertainty.

Work backward from the tenant-mismatch page

The page is late by definition. It fires because the final retrieval guard found a candidate whose tenant did not match the authenticated request. The useful alert payload identifies the request and stage without copying contract text: request ID, hashed tenant identifier, source-connection identifier, index identifier, query mode, rejected-candidate count, and deployment version. Avoid raw clauses, filenames, search queries, and model prompts in the page. They turn an isolation control into another data-handling surface.

Work backward. The final guard rejected a chunk. That chunk came from a search response. The search request came from a query builder. The query builder received a server-side scope. The scope came from authentication and workspace membership. Each handoff should preserve one immutable tenant value, while source and document authorization can narrow the scope but never widen it.

I have been paged by missed jobs and duplicate deliveries, and the same operational reflex applies here: don't trust the fact that a control ran; record which control ran, which input it used, and what it decided. A metric that counts only successful searches proves activity. It doesn't prove isolation. The earlier signal should be retrieval_scope_missing, incremented when code attempts to build a query without a trusted tenant scope. That event should stop the request before the index sees it.

This matters during ingestion too. A contract imported from an acquisition data room may be visible in a listing before its content is indexed. The ingestion job must write the same immutable tenant and source-connection identity into the document manifest and every derived chunk. If a retry observes different ownership metadata, quarantine the job for authorization review instead of merging the records. Duplicate processing is recoverable when writes are idempotent; ambiguous ownership is not.

Stop there. Fail closed.

Put the isolation rule in code, not convention

The safest interface makes an unscoped search difficult to express. The following Go sketch keeps the effective tenant outside caller-controlled query fields, requires the search implementation to accept a scope, and validates every candidate before returning it to the generation stage. It is intentionally generic: the boundary matters more than a particular database client.

package retrieval

import (
    "context"
    "errors"
    "fmt"
)

type Scope struct {
    TenantID string
    SourceIDs []string
}

type Candidate struct {
    ChunkID string
    TenantID string
    SourceID string
    Text string
}

type Searcher interface {
    Search(ctx context.Context, scope Scope, query string, limit int) ([]Candidate, error)
}

var ErrIsolationViolation = errors.New("retrieval isolation violation")

func Retrieve(ctx context.Context, searcher Searcher, scope Scope, query string) ([]Candidate, error) {
    if scope.TenantID == "" {
        return nil, errors.New("trusted tenant scope is required")
    }

    candidates, err := searcher.Search(ctx, scope, query, 12)
    if err != nil {
        return nil, fmt.Errorf("search scoped candidates: %w", err)
    }

    for _, candidate := range candidates {
        if candidate.TenantID != scope.TenantID {
            return nil, ErrIsolationViolation
        }
    }

    return candidates, nil
}

There are two checks because they answer different questions. The search scope reduces the candidate set. The result guard verifies that the retrieval system honored the scope. Neither check authorizes the source by itself; the implementation must also constrain SourceIDs to connections the principal can use. Generation receives only validated candidates and citation identifiers. It never gets a tenant selector.

Citation handling needs the same discipline. An answer can cite only chunks that survived authorization, and each displayed citation should resolve through an authorization-checked document route rather than a durable storage URL. A citation that can't be reopened by the same principal is evidence of a stale permission snapshot or indexing lag; suppress the answer and enqueue a scoped re-index rather than showing an untraceable claim. The RAG paper describes combining retrieved passages with generation [1]. In contract review, retaining the passage-to-document link is what lets a reviewer inspect grounding instead of accepting fluent text on faith.

Test, deploy, and observe the whole retrieval path

Unit tests for the query builder are necessary, but they miss alternate paths. Hybrid search, citation expansion, document previews, saved searches, background summaries, and re-ranking all need the same scope contract. Build a matrix that submits one tenant's authenticated request while planting attractive, near-duplicate clauses in another tenant. The expected result is not merely "no foreign text in the answer." No foreign candidate may cross the final guard, no citation may resolve across the boundary, and no log or trace may contain contract content. Make the deployment boring. Run the isolation suite against the release artifact, deploy to a small slice, and compare counts for missing scopes, rejected candidates, empty retrievals, and citation authorization failures. A rise in empty retrievals with no scope violations may indicate an over-restrictive policy; a scope-missing event means a code path bypassed the typed boundary and should block promotion. Rollback criteria belong in the runbook before deployment — not in a chat thread after the page. For ingestion, test retries and ownership changes. Process the same source event twice and verify that the chunk identifiers and tenant ownership stay stable. Then revoke a source connection while indexing is in flight. New writes must stop under the revoked scope, while already indexed chunks become unavailable to retrieval until deletion or reconciliation finishes. This is where an idempotency key tied to tenant, source, document revision, and chunk identity earns its keep: the retry repeats the same authorized write instead of inventing another record. Observability should preserve cardinality without exposing content. Hash tenant identifiers with an operationally managed key, keep request IDs random, and record bounded labels such as query mode and stage. Put high-cardinality request details in access-controlled traces, not metric labels. The dashboard should connect the early and late controls: requests rejected for missing scope, candidates rejected for mismatched scope, retrievals returning no authorized evidence, and generated answers whose citations all passed authorization.

Cost is a boundary choice too. Per-tenant resources create more provisioning, backup, restore, and alerting work; shared resources demand more exhaustive test coverage and stricter change review. Account for on-call load and recovery drills, not just storage and query spend. A cheap index that nobody can restore within the tenant boundary is an expensive design during an incident.

Choose the boundary by blast radius, then rehearse failure

Write the decision as an invariant: "A retrieval request can observe only chunks whose immutable tenant identity equals the authenticated workspace tenant and whose source remains authorized." Then list the mechanisms that enforce it, the evidence that proves each mechanism ran, and the owner who responds when it doesn't. This turns tenant isolation from a diagram label into an operable control.

A shared index is not automatically weak.

A per-tenant index is not automatically sufficient. Application code can select the wrong index; ingestion can attach the wrong owner; a citation resolver can skip authorization. Layering a trusted scope, storage boundary, result validation, and citation authorization closes different failure paths. For high-consequence agreements, rehearse a deliberately mismatched fixture in a non-production environment and require the request to stop before generation.

The recommendation is conditional: use the least complex boundary that satisfies the contract and that your team can continuously test. If a shared design cannot produce convincing evidence for every retrieval path, its lower operating burden is irrelevant. If separate stacks exceed the team's ability to patch, restore, and monitor them, the nominally stronger boundary may create longer exposure to stale data or policy. Architecture review should include both security blast radius and operational failure rate.

The false-positive cost belongs in the runbook

Paging on every empty retrieval will train on-call to ignore the channel because empty evidence can be a correct result. Paging only after foreign text reaches generation is far too late. The high-urgency threshold should be any confirmed tenant mismatch or any production request that reaches the query builder without trusted scope; rate-based warnings can cover empty results and citation misses.

Thresholds still need calibration. A warning that fires during every planned re-index hides real regressions, while a long aggregation window delays the one signal that must stop traffic. Route deterministic isolation violations immediately, group repeated events by deployment and query mode, and keep content out of the payload. Review suppressed and grouped events after each release so deduplication doesn't erase a new path. False positives have a security cost: they consume the attention needed for the page that matters.

References

  1. Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks: https://arxiv.org/abs/2005.11401
📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.