> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bbrands.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Security

> The security controls of digital contracting: trust boundaries, the contract session, authorization, identity, the signing gesture and its evidence, data exposure and environment guards.

This page lists the controls that protect digital contracting and where each
one lives. It names claims and checks, never secret values.

## Trust boundaries

```mermaid theme={null}
flowchart LR
    email["Customer email"] -->|"one-time link"| portal["Portal wizard"]
    portal -->|"contract session (Bearer)"| api["horizon-api"]
    portal -->|"private channel, authorized by the contract session"| pusher["Pusher"]
    backoffice["Maihue back office"] -->|"staff token + permission"| api
    api -->|"server credentials"| providers["Floid · Fintoc"]
    qstash["QStash"] -->|"signed delivery"| workers["Contract workers"]
    workers --> api
```

| Boundary | Control |
| - | - |
| Email → portal | A one-time Supabase link, redeemed once by the portal |
| Portal → horizon-api | The contract session plus a permission on every route |
| Portal → Pusher | The private channel `private-contract-{id}` is authorized only for a session on the contract's account whose user takes part in that contract |
| Back office → horizon-api | Staff permissions (`contract:review`, `contract:cancel-admin`, `contract:force-status-admin`, `contract:evidence`, …) |
| horizon-api → Floid, Fintoc | Provider credentials kept in server configuration, never sent to the browser |
| Queue → workers | Every QStash delivery is signature-verified before the handler runs |

## Session

| Control | Where |
| - | - |
| The one-time link is exchanged once for an internal token; a reused link answers `409` and an expired one `410` | `lib/common/auth/contract-otp.ts`, `lib/endpoint/contract-session/` |
| The session is issued only to a user who takes part in the contract | `lib/common/auth/contract-participation.ts` |
| Each token carries a unique id (`jti`) | `lib/jwt.ts`, `lib/common/authorizer.ts` |
| The portal keeps the session in the tab's session storage, bound to its contract id, so it is never reused on another contract | `frontend/portal-customer/app/lib/auth/contract-session-storage.ts` |
| A new session after identity was verified must prove the card serial again before signing | `lib/endpoint/contract-state/require-signer-document-number.ts` |
| Request logs mask the one-time link and tokens | `app/api/v3/contract/[document_id]/sessions/route.ts` |

## Authorization

| Control | Where |
| - | - |
| Every route calls `authorize` with its permission | Route files under `app/api/v3/` |
| Every public permission is on a reviewed allowlist that states why it is public and what data it serves | `database/database.public-actions.ts` |
| Wizard routes check, on every call, that the session user takes part in that contract | `lib/common/auth/authorize-contract-session.ts` |
| Summary, payment, rewind and customer cancel are reserved to active members of the contract account (in practice, the account holder) | Same guard, `requireAccountMember` |
| A signer can only request a code for, sign or reject their own seat | `lib/endpoint/signature-signing/signature-signing.service.ts` |
| Company administrators sign strictly in their signing order | `lib/endpoint/signature-signing/assert-customer-sequential-sign-lock.ts` |

## Identity

| Control | Where |
| - | - |
| The RUT check digit is validated before any provider call | `lib/endpoint/contract-identity/contract-identity.service.ts` |
| The RUT must match the one registered for the signer | `lib/endpoint/contract-identity/identity-validation.core.ts` |
| Floid confirms live that the identity card is active for that RUT | Same file |
| An earlier verification is reused only if it happened after this contract started | `lib/endpoint/user-identity/user-identity.resolver.ts` |
| The identity card serial is never stored | `database/horizon-drizzle/database/schemas/(user)/user-dni/` |

## Signing and evidence

| Control | Where |
| - | - |
| Signing codes have six digits from a cryptographically secure generator and are stored only as a salted hash | `lib/integration/signature-provider/internal-signer/otp.util.ts` |
| Codes expire, are single-use, limit wrong attempts, and a new code invalidates the previous one; resends are limited per signer | `lib/integration/signature-provider/internal-signer/internal-signer.client.ts` |
| One gesture signs a hash computed over every document of the group | Same file |
| The signer's document number is recorded in the evidence only as a hash | Same file, `evidence-sheet.ts` |
| Every engine event is appended to a hash chain per signature request | `lib/integration/signature-provider/internal-signer/audit-chain.service.ts` |
| Before sealing, the hash of each original PDF must match the one recorded when it was generated | `lib/integration/signature-provider/internal-signer/sealing.service.ts` |
| Sealed PDFs carry PAdES signatures | `lib/integration/signature-provider/internal-signer/pades-sealer.ts` |
| The evidence export verifies the hash chain | `lib/endpoint/contract-evidence/` |

## Data exposure

| Control | Where |
| - | - |
| `ContractState` masks every signer seat email and bank account | `lib/endpoint/contract-state/` |
| Contract PDFs live in the private `contracts` bucket; the API exposes them only through the session-checked `preview.pdf` route | `lib/common/storage/contracts-bucket.ts`, `app/api/v3/contract/[document_id]/preview.pdf/` |
| Resending access answers the same generic message whether or not the email belongs to the contract, and only ever writes to an email already on it | `lib/endpoint/contract-resend-access/` |
| The lookup by email and the account's identity document answers the same generic `found: false`, with no contract, to every request, a match included, and is rate-limited per IP | `lib/endpoint/auth/contract-lookup-by-email/`, `app/api/v3/auth/contract/lookup-by-email/` |

## Environment guards

| Control | Where |
| - | - |
| The Floid stub refuses to run when the API points to the production host | `lib/common/integration-stub-policy.ts` |
| The placeholder representative credential used in local seeds is refused in production | `lib/integration/signature-provider/internal-signer/internal-signer.config.ts` |
| The inline queue transport for local development refuses to run in production, and the legacy inline-delivery flag is ignored there | `lib/queue/transport/inline.adapter.ts`, `lib/queue/common/local-inline-delivery.ts` |

Paths are relative to `backend/horizon-api/`, except those that start with
`frontend/` or `database/horizon-drizzle/`, which are relative to the
repository root. Monitoring of the flow is described in
[Operations](https://docs.bbrands.io/digital-contracting/operations.md#monitoring).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.