Skip to content

feat(example): mTLS-capable HTTPS variant in nodejs-webhook server - #13

Merged
anhtuank7c merged 1 commit into
mainfrom
feat/example-mtls-receiver
May 8, 2026
Merged

anhtuank7c merged 1 commit into
mainfrom
feat/example-mtls-receiver

Conversation

@anhtuank7c

Copy link
Copy Markdown
Owner

Summary

Ships the runnable mTLS path that v0.4.0's webhook_client_cert / webhook_client_key keys (#8) need to demo end-to-end. The bundled examples/nodejs-webhook/ receiver previously only listened on plain HTTP via app.listen(); the README's "Mutual TLS (advanced)" section showed the construction pattern as a snippet, but the actual code that would do that didn't ship — operators following the README hit a "where do I put this?" wall. Same docs-vs-reality gap that #7 closed for the Bearer-auth side.

Linked issue

Closes #12. Builds on #7 (c96508c) and #8 (19e09ce), both shipped in v0.4.0. No change to pamsignal itself.

What changed

  • src/server.ts — branches on env vars. When TLS_KEY_PATH + TLS_CERT_PATH are set, constructs an HTTPS server (with optional TLS_CLIENT_CA_PATH for trust-store override and TLS_REQUIRE_CLIENT_CERT=true for full mTLS via requestCert + rejectUnauthorized); otherwise the existing plain-HTTP path is unchanged. Backward compatible: operators with a Bearer-only deployment see no behavioral change.
  • .env.example — documents the four new TLS env vars under a comment block that names the corresponding webhook_client_cert / webhook_client_key keys on the pamsignal side.
  • scripts/gen-test-certs.sh — produces a CA + server cert + client cert under ./certs/ via openssl. Server cert gets subjectAltName=DNS:localhost,IP:127.0.0.1 so Node's HTTPS client accepts it; private keys are mode 0600 to match pamsignal's group-readable rejection.
  • tests/mtls.test.ts — three new Jest cases that spin up the full HTTPS server with the same construction pattern as src/server.ts, generate certs at setup-time via openssl spawn, and verify the three behaviours that matter (valid cert + Bearer → 200, no client cert → TLS-layer rejection, Bearer mismatch on successful handshake → 401). Total Jest cases 7 → 10. All pass.
  • README.md — "Mutual TLS (advanced)" subsection rewritten as a runnable end-to-end demo (cert script + env vars + matching pamsignal.conf block) with an explicit production-vs-demo separation pointing operators at cert-manager / certbot / systemd-creds for real deployments.
  • .gitignore — adds certs/, package-lock.json, yarn.lock so locally-generated test certs and accidentally-installed npm/yarn lockfiles don't pollute the pnpm-managed example repo.
  • CHANGELOG.md — ## Unreleased / ### Examples entry.

Test plan

  • npm test (Node 24, locally on macOS) — 10/10 pass including the 3 new mTLS cases.
  • npm run build (TypeScript strict mode) — clean.
  • scripts/gen-test-certs.sh /tmp/foo smoke-tested manually; openssl verify -CAfile ca.crt server.crt client.crt returns OK on both.
  • mTLS test deliberately uses https.request for the no-cert negative path rather than raw tls.connect, because the latter races with the server's reject-after-handshake behavior — Node fires the client's secureConnect callback before the server has finished tearing the connection down. The https.request form surfaces the rejection cleanly as a request-level error.

Threat-model alignment

No new attack surface — this is example code, not a pamsignal feature. But the example does demonstrate the threat-model property #8 added: clients without a valid cert signed by TLS_CLIENT_CA_PATH are rejected at the TLS handshake, before any HTTP request reaches Express. The tests/mtls.test.ts "no client cert" case is the unit test for that property on the receiver side.

