Skip to content

Commit

Permalink
Ot extensions aa (#248)
Browse files Browse the repository at this point in the history
* New: Filter files using file extention and regexp
  • Loading branch information
ArtemAnoshin authored Sep 20, 2023
1 parent a4da475 commit 7e004a7
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 6 deletions.
30 changes: 30 additions & 0 deletions lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/.phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<ruleset name="PHP compatibility">
<description>Sniff code to check different PHP compatibility</description>

<!-- What to scan -->
<file>../</file>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>/lib/</exclude-pattern>

<!-- How to scan -->
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
<!-- Annotated ruleset: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
<arg value="sp"/> <!-- Show sniff and progress -->
<arg name="basepath" value="./"/><!-- Strip the file paths down to the relevant bit -->
<arg name="colors"/>
<arg name="extensions" value="php"/>
<!-- Exclude test directories -->
<exclude-pattern>tests/*</exclude-pattern>
<exclude-pattern>vendor/*</exclude-pattern>
<exclude-pattern>fw_files/*</exclude-pattern>
<arg name="exclude" value="PSR12.ControlStructures.ControlStructureSpacing"/>
<arg value="n"/>

<!-- Rules: Check PHP version compatibility -->
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="7.4-"/>

<rule ref="PHPCompatibility"/>
<rule ref="PSR12"/>
</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use CleantalkSP\Common\Scanner\HeuristicAnalyser\Modules\HTML;
use CleantalkSP\Common\Scanner\HeuristicAnalyser\Modules\Tokens;
use PHPUnit\Framework\TestCase;

class HTMLTest extends TestCase
{
private $html;

public function setUp()
{
$file_content = "<?php
echo(
'<script>alert(1);</script>'
);
?>
<script>alert(2);</script>
";
$tokens = new Tokens($file_content);
$this->html = new HTML($tokens);
}

public function testAnalise()
{
$this->html->analise();
$this->assertEquals('T_INLINE_HTML', $this->html->result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use CleantalkSP\Common\Scanner\HeuristicAnalyser\DataStructures\Token;
use CleantalkSP\Common\Scanner\HeuristicAnalyser\Modules\Tokens;
use PHPUnit\Framework\TestCase;

class TokensTest extends TestCase
{
private $tokens;

public function setUp()
{
$file_content = "<?php
echo('hello');
";
$this->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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Autoloader for \CleantalkSP\* classes
*
* @param string $class
*
* @return void
*/

spl_autoload_register(function ($class) {

// Register class auto loader
// Custom modules1
if ( strpos($class, 'CleantalkSP') !== false ) {
$class = str_replace('CleantalkSP\Common\Scanner\HeuristicAnalyser\\', DIRECTORY_SEPARATOR, $class);
$class_file = dirname(__DIR__) . $class . '.php';
if ( file_exists($class_file) ) {
require_once($class_file);
}
}
});
17 changes: 17 additions & 0 deletions lib/CleantalkSP/Common/Scanner/HeuristicAnalyser/tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
bootstrap="bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true"
beStrictAboutOutputDuringTests="true"
>
<testsuites>
<testsuite name="CleanTalk SPBCT Heuristic Analyser classes Unit Tests">
<directory suffix=".php">./</directory>
<exclude>./bootstrap.php</exclude>
</testsuite>
</testsuites>
</phpunit>
30 changes: 30 additions & 0 deletions lib/CleantalkSP/Common/Scanner/SignaturesAnalyser/tests/.phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<ruleset name="PHP compatibility">
<description>Sniff code to check different PHP compatibility</description>

<!-- What to scan -->
<file>../</file>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>/lib/</exclude-pattern>

<!-- How to scan -->
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
<!-- Annotated ruleset: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
<arg value="sp"/> <!-- Show sniff and progress -->
<arg name="basepath" value="./"/><!-- Strip the file paths down to the relevant bit -->
<arg name="colors"/>
<arg name="extensions" value="php"/>
<!-- Exclude test directories -->
<exclude-pattern>tests/*</exclude-pattern>
<exclude-pattern>vendor/*</exclude-pattern>
<exclude-pattern>fw_files/*</exclude-pattern>
<arg name="exclude" value="PSR12.ControlStructures.ControlStructureSpacing"/>
<arg value="n"/>

<!-- Rules: Check PHP version compatibility -->
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.6-"/>

<rule ref="PHPCompatibility"/>
<rule ref="PSR12"/>
</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
bootstrap="bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true"
beStrictAboutOutputDuringTests="true"
>
<testsuites>
<testsuite name="CleanTalk SPBCT Heuristic Analyser classes Unit Tests">
<directory suffix=".php">./</directory>
<exclude>./bootstrap.php</exclude>
</testsuite>
</testsuites>
</phpunit>
4 changes: 2 additions & 2 deletions lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
Expand Down Expand Up @@ -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(),
Expand Down
52 changes: 48 additions & 4 deletions lib/CleantalkSP/SpbctWP/Scanner/Surface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 7e004a7

Please sign in to comment.