{"record":{"id":"701955dd1c213a5f","repo":"clockworklabs/SpacetimeDB","slug":"unable-to-read-public-key-for-jwt-token-verificati","errorCode":null,"errorMessage":"Unable to read public key for JWT token verification","messagePattern":"Unable to read public key for JWT token verification","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"critical","filePath":"crates/core/src/auth/mod.rs","lineNumber":65,"sourceCode":"\n// Get the key pair if the given files exist. If they don't, create them.\n// If only one of the files exists, return an error.\npub fn get_or_create_keys(certs: &CertificateAuthority) -> anyhow::Result<JwtKeys> {\n    let public_key_path = &certs.jwt_pub_key_path;\n    let private_key_path = &certs.jwt_priv_key_path;\n\n    let public_key_bytes = public_key_path.read().ok();\n    let private_key_bytes = private_key_path.read().ok();\n\n    // If both keys are unspecified, create them\n    let key_pair = match (public_key_bytes, private_key_bytes) {\n        (Some(pub_), Some(priv_)) => EcKeyPair::new(pub_, priv_),\n        (None, None) => {\n            let keys = EcKeyPair::generate()?;\n            keys.write_to_files(public_key_path, private_key_path)?;\n            keys\n        }\n        (None, Some(_)) => anyhow::bail!(\"Unable to read public key for JWT token verification\"),\n        (Some(_), None) => anyhow::bail!(\"Unable to read private key for JWT token signing\"),\n    };\n\n    key_pair.try_into()\n}\n\n// An Ec key pair in pem format.\npub struct EcKeyPair {\n    pub public_key_bytes: Vec<u8>,\n    pub private_key_bytes: Vec<u8>,\n}\n\nimpl TryFrom<EcKeyPair> for JwtKeys {\n    type Error = anyhow::Error;\n    fn try_from(pair: EcKeyPair) -> anyhow::Result<Self> {\n        JwtKeys::new(pair.public_key_bytes, pair.private_key_bytes)\n    }\n}","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/core/src/auth/mod.rs#L47-L83","documentation":"On startup the node loads the EC key pair (PEM files at the configured public/private key paths) used to sign and verify JWTs. The two files are read independently; if the private key loads but the public key file cannot be read (missing, unreadable permissions, or unreadable content), startup aborts. Keys are auto-generated only when BOTH files are absent -- a half-present pair is treated as an error rather than silently replaced.","triggerScenarios":"The private key file exists but the public key file was deleted, moved, truncated, or has restrictive permissions; a partial copy or restore of the key directory; public and private key paths configured to different directories where only one file landed.","commonSituations":"Manual cleanup removing one file; a Docker volume mounting only one key; key rotation writing the new private key but failing before the public key; running as a service user without read permission on the public key file.","solutions":["Restore the matching public key from backup (it must correspond to the existing private key)","Otherwise regenerate the pair: remove BOTH key files and restart the node -- it generates and persists a fresh pair (note: previously issued tokens become invalid)","Fix filesystem permissions so the node process can read both files","Verify pair coherence: `openssl pkey -in private.pem -pubout` should equal the public key file"],"exampleFix":"# before: only the private key is present\n$ ls /keys\njwt_private.pem\n\n# after: remove the orphan and restart -- the node regenerates the pair\n$ rm -f /keys/jwt_private.pem\n$ systemctl restart spacetimedb","handlingStrategy":"validation","validationCode":"#!/usr/bin/env bash\n# run before starting the node\nfor f in \"$JWT_PUBLIC_KEY\" \"$JWT_PRIVATE_KEY\"; do\n  [ -r \"$f\" ] || { echo \"unreadable key: $f\" >&2; exit 1; }\ndone\n# optional: verify the pair matches\nopenssl pkey -in \"$JWT_PRIVATE_KEY\" -pubout 2>/dev/null | diff -q - \"$JWT_PUBLIC_KEY\" \\\n  || echo 'WARNING: public key does not match private key' >&2","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat the key pair as one unit: back up, copy, and rotate both files together","Add a pre-start health check that both files are readable and mutually consistent","Use a dedicated secrets volume; do not hand-edit or truncate key files","Alert on node startup failure so a missing key is caught by on-call, not users"],"tags":["auth","jwt","keys","server-startup","filesystem"],"backgroundTag":"key-pair-missing-or-corrupt","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}