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

Prevent dangerous query method on #order_by_id #1786

Merged
merged 2 commits into from
Oct 22, 2020

Conversation

sejinkim1904
Copy link
Contributor

@sejinkim1904 sejinkim1904 commented Oct 6, 2020

What does this PR do?

  • Adds Arel.sql() to #order_by_id in /lib/order.rb to prevent deprecation warning for dangerous query method.
  • Adds test for sorting by has_many association

Where should the reviewer start?

/lib/order.rb

How should this be manually tested?

  • Temporarily remove Arel.sql() from #order_by_id
  • Run bundle exec appraisal rails52 rspec spec/features/index_page_spec.rb

What are the relevant tickets?

#1758

Questions:

  • Do Migrations Need to be ran? NO
  • Do Environment Variables need to be set? NO
  • Any other deploy steps? NO

@nickcharlton nickcharlton changed the title fix(Order): Wrap raw SQL with Arel.sql() Prevent dangerous query method on #order_by_id Oct 7, 2020
@nickcharlton
Copy link
Member

Thanks!

Can you think of a way we can test this change? Running the specs currently doesn't throw this warning and ideally I'd like to be able to see the warning, apply the (tiny!) fix and then see it go away again.

@sejinkim1904
Copy link
Contributor Author

I will try to recreate the warning and get back to you

@sejinkim1904
Copy link
Contributor Author

@nickcharlton I spent the last couple days trying to recreate the warning in my forked repo but haven't had any luck. However, we recently migrated the app where I originally ran into this deprecation warning to rails 6 and we're not seeing the warning anymore.

I did some digging into the rails code base and found this but still unsure why I am not seeing the deprecation warning anymore.

@pablobm
Copy link
Collaborator

pablobm commented Oct 15, 2020

Had a look at what's possible. How about adding the following to spec/features/index_page_spec.rb?

  it "sorts by count on a has_many association" do
    create_list(:order, 2, customer: create(:customer, name: "Ade"))
    create_list(:order, 3, customer: create(:customer, name: "Ben"))
    create_list(:order, 1, customer: create(:customer, name: "Cam"))

    visit admin_customers_path

    click_on "Orders"
    expect(page).to have_content(/Cam.*1 order.*Ade.*2 orders.*Ben.*3 orders/)

    click_on "Orders"
    expect(page).to have_content(/Ben.*3 orders.*Ade.*2 orders.*Cam.*1 order/)
  end

Running this with Appraisal gemfile for Rails 5.2 will trigger the warning on master:

$ bundle exec appraisal rails52 rspec spec/features/index_page_spec.rb
(...)
DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "COUNT(orders.id) asc". Non-attribute arguments will be disallowed in Rails 6.0. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql(). (called from order_by_count at /home/pablobm/Documents/projects/administrate/gem/lib/administrate/order.rb:65)
DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "COUNT(orders.id) desc". Non-attribute arguments will be disallowed in Rails 6.0. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql(). (called from order_by_count at /home/pablobm/Documents/projects/administrate/gem/lib/administrate/order.rb:65)

On your branch, the warning will not appear.

@sejinkim1904
Copy link
Contributor Author

@pablobm Thanks for this, I was actually able to recreate the deprecation warning on my branch by adding this test, temporarily removing Arel.sql, and running bundle exec appraisal rails52 rspec spec/features/index_page_spec.rb. I guess I should have checked to see if any of the feature tests were checking sorting on the dashboards first.

@sejinkim1904
Copy link
Contributor Author

Updated PR message with new changes

@sejinkim1904 sejinkim1904 force-pushed the fix-deprecation-warning branch from c4dba8a to ff9893c Compare October 15, 2020 15:50
Copy link
Collaborator

@pablobm pablobm left a comment

Choose a reason for hiding this comment

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

This looks good to me 👍

@pablobm pablobm merged commit a5d695d into thoughtbot:master Oct 22, 2020
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