diff --git a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/.phpcs.xml b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/.phpcs.xml new file mode 100644 index 000000000..10ccec065 --- /dev/null +++ b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/.phpcs.xml @@ -0,0 +1,30 @@ + + + Sniff code to check different PHP compatibility + + + ../ + /vendor/ + /lib/ + + + + + + + + + + tests/* + vendor/* + fw_files/* + + + + + + + + + + diff --git a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/Scanner/HTMLTest.php b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/Scanner/HTMLTest.php new file mode 100644 index 000000000..a31814267 --- /dev/null +++ b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/Scanner/HTMLTest.php @@ -0,0 +1,29 @@ +alert(1);' + ); + ?> + + "; + $tokens = new Tokens($file_content); + $this->html = new HTML($tokens); + } + + public function testAnalise() + { + $this->html->analise(); + $this->assertEquals('T_INLINE_HTML', $this->html->result); + } +} diff --git a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/Scanner/TokensTest.php b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/Scanner/TokensTest.php new file mode 100644 index 000000000..966e84bbe --- /dev/null +++ b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/Scanner/TokensTest.php @@ -0,0 +1,26 @@ +tokens = new Tokens($file_content); + } + + public function testGetTokenFromPosition() + { + $echo_token = $this->tokens->getTokenFromPosition(2); + $this->assertInstanceOf(Token::class, $echo_token); + $this->assertEquals($echo_token[0], 'T_ECHO'); + $this->assertEquals($echo_token[1], 'echo'); + } +} diff --git a/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/bootstrap.php b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/bootstrap.php new file mode 100644 index 000000000..7386fad60 --- /dev/null +++ b/lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/bootstrap.php @@ -0,0 +1,22 @@ + + + + + ./ + ./bootstrap.php + + + diff --git a/lib/CleantalkSP/Common/Scanner/SignaturesAnalyser/tests/.phpcs.xml b/lib/CleantalkSP/Common/Scanner/SignaturesAnalyser/tests/.phpcs.xml new file mode 100644 index 000000000..f21f21787 --- /dev/null +++ b/lib/CleantalkSP/Common/Scanner/SignaturesAnalyser/tests/.phpcs.xml @@ -0,0 +1,30 @@ + + + Sniff code to check different PHP compatibility + + + ../ + /vendor/ + /lib/ + + + + + + + + + + tests/* + vendor/* + fw_files/* + + + + + + + + + + diff --git a/lib/CleantalkSP/Common/Scanner/SignaturesAnalyser/tests/bootstrap.php b/lib/CleantalkSP/Common/Scanner/SignaturesAnalyser/tests/bootstrap.php new file mode 100644 index 000000000..b3d9bbc7f --- /dev/null +++ b/lib/CleantalkSP/Common/Scanner/SignaturesAnalyser/tests/bootstrap.php @@ -0,0 +1 @@ + + + + + ./ + ./bootstrap.php + + + diff --git a/lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php b/lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php index 444203679..807bbf065 100755 --- a/lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php +++ b/lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php @@ -788,7 +788,7 @@ public function countFileSystem($path_to_scan = ABSPATH) $init_params = array( 'count' => true, 'file_exceptions' => 'wp-config.php', - 'extensions' => 'php, html, htm, js, php2, php3, php4, php5, php6, php7, phtml, shtml, phar, otc', 'ott', + 'extensions' => 'php, html, htm, js, php2, php3, php4, php5, php6, php7, phtml, shtml, phar, [ot.]', 'files_mandatory' => array(), 'dir_exceptions' => array(SPBC_PLUGIN_DIR . 'quarantine') ); @@ -835,7 +835,7 @@ public function file_system_analysis($offset = null, $amount = null, $path_to_sc 'full_hash' => true, 'offset' => $offset, 'amount' => $amount, - 'extensions' => 'php, html, htm, js, php2, php3, php4, php5, php6, php7, phtml, shtml, phar, otc', 'ott', + 'extensions' => 'php, html, htm, js, php2, php3, php4, php5, php6, php7, phtml, shtml, phar, [ot.]', 'extensions_exceptions' => '', //array('jpg', 'jpeg', 'png', 'gif', 'css', 'txt', 'zip', 'xml', 'json') 'file_exceptions' => 'wp-config.php', 'files_mandatory' => array(), diff --git a/lib/CleantalkSP/SpbctWP/Scanner/Surface.php b/lib/CleantalkSP/SpbctWP/Scanner/Surface.php index be8e5b07a..4e91a1d04 100644 --- a/lib/CleantalkSP/SpbctWP/Scanner/Surface.php +++ b/lib/CleantalkSP/SpbctWP/Scanner/Surface.php @@ -222,9 +222,13 @@ public function countFilesInDir($main_path) // Extensions filter if ( $this->ext_except || $this->ext ) { $tmp = explode('.', $path); + $currentFileExtension = $tmp[count($tmp) - 1]; if ( - ($this->ext_except && in_array($tmp[count($tmp) - 1], $this->ext_except, true)) || - ($this->ext && ! in_array($tmp[count($tmp) - 1], $this->ext, true)) + ! $this->hasFileAllowedExtension( + $currentFileExtension, + $this->ext_except, + $this->ext + ) ) { continue; } @@ -296,9 +300,13 @@ public function getFileStructure($main_path) // Extensions filter if ( $this->ext_except || $this->ext ) { $tmp = explode('.', $path); + $currentFileExtension = $tmp[count($tmp) - 1]; if ( - ($this->ext_except && in_array($tmp[count($tmp) - 1], $this->ext_except, true)) || - ($this->ext && ! in_array($tmp[count($tmp) - 1], $this->ext, true)) + ! $this->hasFileAllowedExtension( + $currentFileExtension, + $this->ext_except, + $this->ext + ) ) { continue; } @@ -474,4 +482,40 @@ public static function dirIsEmpty($dir) return true; } + + public function filterFileExtensionUsingRegexp($extension, array $extensions) + { + foreach ($extensions as $extensionItem) { + $fsymbol = strpos($extensionItem, '['); + $lsymbol = strpos($extensionItem, ']'); + + $regexpExpression = substr($extensionItem, $fsymbol, $lsymbol); + if ($regexpExpression) { + $regexpExpression = '/' . trim($regexpExpression, "][") . '/'; + + if (\CleantalkSP\SpbctWP\Helpers\Helper::isRegexp($regexpExpression) && preg_match($regexpExpression, $extension)) { + return true; + } + } + } + + return false; + } + + private function hasFileAllowedExtension($currentFileExtension, array $ext_except, array $ext) + { + if (in_array($currentFileExtension, $ext_except, true)) { + return false; + } + + if (in_array($currentFileExtension, $ext, true)) { + return true; + } + + if ($this->filterFileExtensionUsingRegexp($currentFileExtension, $ext)) { + return true; + } + + return false; + } }