|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WordPressVIPMinimum_Sniffs_JS_WindowSniff. |
| 4 | + * |
| 5 | + * @package VIPCS\WordPressVIPMinimum |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WordPressVIPMinimum\Sniffs\JS; |
| 9 | + |
| 10 | +use PHP_CodeSniffer\Files\File; |
| 11 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 12 | +use PHP_CodeSniffer\Util\Tokens; |
| 13 | + |
| 14 | +/** |
| 15 | + * WordPressVIPMinimum_Sniffs_JS_WindowSniff. |
| 16 | + * |
| 17 | + * Looks for instances of window properties that should be flagged. |
| 18 | + * |
| 19 | + * @package VIPCS\WordPressVIPMinimum |
| 20 | + */ |
| 21 | +class WindowSniff implements Sniff { |
| 22 | + |
| 23 | + /** |
| 24 | + * A list of tokenizers this sniff supports. |
| 25 | + * |
| 26 | + * @var array |
| 27 | + */ |
| 28 | + public $supportedTokenizers = array( |
| 29 | + 'JS', |
| 30 | + ); |
| 31 | + |
| 32 | + /** |
| 33 | + * Returns an array of tokens this test wants to listen for. |
| 34 | + * |
| 35 | + * @return array |
| 36 | + */ |
| 37 | + public function register() { |
| 38 | + return [ |
| 39 | + T_STRING, |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * List of window properties that need to be flagged. |
| 45 | + * |
| 46 | + * @var array |
| 47 | + */ |
| 48 | + private $windowProperties = [ |
| 49 | + 'location' => [ |
| 50 | + 'href' => true, |
| 51 | + 'protocol' => true, |
| 52 | + 'host' => true, |
| 53 | + 'hostname' => true, |
| 54 | + 'pathname' => true, |
| 55 | + 'protocol' => true, |
| 56 | + 'search' => true, |
| 57 | + 'hash' => true, |
| 58 | + 'username' => true, |
| 59 | + 'port' => true, |
| 60 | + 'password' => true, |
| 61 | + ], |
| 62 | + 'name' => true, |
| 63 | + 'status' => true, |
| 64 | + ]; |
| 65 | + |
| 66 | + /** |
| 67 | + * A list of tokens that are allowed in the syntax. |
| 68 | + * |
| 69 | + * @var array |
| 70 | + */ |
| 71 | + private $syntaxTokens = [ |
| 72 | + T_OBJECT_OPERATOR, |
| 73 | + ]; |
| 74 | + |
| 75 | + /** |
| 76 | + * Processes this test, when one of its tokens is encountered. |
| 77 | + * |
| 78 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 79 | + * @param int $stackPtr The position of the current token in the |
| 80 | + * stack passed in $tokens. |
| 81 | + * |
| 82 | + * @return void |
| 83 | + */ |
| 84 | + public function process( File $phpcsFile, $stackPtr ) { |
| 85 | + $tokens = $phpcsFile->getTokens(); |
| 86 | + |
| 87 | + if ( 'window' !== $tokens[ $stackPtr ]['content'] ) { |
| 88 | + // Doesn't begin with 'window', bail. |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $nextTokenPtr = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true ); |
| 93 | + $nextToken = $tokens[ $nextTokenPtr ]['code']; |
| 94 | + if ( T_OBJECT_OPERATOR !== $nextToken && T_OPEN_SQUARE_BRACKET !== $nextToken ) { |
| 95 | + // No . or [' next, bail. |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + $nextNextTokenPtr = $phpcsFile->findNext( Tokens::$emptyTokens, ( $nextTokenPtr + 1 ), null, true, null, true ); |
| 100 | + if ( false === $nextNextTokenPtr ) { |
| 101 | + // Something went wrong, bail. |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + $nextNextToken = str_replace( [ '"', "'" ], '', $tokens[ $nextNextTokenPtr ]['content'] ); |
| 106 | + if ( ! isset( $this->windowProperties[ $nextNextToken ] ) ) { |
| 107 | + // Not in $windowProperties, bail. |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + $nextNextNextTokenPtr = $phpcsFile->findNext( array_merge( [ T_CLOSE_SQUARE_BRACKET ], Tokens::$emptyTokens ), ( $nextNextTokenPtr + 1 ), null, true, null, true ); |
| 112 | + $nextNextNextToken = $tokens[ $nextNextNextTokenPtr ]['code']; |
| 113 | + |
| 114 | + $nextNextNextNextToken = false; |
| 115 | + if ( T_OBJECT_OPERATOR === $nextNextNextToken || T_OPEN_SQUARE_BRACKET === $nextNextNextToken ) { |
| 116 | + $nextNextNextNextTokenPtr = $phpcsFile->findNext( Tokens::$emptyTokens, ( $nextNextNextTokenPtr + 1 ), null, true, null, true ); |
| 117 | + if ( false === $nextNextNextNextTokenPtr ) { |
| 118 | + // Something went wrong, bail. |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + $nextNextNextNextToken = str_replace( [ '"', "'" ], '', $tokens[ $nextNextNextNextTokenPtr ]['content'] ); |
| 123 | + if ( ! isset( $this->windowProperties[ $nextNextToken ][ $nextNextNextNextToken ] ) ) { |
| 124 | + // Not in $windowProperties, bail. |
| 125 | + return; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + $windowProperty = 'window.'; |
| 130 | + $windowProperty .= $nextNextNextNextToken ? $nextNextToken . '.' . $nextNextNextNextToken : $nextNextToken; |
| 131 | + |
| 132 | + $prevTokenPtr = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true, null, true ); |
| 133 | + if ( T_EQUAL === $tokens[ $prevTokenPtr ]['code'] ) { |
| 134 | + // Variable assignment. |
| 135 | + $phpcsFile->addWarning( sprintf( 'Data from JS global "' . $windowProperty . '" may contain user-supplied values and should be checked.', $tokens[ $stackPtr ]['content'] ), $stackPtr, 'VarAssignment' ); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + $phpcsFile->addError( sprintf( 'Data from JS global "' . $windowProperty . '" may contain user-supplied values and should be sanitized before output to prevent XSS.', $tokens[ $stackPtr ]['content'] ), $stackPtr, $nextNextToken ); |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments