Skip to content

fix(speech-engine): strip residency suffix from API key before JWT verification - #873

Merged
PaulAsjes merged 1 commit into
nextfrom
fix/speech-engine-residency-key-hash-next
Sep 23, 2026
Merged

PaulAsjes merged 1 commit into
nextfrom
fix/speech-engine-residency-key-hash-next

Conversation

@PaulAsjes

Copy link
Copy Markdown
Collaborator

Summary

Speech Engine servers configured with a data-residency API key (for example sk_..._residency_in) rejected every upstream connection with "Invalid JWT: signature mismatch". The API signs the handshake JWT with a hash of the base key, with the _residency_<region> suffix removed, but the SDK hashed the key exactly as given. The SDK now strips the suffix the same way before hashing, so residency keys work without the workaround of passing a hand-trimmed key to the server.

Details

Testing

  • pytest tests/test_speech_engine_auth.py: 24 passed, including two new cases (residency key verifies against a token signed with the base key; residency key with the wrong base key is still rejected).
  • mypy clean on the changed file. Ruff reports one pre-existing import-sort warning on the file, present on the base branch too.

🤖 Generated with Claude Code

…rification

The API signs Speech Engine upstream JWTs with sha256 of the base API key,
stripping any `_residency_<region>` suffix first. The SDK hashed the key
verbatim, so any EU/IN/SG residency key failed the handshake with
"Invalid JWT: signature mismatch". Normalize the key the same way before
hashing.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@PaulAsjes
PaulAsjes marked this pull request as ready for review September 22, 2026 14:17
@cursor

cursor Bot commented Sep 22, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches Speech Engine JWT HMAC verification, but the change is limited to key normalization so residency keys align with API signing; wrong keys remain rejected in tests.

Overview
Fixes Speech Engine upstream auth when the server is configured with a data-residency API key (e.g. sk_..._residency_in), which previously failed JWT verification with a signature mismatch.

verify_speech_engine_jwt now normalizes the configured key via _normalize_api_key_for_signing: trim whitespace and remove a trailing _residency_<region> suffix (regex _residency_[a-z0-9]+$) before SHA-256 hashing, matching how the API signs handshake JWTs with the base key. Issuer, subject, expiry, and Bearer handling are unchanged.

Tests add acceptance for a residency-configured key against a token signed with the base key, and rejection when the base key does not match.

Reviewed by Cursor Bugbot for commit 7a9be94. Bugbot is set up for automated code reviews on this repo. Configure here.

@PaulAsjes
PaulAsjes merged commit ca835d6 into next Sep 23, 2026
4 checks passed
@PaulAsjes
PaulAsjes deleted the fix/speech-engine-residency-key-hash-next branch September 23, 2026 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants