Repository navigation
feat(auth): TLS fingerprint from the trusted TLS proxy (TLS_FINGERPRINT_HEADER) - #9
Draft
BackStacked wants to merge 2 commits into
Draft
BackStacked wants to merge 2 commits into
BackStacked wants to merge 2 commits into
Conversation
…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
Contributor
Author
|
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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(andTRUST_PROXY≥ 1), the JA4/JA3 TLS fingerprint the proxy computed is passed to auth-kit atloginandrefreshSession, stored as a SHA-256 hash on the session, and a change on refresh is audited asauth.tls_fingerprint_changed.auth.device_mismatchalso 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.tlsFingerprintpassed 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 testpasses 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 installhere to updatepackage-lock.jsonand push; CI will then go green.🤖 Generated with Claude Code
https://claude.ai/code/session_01VT1kmGY9yJ2fLxetUn2D5c
Generated by Claude Code