The cert-generation script is not for production and the docs say so explicitly. Operators replace ./certs/* with paths managed by their cert pipeline.

Pre-commit checklist

  • npm run build — TypeScript clean, no warnings.
  • npm test — 10/10 pass (jest), 7 existing + 3 new.
  • CHANGELOG.md — ## Unreleased → ### Examples entry added.
  • Commit subject follows Conventional Commits (feat(example):, imperative, ≤72 chars).
  • Not a breaking change — TLS env vars are opt-in, default behavior unchanged.
  • No change to pamsignal source (src/, include/, tests/test_*.c), so the C build and CMocka tests are not affected — CI's ASAN+UBSAN job is a no-op on this PR.

Notes for the reviewer

  • Cert generation in tests: I considered shipping pre-built fixtures vs. generating on demand. Went with on-demand via openssl spawn because (a) no expiry-management problem in 1 / 5 / 10 years, (b) the test exercises the same pipeline operators will run via gen-test-certs.sh. Cost: tests need openssl in PATH — ubiquitous on Linux/macOS, present in every CI runner we use.
  • Why node:tls rejection isn't tested directly: an earlier draft used tls.connect for the no-cert case but Node's secureConnect callback fires before the server has finished tearing the connection down on rejectUnauthorized — race condition. https.request waits for the actual response or error and is reliable.
  • mTLS env vars are opt-in: with no TLS_* env, the example behaves byte-identically to v0.3.x and the existing 7 supertest cases continue to pass against app directly.

🤖 Generated with Claude Code

Closes #12. The bundled examples/nodejs-webhook/ receiver now
ships a runnable mTLS path that pairs with pamsignal's
webhook_client_cert / webhook_client_key keys (#8). Previously
the README's "Mutual TLS (advanced)" section showed an
https.createServer({ requestCert: true, rejectUnauthorized: true })
snippet, but the actual code shipped only plain http via
app.listen() — operators following the README had to write that
piece themselves.

src/server.ts: branch on env vars. When TLS_KEY_PATH and
TLS_CERT_PATH are set, build an HTTPS server with optional CA
override (TLS_CLIENT_CA_PATH) and optional mTLS enforcement
(TLS_REQUIRE_CLIENT_CERT=true → requestCert + rejectUnauthorized).
With no TLS env vars set, behavior is identical to today: plain
HTTP via Express on the same port.

scripts/gen-test-certs.sh: produces a CA + server cert (CN=localhost
with subjectAltName) + client cert (CN=pamsignal-test-client) under
./certs/ via openssl, mode-locks the private keys to 0600 (matches
pamsignal's group-readable rejection), and prints next steps. For
local end-to-end testing only — production deployments use the
operator's cert pipeline.

tests/mtls.test.ts: three Jest cases over a real HTTPS server
constructed with the same pattern as src/server.ts. Generates
certs at setup-time via openssl spawn (same commands as
gen-test-certs.sh), tears down server + cert dir in afterAll.
Verifies: valid-cert+Bearer → 200, no-client-cert → TLS-layer
rejection (request errors before reaching Express), Bearer
mismatch on a successful handshake → 401. Total Jest cases
7 → 10.

README "Mutual TLS (advanced)" rewritten as a runnable end-to-end
demo (cert script + env vars + matching pamsignal.conf block)
rather than a conceptual snippet, with an explicit
production-vs-demo separation pointing operators at cert-manager /
certbot / systemd-creds for real deployments.

.gitignore adds certs/, package-lock.json, yarn.lock so locally
generated test certs and accidentally-installed npm/yarn
lockfiles don't pollute the pnpm-managed example repo.

CHANGELOG: Unreleased / Examples entry.

No change to pamsignal itself — pamsignal v0.4.0 already speaks
mTLS correctly via #8.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@anhtuank7c
anhtuank7c merged commit dea4f6c into main May 8, 2026
4 checks passed
@anhtuank7c
anhtuank7c deleted the feat/example-mtls-receiver branch May 8, 2026 15:06
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.

Node.js webhook example: HTTPS + mTLS server variant

1 participant