# A record from another business answers "not found"

> Every Biloh route resolves the caller's business from their own sign-in, never from the request, and filters every read and write by that business. An id that belongs to another business is never read or changed, routes built on the shared resolver answer 404 for it, and portal links are never handed out by matching an email address.

URL: https://biloh.com.au/docs/engineering-notes/a-foreign-id-answers-not-found
Category: Engineering notes | Audience: builder | Updated: 2026-11-12

## What is the rule?

A route never trusts the request to say which business it is for. It resolves the caller's business from their own sign-in first. The shared resolver for that is `resolveRouteActor` (`lib/auth/resolve-route-actor.ts`), which returns the business the signed-in person, key or token belongs to; some older routes resolve the signed-in operator's business with their own helper, never from the request. Every query the route then runs, read or write, carries `.eq("tenant_id", tenantId)` for that business, and every insert writes that business's id, never one copied from the target row.

Row-level security in the database is a second wall behind this, not the only one: a route using the service role bypasses row-level security, so the filter in code is what keeps it inside one business.

## What does a caller see when an id is not theirs?

Nothing is read back and nothing is written, because every query carries the tenant filter. A route built on the shared resolver answers `404`: it looks the target up with the caller's tenant filter (`.eq("id", id).eq("tenant_id", tenantId)`) before doing anything, and a row that is not the caller's is indistinguishable from one that does not exist. A request body is validated with a Zod schema before any lookup, so a malformed id is a named `400`.

## Is an email address ever proof of who someone is?

No. An address is not identity on its own: a person can be invited under an address they do not control. So:

- `/api/contexts` lists only the caller's own memberships. It never returns a client or contractor portal link found by matching the caller's email against other businesses' records.
- Accepting a team invite sets a password only on a login that this invite itself created, in the same request as the membership row, and that has never signed in. Any other existing login must sign in, or recover its password through the emailed link (real proof of the mailbox), before it can accept.

## Can another website make a signed-in browser change something?

No. A signed-in browser carries its session cookie to Biloh whichever page sent the request, so a cookie alone does not prove the person meant the change. Every `POST`, `PUT`, `PATCH` or `DELETE` under `/api/` that carries a session cookie and no `Authorization` header must come from Biloh's own page. `middleware.ts` runs that check before any route, from one home, `lib/security/cookie-mutation-guard.ts`. A change sent by another website, or by a sibling `*.biloh.com.au` page, is refused `403 cross_origin` before it reaches the app, so a new route cannot forget it. Requests authenticated another way are not affected: signed webhooks, scheduled jobs, the AI connection, the OAuth token endpoint, portal links and upload links. The platform setting `security.csrf.cookie_mutation_enforcement` (`enforce` by default, `report_only` or `off`) switches it without a deploy, and a failed settings read counts as `enforce`.

## How is it locked?

- `tests/api/reconciliation/bug-a3db16b5-cross-tenant.test.ts` seeds a second business and drives the real route: reads and writes against the other business's client, site or proposal answer `404` and leave its rows unchanged, while the caller's own records still work.
- `tests/api/contexts/bug-864c8039-no-portal-tokens-by-email.test.ts` proves a signed-in user whose email matches another business's client and contractor receives no portal context and no token.
- `tests/routes/team-invite/accept-invite-route.test.ts` (T864-1..3) proves a login made elsewhere is never completed by an invite.
- `tests/api/security/bug-e6e3268a-cookie-mutation-same-origin.test.ts` sends cookie-session changes from a foreign page, a sibling subdomain and Biloh's own page, and asserts only the last reaches a route.
- `tests/integration/supabase-smoke/cross-tenant-rls-live.test.ts` checks the database wall on the live project with test tenants.

## What should a builder do?

Start every new route with `resolveRouteActor`, filter every query by its `tenantId`, look a target up with that filter before acting on it, and answer `404` when it is not found. Never take a tenant id, or a decision about identity, from the request body or from an email match.
