Skip to content

fix: sign a new token request when a retry replays the nonce - #749

Open
theluckystrike wants to merge 1 commit into
ably:mainfrom
theluckystrike:fix/token-request-replayed-nonce
Open

theluckystrike wants to merge 1 commit into
ably:mainfrom
theluckystrike:fix/token-request-replayed-nonce

Conversation

@theluckystrike

@theluckystrike theluckystrike commented Nov 21, 2026 •

Copy link
Copy Markdown

Fixes #710

I took option 2 from the issue, the retry at the auth layer.

Http.make_request serialises the body once and sends the same bytes to each fallback host. If the first requestToken attempt reaches the server and the response is lost, the retry carries the same nonce and the server rejects it with 40105 Nonce value replayed.

request_token now catches a 40105 from the token request POST and, when the library signed the request with the key, signs a new token request (new nonce, new MAC) and sends it once more. The retry does not apply when:

  • the token request came from auth_callback or auth_url (those are not invoked again)
  • the caller gave the nonce or mac in the token params

http.py and the RSC15 fallback logic do not change.

Tests

  • test/unit/token_request_nonce_test.py uses a mock endpoint that uses each nonce one time only and loses the first response. On main request_token() fails with 40105. With the fix it gets the token after 2 distinct nonces.
  • A second test checks that a caller-supplied nonce is not replaced.
  • uv run pytest test/unit test/uts/rest/unit test/uts/realtime/unit (1650 passed) and uv run ruff check pass.

Summary by CodeRabbit

  • Bug Fixes
    • Token requests signed with a configured key can now recover from a nonce replay error by retrying once with a fresh nonce, when no nonce or MAC was supplied. Requests with a caller-supplied nonce retain their existing behavior.

A token request that a fallback host retries is sent with the same nonce.
When the server used that nonce on an attempt whose response was lost, it
rejects the retry with 40105. request_token now signs a new token request
and sends it once more in that case, when the library made the nonce.

Fixes ably#710
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When a key-signed token request receives error 40105, Auth.request_token now retries once with a newly created request if the caller did not provide nonce or mac. Tests cover a consumed nonce after a timed-out response and a caller-supplied nonce.

Changes

Token request nonce retry

Layer / File(s) Summary
Retry handling and nonce tests
ably/pubsub/http/auth.py, test/unit/token_request_nonce_test.py
Auth.request_token retries once after error 40105 when the request used the configured key and token_params contains neither nonce nor mac. The retry creates a new request and uses the same endpoint. Tests cover successful retry with a new nonce and error propagation when the caller supplies a nonce.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: owenpearson, ttypic

Merge Risk: Minimal · up to 14710

Token requests that hit a replayed nonce after a lost response now retry once with a fresh nonce instead of failing. Caller-supplied nonces and requests from auth callbacks or auth URLs are unchanged. No actionable merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: a retry that signs a new token request with a fresh nonce when the previous nonce was replayed. The raw summary confirms that Auth.request_token now retries once on er…
Linked Issues check Passed Issue #710 asks that a token request retried onto a fallback host not reuse the same nonce and MAC, which causes 40105 Nonce value replayed. Option 2 in the issue is retry at the auth layer with a f…
Out of Scope Changes check Passed The changes are limited to the token-request path in ably/pubsub/http/auth.py and a new unit test file, test/unit/token_request_nonce_test.py. Both relate directly to issue #710. The retry is rest…
Full details: Title check

Explanation

The title names the main change: a retry that signs a new token request with a fresh nonce when the previous nonce was replayed. The raw summary confirms that Auth.request_token now retries once on error 40105 for key-signed requests without caller-supplied nonce or mac. The title is specific and matches the changeset and linked objectives.

Full details: Linked Issues check

Explanation

Issue #710 asks that a token request retried onto a fallback host not reuse the same nonce and MAC, which causes 40105 Nonce value replayed. Option 2 in the issue is retry at the auth layer with a freshly generated token request. The PR summary for ably/pubsub/http/auth.py says Auth.request_token catches AblyException with code 40105 from the token-request POST. When the request was key-signed and token_params has no nonce or mac, it creates a new token request (new nonce and MAC) and sends it once more. This matches the requirement. The added test file test/unit/token_request_nonce_test.py uses a mock endpoint that accepts each nonce once and drops the first response. Per the PR summary, it asserts that a retry with a new nonce obtains a token, and that a caller-supplied nonce is kept. The PR does not change http.py or RSC15 fallback logic, as the issue allows for option 2. Limitation: the unified diff could not be read (changed blobs unavailable in the checkout). This verdict relies on the PR summary and the PR description. The test pass counts are author-reported and were not re-run.

Full details: Out of Scope Changes check

Explanation

The changes are limited to the token-request path in ably/pubsub/http/auth.py and a new unit test file, test/unit/token_request_nonce_test.py. Both relate directly to issue #710. The retry is restricted to key-signed requests, which excludes auth_callback and auth_url, and excludes caller-supplied nonce or mac. Per the PR summary, no public declarations change. No unrelated changes are identified. Limitation: the diff could not be read directly, so this assessment relies on the PR summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

I’m a rabbit with a nonce to renew,
A fresh token request hops into view.
The replay error fades from the trail,
A fixed nonce stays put without fail.
I twitch my nose and celebrate too!

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1e75404d-872b-4e31-9c9d-663635e3c54d
Commits

Reviewing files that changed from the base of the PR and between d4e3e5d and 1471057.

Files selected for processing: 2

  • ably/pubsub/http/auth.py
  • test/unit/token_request_nonce_test.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

A retried token request re-sends the same nonce, so the server rejects it with 40105

1 participant