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(policy): field-level override rules don't work properly with non-optional to-one relations #1592

Merged
merged 1 commit into from
Jul 15, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Jul 15, 2024

Fixes #1574

Copy link
Contributor

coderabbitai bot commented Jul 15, 2024

Walkthrough

Walkthrough

The recent changes introduce an enhancement to the PolicyUtil class, ensuring that non-nullable to-one filters in authorization policies are hoisted correctly by injecting an AuthGuard. Additionally, a regression test was added to verify access control policies based on issue 1574, particularly focusing on property-level access policies in database schema.

Changes

Files/Paths Change Summary
packages/runtime/src/enhancements/policy/policy-utils.ts Modified the logic in PolicyUtil to inject an AuthGuard before hoisting non-nullable to-one filters.
tests/regression/tests/issue-1574.test.ts Introduced a new regression test for issue 1574, validating access control policies for referenced object properties.

Assessment against linked issues

Objective (Issue #) Addressed Explanation
Ensure property-level policy is respected when accessed via a non-optional relation (#1574)
Validate user permissions to read and access data based on defined access policies (#1574)

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 Configration 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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2b7c42c and dc0875d.

Files selected for processing (2)
  • packages/runtime/src/enhancements/policy/policy-utils.ts (1 hunks)
  • tests/regression/tests/issue-1574.test.ts (1 hunks)
Additional comments not posted (20)
tests/regression/tests/issue-1574.test.ts (18)

1-2: Import statement is correct.

The loadSchema function is correctly imported from @zenstackhq/testtools.


3-3: Test suite description is clear.

The test suite is correctly named to indicate it addresses issue 1574.


4-4: Test case description is clear.

The test case is correctly named as a regression test.


5-52: Schema is correctly defined.

The schema definition matches the described access policies:

  • User has a one-to-many relation with ModelA.
  • ModelA has a model-level access policy and a property-level read override for name.
  • ModelB and ModelC have allow-all policies, with ModelB having an optional relation to ModelA and ModelC having a non-optional relation to ModelA.

54-57: User creation is correct.

Two users are correctly created for the tests.


58-60: Database instances are correctly enhanced for users.

Two database instances are correctly enhanced for the two users.


62-63: ModelA creation is correct.

A ModelA instance owned by user1 is correctly created.


65-67: ModelB and ModelC creation is correct.

A ModelB instance with a reference to ModelA and a ModelC instance with a reference to ModelA are correctly created.


69-71: Test case for user1 reading ModelB and referenced ModelA is correct.

The test correctly verifies that user1 can read ModelB and the entire referenced ModelA.


73-75: Test case for user1 reading ModelB and referenced ModelA's name is correct.

The test correctly verifies that user1 can read ModelB and the name of the referenced ModelA.


77-79: Test case for user2 reading ModelB and referenced ModelA's name is correct.

The test correctly verifies that user2 can read ModelB and the name of the referenced ModelA.


81-83: Test case for user2 reading ModelB and entire referenced ModelA is correct.

The test correctly verifies that user2 cannot read ModelB with the entire referenced ModelA.


85-87: Comment for ModelC tests is clear.

The comment correctly indicates that the following tests are similar to the previous ones but with ModelC.


89-91: Test case for user1 reading ModelC and referenced ModelA is correct.

The test correctly verifies that user1 can read ModelC and the entire referenced ModelA.


93-95: Test case for user1 reading ModelC and referenced ModelA's name is correct.

The test correctly verifies that user1 can read ModelC and the name of the referenced ModelA.


97-99: Test case for user2 reading ModelC and referenced ModelA is correct.

The test correctly verifies that user2 cannot read ModelC with the referenced ModelA.


101-103: Test case for user2 querying ModelC and accessing ModelA's name directly is correct.

The test correctly verifies that user2 can query ModelC, get the refId to ModelA, and access ModelA's name directly.


105-106: Test case for user2 querying ModelC and referenced ModelA's name directly is correct.

The test correctly verifies that user2 should be able to query ModelC along with the name of the referenced ModelA directly, but this is expected to fail.

packages/runtime/src/enhancements/policy/policy-utils.ts (2)

723-729: Ensure proper authorization checks in hoistNonNullableToOneFilters.

The addition of injectAuthGuardAsWhere ensures that authorization checks are applied before hoisting non-nullable to-one filters. This change appears correct and aligns with the PR objectives.


728-728: New method injectAuthGuardAsWhere is correctly implemented.

The method injectAuthGuardAsWhere correctly injects an AuthGuard as a where condition, ensuring proper authorization checks are applied. This change aligns with the PR objectives and resolves the issue described.

@ymc9 ymc9 merged commit 050f760 into dev Jul 15, 2024
13 checks passed
@ymc9 ymc9 deleted the fix/issue-1574 branch July 15, 2024 00:36
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