From 99fb8bb8e2d6640c8a5e9b6c456e8db8c904befd Mon Sep 17 00:00:00 2001 From: david-binda Date: Fri, 19 Jan 2018 14:29:53 +0100 Subject: [PATCH] Flag attempt to escape function which prints it's output Fixes #120 --- .../VIP/EscapingVoidReturnFunctionsSniff.php | 69 +++++++++++++++++++ .../EscapingVoidReturnFunctionsUnitTest.inc | 4 ++ .../EscapingVoidReturnFunctionsUnitTest.php | 39 +++++++++++ 3 files changed, 112 insertions(+) create mode 100644 WordPressVIPMinimum/Sniffs/VIP/EscapingVoidReturnFunctionsSniff.php create mode 100644 WordPressVIPMinimum/Tests/VIP/EscapingVoidReturnFunctionsUnitTest.inc create mode 100644 WordPressVIPMinimum/Tests/VIP/EscapingVoidReturnFunctionsUnitTest.php diff --git a/WordPressVIPMinimum/Sniffs/VIP/EscapingVoidReturnFunctionsSniff.php b/WordPressVIPMinimum/Sniffs/VIP/EscapingVoidReturnFunctionsSniff.php new file mode 100644 index 00000000..9912049e --- /dev/null +++ b/WordPressVIPMinimum/Sniffs/VIP/EscapingVoidReturnFunctionsSniff.php @@ -0,0 +1,69 @@ +getTokens(); + + if ( 0 !== strpos( $tokens[ $stackPtr ]['content'], 'esc_' ) && 0 !== strpos( $tokens[ $stackPtr ]['content'], 'wp_kses' ) ) { + // Not what we are looking for. + return; + } + + $next_token = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); + + if ( T_OPEN_PARENTHESIS !== $tokens[ $next_token ]['code'] ) { + // Not a function call. + return; + } + + $next_token = $phpcsFile->findNext( Tokens::$emptyTokens, ( $next_token + 1 ), null, true ); + + if ( T_STRING !== $tokens[ $next_token ]['code'] ) { + // Not what we are looking for. + return; + } + + if ( 0 === strpos( $tokens[ $next_token ]['content'], '_e' ) ) { + $phpcsFile->addError( sprintf( 'Attempting to escape %s() which is printing it\'s output.', $tokens[ $next_token ]['content'] ), $stackPtr, 'escapingVoidReturningFunction' ); + return; + } + } + +} diff --git a/WordPressVIPMinimum/Tests/VIP/EscapingVoidReturnFunctionsUnitTest.inc b/WordPressVIPMinimum/Tests/VIP/EscapingVoidReturnFunctionsUnitTest.inc new file mode 100644 index 00000000..9a9de966 --- /dev/null +++ b/WordPressVIPMinimum/Tests/VIP/EscapingVoidReturnFunctionsUnitTest.inc @@ -0,0 +1,4 @@ + => + */ + public function getErrorList() { + return array( + 3 => 1, + ); + } + + /** + * Returns the lines where warnings should occur. + * + * @return array => + */ + public function getWarningList() { + return array(); + } + +} // End class.