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

feat(nestjs-query): effective authorizer on websocket connections #1160

Conversation

asorian0
Copy link
Contributor

@asorian0 asorian0 commented Jun 14, 2021

Relates to #958, which is related to authorizer is not applied to subscriptions. This fact allows any legit user to sniff on any subscription operation, even if the data should be banned to that user.

Solution consists in creating an unique channel name based on data provided in authorizer, if any. If no authorizer is supplied, it behaves as of now.

Usage

Authorizer in your code, in this case we have populated the request with the claims from JWT, so we can bind the authorizer to unique "userId" value in order to make sure that only that user will be able to receive events created/update/delete which "receiverId" value is the same as "userId" value.

@Injectable()
export class MyAuthorizer implements Authorizer<MyDto> { 
  authorize(context: GenericContext, authorizationContext: AuthorizationContext): Promise<Filter<MyDto>> {
    return Promise.resolve({ receiverId: { eq: context.req.user.userId } });
  }

  authorizeRelation(relationName: string, context: GenericContext): Promise<Filter<unknown>> {
    return Promise.resolve({});
  }
}

How it works

Basically it picks the authorizer object of type Filter<MyDto> and creates an unique string based on its contents. The function that handles that is on helpers.ts and it is called getUniqueNameForEvent. This way it binds the event to an unique "channel", allowing to encapsulate for a single user or set of users some events.

@doug-martin thanks for your work!

@asorian0 asorian0 marked this pull request as ready for review June 14, 2021 21:02
doug-martin added a commit that referenced this pull request Jul 24, 2021
* feat: authorizer applied to pub sub

* feat(query-graphql): authorizer for websocket in resolvers

* updating spaces to improve code review

* feat: authorizer for create many

* feat: authorizer applied to update subscriptions

* feat: authorizer applied to delete subscriptions

* fix: adding missing decorator to make authorizer applied to update and delete subscriptions working

* feat: Enable authorizers on subscriptions

* feat: Enable authorizers on subscriptions

* example: Add authorized subscription example

* fix: lock apollo versions until nestjs/graphql updates

Co-authored-by: Armando Soriano <[email protected]>
@doug-martin doug-martin mentioned this pull request Jul 24, 2021
@doug-martin
Copy link
Owner

This will be in v0.28.1 working on getting a few more things merged then I'll release. Thanks for your work on figuring this one out!

doug-martin added a commit that referenced this pull request Jul 26, 2021
* feat: authorizer applied to pub sub

* feat(query-graphql): authorizer for websocket in resolvers

* updating spaces to improve code review

* feat: authorizer for create many

* feat: authorizer applied to update subscriptions

* feat: authorizer applied to delete subscriptions

* fix: adding missing decorator to make authorizer applied to update and delete subscriptions working

* feat: Enable authorizers on subscriptions

* feat: Enable authorizers on subscriptions

* example: Add authorized subscription example

* fix: lock apollo versions until nestjs/graphql updates

Co-authored-by: Armando Soriano <[email protected]>
doug-martin added a commit that referenced this pull request Jul 27, 2021
* feat: authorizer applied to pub sub

* feat(query-graphql): authorizer for websocket in resolvers

* updating spaces to improve code review

* feat: authorizer for create many

* feat: authorizer applied to update subscriptions

* feat: authorizer applied to delete subscriptions

* fix: adding missing decorator to make authorizer applied to update and delete subscriptions working

* feat: Enable authorizers on subscriptions

* feat: Enable authorizers on subscriptions

* example: Add authorized subscription example

* fix: lock apollo versions until nestjs/graphql updates

Co-authored-by: Armando Soriano <[email protected]>
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.

2 participants