{"record":{"id":"3f1ea983f2547202","repo":"clockworklabs/SpacetimeDB","slug":"unexpected-type-for-aud-claim-in-jwt","errorCode":null,"errorMessage":"Unexpected type for 'aud' claim in JWT","messagePattern":"Unexpected type for 'aud' claim in JWT","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bindings/src/lib.rs","lineNumber":2035,"sourceCode":"            .get(\"sub\")\n            .expect(\"Missing 'sub' claim\")\n            .as_str()\n            .expect(\"Token 'sub' claim is not a string\")\n    }\n\n    /// Returns the issuer for these credentials, from the iss claim.\n    pub fn issuer(&self) -> &str {\n        self.get_parsed().get(\"iss\").unwrap().as_str().unwrap()\n    }\n\n    fn extract_audience(&self) -> Vec<String> {\n        let Some(aud) = self.get_parsed().get(\"aud\") else {\n            return Vec::new();\n        };\n        match aud {\n            serde_json::Value::String(s) => vec![s.clone()],\n            serde_json::Value::Array(arr) => arr.iter().filter_map(|v| v.as_str().map(String::from)).collect(),\n            _ => panic!(\"Unexpected type for 'aud' claim in JWT\"),\n        }\n    }\n\n    /// Returns the audience for these credentials, from the aud claim.\n    pub fn audience(&self) -> &[String] {\n        self.audience.get_or_init(|| self.extract_audience())\n    }\n\n    /// Returns the identity for these credentials, which is\n    /// based on the iss and sub claims.\n    pub fn identity(&self) -> Identity {\n        Identity::from_claims(self.issuer(), self.subject())\n    }\n\n    /// Get the whole JWT payload as a json string.\n    ///\n    /// This method is intended for parsing custom claims,\n    /// beyond the methods offered by [`JwtClaims`].","sourceCodeStart":2017,"sourceCodeEnd":2053,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/bindings/src/lib.rs#L2017-L2053","documentation":"Credentials::audience() lazily parses the aud claim of an attached JWT. A missing aud returns an empty vec; a JSON string yields one audience; a JSON array yields its string elements (non-string entries are silently dropped). The panic fires only when aud is present but is neither a string nor an array - for example a number or an object.","triggerScenarios":"A client connects with a JWT whose aud claim is a numeric or structured value (\"aud\": 123, \"aud\": {\"x\": 1}), typically minted by a custom issuer or test token generator; any code path that calls audience() (directly or via identity derivation) then panics.","commonSituations":"Custom OAuth/token services that emit numeric audiences; test fixtures with hand-written JWT payloads; third-party identity providers with non-standard aud formats.","solutions":["Fix the token issuer to emit aud as a string or an array of strings per RFC 7519.","Decode and validate the aud claim type before relying on audience() for untrusted tokens.","Regenerate/replace stored tokens after the issuer is fixed."],"exampleFix":"// issuer: before\n{\"sub\": \"u1\", \"iss\": \"https://x\", \"aud\": 12345}\n\n// issuer: after\n{\"sub\": \"u1\", \"iss\": \"https://x\", \"aud\": [\"my-db\"]}","handlingStrategy":"validation","validationCode":"fn aud_claim_ok(claims: &serde_json::Value) -> bool {\n    match claims.get(\"aud\") {\n        None => true,\n        Some(serde_json::Value::String(_)) => true,\n        Some(serde_json::Value::Array(a)) => a.iter().all(|v| v.is_string()),\n        _ => false,\n    }\n}\n// run on the decoded payload before trusting audience()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Emit aud as a string or array of strings in your token issuer.","Validate claim shapes in test fixtures before running modules against them.","Prefer standard OIDC providers with RFC 7519-compliant aud formatting."],"tags":["rust","jwt","claims","authentication"],"backgroundTag":"jwt-invalid-claims","analyzedSha":"eddf9f5014579a50d4b67630e28b6e15cad9c4af","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}