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 _perm_cache processing #498

Merged
merged 10 commits into from
Mar 16, 2024

Conversation

vecchp
Copy link
Contributor

@vecchp vecchp commented Mar 8, 2024

Description

It appears this line in utils/query.py is not checking cached permissions properly.

In my codebase the _perm_cache comes up as a set of strings which then causes an exception

user_perms: Set[str] = {p.codename for p in perm_cache}
                          ^^^^^^^^^^
AttributeError: 'str' object has no attribute 'codename'

Looking at the django codebase I think this line confirms that the perm_cache is indeed a set of strings. This seems to be the same behavior going back to django 0.90.

Also a previous PR #428, seemed to have run into the same issue but for some reason it was closed.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @vecchp - I've reviewed your changes and they look great!

General suggestions:

  • Ensure that the changes to _perm_cache processing do not inadvertently affect the expected behavior of permission checks.
  • Consider the impact of the changes on performance, especially in scenarios with a large number of permissions.
  • Verify the compatibility of the direct iteration and comparison logic with the structure and content of perm_cache.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Docstrings: all looks good

Thanks for using Sourcery. We offer it for free for open source projects and would be very grateful if you could help us grow. If you like it, would you consider sharing Sourcery on your favourite social media? ✨

Share Sourcery

Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

f = any if any_perm else all
user_perms: Set[str] = {p.codename for p in perm_cache}
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (performance): The removal of the explicit set creation and type hint for user_perms simplifies the code. However, ensure that perm_cache directly supports efficient membership testing as a set does. If perm_cache is a list or another data structure with O(n) lookup time, this change could negatively impact performance, especially with a large number of permissions.

Suggested change
user_perms: Set[str] = {p.codename for p in perm_cache}
if isinstance(perm_cache, set) or isinstance(perm_cache, frozenset):
if f(p in perm_cache for p in perms_list):
pass # Your existing logic here
else:
perm_cache_set = set(perm_cache) # Convert to set for efficient lookup
if f(p in perm_cache_set for p in perms_list):
pass # Your existing logic here

f = any if any_perm else all
user_perms: Set[str] = {p.codename for p in perm_cache}
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): This change assumes that perm_cache directly contains permission codenames or objects that can be compared to perms_list. It's crucial to verify that the structure and content of perm_cache are compatible with this direct iteration and comparison, to avoid introducing bugs related to permission checks.

Suggested change
user_perms: Set[str] = {p.codename for p in perm_cache}
if f(perm_cache.get(p, False) for p in perms_list):

@codecov-commenter
Copy link

codecov-commenter commented Mar 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.97%. Comparing base (67433e6) to head (e4b067d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #498      +/-   ##
==========================================
- Coverage   87.98%   87.97%   -0.02%     
==========================================
  Files          37       37              
  Lines        3180     3184       +4     
==========================================
+ Hits         2798     2801       +3     
- Misses        382      383       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@bellini666 bellini666 left a comment

Choose a reason for hiding this comment

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

Nice, thank you for this! :)

@bellini666 bellini666 merged commit 23c4126 into strawberry-graphql:main Mar 16, 2024
50 checks passed
@vecchp vecchp deleted the pv-fix-cache-perms branch March 16, 2024 16:30
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.

3 participants