ADR-023: Live Certificate Fetch & Deterministic Security Grading
Context
The SSL/TLS Certificate Assistant (ADR-010) could only inspect a certificate the visitor pasted or uploaded — real friction for a demo, since a recruiter must first find a .pem file before anything happens. Two extensions make it land instantly and show deeper PKI judgement: fetch by hostname (type example.com and the server retrieves the certificate the host actually presents) and an A–F security grade with specific findings instead of a raw attribute dump. The hard part is doing the fetch safely: a backend that connects to a user-supplied host is a textbook Server-Side Request Forgery (SSRF) primitive — a caller could aim it at 169.254.169.254 (cloud metadata), 127.0.0.1, or an RFC 1918 address and turn the server into a confused-deputy proxy into the private network.
Decision
Add POST /api/cert/fetch and a deterministic grade behind the existing demoCert flag, reusing the /api/cert/ rate-limit and App Check middleware. The SSRF defenses run cheapest-and-most-decisive first: (1) reject IP literals — callers name a public host, never an address; (2) reject no-dot names, localhost, and *.local/.internal/.lan; (3) DNS-resolve and reject if ANY resolved address is private/reserved (RFC 1918, loopback, 169.254/16 link-local incl. metadata, CGNAT, IPv6 ULA/link-local, IPv4-mapped, multicast, TEST-NETs); (4) pin the TLS socket to the vetted IP with SNI set to the hostname, closing the DNS-rebinding TOCTOU window; (5) restrict the port to an HTTPS allowlist (443/8443); (6) hard 5s timeout, no redirects, no application data. The handshake uses rejectUnauthorized:false on purpose so expired/self-signed/untrusted certs can still be inspected and graded; the trust verdict is captured separately and fed to the grader, never used to abort. The leaf is parsed with Node's native crypto.X509Certificate, which handles EC keys as well as RSA; node-forge only reads the signature-algorithm OID from the ASN.1. /api/cert/parse still tries node-forge first and falls back to the same native parser for EC certificates, so both endpoints return the same certDetails shape. /fetch also returns the presented chain (leaf → root), which the UI visualises. gradeCert() is a pure function scoring validity (expired ⇒ F; ≤30-day and >398-day windows penalised), signature algorithm (SHA-1/MD5 fail), key strength (sub-2048-bit RSA fail; EC flagged, not penalised), self-signed, and — for fetched certs — live chain trust. It runs on the server clock, resolving ADR-010's client-clock limitation.
Consequences
Benefits:
* Zero-friction demo: type a domain and immediately see a parsed, graded certificate — no file hunting.
* Demonstrates security judgement, not just parsing — the SSRF threat model and grading rubric are the portfolio artifact.
* No new trust boundary: the fetch involves no private keys and grading is pure metadata logic, so the ADR-010 dual-boundary model is intact; the new route inherits the existing rate-limit + App Check stack.
* Testable safety: the SSRF guard and grader are pure functions covered by unit tests; the route's synchronous rejections are asserted offline (no network in CI).
Trade-offs:
* Egress is now a capability — bounded (port allowlist, 5s timeout, rate limited, denylisted ranges) but a larger surface than a pure parser.
* IPv6 classification is range-based, not exhaustive; new reserved ranges need a code update.
* The grade is a documented heuristic, not a CA policy engine — it omits CT logs, OCSP/CRL revocation, and full path-building.
Alternatives considered
1. Block-list by hostname instead of by resolved IP: Rejected — trivially bypassed via DNS; only the resolved address tells the truth about where a connection goes.
2. Resolve, validate, then connect by hostname: Rejected — re-resolving at connect time reopens the DNS-rebinding window; pinning to the validated IP is the whole point.
3. Client-side grading: Rejected — the browser clock is untrustworthy for expiry, and a single server-side source of truth keeps /parse and /fetch consistent.
4. A full TLS-scanner library (SSL Labs-style engine): Rejected as over-scoped for a portfolio demo and a heavier dependency/attack surface than the focused node-forge + Node crypto/tls path already in use.