Skip to content

[v3-4-test] Fix JWT token not refreshed when token expires mid-request (#68499) - #74865

Merged
ashb merged 1 commit into
v3-4-testfrom
backport-68499-v3-4-test
Oct 16, 2026
Merged

ashb merged 1 commit into
v3-4-testfrom
backport-68499-v3-4-test

Conversation

@ashb

@ashb ashb commented Oct 16, 2026

Copy link
Copy Markdown
Member

closes: #67939

Long-running tasks (>10 min with default 600s JWT lifetime) could die
with repeated 403 "Signature has expired" errors despite the refresh
mechanism being in place. The failure was a TOCTOU race:

Before:
JWTReissueMiddleware called avalidated_claims a second time after the
handler completed. If the token expired in the milliseconds between
JWTBearer's validation and the middleware's re-validation, jwt raised
ExpiredSignatureError, the except block swallowed it, and no
Refreshed-API-Token header was set. The client's next heartbeat
(30 s later) arrived with a now-fully-expired token, got 403, and
after MAX_FAILED_HEARTBEATS the supervisor killed the task.

After:
JWTBearer already validates and caches the token on request.scope.
JWTReissueMiddleware reads claims from request.scope instead of
calling avalidated_claims again — eliminating the race entirely. Even
if the token crosses its expiry boundary during request processing,
valid_left will be <= refresh_when_less_than and a fresh token is
still issued.

Co-authored-by: Ash Berlin-Taylor [email protected]
(cherry picked from commit cf839ac)


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

closes: #67939

Long-running tasks (>10 min with default 600s JWT lifetime) could die
with repeated 403 "Signature has expired" errors despite the refresh
mechanism being in place. The failure was a TOCTOU race:

Before:
  JWTReissueMiddleware called avalidated_claims a second time after the
  handler completed. If the token expired in the milliseconds between
  JWTBearer's validation and the middleware's re-validation, jwt raised
  ExpiredSignatureError, the except block swallowed it, and no
  Refreshed-API-Token header was set. The client's next heartbeat
  (30 s later) arrived with a now-fully-expired token, got 403, and
  after MAX_FAILED_HEARTBEATS the supervisor killed the task.

After:
  JWTBearer already validates and caches the token on request.scope.
  JWTReissueMiddleware reads claims from request.scope instead of
  calling avalidated_claims again — eliminating the race entirely. Even
  if the token crosses its expiry boundary during request processing,
  valid_left will be <= refresh_when_less_than and a fresh token is
  still issued.

Co-authored-by: Ash Berlin-Taylor <[email protected]>
(cherry picked from commit cf839ac)
@boring-cyborg boring-cyborg Bot added area:API Airflow's REST/HTTP API area:task-sdk labels Oct 16, 2026
@ashb
ashb merged commit 37ba015 into v3-4-test Oct 16, 2026
73 checks passed
@ashb
ashb deleted the backport-68499-v3-4-test branch October 16, 2026 20:34
@github-actions github-actions Bot added this to the Airflow 3.4.1 milestone Oct 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi maintainer, this PR was merged without a milestone set.
We've automatically set the milestone to Airflow 3.4.1 based on: bug fix merged to version branch
If this milestone is not correct, please update it to the appropriate milestone.

This comment was generated by Milestone Tag Assistant.

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

Labels

area:API Airflow's REST/HTTP API area:task-sdk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants