-
Notifications
You must be signed in to change notification settings - Fork 428
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
filter_var() should return non empty string only when it will not be sanitized #650
filter_var() should return non empty string only when it will not be sanitized #650
Conversation
|
||
// All validation filters match 0x100 | ||
// If it is a validation filter, the string will not be changed | ||
if (($filterValue & 0x100) !== 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For readability maybe this magic constant should be extracted and documented at the definition level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done; let me know if that's sufficient.
/** | ||
* All validation filters match 0x100. | ||
*/ | ||
private const VALIDATION_FILTER_BITMASK = 0x100; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, do we know if this is intentional, or a coincidence?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks intentional to me. I went through the constants and all validation filters start with 0x100, and all sanitization filters start with 0x200. FILTER_CALLBACK is 0x400. I figured taking advantage of the pattern was better than iterating through every possible filter that should match.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks for checking that
@@ -222,4 +229,26 @@ private function getFlagsValue(Type $exprType): Type | |||
return $exprType->getOffsetValueType($this->flagsString); | |||
} | |||
|
|||
private function canStringBeSanitized(Type $filterType, int $filterValue, ?Node\Arg $flagsArg, Scope $scope): bool | |||
{ | |||
if (!$filterType instanceof StringType) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instanceof *Type
is rarely correct, see: https://phpstan.org/developing-extensions/type-system
For example this would fail for numeric-string
or non-empty-string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a test with e.g. a numeric-string would cover the last change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can, but as I was saying below, that wouldn't really accomplish anything. $filterType
is the type returned by the filter, per getFilterTypeMap()
, not the type of the input itself. I can still add a test if you think it's necessary.
@@ -231,7 +231,7 @@ private function getFlagsValue(Type $exprType): Type | |||
|
|||
private function canStringBeSanitized(Type $filterType, int $filterValue, ?Node\Arg $flagsArg, Scope $scope): bool | |||
{ | |||
if (!$filterType instanceof StringType) { | |||
if ($filterType->isSuperTypeOf(new StringType())->no()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem right. For example what do you want to happen for these types?
mixed
string|null
string
'1'
int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're missing where $filterType is coming from. It's the type returned from getFilterTypeMap()
, which only ever returns BooleanType
, FloatType
, IntType
, or StringType
. So all I'm doing here is ensuring that we're only operating on filters that return strings. It's not verifying the type of the input itself, which is in $inputType
. See where I'm calling it in line 140.
It's also why I felt OK using instanceof StringType
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused. You're caling a method called canStringBeSanitized
and if the input isn't a string, it returns true
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps I should move that check to the condition on line 139, or else rename the method. if I return false, then it'll return non-empty-string if something like this is done:
$str = 'foo';
filter_var($str, FILTER_VALIDATE_INT);
Since a string is being passed, but FILTER_VALIDATE_INT should return an int, not a string. So I only want to operate on filters that return strings. Let me move the check and see if that makes more sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I like this more, just make sure this case is also tested filter_var('foo', FILTER_VALIDATE_INT);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, done.
Thank you! |
This expands on PR #642. @BackEndTea pointed out that
FILTER_SANITIZE_STRING
could strip out all characters in a non-empty-string and so make it empty. So I added more edge cases and it should now only return non-empty-string when there is no chance of it being sanitized.I also added support for
FILTER_VALIDATE_DOMAIN
, which was not included in the filter map and so always returnedmixed
.