Skip to content

Conversation

@LauraBeatris
Copy link
Member

@LauraBeatris LauraBeatris commented Dec 19, 2024

Description

Resolves ORGS-441

Context

System permissions (e.g., org:sys_domains:manage) are intentionally excluded from session claims to maintain reasonable JWT sizes. While these permissions work in client-side authorization checks (where they're validated against FAPI organization memberships), they cannot be verified server-side.

Problem

Despite documentation updates, developers continue to use server-side authorization checks with system permissions, leading to confusion and support tickets.

Solution

Add type-level validation to catch misuse of system permissions during development. I've opted not to introduce a runtime warning since developers might ignore it.

CleanShot 2024-12-19 at 14 02 48

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@LauraBeatris LauraBeatris self-assigned this Dec 19, 2024
@changeset-bot
Copy link

changeset-bot bot commented Dec 19, 2024

🦋 Changeset detected

Latest commit: afad6c9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@clerk/backend Patch
@clerk/types Patch
@clerk/nextjs Patch
@clerk/astro Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/remix Patch
@clerk/clerk-sdk-node Patch
@clerk/tanstack-start Patch
@clerk/testing Patch
@clerk/clerk-js Patch
@clerk/elements Patch
@clerk/expo-passkeys Patch
@clerk/clerk-expo Patch
@clerk/localizations Patch
@clerk/clerk-react Patch
@clerk/shared Patch
@clerk/themes Patch
@clerk/ui Patch
@clerk/vue Patch
@clerk/chrome-extension Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Dec 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 7, 2025 4:10pm

@LauraBeatris LauraBeatris force-pushed the laura/orgs-441-do-not-allow-passing-system-permissions-to-has-from-the branch from 4b60ed7 to 44bb684 Compare December 19, 2024 17:56
@LauraBeatris LauraBeatris changed the title chore(backend,types): Prevent system permissions usage in server-side chore(backend,types): Prevent system permissions usage in server-side Dec 19, 2024
@LauraBeatris LauraBeatris force-pushed the laura/orgs-441-do-not-allow-passing-system-permissions-to-has-from-the branch from 65a25ba to ca63d27 Compare December 19, 2024 18:03
@LauraBeatris LauraBeatris marked this pull request as ready for review December 19, 2024 18:10
Comment on lines 116 to 122
it('prevents usage of system permissions with auth.has()', () => {
clerkMiddlewareMock(async (auth, _event, _request) => {
// @ts-expect-error - system permissions are not allowed
(await auth()).has({ permission: 'org:sys_foo' });
});
});

Copy link
Member

Choose a reason for hiding this comment

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

do you think we should add a test case for useAuth() where the type says that sys permissions are allowed ?

In order to not break something in the future

Copy link
Member Author

Choose a reason for hiding this comment

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

Applied on 1da1b64

Comment on lines +29 to +31
type DisallowSystemPermissions<P extends string> = P extends `${OrganizationSystemPermissionPrefix}${string}`
? 'System permissions are not included in session claims and cannot be used on the server-side'
: P;
Copy link
Member

Choose a reason for hiding this comment

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

This is really cool!

@LauraBeatris LauraBeatris force-pushed the laura/orgs-441-do-not-allow-passing-system-permissions-to-has-from-the branch from ca63d27 to 2f87d65 Compare January 6, 2025 19:06
@panteliselef
Copy link
Member

This is great initiative. I opened another PR pointing to this one that also addresses usage with protect().

I noticed that type checking does not work currently in our unit tests, so I would avoid merging this PR until that is resolved.

@LauraBeatris LauraBeatris changed the title chore(backend,types): Prevent system permissions usage in server-side chore(backend,nextjs,types): Prevent system permissions usage in server-side Jan 7, 2025
@panteliselef
Copy link
Member

🙃 Avoid merging until this is merged

@jacekradko
Copy link
Member

I am curious what our stance is on these type-only (build-time) checks when it comes to users that don't use Typescript is their projects? Is there a downside to also have a run-time check/warning?

@panteliselef
Copy link
Member

In this case it's mostly improved DX. You don't really care about DX if you are using JS in my opinion. But if you feel strongly a warning makes sense more than an error here. Since the utility will simply return false or redirect you.

@LauraBeatris
Copy link
Member Author

Also worth mentioning that this requirement might change in the future, when we deal with the wider problem of session claims bloating the browser cookie size

Most developers might not even notice the console warning when running on the server-side, although I see the point that they won't get the DX benefits of type-checks when using JavaScript only

@LauraBeatris LauraBeatris merged commit 44cab60 into main Jan 8, 2025
29 checks passed
@LauraBeatris LauraBeatris deleted the laura/orgs-441-do-not-allow-passing-system-permissions-to-has-from-the branch January 8, 2025 18:58
jakobevangelista pushed a commit that referenced this pull request Jan 9, 2025
…er-side (#4816)

Co-authored-by: panteliselef <panteliselef@outlook.com>
wobsoriano pushed a commit that referenced this pull request Feb 8, 2025
…er-side (#4816)

Co-authored-by: panteliselef <panteliselef@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants