From e0dfd3fc0f4fc6bba0e0243ef4a2ffc28ca680e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86ndra=20Rininsland?= Date: Thu, 27 Aug 2026 22:56:06 +0100 Subject: [PATCH 1/4] Accept a JWT audience sent as a string, not only an array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AccessClaims typed `aud` as Vec. RFC 7519 §4.1.3 allows either a single string or an array, and Cloudflare Access uses the string form when a token is scoped to one application — which is the ordinary case. So the claims failed to deserialize against every real token. The failure was badly disguised. It happened while parsing the claims, inside the same `decode` call that verifies the signature, so it surfaced as "Invalid Access token: JSON error: invalid type: string … expected a sequence". That reads like a rejected credential, and sent the first caller looking at service tokens and Access policies — all of which were correct. Access had authenticated the request and handed us a valid assertion; this code then declined to parse it. The audience is still checked by Validation::set_audience, which handles both shapes itself. This field only needs to deserialize, so it now normalises one-or-many to a list and nothing downstream changes. A number or an object is still rejected: tolerating the two shapes the spec allows is not the same as tolerating anything. Found by the first real request through Access, from the vetting Worker's service token. Co-Authored-By: Claude Opus 5 --- .vscode/settings.json | 22 ++++++++++++++ src/access.rs | 71 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3e49044 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,22 @@ +{ + "workbench.colorCustomizations": { + "activityBar.activeBackground": "#80eae9", + "activityBar.background": "#80eae9", + "activityBar.foreground": "#15202b", + "activityBar.inactiveForeground": "#15202b99", + "activityBarBadge.background": "#e35be4", + "activityBarBadge.foreground": "#15202b", + "commandCenter.border": "#15202b99", + "sash.hoverBorder": "#80eae9", + "statusBar.background": "#54e3e1", + "statusBar.foreground": "#15202b", + "statusBarItem.hoverBackground": "#28dcd9", + "statusBarItem.remoteBackground": "#54e3e1", + "statusBarItem.remoteForeground": "#15202b", + "titleBar.activeBackground": "#54e3e1", + "titleBar.activeForeground": "#15202b", + "titleBar.inactiveBackground": "#54e3e199", + "titleBar.inactiveForeground": "#15202b99" + }, + "peacock.color": "#54e3e1" +} diff --git a/src/access.rs b/src/access.rs index 0a468af..8bb42ef 100644 --- a/src/access.rs +++ b/src/access.rs @@ -29,10 +29,41 @@ type CachedKeys = Arc)>>>; /// every request. const JWKS_TTL: Duration = Duration::from_secs(3600); +/// One or many, as JWT audiences are allowed to be. +/// +/// RFC 7519 §4.1.3 permits `aud` to be either a single string or an array of +/// them, and Cloudflare Access uses the string form when a token is scoped to +/// one application — which is the normal case here. Typing the field as a +/// `Vec` therefore failed to deserialize against real tokens, and +/// because it failed while parsing the claims it surfaced as +/// "Invalid Access token", which reads like a rejected credential rather than +/// a shape this code declined to accept. +fn one_or_many_audience<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + #[derive(Deserialize)] + #[serde(untagged)] + enum OneOrMany { + One(String), + Many(Vec), + } + + Ok(match OneOrMany::deserialize(deserializer)? { + OneOrMany::One(aud) => vec![aud], + OneOrMany::Many(auds) => auds, + }) +} + /// Claims we care about from an Access JWT. #[derive(Debug, Clone, Deserialize, Serialize)] pub struct AccessClaims { /// Access application audience tag. + /// + /// Normalised to a list on the way in; see `one_or_many_audience`. The + /// audience is still *checked* by `Validation::set_audience`, not here — + /// this field exists so the claims deserialize at all. + #[serde(deserialize_with = "one_or_many_audience")] pub aud: Vec, /// Issuer — the team domain. pub iss: String, @@ -189,6 +220,46 @@ mod tests { ) } + #[test] + fn accepts_an_audience_sent_as_a_bare_string() { + // The shape Cloudflare Access actually sends for a single-application + // token, and the one that broke the first real request through Access: + // it failed while parsing the claims, so a perfectly valid token was + // reported as "Invalid Access token". + let claims: AccessClaims = serde_json::from_str( + r#"{"aud":"0fbf6f198910584758aa5c42453a1a8aa90115e2f0de619c41e2bb9056e04ca0", + "iss":"https://team.cloudflareaccess.com","exp":9999999999, + "common_name":"vetting-worker"}"#, + ) + .expect("a string audience must deserialize"); + + assert_eq!( + claims.aud, + vec!["0fbf6f198910584758aa5c42453a1a8aa90115e2f0de619c41e2bb9056e04ca0"] + ); + assert_eq!(claims.subject(), "vetting-worker"); + } + + #[test] + fn still_accepts_an_audience_sent_as_an_array() { + let claims: AccessClaims = serde_json::from_str( + r#"{"aud":["one","two"],"iss":"https://team.cloudflareaccess.com","exp":9999999999}"#, + ) + .expect("an array audience must keep working"); + + assert_eq!(claims.aud, vec!["one", "two"]); + } + + #[test] + fn rejects_an_audience_that_is_neither() { + // Tolerating the two shapes the spec allows is not the same as + // tolerating anything. + let err = serde_json::from_str::( + r#"{"aud":42,"iss":"https://team.cloudflareaccess.com","exp":9999999999}"#, + ); + assert!(err.is_err()); + } + #[test] fn trims_a_trailing_slash_from_the_team_domain() { assert_eq!(config().team_domain, "https://team.cloudflareaccess.com"); From c6953eb59fcca4fa4452befab9ac9e2862b82712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86ndra=20Rininsland?= Date: Thu, 27 Aug 2026 22:57:54 +0100 Subject: [PATCH 2/4] Use a placeholder audience in the test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The value was a real Access AUD tag from the deployment that surfaced this bug. It is not a credential — it identifies an Access application and cannot mint, sign or forge anything — but it is deployment configuration and this repository is public, so it has no business being here when any 64-character string proves the same point. Co-Authored-By: Claude Opus 5 --- src/access.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/access.rs b/src/access.rs index 8bb42ef..40383ae 100644 --- a/src/access.rs +++ b/src/access.rs @@ -226,8 +226,13 @@ mod tests { // token, and the one that broke the first real request through Access: // it failed while parsing the claims, so a perfectly valid token was // reported as "Invalid Access token". + // + // The audience here is a placeholder. A real AUD tag is not a + // credential — it identifies an Access application and cannot mint or + // forge anything — but it is deployment configuration, and this + // repository is public. let claims: AccessClaims = serde_json::from_str( - r#"{"aud":"0fbf6f198910584758aa5c42453a1a8aa90115e2f0de619c41e2bb9056e04ca0", + r#"{"aud":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "iss":"https://team.cloudflareaccess.com","exp":9999999999, "common_name":"vetting-worker"}"#, ) @@ -235,7 +240,7 @@ mod tests { assert_eq!( claims.aud, - vec!["0fbf6f198910584758aa5c42453a1a8aa90115e2f0de619c41e2bb9056e04ca0"] + vec!["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"] ); assert_eq!(claims.subject(), "vetting-worker"); } From 74c2b11182fa25e7c1d53490ea0d04b3e416567c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86ndra=20Rininsland?= Date: Thu, 27 Aug 2026 23:01:38 +0100 Subject: [PATCH 3/4] Remove the committed VS Code window colours MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit .vscode/settings.json held only Peacock colour customisations — a per-person choice about what tint one editor window has. Committing it pushes one contributor's theme onto everyone who opens the repository, and the file churns whenever Peacock runs. Co-Authored-By: Claude Opus 5 --- .vscode/settings.json | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 3e49044..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "workbench.colorCustomizations": { - "activityBar.activeBackground": "#80eae9", - "activityBar.background": "#80eae9", - "activityBar.foreground": "#15202b", - "activityBar.inactiveForeground": "#15202b99", - "activityBarBadge.background": "#e35be4", - "activityBarBadge.foreground": "#15202b", - "commandCenter.border": "#15202b99", - "sash.hoverBorder": "#80eae9", - "statusBar.background": "#54e3e1", - "statusBar.foreground": "#15202b", - "statusBarItem.hoverBackground": "#28dcd9", - "statusBarItem.remoteBackground": "#54e3e1", - "statusBarItem.remoteForeground": "#15202b", - "titleBar.activeBackground": "#54e3e1", - "titleBar.activeForeground": "#15202b", - "titleBar.inactiveBackground": "#54e3e199", - "titleBar.inactiveForeground": "#15202b99" - }, - "peacock.color": "#54e3e1" -} From be4e5113eed1cb2db7f3b8361bdb5d5879ac6a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86ndra=20Rininsland?= Date: Thu, 27 Aug 2026 23:19:10 +0100 Subject: [PATCH 4/4] Say what common_name actually holds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The field was documented as carrying "the token's name". Cloudflare sets it to the service token's Client ID (CF-Access-Client-Id), and CF_ACCESS_ALLOWED_SERVICE_TOKENS is matched against it — so an operator following the comment would fill the allowlist with dashboard names that can never match, and the rejection would name a token that looks entirely correct. Nothing changes at runtime; the code was already comparing against the right value. Only the comments were wrong, which is the kind of wrong that surfaces as a misconfigured deployment rather than a failing test. Also records why the audience fix accepts both shapes: Cloudflare's own documentation shows `aud` as an array, and the deployment that hit this sent a string. The RFC permits either, so the parser takes either rather than trusting one source over the other. Co-Authored-By: Claude Opus 5 --- src/access.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/access.rs b/src/access.rs index 40383ae..ad79157 100644 --- a/src/access.rs +++ b/src/access.rs @@ -71,7 +71,10 @@ pub struct AccessClaims { /// Set for a *user* login; absent for a service token. #[serde(default)] pub email: Option, - /// Set for a *service token*; carries the token's name. + /// Set for a *service token*: the token's **Client ID** + /// (`CF-Access-Client-Id`), not the human-readable name it was given in + /// the dashboard. `CF_ACCESS_ALLOWED_SERVICE_TOKENS` is matched against + /// this, so it must be filled with Client IDs. #[serde(default)] pub common_name: Option, } @@ -104,7 +107,9 @@ pub struct AccessConfig { pub team_domain: String, /// The Access application's AUD tag. pub aud: String, - /// Optional allowlist of service token names. Empty means any principal + /// Optional allowlist of service token **Client IDs** — the value Access + /// puts in `common_name`, not the token's dashboard name. Empty means any + /// principal /// Access lets through is accepted, which is usually what the Access policy /// is already for. pub allowed_service_tokens: Vec, @@ -220,6 +225,21 @@ mod tests { ) } + #[test] + fn the_allowlist_matches_a_service_token_client_id() { + // Access sets `common_name` to the service token's Client ID, not the + // name it was given in the dashboard. Getting that backwards puts a + // plausible-looking value in the allowlist that can never match, and + // the resulting rejection names a token that looks correct. + let claims: AccessClaims = serde_json::from_str( + r#"{"aud":["tag"],"iss":"https://team.cloudflareaccess.com","exp":9999999999, + "sub":"","common_name":"1234abcd.access"}"#, + ) + .expect("service token claims must deserialize"); + + assert_eq!(claims.subject(), "1234abcd.access"); + } + #[test] fn accepts_an_audience_sent_as_a_bare_string() { // The shape Cloudflare Access actually sends for a single-application