Skip to content

fix: registerUser token subject mismatch bug (closes #3354)#3355

Open
patchninja-my wants to merge 2 commits into
SecureBananaLabs:mainfrom
patchninja-my:fix/registerUser-token-subject-mismatch-2845
Open

fix: registerUser token subject mismatch bug (closes #3354)#3355
patchninja-my wants to merge 2 commits into
SecureBananaLabs:mainfrom
patchninja-my:fix/registerUser-token-subject-mismatch-2845

Conversation

@patchninja-my
Copy link
Copy Markdown

Summary

Fixes a bug in registerUser() where the user ID returned by the API could differ from the sub claim in the JWT token, due to two separate Date.now() calls.

Bug

In apps/api/src/services/authService.js, registerUser() called Date.now() twice:

  • Once for the user id in the response
  • Once for the sub claim in the JWT

If the system clock advanced between calls, the API returned one user ID while signing a JWT for a different subject.

Fix

Generate the user ID once, store it in a local variable (id), and use the same value for both the response and the JWT sub claim.

Tests

Added two regression tests:

  1. Basic match test: verifies user.id === decoded.sub under normal conditions
  2. Clock-advance test: mocks Date.now() to return different values on successive calls, then verifies the token subject still matches the returned user id — proving only one Date.now() call is made

All 3 tests (2 new + 1 existing health check) pass.

Closes #3354
References #2845, #743

- Add Zod schema for payment requests (amount, currency, description)
- Validate positive amount with reasonable max
- Restrict currency to known values
- Return 400 with specific error messages on invalid input
…2845)

RegisterUser() called Date.now() twice — once for the returned user id
and once for the JWT sub claim. If the system clock advanced between
calls, the API would return one user ID while signing a JWT for a
different subject, allowing downstream authenticated requests to
identify a different user.

Fix: Generate the user ID once, store it in a local variable, and use
the same value for both the response and the JWT sub claim.

Adds a regression test that mocks Date.now to return different values
on successive calls, verifying the token subject still matches the
returned user id.
github-actions Bot added a commit that referenced this pull request Jun 1, 2026
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.

registerUser access token can reference a different user id

1 participant