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.
For readability maybe this magic constant should be extracted and documented at the definition level
There was a problem hiding this comment.
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.
Just curious, do we know if this is intentional, or a coincidence?
There was a problem hiding this comment.
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.
Makes sense, thanks for checking that
|
|
||
| private function canStringBeSanitized(Type $filterType, int $filterValue, ?Node\Arg $flagsArg, Scope $scope): bool | ||
| { | ||
| if (!$filterType instanceof StringType) { |
There was a problem hiding this comment.
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.
Adding a test with e.g. a numeric-string would cover the last change
There was a problem hiding this comment.
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.
| 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.
This doesn't seem right. For example what do you want to happen for these types?
mixedstring|nullstring'1'int
There was a problem hiding this comment.
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.
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.
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.
Yeah, I like this more, just make sure this case is also tested filter_var('foo', FILTER_VALIDATE_INT);
|
Thank you! |
This expands on PR #642. @BackEndTea pointed out that
FILTER_SANITIZE_STRINGcould 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.