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: field-level policy should filter out records when the field used for filtering is not allowed to read #1661

Merged
merged 1 commit into from
Aug 27, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Aug 26, 2024

Fixes #1644

Copy link
Contributor

coderabbitai bot commented Aug 26, 2024

Walkthrough

Walkthrough

The changes involve refactoring the PolicyUtil class to enhance the guard injection mechanism for query conditions, expanding its scope to include all fields, not just relation fields. The testing logic for the createManyAndReturn function has been modified to assert policy compliance instead of successful data retrieval. Additionally, a new regression test has been added to ensure the expected behavior of the User model in relation to field-level read policies.

Changes

File Change Summary
packages/runtime/src/enhancements/policy/policy-utils.ts Refactored methods in PolicyUtil to generalize guard injection for all fields, renamed injectReadGuardForRelationFields to buildReadGuardForFields, and streamlined guard merging logic.
tests/integration/tests/enhancements/with-policy/create-many-and-return.test.ts Updated test logic for createManyAndReturn to check for policy rejection instead of successful returns, confirming that posts are created while ensuring compliance with policy restrictions.
tests/regression/tests/issue-1644.test.ts Introduced a regression test for issue 1644, validating the behavior of the User model with respect to read permissions and ensuring that the expected counts and retrievals comply with defined access rules.

Assessment against linked issues

Objective Addressed Explanation
Field-level read policy should filter out the unmet data returned from findMany if the field is used in the filter (#1644)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (9)
tests/regression/tests/issue-1644.test.ts (1)

2-22: Consider adding more assertions to verify the behavior of the findMany function.

The test case is correctly testing the behavior of the findMany function with field-level read policies. However, it can be improved by adding more assertions to verify the behavior of the findMany function, such as checking the content of the returned records.

Consider adding assertions like:

await expect(db.user.findMany({ where: { email: { contains: 'example.com' } } })).resolves.toEqual([
    { id: 1, email: '[email protected]' }
]);
tests/integration/tests/enhancements/with-policy/create-many-and-return.test.ts (2)

Line range hint 4-4: Consider adding more assertions to verify the behavior of the createManyAndReturn function.

The test case is correctly testing the behavior of the createManyAndReturn function with model-level policies. However, it can be improved by adding more assertions to verify the behavior of the createManyAndReturn function, such as checking the content of the returned records.

Consider adding assertions like:

expect(r).toEqual([
    { id: 1, title: 'hello1', userId: 1, published: true }
]);

Also applies to: 23-23, 37-37, 51-51, 59-59


95-106: Consider adding more assertions to verify the behavior of the createManyAndReturn function.

The test case is correctly testing the behavior of the createManyAndReturn function with field-level policies. However, it can be improved by adding more assertions to verify the behavior of the createManyAndReturn function, such as checking the content of the returned records.

Consider adding assertions like:

await expect(prisma.post.findMany()).resolves.toEqual([
    { id: 1, title: 'post1', published: true },
    { id: 2, title: 'post2', published: false }
]);
packages/runtime/src/enhancements/policy/policy-utils.ts (6)

476-480: Consider adding more comments to explain the logic.

The method is correctly injecting the auth guard as a where clause. However, it can be improved by adding more comments to explain the logic, such as explaining the purpose of buildReadGuardForFields.

Consider adding comments like:

// Inject guard for relation fields and regular fields
mergedGuard = this.buildReadGuardForFields(db, model, args.where, guard);

489-489: Consider adding more comments to explain the logic.

The method is correctly building read guards for fields. However, it can be improved by adding more comments to explain the logic, such as explaining the purpose of allFieldGuards and allFieldOverrideGuards.

Consider adding comments like:

// Collect guards for all fields
const allFieldGuards: object[] = [];
const allFieldOverrideGuards: object[] = [];

534-534: Consider adding more comments to explain the logic.

The method is correctly injecting read guard for to-many fields. However, it can be improved by adding more comments to explain the logic, such as explaining the purpose of buildReadGuardForFields.

Consider adding comments like:

// Inject guard for "some" condition
const mergedGuard = this.buildReadGuardForFields(db, fieldInfo.type, payload.some, guard);

Also applies to: 539-539, 549-549


573-573: Consider adding more comments to explain the logic.

The method is correctly injecting read guard for to-one fields. However, it can be improved by adding more comments to explain the logic, such as explaining the purpose of buildReadGuardForFields.

Consider adding comments like:

// Inject guard for "is" condition
const mergedGuard = this.buildReadGuardForFields(db, fieldInfo.type, payload.is, guard);

Also applies to: 579-579, 584-584


604-609: Consider adding more comments to explain the logic.

The method is correctly injecting auth guard for read operations. However, it can be improved by adding more comments to explain the logic, such as explaining the purpose of buildReadGuardForFields.

Consider adding comments like:

// Inject guard for relation fields and regular fields
const mergedGuard = this.buildReadGuardForFields(db, model, args.where, {});

618-619: Consider adding more comments to explain the logic.

The method is correctly injecting read conditions into nested select, include, and _count. However, it can be improved by adding more comments to explain the logic, such as explaining the purpose of injectAuthGuardAsWhere.

Consider adding comments like:

// No user-provided where clause, use the injected one
args.where = injected.where;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1d81325 and 26a5a77.

Files selected for processing (3)
  • packages/runtime/src/enhancements/policy/policy-utils.ts (6 hunks)
  • tests/integration/tests/enhancements/with-policy/create-many-and-return.test.ts (1 hunks)
  • tests/regression/tests/issue-1644.test.ts (1 hunks)

@ymc9 ymc9 merged commit 19a3b5d into dev Aug 27, 2024
13 checks passed
@ymc9 ymc9 deleted the fix/issue-1664 branch August 27, 2024 00:11
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.

1 participant