Validate string collection filters to prevent SQL injection #40435
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.
Harden collection string filters against SQL injection
Summary
This PR addresses a SQL injection risk in Magento’s collection filtering
system related to the
'string'filter type.The collection API allows raw SQL fragments to be passed when using
addFilter(..., 'string'). These fragments were previously applieddirectly to the query without validation, relying entirely on developer
discipline to ensure proper escaping.
While Magento core uses this API safely, the design created a dangerous
footgun for custom and third-party code.
Root Cause
The
'string'filter type accepts arbitrary SQL fragments and passesthem directly to the database layer via
$select->where()with novalidation or enforcement.
This makes it easy for unsafe code paths to introduce SQL injection if
user-controlled input is passed without proper escaping.
Fix
This change introduces validation for
'string'filters before they areapplied to the query:
quoteInto())continue to work unchanged
Files updated:
Magento/Framework/Data/Collection/AbstractDb.phpMagento/Framework/Data/Collection/SqlInjectionValidator.php(new)Security Impact
Compatibility
Resolved issues: