-
Notifications
You must be signed in to change notification settings - Fork 465
fix(shared): decode JWT v2 permission masks exactly #9381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c83fe31
9dd9f76
527f0ca
ccf4741
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/shared': patch | ||
| --- | ||
|
|
||
| Ensure organization permission checks remain accurate when JWT v2 permission masks exceed JavaScript's safe integer range. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,26 +6,52 @@ import type { | |
| SharedSignedInAuthObjectProperties, | ||
| } from './types'; | ||
|
|
||
| const decimalToBinaryBits = (decimal: string, minimumLength: number): number[] | undefined => { | ||
| if (!/^\d+$/.test(decimal)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| let remaining = decimal.replace(/^0+/, '') || '0'; | ||
| const bits: number[] = []; | ||
|
|
||
| while (remaining !== '0') { | ||
| let quotient = ''; | ||
| let remainder = 0; | ||
|
|
||
| for (let i = 0; i < remaining.length; i++) { | ||
| const value = remainder * 10 + remaining.charCodeAt(i) - 48; | ||
| const quotientDigit = Math.floor(value / 2); | ||
|
|
||
| if (quotient || quotientDigit !== 0) { | ||
| quotient += quotientDigit; | ||
| } | ||
| remainder = value % 2; | ||
| } | ||
|
|
||
| bits.push(remainder); | ||
| remaining = quotient || '0'; | ||
| } | ||
|
|
||
| if (bits.length === 0) { | ||
| bits.push(0); | ||
| } | ||
| while (bits.length < minimumLength) { | ||
| bits.push(0); | ||
| } | ||
|
|
||
| return bits; | ||
| }; | ||
|
|
||
| export const parsePermissions = ({ per, fpm }: { per?: string; fpm?: string }) => { | ||
| if (!per || !fpm) { | ||
| return { permissions: [], featurePermissionMap: [] }; | ||
| } | ||
|
|
||
| const permissions = per.split(',').map(p => p.trim()); | ||
|
|
||
| // TODO: make this more efficient | ||
| const featurePermissionMap = fpm | ||
| .split(',') | ||
| .map(permission => Number.parseInt(permission.trim(), 10)) | ||
| .map((permission: number) => | ||
| permission | ||
| .toString(2) | ||
| .padStart(permissions.length, '0') | ||
| .split('') | ||
| .map(bit => Number.parseInt(bit, 10)) | ||
| .reverse(), | ||
| ) | ||
| .filter(Boolean); | ||
| .map(permission => decimalToBinaryBits(permission.trim(), permissions.length) ?? []); | ||
|
|
||
| return { permissions, featurePermissionMap }; | ||
| }; | ||
|
|
@@ -62,7 +88,7 @@ function buildOrgPermissions({ | |
| continue; | ||
| } | ||
|
|
||
| for (let permIndex = 0; permIndex < permissionBits.length; permIndex++) { | ||
| for (let permIndex = 0; permIndex < permissionBits.length && permIndex < permissions.length; permIndex++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM]
This predates the diff, but it is in the function this PR rewrites and the PR's stated goal is decoding these masks exactly — the new — Comment generated with Claude with @dominic-clerk's supervision
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed — this is a valid pre-existing encoder issue. I reproduced the false grant through the JWT v2 auth-object conversion and There isn't enough information in the current claims for ClerkJS to repair the mapping safely: once zero masks are omitted, the decoder cannot tell whether the next compacted mask belongs to the next The correct fix is in the backend: emit one — Comment generated with Codex with @jeremy-clerk's supervision
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I'll follow-up on this with a backend PR
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified that the backend fix is merged. This PR is now updated to match the new positional contract: fixtures include — Comment generated with Codex with @jeremy-clerk's supervision |
||
| if (permissionBits[permIndex] === 1) { | ||
| orgPermissions.push(`org:${feature}:${permissions[permIndex]}`); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.