Skip to content

Commit fbaf8f1

Browse files
authored
Merge pull request #53 from kevcooper/type-juggle
add TypeJuggleSniff.php
2 parents f868f06 + f40e76e commit fbaf8f1

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace PHPCS_SecurityAudit\Security\Sniffs\Misc;
3+
4+
use PHP_CodeSniffer\Sniffs\Sniff;
5+
use PHP_CodeSniffer\Files\File;
6+
7+
8+
class TypeJuggleSniff implements Sniff {
9+
10+
/**
11+
* Returns the token types that this sniff is interested in.
12+
*
13+
* @return array(int)
14+
*/
15+
public function register() {
16+
return array(T_IS_EQUAL, T_IS_NOT_EQUAL);
17+
}
18+
19+
/**
20+
* Processes the tokens that this sniff is interested in.
21+
*
22+
* @param File $phpcsFile The file where the token was found.
23+
* @param int $stackPtr The position in the stack where
24+
* the token was found.
25+
*
26+
* @return void
27+
*/
28+
public function process(File $phpcsFile, $stackPtr) {
29+
$tokens = $phpcsFile->getTokens();
30+
if (\PHP_CodeSniffer\Config::getConfigData('ParanoiaMode')) {
31+
$warning = 'You are using the comparison operator "'. $tokens[$stackPtr]['content'] .'" that converts type and may cause unintended results.';
32+
$phpcsFile->addWarning($warning, $stackPtr, 'TypeJuggle');
33+
}
34+
}
35+
36+
}
37+
38+
?>

example_base_ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<!-- Misc -->
4848
<rule ref="Security.Misc.BadCorsHeader"/>
4949
<rule ref="Security.Misc.IncludeMismatch"/>
50+
<rule ref="Security.Misc.TypeJuggle"/>
5051

5152
</ruleset>
5253

tests.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
preg_replace($_GET['b'], $_GET['a'], $_GET['c']);
1212
preg_replace($b, $_GET['a'], 'aaaaaa');
1313
preg_replace("aaa", $_GET['a'], 'ababaaa');
14-
14+
1515

1616
// BadFunctions
1717
md5();
@@ -44,6 +44,7 @@
4444
$a->withHeader('Access-Control-Allow-Origin', '*');
4545
include('abc.xyz');
4646
require_once EXTENSION_PATH . '/path/to' . $name . '.jkl';
47+
0 == '0 cats';
4748

4849
// Easy user input
4950
$_GET['a'] = 'xss';

0 commit comments

Comments
 (0)