Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EscapeOutput: allow for typical pattern with _deprecated_file() #1818

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions WordPress/Sniffs/Security/EscapeOutputSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ public function process_token( $stackPtr ) {
$end_of_statement = ( $first_param['end'] + 1 );
unset( $first_param );
}

/*
* If the first param to `_deprecated_file()` follows the typical `basename( __FILE__ )`
* pattern, it doesn't need to be escaped.
*/
if ( '_deprecated_file' === $function ) {
$first_param = $this->get_function_call_parameter( $stackPtr, 1 );

// Quick check. This disregards comments.
if ( preg_match( '`^basename\s*\(\s*__FILE__\s*\)$`', $first_param['raw'] ) === 1 ) {
$stackPtr = ( $first_param['end'] + 2 );
}
unset( $first_param );
}
}

// Checking for the ignore comment, ex: //xss ok.
Expand Down
3 changes: 3 additions & 0 deletions WordPress/Tests/Security/EscapeOutputUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,6 @@ echo esc_html( get_the_title() ); // Ok.

echo implode( '<br>', map_deep( $items, 'esc_html' ) ); // Ok.
echo implode( '<br>', map_deep( $items, 'foo' ) ); // Bad.

_deprecated_file( basename( __FILE__ ), '1.3.0' ); // Ok.
_deprecated_file( $file, '1.3.0' ); // Error.
1 change: 1 addition & 0 deletions WordPress/Tests/Security/EscapeOutputUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function getErrorList() {
266 => 1,
289 => 1,
294 => 1,
297 => 1,
);
}

Expand Down