# Reverse proxy

> The paths, cookies, headers, and timeouts the app's proxy forwards, and how the server finds the client's address.

From `docs/server.md` in tensile-tools/tensile-auth at commit `e66760d` (v0.4.1-7-ge66760d). The HTML page is https://auth.tensile.tools/docs/server/reverse-proxy.

The app forwards these path prefixes to the server, and no others:

- `/auth/`, always.
- `/admin/`, when administrators use the admin API through the app.
- `/login/oauth/`, `/login/device/`, and `/.well-known/`, with OAuth.

The SDK's proxy does this for Node apps; see `createProxy` in the SDK. A proxy that you write yourself must:

- Forward only the library's cookies: `__Host-session`, `__Host-pending`, `__Host-signup`, `__Host-known`, `__Host-reset`, and the device-bound session cookies.
- Forward the `Origin`, `Sec-Fetch-Site`, `Sec-Fetch-Mode`, `User-Agent`, `Content-Type`, `Accept`, `Authorization`, and `DPoP` request headers, and the `Secure-Session-*` and `Sec-Secure-Session-Id` headers.
- Return the server's status, body, `Set-Cookie`, and headers unchanged.
- Cap request bodies at 1 MiB.
- Never send the app key back in a response.

Set these timeouts:

| Path | Timeout | Why |
|---|---|---|
| `/auth/claims/` | 65 seconds | A claim long-polls for up to 60 seconds |
| Every other `/auth/` path | 30 seconds | Sign-in, step-up, and account calls |
| `POST /v1/check`, from the app's backend | 5 seconds, with no retry | It's on every request's path. A timeout fails the request closed |
| `POST /v1/check` with `Prefer: wait=N` | N seconds plus 5, at most 65 | The server holds an agent's approval retry for up to N seconds |

The server takes the client's address, which per-client rate limits and the audit log use, from the first of these that applies:

1. `Tensile-Client-IP`, which the SDK's proxy sets, when the same request carries a live app key in `Tensile-App-Key`. The server checks the key before it trusts the header. It ignores the header when the key is missing, malformed, unknown, or revoked, when the request holds more than one `Tensile-Client-IP` header, or when the value isn't a single IP address, for example a list, an address with a port, a prefix, or an address with a zone.
2. `X-Forwarded-For`, read from the right, when the request comes from an address in `trusted_proxies`.
3. The address of the peer.

An IPv6 address counts as the /64 that it's in. Behind the SDK's proxy, you don't need `trusted_proxies`. Behind another proxy, set `trusted_proxies` to the addresses of the proxy that connects to the server, and have it append the client's address to `X-Forwarded-For`.

`/v1/check` takes the end user's address from `request.client_ip`, which the app key on the call vouches for. When `request.client_ip` is missing or isn't a single IP address, the check counts against the app's own address.

Don't publish the ops port, `9090`, which serves [`/livez` and `/readyz`](/docs/server/operate#monitor), and don't forward it. The server refuses `ops_listen` equal to `listen`.

## Approval waits

An agent that retries an approvable route with `Prefer: wait=N` waits in that request for its person's decision. The app holds the agent's connection, and the server holds the app's `/v1/check` call, which sends `Prefer: wait=N` too. The SDK does this for you, with its `approvalWait` option as the cap:

- On a long-running server, `approvalWait` defaults to 50 seconds, under the common 60-second idle timeout of load balancers. Keep it below the idle timeout of every proxy between the agent and the app.
- On a detected serverless platform (`VERCEL`, `AWS_LAMBDA_FUNCTION_NAME`, `NETLIFY`, or an edge runtime), it defaults to 0, so the agent retries after `Retry-After` instead. A function stopped mid-wait can spend the approval without running the route, which fails closed but makes the person tap again. To wait there, set `approvalWait` and the function's limit together: `maxDuration >= approvalWait + 5`, for example `approvalWait: 25` with `export const maxDuration = 30`.

The server holds at most 4 waits for each agent, 256 for each app key, and 1,024 in all, on each replica. A wait over any of them answers `approval_pending` at once, never 429. Each wait rereads the database every 2 seconds, so a decision on another replica reaches it within 2 seconds. When the server shuts down, every wait answers `approval_pending` at once, so none outlives `shutdown_timeout`. A check without `Prefer` answers at once, and its refusal offers the agent no wait.
