Skip to content

Commit 6ca3459

Browse files
committed
added CORS check #12 and fixed missing Misc includes
1 parent d71afd2 commit 6ca3459

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace PHPCS_SecurityAudit\Sniffs\Misc;
3+
4+
use PHP_CodeSniffer\Sniffs\Sniff;
5+
use PHP_CodeSniffer\Files\File;
6+
7+
8+
class BadCorsHeaderSniff implements Sniff {
9+
/**
10+
* Returns the token types that this sniff is interested in.
11+
*
12+
* @return array(int)
13+
*/
14+
public function register() {
15+
return array(T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_QUOTED_STRING);
16+
}
17+
18+
/**
19+
* Processes the tokens that this sniff is interested in.
20+
*
21+
* @param File $phpcsFile The file where the token was found.
22+
* @param int $stackPtr The position in the stack where
23+
* the token was found.
24+
*
25+
* @return void
26+
*/
27+
public function process(File $phpcsFile, $stackPtr) {
28+
$utils = \PHPCS_SecurityAudit\Sniffs\UtilsFactory::getInstance();
29+
$tokens = $phpcsFile->getTokens();
30+
if (stristr($tokens[$stackPtr]['content'], 'Access-Control-Allow-Origin')) {
31+
$closer = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $stackPtr);
32+
$s = $phpcsFile->findNext(\PHP_CodeSniffer\Util\Tokens::$stringTokens, $stackPtr + 1, $closer);
33+
if ($s && stristr($tokens[$s]['content'], '*')) {
34+
$phpcsFile->addWarning('Bad CORS header detected.', $stackPtr, 'WarnPCKS1Crypto');
35+
}
36+
37+
}
38+
/*
39+
if (preg_match("/^mcrypt_/", ) || in_array($tokens[$stackPtr]['content'], $utils::getCryptoFunctions())) {
40+
$tokstr = $tokens[$stackPtr]['content'];
41+
if ( $tokstr == "openssl_public_encrypt" || $tokstr == "openssl_private_decrypt") {
42+
$p4 = $utils::get_param_tokens($phpcsFile, $stackPtr, 4);
43+
$p4 == null ? $s = false : $s = $phpcsFile->findNext(T_STRING, $p4[0]['stackPtr'], end($p4)['stackPtr']+1);
44+
if ($s) {
45+
if ($tokens[$s]['content'] != "OPENSSL_PKCS1_OAEP_PADDING") {
46+
$phpcsFile->addError('Bad use of ' . $tokstr . ' without OPENSSL_PKCS1_OAEP_PADDING', $s, 'ErrPCKS1Crypto');
47+
}
48+
} else {
49+
// there's no 4th parameter, according to the doc the default is OPENSSL_PKCS1_PADDING
50+
$phpcsFile->addWarning($tokstr . ' is using insecure OPENSSL_PKCS1_PADDING by default.', $stackPtr, 'WarnPCKS1Crypto');
51+
}
52+
} else {
53+
// Only warn on crypto functions in paranoia mode
54+
if (\PHP_CodeSniffer\Config::getConfigData('ParanoiaMode')) {
55+
$phpcsFile->addWarning('Crypto function ' . $tokens[$stackPtr]['content'] . ' used.', $stackPtr, 'WarnCryptoFunc');
56+
}
57+
}
58+
}
59+
60+
*/
61+
}
62+
63+
}
64+
65+
?>

example_base_ruleset.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
<rule ref="Security.CVE.20132110"/>
4545
<rule ref="Security.CVE.20134113"/>
4646

47+
<!-- Misc -->
48+
<rule ref="Security.Misc.BadCorsHeader"/>
49+
<rule ref="Security.Misc.IncludeMismatch"/>
4750

4851
</ruleset>
4952

example_drupal7_ruleset.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
<rule ref="Security.CVE.20132110"/>
4141
<rule ref="Security.CVE.20134113"/>
4242

43+
<!-- Misc -->
44+
<rule ref="Security.Misc.BadCorsHeader"/>
45+
<rule ref="Security.Misc.IncludeMismatch"/>
46+
4347
<!-- Drupal7 -->
4448
<!-- Specific security issues of Drupal7 and advisories -->
4549
<rule ref="Security.Drupal7.AdvisoriesContrib">

tests.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
xml_parse_into_struct(xml_parser_create_ns(), str_repeat("<blah>", 1000), $a);
4040
quoted_printable_encode(str_repeat("\xf4", 1000));
4141

42+
// Misc
43+
$a->withHeader('Access-Control-Allow-Origin', '*');
44+
include('abc.xyz');
4245

4346
// Easy user input
4447
$_GET['a'] = 'xss';

0 commit comments

Comments
 (0)