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

[BUG][RoR 7.1] ActiveRecord::normalizes can break cont predicate #1488

Open
AmShaegar13 opened this issue Apr 11, 2024 · 3 comments
Open

[BUG][RoR 7.1] ActiveRecord::normalizes can break cont predicate #1488

AmShaegar13 opened this issue Apr 11, 2024 · 3 comments

Comments

@AmShaegar13
Copy link
Contributor

AmShaegar13 commented Apr 11, 2024

When normalizing attributes using the new ActiveRecord::normalizes method, % could be tampered with.

class User < ActiveRecord::Base
  normalizes :name, with: ->(name) { name.gsub(/[^a-z0-9]/, '_') }
end

User.ransack({ name_cont: 'foo' }).result.to_sql
# => "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"name\" LIKE '_foo_'"
#                                                                      ^   ^
#                                                                      %foo%

Failing test

In my opinion, this should not be possible as normalizes should only apply to the attribute itself and the search term but not the wildcards.

@AmShaegar13 AmShaegar13 changed the title [BUG] ActiveRecord::normalizes can break cont predicate [BUG][RoR 7.1] ActiveRecord::normalizes can break cont predicate May 15, 2024
@armchairdj
Copy link

Still an issue in Rails 8.0.

@armchairdj
Copy link

armchairdj commented Feb 5, 2025

There's a bug filed about this in Rails, but it's closed as "Not Planned":

rails/rails#53532

Seems like maybe Ransack is using private Arel APIs and the fix would be to use Arel::Nodes::Quoted.

@dmitry
Copy link

dmitry commented Feb 6, 2025

It might be a good solution to write a spec for Ransack, so if it breaks in newer Rails versions, it can be fixed later.

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

No branches or pull requests

3 participants