From 7ffba7f611ab5bc3199701aed77e07f75bc2ab20 Mon Sep 17 00:00:00 2001 From: Mark McEver Date: Tue, 8 Aug 2023 15:07:15 -0500 Subject: [PATCH 1/3] Respect stubs in all cases --- src/Psalm/Internal/Codebase/Functions.php | 15 +------ tests/StubTest.php | 44 +++++++++++++++++++ tests/fixtures/stubs/custom_taint_source.php | 3 ++ .../stubs/custom_taint_source.phpstub | 6 +++ .../stubs/define_custom_require_path.php | 3 ++ 5 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 tests/fixtures/stubs/custom_taint_source.php create mode 100644 tests/fixtures/stubs/custom_taint_source.phpstub create mode 100644 tests/fixtures/stubs/define_custom_require_path.php diff --git a/src/Psalm/Internal/Codebase/Functions.php b/src/Psalm/Internal/Codebase/Functions.php index 50e3f60fba5..bd18596c2a7 100644 --- a/src/Psalm/Internal/Codebase/Functions.php +++ b/src/Psalm/Internal/Codebase/Functions.php @@ -79,9 +79,8 @@ public function getStorage( $function_id = substr($function_id, 1); } - $from_stubs = false; if (isset(self::$stubbed_functions[$function_id])) { - $from_stubs = self::$stubbed_functions[$function_id]; + return self::$stubbed_functions[$function_id]; } $file_storage = null; @@ -113,10 +112,6 @@ public function getStorage( return $this->reflection->getFunctionStorage($function_id); } - if ($from_stubs) { - return $from_stubs; - } - throw new UnexpectedValueException( 'Expecting non-empty $root_file_path and $checked_file_path', ); @@ -135,10 +130,6 @@ public function getStorage( } } - if ($from_stubs) { - return $from_stubs; - } - throw new UnexpectedValueException( 'Expecting ' . $function_id . ' to have storage in ' . $checked_file_path, ); @@ -149,10 +140,6 @@ public function getStorage( $declaring_file_storage = $this->file_storage_provider->get($declaring_file_path); if (!isset($declaring_file_storage->functions[$function_id])) { - if ($from_stubs) { - return $from_stubs; - } - throw new UnexpectedValueException( 'Not expecting ' . $function_id . ' to not have storage in ' . $declaring_file_path, ); diff --git a/tests/StubTest.php b/tests/StubTest.php index c52239d68ee..14b3873ebdb 100644 --- a/tests/StubTest.php +++ b/tests/StubTest.php @@ -1514,4 +1514,48 @@ function em(EntityManager $em) : void { $this->analyzeFile($file_path, new Context()); } + + /** + * This covers the following case encountered by mmcev106: + * - A function was defined without a docblock + * - The autoloader defined a global containing the path to that file + * - The code being scanned required the path specified by the autoloader defined global + * - A docblock was added via a stub that marked the function as a taint source + * - The stub docblock was incorrectly ignored, causing the the taint source to be ignored. + */ + public function testAutoloadDefinedRequirePath(): void + { + $this->project_analyzer = $this->getProjectAnalyzerWithConfig( + TestConfig::loadFromXML( + dirname(__DIR__), + ' + + + + + + + + + ', + ), + ); + + $this->project_analyzer->trackTaintedInputs(); + + $file_path = getcwd() . '/src/somefile.php'; + + $this->addFile( + $file_path, + 'expectExceptionMessage('TaintedHtml - /src/somefile.php'); + $this->analyzeFile($file_path, new Context()); + } } diff --git a/tests/fixtures/stubs/custom_taint_source.php b/tests/fixtures/stubs/custom_taint_source.php new file mode 100644 index 00000000000..59eb33da49d --- /dev/null +++ b/tests/fixtures/stubs/custom_taint_source.php @@ -0,0 +1,3 @@ + Date: Tue, 8 Aug 2023 15:34:23 -0500 Subject: [PATCH 2/3] Removed trailing whitespace to follow code style --- tests/StubTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/StubTest.php b/tests/StubTest.php index 14b3873ebdb..b54039aa794 100644 --- a/tests/StubTest.php +++ b/tests/StubTest.php @@ -1521,7 +1521,7 @@ function em(EntityManager $em) : void { * - The autoloader defined a global containing the path to that file * - The code being scanned required the path specified by the autoloader defined global * - A docblock was added via a stub that marked the function as a taint source - * - The stub docblock was incorrectly ignored, causing the the taint source to be ignored. + * - The stub docblock was incorrectly ignored, causing the the taint source to be ignored */ public function testAutoloadDefinedRequirePath(): void { From 09cdb3563d2a1cae7d3934bf0e49e326e125cf6a Mon Sep 17 00:00:00 2001 From: Mark McEver Date: Tue, 15 Aug 2023 09:29:48 -0500 Subject: [PATCH 3/3] Commit to trigger unit tests after switching the base branch from 5.x to master in the PR