Skip to content

Commit 76cf2f2

Browse files
authored
Merge pull request #107 from Automattic/flag-batcache-whitelisted-get-params
Adding sniff for flagging Batcache whitelisted GET params, which woul…
2 parents 45f8e0d + 7ee44be commit 76cf2f2

File tree

5 files changed

+167
-1
lines changed

5 files changed

+167
-1
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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\Cache;
10+
11+
use PHP_CodeSniffer_File as File;
12+
use PHP_CodeSniffer_Tokens as Tokens;
13+
14+
/**
15+
* Checks whether proper escaping function is used.
16+
*
17+
* @package VIPCS\WordPressVIPMinimum
18+
*/
19+
class BatcacheWhitelistedParamsSniff implements \PHP_CodeSniffer_Sniff {
20+
21+
/**
22+
* List of whitelisted batcache params.
23+
*
24+
* @var array
25+
*/
26+
public $whitelistes_batcache_params = array(
27+
'hpt',
28+
'eref',
29+
'iref',
30+
'fbid',
31+
'om_rid',
32+
'utm',
33+
'utm_source',
34+
'utm_content',
35+
'utm_medium',
36+
'utm_campaign',
37+
'utm_term',
38+
'fb_xd_bust',
39+
'fb_xd_fragment',
40+
'npt',
41+
'module',
42+
'iid',
43+
'cid',
44+
'icid',
45+
'ncid',
46+
'snapid',
47+
'_',
48+
'fb_ref',
49+
'fb_source',
50+
'omcamp',
51+
'affiliate',
52+
'utm_affiliate',
53+
'utm_subid',
54+
'utm_keyword',
55+
'migAgencyId',
56+
'migSource',
57+
'migTrackDataExt',
58+
'migRandom',
59+
'migTrackFmtExt',
60+
'bust',
61+
'linkId',
62+
'_ga',
63+
'xid',
64+
'hootPostID',
65+
'pretty',
66+
'__lsa',
67+
'rpx_response',
68+
'__rmid',
69+
'sr_share',
70+
'ia_share_url',
71+
);
72+
73+
/**
74+
* Returns an array of tokens this test wants to listen for.
75+
*
76+
* @return array
77+
*/
78+
public function register() {
79+
return array( T_VARIABLE );
80+
}
81+
82+
/**
83+
* Process this test when one of its tokens is encountered
84+
*
85+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
86+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
87+
*
88+
* @return void
89+
*/
90+
public function process( File $phpcsFile, $stackPtr ) {
91+
92+
$tokens = $phpcsFile->getTokens();
93+
94+
if ( '$_GET' !== $tokens[ $stackPtr ]['content'] ) {
95+
return;
96+
}
97+
98+
$key = $phpcsFile->findNext( array_merge( Tokens::$emptyTokens, array( T_OPEN_SQUARE_BRACKET ) ), ( $stackPtr + 1 ), null, true );
99+
100+
if ( T_CONSTANT_ENCAPSED_STRING !== $tokens[ $key ]['code'] ) {
101+
return;
102+
}
103+
104+
$variable_name = $tokens[ $key ]['content'];
105+
106+
$variable_name = substr( $variable_name, 1, -1 );
107+
108+
if ( true === in_array( $variable_name, $this->whitelistes_batcache_params, true ) ) {
109+
$phpcsFile->addWarning( sprintf( 'Batcache whitelisted GET param, %s, found. Batcache whitelisted parameters get stripped and are not available in PHP.', $variable_name ), $stackPtr, 'strippedGetParam' );
110+
return;
111+
}
112+
}//end process()
113+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
if ( isset( $_GET['utm_medium'] ) && 'email' === $_GET["utm_medium"] ) { // 2 warnings.
4+
5+
}
6+
7+
$hello = $_GET['migSource']; // Warning.
8+
9+
$hey = $_GET['ThisIsOkay']; // OK.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Unit test class for WordPressVIPMinimum Coding Standard.
4+
*
5+
* @package VIPCS\WordPressVIPMinimum
6+
*/
7+
8+
namespace WordPressVIPMinimum\Tests\Cache;
9+
10+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
11+
12+
/**
13+
* Unit test class for the BatcacheWhitelistedParams sniff.
14+
*
15+
* @package VIPCS\WordPressVIPMinimum
16+
*/
17+
class BatcacheWhitelistedParamsUnitTest 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+
}
27+
28+
/**
29+
* Returns the lines where warnings should occur.
30+
*
31+
* @return array <int line number> => <int number of warnings>
32+
*/
33+
public function getWarningList() {
34+
return array(
35+
3 => 2,
36+
7 => 1,
37+
);
38+
39+
}
40+
41+
} // End class.

ruleset_test.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,7 @@ add_option( 'taxonomy_rating_' . $obj->term_id ); // Bad. Warning.
165165

166166
echo "<a href='" . esc_attr( $some_var ) . "'></a>"; // NOK. Error.
167167

168+
$hello = true === isset( $_GET['utm_medium'] ) ? true : false; // NOK. Warning 3 times.
169+
168170
?>
169171

ruleset_test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
133 => 1,
5050
157 => 1,
5151
166 => 1,
52-
168 => 1, // Error on the end of the file. When any code is added, bounce this.
52+
170 => 1, // Error on the end of the file. When any code is added, bounce this.
5353
),
5454
'warnings' => array(
5555
9 => 1,
@@ -73,6 +73,7 @@
7373
160 => 1,
7474
162 => 1,
7575
164 => 1,
76+
168 => 1,
7677
),
7778
'messages' => array(
7879
129 => array(

0 commit comments

Comments
 (0)