Skip to content
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

fix(clerk): Remove privateMetadata property from getCurrentUser #7668

Merged
merged 2 commits into from
Feb 22, 2023

Conversation

anagstef
Copy link
Contributor

The privateMetadata property should not be accessible on the client side. This commit removes this property from the getCurrentUser function returned user object.

The privateMetadata property should not be accessible on the client side. This commit removes this property from the getCurrentUser function returned user object.
@BurnedChris
Copy link
Contributor

This won't fix the actual problem. Just stop the API using PrivateMetadata.

I propose overwriting the default redwood currentUser with the ability to remove parts that you may want to be available in the backend context but not pass back to the front end, such as privateMetadata

currentUser: (_args: any, context: GlobalContext) => {

@BurnedChris
Copy link
Contributor

BurnedChris commented Feb 21, 2023

Clerk need to override the default currentUser function to remove privateMetadata, It cant be done in the auth.ts, as the privateMetadata is needed in the the context for other resolvers.

Example on how to remove privateMetadata being sent to the frontend useAuth Hooks

export const resolvers: Resolvers = {
  BigInt: BigIntResolver,
  Date: DateResolver,
  Time: TimeResolver,
  DateTime: DateTimeResolver,
  JSON: JSONResolver,
  JSONObject: JSONObjectResolver,
  Query: {
    redwood: () => ({
      version: redwoodVersion,
      prismaVersion: prismaVersion,
      currentUser: (_args: any, context: GlobalContext) => {
      const { ...userWithoutPrivateMetadata, privateMetadata } = context?.currentUser
        return userWithoutPrivateMetadata
      },
    }),
  },
}

@dthyresson
Copy link
Contributor

It seems to be that the currentUser in global context — and the same info passed back to client — should be “safe” data to share.

If private data is needed elsewhere in the api side (or web side) then I’d argue that should be an explicit request to ensure that it’s ok to show this info for the given user.

I could see this being done:

a) via a Clerk backed api call
b) via a Yoga plugin like useRedwoodAuthContext that populates context.fullCurrentUser that is given a function that sets it with the full decoded value
c) Instead of a second plugin, useRedwoodAuthContext takes an optional function that populates that additional global context “complete or full currentUser”

@dthyresson
Copy link
Contributor

Another approach would be to do the “sanitizing” at the decoder level here:

const user = await users.getUser(jwtPayload.sub)

if there is a Clerk api method that gets only a subset of “safe” user attributes and use that … or deconstruct there instead of in the getCurrentUser

@Tobbe
Copy link
Member

Tobbe commented Feb 22, 2023

After discussing this in the team we decided that this is the quickest solution that's the best option for most people.
Unfortunately it breaks @BurnedChris's use case. For advanced uses like his, you'd have to (for now) go back to the way it was before this PR
We'll continue working on a better solution, but wanted to get this fix out as soon as possible

@Tobbe Tobbe added the release:fix This PR is a fix label Feb 22, 2023
@jtoar jtoar merged commit 3303ee7 into redwoodjs:main Feb 22, 2023
@redwoodjs-bot redwoodjs-bot bot added this to the next-release milestone Feb 22, 2023
@anagstef anagstef deleted the clerk-auth-api-remove-privateMetadata branch February 22, 2023 09:04
jtoar pushed a commit that referenced this pull request Feb 22, 2023
The privateMetadata property should not be accessible on the client side. This commit removes this property from the getCurrentUser function returned user object.

Co-authored-by: Stefanos Anagnostou <stefanos@clerk.dev>
@jtoar jtoar modified the milestones: next-release-patch, v4.2.0 Feb 22, 2023
dac09 added a commit to dac09/redwood that referenced this pull request Feb 28, 2023
* 'main' of github.com:redwoodjs/redwood: (21 commits)
  chore(deps): update dependency @types/uuid to v9.0.1 (redwoodjs#7680)
  chore(deps): update dependency @replayio/playwright to v0.3.23 (redwoodjs#7677)
  chore(deps): update dependency @npmcli/arborist to v6.2.3 (redwoodjs#7675)
  chore(deps): update dependency @envelop/types to v3.0.2 (redwoodjs#7674)
  chore: add codemod for clerk fix in v4.2.0 (redwoodjs#7676)
  chore(deps): update dependency @clerk/types to v3.28.1 (redwoodjs#7652)
  chore(deps): update dependency @envelop/testing to v5.0.6 (redwoodjs#7673)
  Update directives.md (redwoodjs#7670)
  fix(deps): update dependency vscode-languageserver-types to v3.17.3 (redwoodjs#7636)
  Fix `yarn rw exec` to set nonzero exit code on error (redwoodjs#7660)
  fix(deps): update dependency core-js to v3.28.0 (redwoodjs#7666)
  fix(deps): update dependency @clerk/clerk-sdk-node to v4.7.4 (redwoodjs#7663)
  fix(deps): update dependency vite to v4.1.3 (redwoodjs#7664)
  fix(deps): update dependency @fastify/url-data to v5.3.1 (redwoodjs#7665)
  fix(deps): update dependency yargs to v17.7.1 (redwoodjs#7667)
  fix(clerk): Remove privateMetadata property from getCurrentUser (redwoodjs#7668)
  chore(deps): update dependency esbuild to v0.17.10 (redwoodjs#7662)
  chore(deps): bump setup of Chakra UI to V2 (redwoodjs#7649)
  Forms: Export EmptyAsValue (redwoodjs#7656)
  Update useRequireAuth docs to v4 auth (redwoodjs#7646)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix This PR is a fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants