|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pressbooks\Sniffs\Security; |
| 4 | + |
| 5 | +use Pressbooks\Sniffs\ExtraSniffCode; |
| 6 | +use PHP_CodeSniffer\Files\File as PhpcsFile; |
| 7 | +use WordPressCS\WordPress\Sniffs\Security\ValidatedSanitizedInputSniff as WPCSValidatedSanitizedInputSniff; |
| 8 | + |
| 9 | +class ValidatedSanitizedInputSniff extends WPCSValidatedSanitizedInputSniff { |
| 10 | + use ExtraSniffCode; |
| 11 | + |
| 12 | + /** |
| 13 | + * Keys to allow in the $_SERVER variable. |
| 14 | + * |
| 15 | + * Set this to override. |
| 16 | + * |
| 17 | + * @var array |
| 18 | + */ |
| 19 | + public $allowedServerKeys = [ |
| 20 | + 'HTTP_HOST', |
| 21 | + |
| 22 | + // User-Agent is forced to a static value when passing through |
| 23 | + // CloudFront, so is safe to use. |
| 24 | + 'HTTP_USER_AGENT', |
| 25 | + |
| 26 | + 'HTTPS', |
| 27 | + 'REMOTE_ADDR', |
| 28 | + 'REQUEST_METHOD', |
| 29 | + 'REQUEST_TIME', |
| 30 | + 'REQUEST_TIME_FLOAT', |
| 31 | + 'REQUEST_URI', |
| 32 | + 'QUERY_STRING', |
| 33 | + 'SERVER_ADDR', |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * Override init to duplicate any ignores. |
| 38 | + * |
| 39 | + * @param PhpcsFile $phpcsFile |
| 40 | + */ |
| 41 | + protected function init( PhpcsFile $phpcsFile ) { |
| 42 | + parent::init( $phpcsFile ); |
| 43 | + |
| 44 | + $this->duplicate_ignores( 'WordPress.Security.ValidatedSanitizedInput' ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Process a token for validation and sanitisation. |
| 49 | + * |
| 50 | + * @param int $stackPtr |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function process_token( $stackPtr ) { |
| 54 | + // Process our custom server rules first. |
| 55 | + if ( $this->tokens[ $stackPtr ]['content'] === '$_SERVER' ) { |
| 56 | + $pass = $this->check_server_variable( $stackPtr ); |
| 57 | + if ( $pass ) { |
| 58 | + // Variable is fine, skip upstream checks. |
| 59 | + return; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Not an allowed usage, so run the regular check on it. |
| 64 | + return parent::process_token( $stackPtr ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Check whether a $_SERVER variable is constant and allowed. |
| 69 | + * |
| 70 | + * @param int $stackPtr Current token to check. |
| 71 | + * @return bool True if this is a $_SERVER variable and is safe, false to run regular checks. |
| 72 | + */ |
| 73 | + protected function check_server_variable( $stackPtr ) { |
| 74 | + $key = $this->get_array_access_key( $stackPtr ); |
| 75 | + |
| 76 | + // Find the next non-whitespace token. |
| 77 | + $open_bracket = $this->phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); |
| 78 | + if ( $this->tokens[ $open_bracket ]['code'] !== T_OPEN_SQUARE_BRACKET ) { |
| 79 | + // No index access, run regular checks. |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + $index_token = $this->phpcsFile->findNext( T_WHITESPACE, ( $open_bracket + 1 ), null, true ); |
| 84 | + if ( $this->tokens[ $index_token ]['code'] !== T_CONSTANT_ENCAPSED_STRING ) { |
| 85 | + // Dynamic string, run regular checks. |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + // Possible constant string, check there's no further dynamic parts. |
| 90 | + $maybe_close_bracket = $this->phpcsFile->findNext( T_WHITESPACE, ( $index_token + 1 ), null, true ); |
| 91 | + if ( $this->tokens[ $maybe_close_bracket ]['code'] !== T_CLOSE_SQUARE_BRACKET ) { |
| 92 | + // Dynamic string, run regular checks. |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + // Constant string, check if it's allowed. |
| 97 | + $key = $this->strip_quotes( $this->tokens[ $index_token ]['content'] ); |
| 98 | + if ( ! in_array( $key, $this->allowedServerKeys, true ) ) { |
| 99 | + // Unsafe key, requires sanitising. |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + // Safe key, allow it. |
| 104 | + return true; |
| 105 | + } |
| 106 | +} |
0 commit comments