Skip to content

Commit d3ce1d0

Browse files
committed
Security/Underscorejs: improve check for interpolate in PHP files
Match the `interpolate` property in PHP files with the similar precision as in JS files. Includes unit tests.
1 parent 70ec811 commit d3ce1d0

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

WordPressVIPMinimum/Sniffs/Security/UnderscorejsSniff.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class UnderscorejsSniff extends Sniff {
2626
*/
2727
const UNESCAPED_INTERPOLATE_REGEX = '`<%=\s*(?:.+?%>|$)`';
2828

29+
/**
30+
* Regex to match the "interpolate" keyword when used to overrule the ERB-style delimiters.
31+
*
32+
* @var string
33+
*/
34+
const INTERPOLATE_KEYWORD_REGEX = '`(?:templateSettings\.interpolate|\.interpolate\s*=\s*/|interpolate\s*:\s*/)`';
35+
2936
/**
3037
* A list of tokenizers this sniff supports.
3138
*
@@ -107,7 +114,7 @@ public function process_token( $stackPtr ) {
107114
}
108115

109116
if ( $this->phpcsFile->tokenizerType !== 'JS'
110-
&& strpos( $content, 'interpolate' ) !== false
117+
&& preg_match( self::INTERPOLATE_KEYWORD_REGEX, $content ) > 0
111118
) {
112119
// Underscore.js delimiter change.
113120
$message = 'Found Underscore.js delimiter change notation.';

WordPressVIPMinimum/Tests/Security/UnderscorejsUnitTest.inc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,26 @@ EOD;
5050

5151
// Make sure the JS specific check does not trigger on PHP code.
5252
$obj->interpolate = true;
53+
54+
// Test matching the "interpolate" keyword with higher precision (mirrors same check in JS).
55+
function test_interpolate_match_precision() {
56+
?>
57+
<script type="text/javascript">
58+
_.templateSettings.interpolate = /\{\{(.+?)\}\}/g; /* NOK */
59+
60+
options.interpolate=_.templateSettings.interpolate; /* NOK */
61+
var interpolate = options.interpolate || reNoMatch, /* Ignore */
62+
source = "__p += '";
63+
64+
// Prevent false positives on "interpolate".
65+
var preventMisidentification = 'text interpolate text'; // OK.
66+
var interpolate = THREE.CurveUtils.interpolate; // OK.
67+
68+
var p = function(f, d) {
69+
return s.interpolate(m(f), _(d), 0.5, e.color_space) // OK.
70+
}
71+
72+
y.interpolate.bezier = b; // OK.
73+
</script>
74+
<?php
75+
}

WordPressVIPMinimum/Tests/Security/UnderscorejsUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function getWarningList( $testFile = '' ) {
4848
45 => 1,
4949
46 => 1,
5050
47 => 1,
51+
58 => 1,
52+
60 => 1,
5153
];
5254

5355
case 'UnderscorejsUnitTest.js':

0 commit comments

Comments
 (0)