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

filter_var() should return non empty string only when it will not be sanitized #650

Conversation

devbanana
Copy link
Contributor

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 returned mixed.

@devbanana devbanana changed the title Filter var should return non empty string only when it will not be sanitized filter_var() should return non empty string only when it will not be sanitized Aug 28, 2021

// All validation filters match 0x100
// If it is a validation filter, the string will not be changed
if (($filterValue & 0x100) !== 0) {
Copy link
Contributor

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

Copy link
Contributor Author

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;
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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) {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.

Copy link
Contributor

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

Copy link
Contributor Author

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()) {
Copy link
Member

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

Copy link
Contributor Author

@devbanana devbanana Aug 29, 2021

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.

Copy link
Member

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?

Copy link
Contributor Author

@devbanana devbanana Aug 29, 2021

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.

Copy link
Member

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);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done.

@ondrejmirtes ondrejmirtes merged commit 9cf20f3 into phpstan:master Aug 29, 2021
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants