Skip to content

Avoid crash in FilterFunctionReturnTypeHelper #4158

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ["8.2"]
include:
- script: |
cd e2e/result-cache-1
Expand Down Expand Up @@ -282,6 +283,10 @@ jobs:
cd e2e/ignore-error-extension
composer install
../../bin/phpstan
- script: |
cd e2e/bug10483
../../bin/phpstan -vvv
php-version: "7.2"

steps:
- name: "Checkout"
Expand All @@ -291,7 +296,7 @@ jobs:
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "8.2"
php-version: ${{ matrix.php-version }}
extensions: mbstring
ini-values: memory_limit=256M

Expand Down Expand Up @@ -408,6 +413,11 @@ jobs:
- name: "Install dependencies"
run: "composer install --no-interaction --no-progress"

- name: "Transform source code"
if: matrix.php-version == '7.2'
shell: bash
run: "vendor/bin/simple-downgrade downgrade -c build/downgrade.php ${{ matrix.php-version }}"

- name: "Install bashunit"
run: "curl -s https://bashunit.typeddevs.com/install.sh | bash -s e2e/ 0.22.0"

Expand Down
5 changes: 5 additions & 0 deletions e2e/bug-10483/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

// constant that's used in the Filter extension that was introduced in a later version of PHP.
// on earlier php version introduce the same constant via a bootstrap file but with a wrong type
if(!defined("FILTER_SANITIZE_ADD_SLASHES"))define("FILTER_SANITIZE_ADD_SLASHES",false);
5 changes: 5 additions & 0 deletions e2e/bug-10483/bug-10483.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

function doFoo(mixed $filter): void {
\PHPStan\Testing\assertType('non-falsy-string|false', filter_var("no", FILTER_VALIDATE_REGEXP));
}
7 changes: 7 additions & 0 deletions e2e/bug-10483/phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 9
paths:
- src

bootstrapFiles:
- bootstrap.php
100 changes: 65 additions & 35 deletions src/Type/Php/FilterFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

public function getOffsetValueType(Type $inputType, Type $offsetType, ?Type $filterType, ?Type $flagsType): Type
{
$inexistentOffsetType = $this->hasFlag($this->getConstant('FILTER_NULL_ON_FAILURE'), $flagsType)
$inexistentOffsetType = $this->hasFlag('FILTER_NULL_ON_FAILURE', $flagsType)
? new ConstantBooleanType(false)
: new NullType();

Expand Down Expand Up @@ -107,6 +107,9 @@

if ($filterType === null) {
$filterValue = $this->getConstant('FILTER_DEFAULT');
if (null === $filterValue) {
return $mixedType;
}
} else {
if (!$filterType instanceof ConstantIntegerType) {
return $mixedType;
Expand All @@ -121,17 +124,17 @@
$hasOptions = $this->hasOptions($flagsType);
$options = $hasOptions->yes() ? $this->getOptions($flagsType, $filterValue) : [];

$defaultType = $options['default'] ?? ($this->hasFlag($this->getConstant('FILTER_NULL_ON_FAILURE'), $flagsType)
$defaultType = $options['default'] ?? ($this->hasFlag('FILTER_NULL_ON_FAILURE', $flagsType)
? new NullType()
: new ConstantBooleanType(false));

$inputIsArray = $inputType->isArray();
$hasRequireArrayFlag = $this->hasFlag($this->getConstant('FILTER_REQUIRE_ARRAY'), $flagsType);
$hasRequireArrayFlag = $this->hasFlag('FILTER_REQUIRE_ARRAY', $flagsType);
if ($inputIsArray->no() && $hasRequireArrayFlag) {
return $defaultType;
}

$hasForceArrayFlag = $this->hasFlag($this->getConstant('FILTER_FORCE_ARRAY'), $flagsType);
$hasForceArrayFlag = $this->hasFlag('FILTER_FORCE_ARRAY', $flagsType);
if ($inputIsArray->yes() && ($hasRequireArrayFlag || $hasForceArrayFlag)) {
$inputArrayKeyType = $inputType->getIterableKeyType();
$inputType = $inputType->getIterableValueType();
Expand Down Expand Up @@ -187,32 +190,46 @@
$stringType = new StringType();
$nonFalsyStringType = TypeCombinator::intersect($stringType, new AccessoryNonFalsyStringType());

$this->filterTypeMap = [
$this->getConstant('FILTER_UNSAFE_RAW') => $stringType,
$this->getConstant('FILTER_SANITIZE_EMAIL') => $stringType,
$this->getConstant('FILTER_SANITIZE_ENCODED') => $stringType,
$this->getConstant('FILTER_SANITIZE_NUMBER_FLOAT') => $stringType,
$this->getConstant('FILTER_SANITIZE_NUMBER_INT') => $stringType,
$this->getConstant('FILTER_SANITIZE_SPECIAL_CHARS') => $stringType,
$this->getConstant('FILTER_SANITIZE_STRING') => $stringType,
$this->getConstant('FILTER_SANITIZE_URL') => $stringType,
$this->getConstant('FILTER_VALIDATE_BOOLEAN') => $booleanType,
$this->getConstant('FILTER_VALIDATE_DOMAIN') => $stringType,
$this->getConstant('FILTER_VALIDATE_EMAIL') => $nonFalsyStringType,
$this->getConstant('FILTER_VALIDATE_FLOAT') => $floatType,
$this->getConstant('FILTER_VALIDATE_INT') => $intType,
$this->getConstant('FILTER_VALIDATE_IP') => $nonFalsyStringType,
$this->getConstant('FILTER_VALIDATE_MAC') => $nonFalsyStringType,
$this->getConstant('FILTER_VALIDATE_REGEXP') => $stringType,
$this->getConstant('FILTER_VALIDATE_URL') => $nonFalsyStringType,
$map = [
'FILTER_UNSAFE_RAW' => $stringType,
'FILTER_SANITIZE_EMAIL' => $stringType,
'FILTER_SANITIZE_ENCODED' => $stringType,
'FILTER_SANITIZE_NUMBER_FLOAT' => $stringType,
'FILTER_SANITIZE_NUMBER_INT' => $stringType,
'FILTER_SANITIZE_SPECIAL_CHARS' => $stringType,
'FILTER_SANITIZE_STRING' => $stringType,
'FILTER_SANITIZE_URL' => $stringType,
'FILTER_VALIDATE_BOOLEAN' => $booleanType,
'FILTER_VALIDATE_DOMAIN' => $stringType,
'FILTER_VALIDATE_EMAIL' => $nonFalsyStringType,
'FILTER_VALIDATE_FLOAT' => $floatType,
'FILTER_VALIDATE_INT' => $intType,
'FILTER_VALIDATE_IP' => $nonFalsyStringType,
'FILTER_VALIDATE_MAC' => $nonFalsyStringType,
'FILTER_VALIDATE_REGEXP' => $stringType,
'FILTER_VALIDATE_URL' => $nonFalsyStringType,
];

$this->filterTypeMap = [];
foreach ($map as $filter => $type) {
$constant = $this->getConstant($filter);
if (null !== $constant) {
$this->filterTypeMap[$constant] = $type;
}
}

if ($this->reflectionProvider->hasConstant(new Node\Name('FILTER_SANITIZE_MAGIC_QUOTES'), null)) {
$this->filterTypeMap[$this->getConstant('FILTER_SANITIZE_MAGIC_QUOTES')] = $stringType;
$sanitizeMagicQuote = $this->getConstant('FILTER_SANITIZE_MAGIC_QUOTES');
if (null !== $sanitizeMagicQuote) {
$this->filterTypeMap[$sanitizeMagicQuote] = $stringType;
}
}

if ($this->reflectionProvider->hasConstant(new Node\Name('FILTER_SANITIZE_ADD_SLASHES'), null)) {
$this->filterTypeMap[$this->getConstant('FILTER_SANITIZE_ADD_SLASHES')] = $stringType;
$sanitizeAddSlashes = $this->getConstant('FILTER_SANITIZE_ADD_SLASHES');
if (null !== $sanitizeAddSlashes) {
$this->filterTypeMap[$sanitizeAddSlashes] = $stringType;
}
}

return $this->filterTypeMap;
Expand All @@ -227,24 +244,32 @@
return $this->filterTypeOptions;
}

$this->filterTypeOptions = [
$this->getConstant('FILTER_VALIDATE_INT') => ['min_range', 'max_range'],
$map = [
'FILTER_VALIDATE_INT' => ['min_range', 'max_range'],
// PHPStan does not yet support FloatRangeType
// $this->getConstant('FILTER_VALIDATE_FLOAT') => ['min_range', 'max_range'],
// 'FILTER_VALIDATE_FLOAT' => ['min_range', 'max_range'],
];

$this->filterTypeOptions = [];
foreach ($map as $filter => $type) {
$constant = $this->getConstant($filter);
if (null !== $constant) {
$this->filterTypeOptions[$constant] = $type;
}
}

return $this->filterTypeOptions;
}

/**
* @param non-empty-string $constantName
*/
private function getConstant(string $constantName): int
private function getConstant(string $constantName): ?int
{
$constant = $this->reflectionProvider->getConstant(new Node\Name($constantName), null);
$valueType = $constant->getValueType();
if (!$valueType instanceof ConstantIntegerType) {
throw new ShouldNotHappenException(sprintf('Constant %s does not have integer type.', $constantName));
return null;
}

return $valueType->getValue();
Expand Down Expand Up @@ -301,8 +326,8 @@

if ($in instanceof ConstantStringType) {
$value = $in->getValue();
$allowOctal = $this->hasFlag($this->getConstant('FILTER_FLAG_ALLOW_OCTAL'), $flagsType);
$allowHex = $this->hasFlag($this->getConstant('FILTER_FLAG_ALLOW_HEX'), $flagsType);
$allowOctal = $this->hasFlag('FILTER_FLAG_ALLOW_OCTAL', $flagsType);
$allowHex = $this->hasFlag('FILTER_FLAG_ALLOW_HEX', $flagsType);

if ($allowOctal && preg_match('/\A0[oO][0-7]+\z/', $value) === 1) {
$octalValue = octdec($value);
Expand Down Expand Up @@ -411,8 +436,13 @@
return $options;
}

private function hasFlag(int $flag, ?Type $flagsType): bool
private function hasFlag(string $flagName, ?Type $flagsType): bool
{
$flag = $this->getConstant($flagName);

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 441 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.
if (null === $flag) {
return false;
}

Check failure on line 445 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.

Check failure on line 445 in src/Type/Php/FilterFunctionReturnTypeHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Parameter #1 $constantName of method PHPStan\Type\Php\FilterFunctionReturnTypeHelper::getConstant() expects non-empty-string, string given.
if ($flagsType === null) {
return false;
}
Expand Down Expand Up @@ -441,9 +471,9 @@
// FILTER_DEFAULT will not sanitize, unless it has FILTER_FLAG_STRIP_LOW,
// FILTER_FLAG_STRIP_HIGH, or FILTER_FLAG_STRIP_BACKTICK
if ($filterValue === $this->getConstant('FILTER_DEFAULT')) {
return $this->hasFlag($this->getConstant('FILTER_FLAG_STRIP_LOW'), $flagsType)
|| $this->hasFlag($this->getConstant('FILTER_FLAG_STRIP_HIGH'), $flagsType)
|| $this->hasFlag($this->getConstant('FILTER_FLAG_STRIP_BACKTICK'), $flagsType);
return $this->hasFlag('FILTER_FLAG_STRIP_LOW', $flagsType)
|| $this->hasFlag('FILTER_FLAG_STRIP_HIGH', $flagsType)
|| $this->hasFlag('FILTER_FLAG_STRIP_BACKTICK', $flagsType);
}

return true;
Expand Down
Loading