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

2 changes: 1 addition & 1 deletion src/Type/Php/FilterVarDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

return true;
}

Expand Down