# Device preview pass

Approved by the controller, 2026-11-26. Branch `preview/pass`, main `7c113ac8`.
No implementation changes precede this note.

## Contract and binding

A tap on Preview calls bearer-authenticated
`POST /api/v1/tasks/{id}/preview-pass` with the candidate SHA the person saw.
Use the same current-person and task-read authority as `GET /tasks/{id}`,
including team visibility. Recheck the active roster and originating device
credential inside the write transaction. Refuse a stale candidate or a preview
that the existing wrapper would refuse. Do not mint authority to approve an
unsafe preview; an already approved preview keeps the wrapper's warning.

Return an opaque, cryptographically random 32-byte base64url pass and an
RFC 3339 UTC expiry and bounded whole-second lifetimes from mint time, never a raw preview address. Native subtracts elapsed request time and uses only these lifetimes for its local deadlines, independent of server/device clock skew. Store only its SHA-256 hash,
with a separate non-secret audit ID. Bind the record to this instance, person,
device credential and session epoch, task ID, candidate SHA, Preview step ID
and attempt, and a fingerprint of that deployment (URL, digest, expiry and
unsafe flag). The fingerprint prevents a redeployment of the same candidate
from silently changing what the pass opens. The pass expires after 60 seconds,
or earlier if the device credential or preview expires. Use the injected
server clock throughout. A pass is a bearer capability; its person binding
comes from minting, not a second sign-in or a submitted address at exchange.

## Exchange and browser session

The native web view POSTs the pass to the fixed same-origin path
`/native/preview/exchange`. No pass in a URL, fragment, redirect, HTML or logs.
The request has a bounded form body and the configured Origin; it passes the
existing same-origin CSRF checks. There is no wildcard CSRF exemption and no
GET exchange. Reject conflicting Origin/Referer/fetch-site headers, unexpected
fields, and any device Authorization header here. A browser session cookie
alone cannot mint or exchange a pass.

In one transaction, hash and find the pass, conditionally consume it once,
recheck membership/device/session epoch and the full preview binding, create a
fresh random browser grant, and append the audit event. Concurrent exchanges
have one winner. A known expired or stale pass is terminal, with no grant.
DB or audit failures cannot issue a cookie. A lost exchange response requires
a new pass; replay does not reproduce a grant.

Success sets a host-only `__Host-equipo-preview-{taskID}` cookie, Secure, HttpOnly,
SameSite=Strict, with Path `/`, and redirects with 303 to
that exact wrapper URL constructed by the server. No caller-provided redirect.
The cookie contains a fresh opaque secret, stored hashed. Its lifetime is at
most 15 minutes, capped by the device and preview expiries, without sliding
renewal. All exchange/wrapper responses use no-store and no-referrer.

This is an ordinary cookie-based browser session with restricted authority,
implemented as an app-owned preview grant. It must not create a general
Rastrillo/Oficina ID sign-in session. The host prefix prevents sibling-domain cookie planting:
server checks permit exactly GET of the bound task's wrapper. No task
page, API, event stream, attachment, other task, unsafe-preview POST or other
mutation accepts it. General session authentication never recognizes this
cookie. Keep normal browser sign-in behavior for requests without a preview
grant. Only a well-formed grant secret under this task’s cookie name selects the restricted lane; junk and other tasks’ cookies are ignored. Once selected, an invalid or expired grant cannot fall through to a broader signed-in session.

## Wrapper, expiry and candidate changes

Share the existing `previewWrapper` renderer and preview eligibility checks.
Keep its CSP, iframe sandbox (`allow-scripts allow-same-origin`), origin
validation, header warnings and referrer policy. Never load the raw origin as
the top-level document. Each wrapper request rechecks the originating device,
current roster, task visibility, expiry and exact deployment binding. It does
not switch to a newer candidate. Candidate changes return 409 with the reviewed
`evidence.candidate_changed` words; expired passes/grants/previews show the
reviewed `question.expired` label. Other failures use existing reviewed words.

