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

Betterment/UnscopedFind Improvement: Flag unscoped #52

Open
6f6d6172 opened this issue Aug 13, 2024 · 0 comments
Open

Betterment/UnscopedFind Improvement: Flag unscoped #52

6f6d6172 opened this issue Aug 13, 2024 · 0 comments
Labels
good first issue Good for newcomers

Comments

@6f6d6172
Copy link
Contributor

UnscopedFind has made the assumption that if you're doing a find off of an object (e.g. current_user), your query will be scoped to whatever belongs to that object. However, it turns out there's an unscoped method which when called at any point will remove any scopes, effectively letting you find any object regardless of who owns it.

For example, current_user.cats.find(params[:id]) will not raise an offense because the find call on cats is scoped to the current user. This same logic is erroneously extended to current_user.cats.unscoped.find(params[:id]), which does the same query but without the where clause that scopes it to the current user.

This cop should be modified to flag any uses of unscoped.

I was able to put together this diff that is the bare minimum needed to start detecting unscoped calls, but I haven't taken the time to sit down and list out test cases or write any specs.

diff --git a/lib/rubocop/cop/betterment/unscoped_find.rb b/lib/rubocop/cop/betterment/unscoped_find.rb
index fe3aa8e..bc6a71d 100644
--- a/lib/rubocop/cop/betterment/unscoped_find.rb
+++ b/lib/rubocop/cop/betterment/unscoped_find.rb
@@ -31,6 +31,10 @@ module RuboCop
           (send (const ... _) {#{FINDS.map(&:inspect).join(' ')}} ...)
         PATTERN
 
+        def_node_matcher :unscoped?, <<-PATTERN
+          (send (send ... :unscoped) {#{FINDS.map(&:inspect).join(' ')}} ...)
+        PATTERN
+
         def_node_search :find_graphql_namespace_nodes, <<~PATTERN, name: GRAPHQL_PATTERN
           (const _ %name)
         PATTERN
@@ -49,6 +53,7 @@ module RuboCop
           _, _, *arg_nodes = *node # rubocop:disable InternalAffairs/NodeDestructuring
           return unless
             (
+              unscoped?(node) ||
               find?(node) ||
               custom_scope_find?(node) ||
               static_method_name(node.method_name)

tl;dr todo

  • flag all unscoped
  • list out test cases
  • write tests
@6f6d6172 6f6d6172 added the good first issue Good for newcomers label Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant