feat: add role-based field visibility enforcement at the serialization layer#916
Open
Xaxxoo wants to merge 1 commit into
Open
feat: add role-based field visibility enforcement at the serialization layer#916Xaxxoo wants to merge 1 commit into
Xaxxoo wants to merge 1 commit into
Conversation
…yer (rinafcode#877) Replace per-endpoint manual masking with a declarative, interceptor-driven approach so any newly added field is automatically protected the moment it carries a @VisibleTo annotation. Changes: - Add @VisibleTo(...roles) property decorator (src/common/decorators/visible-to.decorator.ts) that stores role lists in reflect-metadata on the entity constructor - Add RoleVisibilityInterceptor (src/common/interceptors/role-visibility.interceptor.ts) registered globally as APP_INTERCEPTOR; strips @VisibleTo fields that the viewer's role is not permitted to see before the response reaches the client - Supports single objects, arrays, and paginated {data:[]} / {items:[]} shapes - Annotate User entity fields: @VisibleTo(UserRole.ADMIN) refreshToken @VisibleTo(UserRole.ADMIN) passwordHistory @VisibleTo(UserRole.ADMIN) providerAccessToken @VisibleTo(UserRole.ADMIN) providerRefreshToken - 12 unit tests covering ADMIN/STUDENT/MODERATOR roles, arrays, paginated responses, primitives, null, and plain objects without annotations
|
@Xaxxoo Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
Great job so far There’s just one blocker — the workflow is failing. Could you take a look and fix it so all checks pass? Happy to review again once that’s done. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #877
Summary
Replaces per-endpoint manual masking with a declarative, interceptor-driven approach so any new endpoint that returns a
Userobject is automatically protected without extra code.New files:
src/common/decorators/visible-to.decorator.ts@VisibleTo(...roles)property decorator; stores role lists inreflect-metadataon the class constructorsrc/common/interceptors/role-visibility.interceptor.tsRoleVisibilityInterceptorregistered as a globalAPP_INTERCEPTOR; strips restricted fields before the response leaves the processsrc/common/interceptors/role-visibility.interceptor.spec.tsModified files:
src/users/entities/user.entity.ts— adds@VisibleTo(UserRole.ADMIN)torefreshToken,passwordHistory,providerAccessToken, andproviderRefreshTokensrc/app.module.ts— registersRoleVisibilityInterceptoras a global interceptorHow it works:
@VisibleTo(UserRole.ADMIN)on an entity field writes the allowed roles intoreflect-metadatakeyed by the class constructor.RoleVisibilityInterceptorresolves the viewer's role fromrequest.user, walks the value recursively (supports arrays and{data:[]}/{items:[]}paginated shapes), and deletes any field whose@VisibleTolist does not include the viewer's role.@VisibleToannotation are always returned as-is — zero risk of breaking existing endpoints.Test plan
@VisibleTo(ADMIN)onessecretToken(@VisibleTo(ADMIN)) is strippedinternalScore(@VisibleTo(ADMIN, MODERATOR)) is stripped@VisibleTo(ADMIN, MODERATOR)field but not@VisibleTo(ADMIN)field{ data: [] }shape: restriction applied inside array{ items: [] }shape: restriction applied inside arraynpm test)