Fix deep association search security bypass in condition extraction #1632
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a critical security vulnerability where deep association searches could bypass
ransackable_associations
security checks, allowing unauthorized access to model attributes through association chains.Problem
In Ransack 4.3.0, the condition extraction logic in
extract_values_for_condition
had a security bypass where invalid deep association conditions were processed instead of being rejected. Whencontext.attribute_method?(str)
correctly returnedfalse
for unauthorized deep associations, the code would fall back tostr.split(/_and_|_or_/)
and create conditions anyway.Example of the vulnerability:
Root Cause
In
lib/ransack/nodes/condition.rb
, theextract_values_for_condition
method had this logic:When the security check failed, it would blindly split the string and process it as separate attributes, effectively bypassing the ransackable security checks.
Solution
Modified the condition extraction logic to properly validate security at each level:
Security Impact
Before: Deep association searches could bypass ransackable security:
Comment.ransack(article_person_email_cont: 'x')
would work even whenArticle.ransackable_associations
didn't include'person'
After: All deep association queries properly respect ransackable configuration:
Testing
Added comprehensive test coverage verifying:
_and_
/_or_
combinators still workBreaking Changes
None. The fix maintains backward compatibility while closing the security vulnerability. Valid searches continue to work exactly as before, while invalid searches are now properly blocked.
Example Usage
Fixes the deep association search issue reported where users expected ransackable security to be properly enforced for multi-level association queries.
Original prompt
Fixes #1558
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.