clockworklabs/SpacetimeDB · error

Unexpected type for 'aud' claim in JWT

Error message

Unexpected type for 'aud' claim in JWT

What it means

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.

Solutions

  1. Fix the token issuer to emit aud as a string or an array of strings per RFC 7519.
  2. Decode and validate the aud claim type before relying on audience() for untrusted tokens.
  3. Regenerate/replace stored tokens after the issuer is fixed.

Example fix

// issuer: before
{"sub": "u1", "iss": "https://x", "aud": 12345}

// issuer: after
{"sub": "u1", "iss": "https://x", "aud": ["my-db"]}
Defensive patterns

Strategy: validation

Validate before calling

fn aud_claim_ok(claims: &serde_json::Value) -> bool {
    match claims.get("aud") {
        None => true,
        Some(serde_json::Value::String(_)) => true,
        Some(serde_json::Value::Array(a)) => a.iter().all(|v| v.is_string()),
        _ => false,
    }
}
// run on the decoded payload before trusting audience()

Prevention

When it happens

Trigger: 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.

Common situations: Custom OAuth/token services that emit numeric audiences; test fixtures with hand-written JWT payloads; third-party identity providers with non-standard aud formats.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@eddf9f5014 (2026-08-20). Data as JSON: /api/errors/3f1ea983f2547202. Report an issue: GitHub.

Appendix: source

Thrown at crates/bindings/src/lib.rs:2035

            .get("sub")
            .expect("Missing 'sub' claim")
            .as_str()
            .expect("Token 'sub' claim is not a string")
    }

    /// Returns the issuer for these credentials, from the iss claim.
    pub fn issuer(&self) -> &str {
        self.get_parsed().get("iss").unwrap().as_str().unwrap()
    }

    fn extract_audience(&self) -> Vec<String> {
        let Some(aud) = self.get_parsed().get("aud") else {
            return Vec::new();
        };
        match aud {
            serde_json::Value::String(s) => vec![s.clone()],
            serde_json::Value::Array(arr) => arr.iter().filter_map(|v| v.as_str().map(String::from)).collect(),
            _ => panic!("Unexpected type for 'aud' claim in JWT"),
        }
    }

    /// Returns the audience for these credentials, from the aud claim.
    pub fn audience(&self) -> &[String] {
        self.audience.get_or_init(|| self.extract_audience())
    }

    /// Returns the identity for these credentials, which is
    /// based on the iss and sub claims.
    pub fn identity(&self) -> Identity {
        Identity::from_claims(self.issuer(), self.subject())
    }

    /// Get the whole JWT payload as a json string.
    ///
    /// This method is intended for parsing custom claims,
    /// beyond the methods offered by [`JwtClaims`].

View on GitHub (pinned to eddf9f5014)