Skip to content

fix(jwt-auth): accept tokens at the not-before boundary - #14274

Open
AlinsRan wants to merge 1 commit into
apache:masterfrom
AlinsRan:fix/jwt-auth-nbf-inclusive
Open

AlinsRan wants to merge 1 commit into
apache:masterfrom
AlinsRan:fix/jwt-auth-nbf-inclusive

Conversation

@AlinsRan

Copy link
Copy Markdown
Contributor

Description

jwt-auth rejects an otherwise valid token at its not-before instant. RFC 7519 §4.1.5 requires the current time to be after or equal to nbf, but the request-time check uses a strict comparison:

if nbf < ngx_time() + clock_leeway then

So when nbf equals the current second plus the consumer's lifetime_grace_period, the request gets 401. Issuers commonly set nbf to the issue time, so a client that uses a fresh token within the same second sees an intermittent 401 that goes away on retry.

This is a regression from #12944. Before it, the check was done by lua-resty-jwt's is_not_before validator, which is inclusive (nbf <= ngx.now() + leeway). The rewritten parser also switched to the integer ngx.time(), so the strict comparison rejects the token for that whole second. With <= the result matches the previous behavior for integer nbf values.

The exp check (exp > now - leeway) is left unchanged: RFC 7519 §4.1.4 requires the current time to be before exp.

The new test signs the token in the same rewrite phase that verifies it, so both use nginx's cached time and the equality boundary is hit deterministically. It checks before/at/after the boundary with the default leeway and with lifetime_grace_period: 2. Without the fix, the two "at the boundary" requests return 401.

Which issue(s) this PR fixes:

N/A

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

RFC 7519 section 4.1.5 requires the current time to be after or equal to
the nbf claim. The request-time check used a strict comparison, so a
valid token whose nbf equalled the current second plus the consumer's
lifetime_grace_period was rejected with 401.
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.

1 participant