Skip to content

Conversation

adamruzicka
Copy link
Member

@adamruzicka adamruzicka commented Aug 13, 2025

Loose adaptation of theforeman/foreman#10645

What are the changes introduced in this pull request?

Users could use autocompletion to get details about resources they're not permitted to see, this PR ensures even autocomplete runs through the authorized(:view_...) scope.

Considerations taken when implementing this change?

Mostly just replicating what's in the Foreman counterpart.

What are the testing steps for this pull request?

See the test

Summary by Sourcery

Enforce authorization in Katello autocompletion so that only view-permitted resources are suggested and add a test to verify this behavior.

Bug Fixes:

  • Ensure auto_complete_search applies the authorized(:view) scope to filter out unauthorized resources during autocompletion.

Tests:

  • Add controller test to verify that autocomplete suggestions only include resources the user is allowed to view.

Copy link

sourcery-ai bot commented Aug 13, 2025

Reviewer's Guide

This patch ensures autocomplete search results respect user view permissions by adding an authorized(:view) scope to the query and includes a test to verify only permitted resources are suggested.

Sequence diagram for permission-enforced autocomplete search

sequenceDiagram
    actor User
    participant Controller
    participant ResourceClass
    User->>Controller: Request autocomplete suggestions
    Controller->>ResourceClass: completer_scope_options(search)
    Controller->>ResourceClass: authorized(:view)
    Controller->>ResourceClass: where(id: index_relation)
    Controller->>ResourceClass: complete_for(search, options)
    ResourceClass-->>Controller: Permitted autocomplete items
    Controller-->>User: Return filtered suggestions
Loading

Class diagram for updated FilteredAutoCompleteSearch concern

classDiagram
    class FilteredAutoCompleteSearch {
        +auto_complete_search()
    }
    class ResourceClass {
        +completer_scope_options(search)
        +authorized(permission)
        +where(id)
        +complete_for(search, options)
        +find_permission_name(:view)
    }
    FilteredAutoCompleteSearch --> ResourceClass: uses
Loading

File-Level Changes

Change Details Files
Enforce view permissions in auto_complete_search
  • Retrieve view permission name via find_permission_name(:view)
  • Apply authorized(permission) scope before filtering by id
  • Chain authorized scope with existing where and complete_for call
app/controllers/katello/concerns/filtered_auto_complete_search.rb
Add test for autocomplete respecting user permissions
  • Set up user, organization, and products in test
  • Apply view permission filter and invoke auto_complete_search endpoint
  • Assert only permitted suggestions are returned
test/controllers/api/v2/concerns/filtered_auto_complete_search_test.rb

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@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 @adamruzicka - I've reviewed your changes - here's some feedback:

  • Add a safe fallback in case find_permission_name(:view) returns nil so the autocomplete still works for resources without a defined view permission.
  • Consider chaining authorized before where to let the database prune unauthorized rows earlier and improve query performance.
  • Include a test for a user with no view permissions to confirm the endpoint returns an empty array rather than raising an error.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Add a safe fallback in case `find_permission_name(:view)` returns nil so the autocomplete still works for resources without a defined view permission.
- Consider chaining `authorized` before `where` to let the database prune unauthorized rows earlier and improve query performance.
- Include a test for a user with no view permissions to confirm the endpoint returns an empty array rather than raising an error.

## Individual Comments

### Comment 1
<location> `test/controllers/api/v2/concerns/filtered_auto_complete_search_test.rb:11` </location>
<code_context>
+    setup_controller_defaults_api
+  end
+
+  test "only suggests options the user is allowed to see" do
+    user = users(:one)
+    org = user.organizations.first
+    product1 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
+    _product2 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
+    setup_user('view', 'products', "name = \"#{product1.name}\"")
+
+    get :auto_complete_search, session: set_session_user(:one), params: { search: "name =", organization_id: org.id }
+    assert_predicate response, :successful?
+    suggestions = ActiveSupport::JSON.decode(response.body)
+    assert_equal 1, suggestions.length
+    assert_equal suggestions.first['part'], "name = \"#{product1.name}\""
+  end
+end
</code_context>

<issue_to_address>
Consider adding tests for users with no permissions and for users with full permissions.

Please add tests for users with no view permissions to verify no suggestions are returned, and for users with full permissions to confirm all suggestions are shown. This will ensure comprehensive coverage of permission scenarios.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH

  test "only suggests options the user is allowed to see" do
    user = users(:one)
    org = user.organizations.first
    product1 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
    _product2 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
    setup_user('view', 'products', "name = \"#{product1.name}\"")

    get :auto_complete_search, session: set_session_user(:one), params: { search: "name =", organization_id: org.id }
    assert_predicate response, :successful?
    suggestions = ActiveSupport::JSON.decode(response.body)
    assert_equal 1, suggestions.length
    assert_equal suggestions.first['part'], "name = \"#{product1.name}\""
  end
end
=======
  test "only suggests options the user is allowed to see" do
    user = users(:one)
    org = user.organizations.first
    product1 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
    _product2 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
    setup_user('view', 'products', "name = \"#{product1.name}\"")

    get :auto_complete_search, session: set_session_user(:one), params: { search: "name =", organization_id: org.id }
    assert_predicate response, :successful?
    suggestions = ActiveSupport::JSON.decode(response.body)
    assert_equal 1, suggestions.length
    assert_equal suggestions.first['part'], "name = \"#{product1.name}\""
  end

  test "returns no suggestions for user with no view permissions" do
    user = users(:one)
    org = user.organizations.first
    product1 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
    product2 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
    # User has no view permissions
    setup_user(nil, 'products', nil)

    get :auto_complete_search, session: set_session_user(:one), params: { search: "name =", organization_id: org.id }
    assert_predicate response, :successful?
    suggestions = ActiveSupport::JSON.decode(response.body)
    assert_equal 0, suggestions.length
  end

  test "returns all suggestions for user with full permissions" do
    user = users(:one)
    org = user.organizations.first
    product1 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
    product2 = FactoryBot.create(:katello_product, :with_provider, organization_id: org.id)
    # User has full view permissions
    setup_user('view', 'products', nil)

    get :auto_complete_search, session: set_session_user(:one), params: { search: "name =", organization_id: org.id }
    assert_predicate response, :successful?
    suggestions = ActiveSupport::JSON.decode(response.body)
    # Should return both products
    expected_parts = [
      "name = \"#{product1.name}\"",
      "name = \"#{product2.name}\""
    ]
    assert_equal 2, suggestions.length
    assert_equal expected_parts.sort, suggestions.map { |s| s['part'] }.sort
  end
end
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@adamruzicka adamruzicka force-pushed the autocomplete branch 2 times, most recently from c1ed210 to 700aa89 Compare August 13, 2025 14:37
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