Skip to content

fix: Verify TLS for requests through the mTLS agent - #14734

Draft
amoore108 wants to merge 2 commits into
latestfrom
amoore108-scope-mtls-agent
Draft

amoore108 wants to merge 2 commits into
latestfrom
amoore108-scope-mtls-agent

Conversation

@amoore108

@amoore108 amoore108 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Resolves JIRA:

Summary

getAgent installs the mTLS agent as undici's global dispatcher, and that agent set rejectUnauthorized: false. So once any request had loaded the certificates, every server-side fetch in the process skipped TLS certificate verification, including calls to third parties. This PR keeps the global dispatcher but turns verification back on.

The global dispatcher has to stay: it's the only way the agent takes effect. Node's built-in fetch ignores the agent option callers pass, and rejects our undici@8 Agent as a dispatcher (invalid onRequestStart method, on Node 22 and 24).

Code changes

  • Remove rejectUnauthorized: false, so all server-side requests verify certificates.
  • Trust Node's default root CAs alongside the CA bundle (ca: [...tls.rootCertificates, ca]), because setting ca replaces the default trust store and requests to services that don't use the BBC CA also go through this agent.
  • Memoise the in-flight promise, so concurrent first calls share one cert load and the dispatcher is installed once. A failed cert load isn't cached, so the next call retries.
  • getAgent now always resolves to the agent. Its first call used to return setGlobalDispatcher(...), which is always undefined.

Testing

  1. Run yarn workspace simorgh-nextjs test utilities/getAgent. The tests now cover verification being on, the dispatcher being installed once, concurrent calls and retrying after a failure.
  2. Verified locally on Node 24 with the built-in fetch and generated certificates:
    • a server that requires a client cert: 200 with the cert accepted
    • a server signed by an untrusted CA: rejected
    • https://www.bbc.co.uk/: 200
  3. On a test environment, please check that pages, navigation config and toggles load from the BFF. This confirms the BFF's certificate verifies against the CA bundle. Also check that server-side requests to services that don't use the BBC CA (idcta config, ws-downloads.files.bbci.co.uk, OG image fonts) still succeed.

Useful Links

@amoore108 amoore108 self-assigned this Nov 29, 2026
getAgent returned the result of setGlobalDispatcher (always undefined)
on its first call, and the agent it installed as undici's global
dispatcher set rejectUnauthorized: false. Once any request had loaded
the certs, every server-side fetch in the process skipped TLS
certificate verification, including calls to third parties.

The global dispatcher stays, because it is the only way the agent takes
effect: Node's built-in fetch ignores the `agent` option callers pass,
and rejects our undici@8 Agent as a `dispatcher` ("invalid
onRequestStart method").

- Remove rejectUnauthorized: false, so all requests verify certificates.
- Trust Node's default root CAs alongside the CA bundle, as setting `ca`
  replaces the default trust store.
- Memoise the in-flight promise, so concurrent first calls share one
  cert load and agent, and the dispatcher is installed once. Always
  resolve to the agent, and retry after a failed cert load.
@amoore108
amoore108 force-pushed the amoore108-scope-mtls-agent branch from 5d1afe1 to 4c18275 Compare November 29, 2026 23:07
@amoore108 amoore108 changed the title fix: Scope mTLS agent to the requests that use it fix: Verify TLS for requests through the mTLS agent Nov 29, 2026

This branch has not been deployed

No deployments
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