Skip to content

feat(auth): TLS fingerprint from the trusted TLS proxy (TLS_FINGERPRINT_HEADER) - #9

Draft
BackStacked wants to merge 2 commits into
mainfrom
claude/project-thread-9efqn4
Draft

BackStacked wants to merge 2 commits into
mainfrom
claude/project-thread-9efqn4

Conversation

@BackStacked

Copy link
Copy Markdown
Contributor

Requested by Himanshu · project thread

Before: login and refresh bound a session to the client's device signals only.

After: with TLS_FINGERPRINT_HEADER=x-ja4 (and TRUST_PROXY ≥ 1), the JA4/JA3 TLS fingerprint the proxy computed is passed to auth-kit at login and refreshSession, stored as a SHA-256 hash on the session, and a change on refresh is audited as auth.tls_fingerprint_changed. auth.device_mismatch also records whether the TLS fingerprint matched. Nothing changes in the GraphQL schema or the client.

A TLS fingerprint comes from the ClientHello, so only the TLS terminator can compute it. The header is read with security-kit's trusted-proxy rule, the same as X-Forwarded-For, and the api refuses to start with the header set but no trusted proxy. A missing fingerprint is never an error, and a change never blocks (browser updates and TLS-inspecting networks change it).

How: src/config/env.ts (new variable and its check), src/server.ts (fastifySecurity({ tlsFingerprintHeader }) → context), ctx.tlsFingerprint passed by the auth controller and service. Docs in 06 (proxy setup table), 09, 12, .env.example. 4 new end-to-end checks (hash only stored, change audited, header ignored without the setting, startup refusal); TEST_BACKENDS=pg npm test passes locally with the kits built from source (112 checks).

Draft until the kits are published: needs security-kit 0.3.0 (mgpl-diagnostics/security-kit#3) and auth-kit 1.3.0 (mgpl-diagnostics/auth-kit#8), both merged. After publishing them, run npm install here to update package-lock.json and push; CI will then go green.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VT1kmGY9yJ2fLxetUn2D5c


Generated by Claude Code

…NT_HEADER)

security-kit 0.3.0 reads the JA4/JA3 header only through TRUST_PROXY proxies;
login and refreshSession pass it to auth-kit 1.3.0, which stores a hash on the
session and audits a change (auth.tls_fingerprint_changed). The api refuses to
start with TLS_FINGERPRINT_HEADER but no trusted proxy.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VT1kmGY9yJ2fLxetUn2D5c
@BackStacked BackStacked self-assigned this Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

ci fails at npm ci: No matching version found for @mgpl-diagnostics/auth-kit@^1.3.0. The code isn't at fault. The PR needs security-kit 0.3.0 and auth-kit 1.3.0, and neither is on GitHub Packages yet (both are merged: security-kit#3, auth-kit#8). Only the code owner can publish them. Once they're published, run npm install here, commit the updated package-lock.json, and CI should pass. Locally, with the kits built from source, npm run typecheck, TEST_BACKENDS=pg npm test (112 checks) and npm run build all pass.


Generated by Claude Code

BackStacked added a commit that referenced this pull request Oct 16, 2026
<!-- claude-projects-attribution: {"github_login":"BackStacked"} -->
_Requested by **Himanshu** · [project
thread](https://claude.ai/code/project/chan_016JXeBZ2UxSi4N5VU2C26fQ?thread=cmsg_016JXeBZ2UxSi4N5VU2C26fQQc5WpMAjyWkzD5MQ9cYbKC)_

Before: the api connected straight to PostgreSQL with Sequelize's
default pool, every process created tables on start, `GET /health` only
checked the database, and SIGTERM closed the server at once. Running
several replicas was described but not tested or packaged.

After: the api runs as N replicas behind a proxy, connecting through
PgBouncer in transaction mode. `docker-compose.yml` brings up
PostgreSQL, PgBouncer, Redis, a one-shot table sync, `API_REPLICAS` api
containers, the worker and nginx. Replicas drain cleanly on SIGTERM, and
`/health` tells the load balancer when a replica can't serve.

How:
- **PgBouncer.** `DATABASE_URL` points at PgBouncer; no code change was
needed in the db layer. db-kit uses unnamed statements only, `FOR
UPDATE` and isolation inside one transaction, one-statement counters,
and no advisory locks, `LISTEN`, or temp tables. Sequelize's
connect-time `SET TIME ZONE` is a parameter PgBouncer tracks per client.
`npm test` now runs the **whole PostgreSQL suite a second time through
PgBouncer** (transaction mode, 3 server connections).
- **Pools.** `DB_POOL_MAX` / `DB_POOL_MIN` / `DB_POOL_IDLE_MS` /
`DB_POOL_ACQUIRE_MS` size the Sequelize pool (pg) or the MongoDB driver
pool. `DB_SYNC=false` plus `node dist/sync.js` (`npm run db:sync`) stops
replicas racing to create tables.
- **Replicas.** A new `[replicas]` test runs two servers on one
PgBouncer and one Redis. A session, a refresh and a logout each cross
replicas, and login limits are shared (limited at attempt 11, not 21).
16 concurrent invoices with payments from both replicas get unique
numbers and the books balance. PostgreSQL saw at most 3 connections.
- **Readiness and shutdown** (`src/lifecycle.ts`). `GET /health` returns
503 when the database or Redis fails (2 s timeout each) or the replica
is draining. `GET /health?live=1` is liveness. This stays within the
one-route rule. On SIGTERM the replica drains for `SHUTDOWN_DRAIN_MS`,
then `app.close()` finishes in-flight requests, and it exits anyway
after `SHUTDOWN_TIMEOUT_MS`. The worker gets the same timeout.
- **Packaging.** Adds a `Dockerfile` (kits token as a build secret),
`docker-compose.yml` and `deploy/nginx.conf`, which re-resolves `api` so
`--scale` needs no restart and retries a request on another replica
after a 503. CI installs `pgbouncer`.
- **Docs.** New `docs/17-scaling.md` covers what each replica needs,
where state lives, PgBouncer compatibility, pool sizing, health,
shutdown, compose and Kubernetes. Also updates `.env.example`, docs
09/11/12 and AGENTS.md (rule 14).

Verified locally: typecheck; `TEST_BACKENDS=pg npm test` 193/193
(MongoDB binaries are blocked in this sandbox, so CI runs mongo). The
compose stack came up with 3 healthy replicas through nginx. Stopping
one replica under constant traffic answered 40 of 40 requests with 200.

Overlap with #9 (TLS fingerprint): both PRs touch `.env.example`,
`src/config/env.ts`, `src/server.ts`, `test/run.ts`, docs 09/12 and
`llms-full.txt`, in different hunks. Whichever merges second needs a
rebase and `npm run llms`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01Dcqyj37vsGrmgdnp7DLbkn

---
_Generated by [Claude
Code](https://claude.ai/code/session_01Dcqyj37vsGrmgdnp7DLbkn)_

---------

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants