Skip to content

Update cert pinning logic - #100518

Closed
staszekscp wants to merge 0 commit into
Expensify:mainfrom
software-mansion-labs:chore/update-cert-pins-2
Closed

staszekscp wants to merge 0 commit into
Expensify:mainfrom
software-mansion-labs:chore/update-cert-pins-2

Conversation

@staszekscp

@staszekscp staszekscp commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Switches NewDot's certificate pins from leaf + issuing intermediate to root CA only.

Leaf/intermediate pins have already broken twice in production — Cloudflare rotated the www/secure edge cert from Let's Encrypt to Google Trust Services (2026-07-07), and Amazon rotated CloudFront's intermediate from RSA 2048 M01 to M04 (2026-07). Leaves are re-keyed on every renewal and every CA issues from a rotating pool of intermediates, so roots are the only durable pin target. Pinning roots survives leaf rotation, intermediate rotation, and (since all three Cloudflare CAs are pinned) a CA switch, without an emergency release.

Pins are now:

  • Cloudflare-fronted *.expensify.com hosts → ISRG Root X1/X2 (Let's Encrypt), GTS Root R1–R4, SSL.com TLS ECC/RSA Root CA 2022.
  • d2k5nsl2zxldvw.cloudfront.net → Amazon Root CA 1–4 + Starfield Services Root CA G2 (the only stable pin targets per AWS docs).

Pinned hosts are unchanged: www, secure, new, integrations, travel (.expensify.com), their staging.* counterparts, and d2k5nsl2zxldvw.cloudfront.net.

Updated across all pin targets — config/certificatePinning/pins.json (canonical), CertificatePinning.kt (OkHttp), CertificatePinning.swift (TrustKit), the react-native-nitro-fetch Cronet patch, and network_security_config_enforce.xml.

Two supporting changes:

anchoredChain() in CertificatePinning.kt — a TLS server never sends its root, so the OkHttp and HttpsURLConnection monitors now rebuild the validated chain up to its trust anchor (X509TrustManagerExtensions.checkServerTrusted) before checking pins. Without it root pins could never match there and every request would report a false mismatch. Android , OkHttp enforce mode, TrustKit and Cronet include the anchor natively.
scripts/generateCertificatePins.sh — for the same reason, roots can't be read off a live handshake, so the script now downloads the Mozilla CA bundle and verifies each root against SHA-256 fingerprints committed in its ROOT_MANIFEST (the fingerprints, not the download, are the source of trust). Adds --verify (confirms each live chain anchors at a pinned root), --refresh and --android, plus ROOTS_BUNDLE= for offline/CI use. The root cache is gitignored.

Still monitor-only (enforcePinning = false everywhere): mismatches are reported to Sentry, connections are not blocked. The root pin-set for Android lives in network_security_config_enforce.xml, ready to copy over when we flip to enforce.

No JS or UI changes.

Fixed Issues

$ https://github.com/Expensify/Expensify/issues/633469
PROPOSAL:

MOBILE-EXPENSIFY: https://github.com/Expensify/Mobile-Expensify/pull/14106

Tests

Run a release build on Android and iOS (pinning is a no-op in debug). Throughout, check Sentry for certificate_pinning_* events — there should be none.

  1. Log in and open a report → app loads, API calls succeed, no certificate errors.
  2. Open the app fresh (triggers OpenApp) → initializes and syncs without errors.
  3. On a workspace with an accounting connection (e.g. QuickBooks), Settings → Workspace → Accounting → Sync now → completes without a certificate error.
  4. Open the Travel tab and start the booking flow → content loads without errors.
  5. Open a report with an image attachment or receipt, then download it → renders and downloads without errors (covers Cronet/fetch and react-native-blob-util/OkHttp).

Offline tests

QA Steps

  1. Log in and open a report → app loads, API calls succeed, no certificate errors.
  2. Open the app fresh (triggers OpenApp) → initializes and syncs without errors.
  3. On a workspace with an accounting connection (e.g. QuickBooks), Settings → Workspace → Accounting → Sync now → completes without a certificate error.
  4. Open the Travel tab and start the booking flow → content loads without errors.
  5. Open a report with an image attachment or receipt, then download it → renders and downloads without errors (covers Cronet/fetch and react-native-blob-util/OkHttp).

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Screen.Recording.2026-09-09.at.14.15.51.mov
Android: mWeb Chrome
iOS: Native
Simulator.Screen.Recording.-.iPhone.17.Pro.-.2026-09-09.at.14.05.34.mov
iOS: mWeb Safari
MacOS: Chrome / Safari

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

⚠️ This PR is possibly changing native code and/or updating libraries, it may cause problems with HybridApp. Please check if any patch updates are required in the HybridApp repo and run an AdHoc build to verify that HybridApp will not break. Ask Contributor Plus for help if you are not sure how to handle this. ⚠️

@staszekscp
staszekscp marked this pull request as ready for review September 10, 2026 08:29
@staszekscp
staszekscp requested review from a team as code owners September 10, 2026 08:29
@melvin-bot
melvin-bot Bot requested review from garrettmknight and nyomanjyotisa and removed request for a team September 10, 2026 08:29
@melvin-bot

melvin-bot Bot commented Sep 10, 2026

Copy link
Copy Markdown

@nyomanjyotisa Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot removed the request for review from a team September 10, 2026 08:29
@garrettmknight
garrettmknight removed their request for review September 10, 2026 08:41

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 342d4d6a37

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread scripts/generateCertificatePins.sh Outdated
Comment on lines +271 to +274
if [ ! -s "${tmpdir}/cert_1.pem" ]; then
printf ' %-34s UNREACHABLE (could not fetch chain)\n' "$host"
rm -rf "$tmpdir"
return

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Mark unreachable verification hosts as failures

When --verify cannot fetch a host's certificate chain (for example, due to a DNS, routing, or TLS outage), this path only prints UNREACHABLE and returns without setting VERIFY_FAILED. Consequently, a run where one or every pinned endpoint could not be checked still exits successfully, so CI or a release validation can report that pin verification passed without verifying all live chains.

Useful? React with 👍 / 👎.

Comment thread scripts/generateCertificatePins.sh Outdated
Comment on lines +204 to +207
for name in "${needed[@]}"; do
if [ "$fp" = "$(manifest_fingerprint "$name")" ]; then
openssl x509 -in "$split" -out "${ROOTS_DIR}/${name}.pem" 2>/dev/null
fi

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject stale roots during a forced refresh

With --refresh, every manifest root is considered needed, but an existing cached PEM is left in place when its certificate is absent from the newly downloaded bundle. The subsequent cached_root_ok check only compares that stale PEM to the manifest fingerprint, so the command succeeds and regenerates pins from the old cache rather than detecting that Mozilla has removed the root—the distrust condition this refresh flow is meant to surface.

Useful? React with 👍 / 👎.

Comment thread scripts/generateCertificatePins.sh Outdated
local failed=0
for name in "${needed[@]}"; do
if ! cached_root_ok "$name"; then
echo "ERROR: root '${name}' was not found in the CA bundle (or failed fingerprint" >&2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check the clean-checkout path? It fails for me because the current Mozilla bundle does not contain GTS Root R2.

Also, I think we should add regression tests for clean generation, refresh, and unreachable verification.

// Try the next authType; fall back to the raw peer chain if all fail.
}
}
return peerCertificates

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you confirm this fallback? If chain rebuilding fails, the raw chain for integrations.expensify.com has no pinned root, so I think it may report a false mismatch.

