Skip to content

fix: reply.jwtSign(payload, cb) with a dynamic secret - #440

Merged
mcollina merged 2 commits into
fastify:mainfrom
TalhaHunter101:fix/jwt-sign-callback-dynamic-secret
Nov 22, 2026
Merged

mcollina merged 2 commits into
fastify:mainfrom
TalhaHunter101:fix/jwt-sign-callback-dynamic-secret

Conversation

@TalhaHunter101

Copy link
Copy Markdown
Contributor

Description

When reply.jwtSign(payload, cb) is invoked with a dynamic secret function (e.g. (req, payload, cb) => cb(...) or async (req, payload) => ...), typeof options === 'function' sets useLocalSigner = false and calls signer(payload, next).

Because the global signer was compiled with an asynchronous key fetcher, signer(payload, next) passes FastJwt's internal callback (payload, fastJwtCb) rather than Fastify's expected (request, payload, cb) signature. This leaves request omitted and results in an unhandled TypeError: cb is not a function.

This fix tracks hasStaticPrivateKey and only sets useLocalSigner = false when hasStaticPrivateKey is true. For dynamic secret providers, useLocalSigner remains true so signing goes through steed.waterfall with secretCallbackSign(reply.request, payload, callback).

Split from #439 as requested by @Tony133 in #438 (comment) (Part 1/3).

Tests

  • Added tests for reply.jwtSign(payload, cb) with callback-based dynamic secret function.
  • Added tests for reply.jwtSign(payload, cb) with async dynamic secret function.
  • Verified all 204 unit tests pass with 100% code coverage.
  • Verified TypeScript tests (tstyche) and eslint pass cleanly.

Checklist

Signed-off-by: Talha Shafeeq <talha.shafeeq.ts@gmail.com>

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for splitting this out of #439.

Note (non-blocking): it would be good to add a test for reply.jwtSign(payload, null, cb) using a dynamic secret.

@gurgunday gurgunday left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Add test verifying reply.jwtSign(payload, null, cb) functions correctly when a dynamic secret callback is configured.

Signed-off-by: Talha Shafeeq <talha.shafeeq.ts@gmail.com>
@TalhaHunter101

Copy link
Copy Markdown
Contributor Author

Thanks @Tony133 and @gurgunday for the review!

I have added the requested test for reply.jwtSign(payload, null, cb) with dynamic secrets in 79762d6. All unit tests, TypeScript types, and ESLint checks pass with 100% coverage.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
mcollina merged commit 50c5278 into fastify:main Nov 22, 2026
19 checks passed
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.

4 participants