Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 469
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
Changes from all commits
c83fe319dd9f76527f0caccf4741File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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
| ||
| if (permissionBits[permIndex] === 1) { | ||
| orgPermissions.push(`org:${feature}:${permissions[permIndex]}`); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.