"staging.travel.expensify.com" to listOf("sha256/Qb3qmTdRt/xHEN5PVtn+YhKoGqF/lhRX88cSFuSCJqM=", "sha256/kIdp6NNEd8wsugYyyIYFsi1ylMCED3hZbSR8ZFsa/A4="),
// Group E: CloudFront CDN leaf + Amazon RSA 2048 M01 intermediate
"d2k5nsl2zxldvw.cloudfront.net" to listOf("sha256/P9HBoLji8YncXSnb0AnAm72fJO/vpmxZrsl4fvUBkxc=", "sha256/DxH4tt40L+eduF6szpY6TONlxhZhBd+pJ9wbHlQ2fuw="),
// Groups A-D: Cloudflare-fronted expensify.com hosts. Cloudflare can rotate the edge cert

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you confirm whether any of these pinned Cloudflare hostnames can use Sectigo backup certificates? Cloudflare lists Sectigo as a possible backup CA, but it is not included here.

@staszekscp

Copy link
Copy Markdown
Contributor Author

Thanks for the review, I've applied the requested changes!

@melvin-bot

melvin-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

I could not review this diff, so I did not approve the workflow runs. An Expensify employee will review it and approve them manually.

@staszekscp staszekscp closed this Sep 11, 2026
@staszekscp
staszekscp force-pushed the chore/update-cert-pins-2 branch from 96bb23a to 93ee237 Compare September 11, 2026 13:15
@melvin-bot

melvin-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

I could not review this diff, so I did not approve the workflow runs. An Expensify employee will review it and approve them manually.

@staszekscp

Copy link
Copy Markdown
Contributor Author

There is one unsigned commit up there. For simplicity I'll create a new branch and lint this PR there

@staszekscp

Copy link
Copy Markdown
Contributor Author

Closed in favor of #100965

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