[PR #5485] [Engine] serve: Cloudflare Access zero-trust JWT authentication (STRATA_CF_ACCESS_TEAM) #5463

Open
opened 2026-11-07 20:39:53 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5485
Author: @DamirLukina88
Created: 11/7/2026
Status: 🔄 Open

Base: main ← Head: feat/cloudflare-access-auth


📝 Commits (2)

  • 1b17f88 feat(serve): opt-in Cloudflare Access JWT authentication (STRATA_CF_ACCESS_TEAM)
  • 58be0d3 fix(serve): address review on CSRF origin checks, retry throttling, and non-dict JWT headers

📊 Changes

4 files changed (+414 additions, -10 deletions)

View changed files

📝 docs/DETAILS.md (+2 -0)
➕ serve/cf_access.py (+145 -0)
📝 serve/server.py (+29 -10)
➕ serve/test_cf_access.py (+238 -0)

📄 Description

Problem and result

When exposing Strata remotely through a tunnel (such as cloudflared), clients connecting through a Cloudflare Access application must currently enter and store the server API key in browser storage, which presents credential leakage risks on shared machines.

This change adds opt-in support for verifying Cloudflare Access JWT assertions (Cf-Access-Jwt-Assertion header and CF_Authorization cookie). When configured via STRATA_CF_ACCESS_TEAM and STRATA_CF_ACCESS_AUD, requests arriving over loopback from the local tunnel connector are authorized without requiring an API key.

Refs #4207 (remote host access and tunnel overrides).

Approach

  • Added serve/cf_access.py: A standard-library-only RSA PKCS#1 v1.5 SHA-256 JWT verifier (zero external dependencies). Verifies signatures against Cloudflare published JWKs (https://<team>.cloudflareaccess.com/cdn-cgi/access/certs, cached for 1 hour with a 60-second minimum refetch interval on both unknown kid lookups and failed network refreshes).
  • Validates that the decoded JWT header and payload are JSON objects, and checks the token issuer, audience tags, and timestamp validity (exp, nbf with 60-second clock skew tolerance).
  • When only STRATA_CF_ACCESS_TEAM is set, the server operates in discovery mode: it logs the AUD tag of the first valid login seen without authorizing, allowing the audience tag to be copied easily from logs.
  • serve/server.py: In _authorized() and _authorized_telemetry(), checks _access_claims() if self.cf_access is active and the client address is loopback (127.0.0.1 / ::1).
  • Preserves CSRF protection in _foreign_page(): browser POSTs authenticated via ambient Cloudflare Access credentials (rather than an explicit API key via _given_key()) still must pass Origin and Content-Type: application/json checks.
  • If unconfigured, self.cf_access is None and the default authentication path remains byte-identical.
  • Unit tests added in serve/test_cf_access.py (covering JWT verification, non-dict header rejection, failed fetch retry throttling, CSRF Origin enforcement on Access cookies, and end-to-end HTTP mock server authorization).
  • Documentation updated in docs/DETAILS.md.

Validation

Passed:

  • python3 serve/test_cf_access.py -v: All 11 unit tests passed in 1.80s (key caching, tamper rejection, non-dict header rejection, failed fetch retry throttling, expiry checks, discovery mode, token extraction, CSRF Origin check with Access cookies, and live HTTP mock server auth). Tested on revision 58be0d3d.
  • python3 -m unittest serve.test_security -v: All 30 security and auth tests passed in 10.3s.
  • python3 tools/test_source_nul_bytes.py: Passed (0.16s).
  • Tested live on Linux host (CachyOS, kernel 6.13) behind a Cloudflare tunnel connector.

Failed:

  • None.

Not run:

  • Non-Linux platforms (the implementation is pure Python standard library and OS-independent).

Limits

  • Only requests originating from loopback (127.0.0.1, ::1, ::ffff:127.0.0.1) are eligible for Access claim authorization to prevent spoofing if the port is exposed directly.
  • Only RS256 algorithm is supported (Cloudflare Access default).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/5485 **Author:** [@DamirLukina88](https://github.com/DamirLukina88) **Created:** 11/7/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/cloudflare-access-auth` --- ### 📝 Commits (2) - [`1b17f88`](https://github.com/Niko1221/Strata/commit/1b17f88cb2cc8f31c65fd7db8ff7374cf02f5659) feat(serve): opt-in Cloudflare Access JWT authentication (STRATA_CF_ACCESS_TEAM) - [`58be0d3`](https://github.com/Niko1221/Strata/commit/58be0d3d80b6adc6b0cee2969ecd9f56ac1c507f) fix(serve): address review on CSRF origin checks, retry throttling, and non-dict JWT headers ### 📊 Changes **4 files changed** (+414 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+2 -0) ➕ `serve/cf_access.py` (+145 -0) 📝 `serve/server.py` (+29 -10) ➕ `serve/test_cf_access.py` (+238 -0) </details> ### 📄 Description ## Problem and result When exposing Strata remotely through a tunnel (such as cloudflared), clients connecting through a Cloudflare Access application must currently enter and store the server API key in browser storage, which presents credential leakage risks on shared machines. This change adds opt-in support for verifying Cloudflare Access JWT assertions (`Cf-Access-Jwt-Assertion` header and `CF_Authorization` cookie). When configured via `STRATA_CF_ACCESS_TEAM` and `STRATA_CF_ACCESS_AUD`, requests arriving over loopback from the local tunnel connector are authorized without requiring an API key. ## Related discussion Refs #4207 (remote host access and tunnel overrides). ## Approach - Added `serve/cf_access.py`: A standard-library-only RSA PKCS#1 v1.5 SHA-256 JWT verifier (zero external dependencies). Verifies signatures against Cloudflare published JWKs (`https://<team>.cloudflareaccess.com/cdn-cgi/access/certs`, cached for 1 hour with a 60-second minimum refetch interval on both unknown `kid` lookups and failed network refreshes). - Validates that the decoded JWT header and payload are JSON objects, and checks the token issuer, audience tags, and timestamp validity (`exp`, `nbf` with 60-second clock skew tolerance). - When only `STRATA_CF_ACCESS_TEAM` is set, the server operates in discovery mode: it logs the AUD tag of the first valid login seen without authorizing, allowing the audience tag to be copied easily from logs. - `serve/server.py`: In `_authorized()` and `_authorized_telemetry()`, checks `_access_claims()` if `self.cf_access` is active and the client address is loopback (`127.0.0.1` / `::1`). - Preserves CSRF protection in `_foreign_page()`: browser POSTs authenticated via ambient Cloudflare Access credentials (rather than an explicit API key via `_given_key()`) still must pass `Origin` and `Content-Type: application/json` checks. - If unconfigured, `self.cf_access` is `None` and the default authentication path remains byte-identical. - Unit tests added in `serve/test_cf_access.py` (covering JWT verification, non-dict header rejection, failed fetch retry throttling, CSRF `Origin` enforcement on Access cookies, and end-to-end HTTP mock server authorization). - Documentation updated in `docs/DETAILS.md`. ## Validation **Passed:** - `python3 serve/test_cf_access.py -v`: All 11 unit tests passed in 1.80s (key caching, tamper rejection, non-dict header rejection, failed fetch retry throttling, expiry checks, discovery mode, token extraction, CSRF `Origin` check with Access cookies, and live HTTP mock server auth). Tested on revision 58be0d3d. - `python3 -m unittest serve.test_security -v`: All 30 security and auth tests passed in 10.3s. - `python3 tools/test_source_nul_bytes.py`: Passed (0.16s). - Tested live on Linux host (CachyOS, kernel 6.13) behind a Cloudflare tunnel connector. **Failed:** - None. **Not run:** - Non-Linux platforms (the implementation is pure Python standard library and OS-independent). ## Limits - Only requests originating from loopback (`127.0.0.1`, `::1`, `::ffff:127.0.0.1`) are eligible for Access claim authorization to prevent spoofing if the port is exposed directly. - Only RS256 algorithm is supported (Cloudflare Access default). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5463
No description provided.