Skip to content

Commit 93db5cd

Browse files
committed
Detect Basic Auth implementation in PHP
Basic Auth should not be set via PHP. This commit adds new sniff which is producing errors in case it spots `$_SERVER['PHP_AUTH_PW']` in the code. Unit tests are included.
1 parent 411c212 commit 93db5cd

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* WordPress-VIP-Minimum Coding Standard.
4+
*
5+
* @package VIPCS\WordPressVIPMinimum
6+
* @link https://github.com/Automattic/VIP-Coding-Standards
7+
*/
8+
9+
namespace WordPressVIPMinimum\Sniffs\Variables;
10+
11+
use PHP_CodeSniffer_File as File;
12+
use PHP_CodeSniffer_Tokens as Tokens;
13+
14+
/**
15+
* Restricts usage of some server variables.
16+
*
17+
* @package VIPCS\WordPressVIPMinimum
18+
*/
19+
class ServerVariablesSniff implements \PHP_CodeSniffer_Sniff {
20+
21+
/**
22+
* List of restricted constant names.
23+
*
24+
* @var array
25+
*/
26+
public $restrictedVariables = array(
27+
'PHP_AUTH_PW',
28+
);
29+
30+
/**
31+
* Returns an array of tokens this test wants to listen for.
32+
*
33+
* @return array
34+
*/
35+
public function register() {
36+
return array(
37+
T_VARIABLE,
38+
);
39+
}//end register()
40+
41+
/**
42+
* Process this test when one of its tokens is encoutnered
43+
*
44+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
45+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
46+
*
47+
* @return void
48+
*/
49+
public function process( File $phpcsFile, $stackPtr ) {
50+
51+
$tokens = $phpcsFile->getTokens();
52+
53+
if ( '$_SERVER' !== $tokens[ $stackPtr ]['content'] ) {
54+
// Not the variable we are looking for.
55+
return;
56+
}
57+
58+
$variableNamePtr = $phpcsFile->findNext( array( T_CONSTANT_ENCAPSED_STRING ), ($stackPtr + 1), null, false, null, true );
59+
$variableName = str_replace( "'", '', $tokens[$variableNamePtr]['content'] );
60+
61+
if ( false === in_array( $variableName, $this->restrictedVariables , true ) ) {
62+
// Not the variable we are looking for.
63+
return;
64+
}
65+
66+
$phpcsFile->addError( 'Basic authentication should not be handled via PHP code.', $stackPtr, 'ServerVariables' );
67+
}
68+
69+
} // End class.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$_SERVER['PHP_AUTH_PW']; // Bad. Should never happen.
4+
5+
$_SERVER['SOME_OTHER_VARIABLE']; // We don't care.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Unit test class for WordPressVIPMinimum Coding Standard.
4+
*
5+
* @package VIPCS\WordPressVIPMinimum
6+
*/
7+
8+
namespace WordPressVIPMinimum\Tests\Variables;
9+
10+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
11+
12+
/**
13+
* Unit test class for the Variable Analysis sniff.
14+
*
15+
* @package VIPCS\WordPressVIPMinimum
16+
*/
17+
class ServerVariablesUnitTest extends AbstractSniffUnitTest {
18+
19+
/**
20+
* Returns the lines where errors should occur.
21+
*
22+
* @return array <int line number> => <int number of errors>
23+
*/
24+
public function getErrorList() {
25+
return array(
26+
3 => 1,
27+
);
28+
}
29+
30+
/**
31+
* Returns the lines where warnings should occur.
32+
*
33+
* @return array <int line number> => <int number of warnings>
34+
*/
35+
public function getWarningList() {
36+
return array();
37+
38+
}
39+
40+
} // End class.

0 commit comments

Comments
 (0)