Native drops the web view at expiry, sign-out or a task update that changes the
binding, and revalidates when returning to the foreground. A new tap can mint
a new pass. Server expiry prevents further wrapper reads; it cannot retract
HTML already delivered to a browser or revoke the preview origin itself.

## Native choice

Use a dedicated WKWebView sheet on both platforms with a fresh
`WKWebsiteDataStore.nonPersistent()` per presentation. WKWebView accepts a
URLRequest, allowing the POST-body exchange, and isolates this cookie from
ordinary browser sign-ins. Load the actual server response, not an HTML string
or a native proxy, so the wrapper's response headers and iframe remain intact.
SFSafariViewController's URL initializer is less suitable for this exchange.

Extend the allowlisted opener with a typed preview destination: exact session
origin plus the fixed exchange and wrapper paths for a validated task ID.
Allow only those top-level navigations; reject unexpected redirects, popups,
downloads and custom schemes. Keep the preview within its sandboxed child
frame. No injected scripts, native script-message bridge, relaxed TLS, device
bearer in WebKit, or automatic opening of blocked links. Test subframe loading
separately so the navigation policy does not accidentally block the preview.

Apple references: [WKWebView request loading](https://developer.apple.com/documentation/webkit/wkwebview/load(_:)-5siv6),
[nonpersistent storage](https://developer.apple.com/documentation/webkit/wkwebsitedatastore/nonpersistent()),
[Safari view controller](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller/).

## Storage, audit and proof

Add app-owned pass/grant tables via additive migration and register them in
Models/Schema; do not modify framework session tables. Bound issuance per
person/device/task, rate-limit exchange, and prune expired records. Audit mint,
exchange and identifiable refusals with actor/task/non-secret grant ID and
reason, transactionally; never include secrets, cookies, preview URLs or body
contents. Unknown-token refusals remain generic and rate-limited.

Test first and mutate: task visibility, revoked member/device/backing native
session, person/instance/task/candidate/deployment binding, exact expiry,
concurrent single use and replay, failed-audit rollback, cookie attributes,
GET-only route isolation, CSRF/redirect refusal, wrapper sandbox/CSP and raw
origin refusal. Native fake-server tests cover request/expiry, stale candidate,
allowlist, redirects, foreground revalidation and sign-out. Browser tests cover
the real exchange-to-wrapper flow. Add OpenAPI contracts and a perf-budget
record for any new GET, then native and Go gates and Linux CI. No runner
protocol change is needed.

## Approved refinements

Pass and grant lookups are indexed exact hash matches. Unknown, consumed and
wrong-binding requests share the same generic refusal; stale/expired copy is
available only after the capability's identity and submitted context validate.
Device and IP limits apply separately. A well-formed secret in this task’s Host-prefixed cookie selects the restricted lane, even if unknown or expired alongside a general session. Junk cannot select the lane.
Host-only scope and a different iframe cookie host are tested in a real browser.
Dismissal, expiry, sign-out and account replacement drop both native resources.

## Security review refinements, 2026-11-26

The server returns expires_in (1–60) and grant_expires_in (1–900), rounded down
from the mint clock. Absolute timestamps are audit/display metadata. Native
subtracts the full elapsed request duration before starting either local
lifetime; it never adds transit time or renews a grant.

A terminal known pass is consumed and a refused grant is deleted in the same
transaction as its one refusal audit. Replays do not add refusal audits. Expired
rows are pruned after exchange and read too, allowing a first bound expired
request to receive its reviewed words. Both tables have composite device/task
indexes in additive migration 0110.

Child frames may navigate to about:blank, about:srcdoc, blob: and data: inside
the wrapper sandbox. These schemes remain forbidden for the main frame. Link
previews are disabled. A real nested-frame fixture checks srcdoc and blob
loading through the wrapper in the browser lane and the iPhone simulator.
