| Conditions | 8 |
| Paths | 9 |
| Total Lines | 57 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 71 | public static function filterRequestUri($uri = null, $exit = true) |
||
| 72 | { |
||
| 73 | if (!isset($uri)) { |
||
| 74 | |||
| 75 | if (!isset($_SERVER['REQUEST_URI'])) { |
||
| 76 | return false; |
||
| 77 | } |
||
| 78 | |||
| 79 | $uri = $_SERVER['REQUEST_URI']; |
||
| 80 | } |
||
| 81 | |||
| 82 | $uriOrig = $uri; |
||
| 83 | |||
| 84 | // |
||
| 85 | // Ensures the URL is well formed UTF-8 |
||
| 86 | // |
||
| 87 | |||
| 88 | if (preg_match('//u', urldecode($uri))) { |
||
| 89 | return $uri; |
||
| 90 | } |
||
| 91 | |||
| 92 | // |
||
| 93 | // When not, assumes Windows-1252 and redirects to the corresponding UTF-8 encoded URL |
||
| 94 | // |
||
| 95 | |||
| 96 | $uri = preg_replace_callback( |
||
| 97 | '/[\x80-\xFF]+/', |
||
| 98 | function ($m) { |
||
| 99 | return urlencode($m[0]); |
||
| 100 | }, |
||
| 101 | $uri |
||
| 102 | ); |
||
| 103 | |||
| 104 | $uri = preg_replace_callback( |
||
| 105 | '/(?:%[89A-F][0-9A-F])+/i', |
||
| 106 | function ($m) { |
||
| 107 | return urlencode(UTF8::encode('UTF-8', urldecode($m[0]))); |
||
| 108 | }, |
||
| 109 | $uri |
||
| 110 | ); |
||
| 111 | |||
| 112 | if ( |
||
| 113 | $uri !== $uriOrig |
||
| 114 | && |
||
| 115 | $exit === true |
||
| 116 | && |
||
| 117 | headers_sent() === false |
||
| 118 | ) { |
||
| 119 | // Use ob_start() to buffer content and avoid problem of headers already sent... |
||
| 120 | $severProtocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'); |
||
| 121 | header($severProtocol . ' 301 Moved Permanently'); |
||
| 122 | header('Location: ' . $uri); |
||
|
|
|||
| 123 | exit(); |
||
| 124 | } |
||
| 125 | |||
| 126 | return $uri; |
||
| 127 | } |
||
| 128 | |||
| 196 | } |
'Location: ' . $urican contain request data and is used in response header context(s) leading to a potential security vulnerability.1 path for user data to reach this point
REQUEST_URIfrom$_SERVER,and$uriis assignedin includes/libraries/protect/AntiXSS/bootup.php on line 79
$uriis passed through preg_replace_callback()in includes/libraries/protect/AntiXSS/bootup.php on line 101
$uriis assignedin includes/libraries/protect/AntiXSS/bootup.php on line 96
$uriis passed through preg_replace_callback()in includes/libraries/protect/AntiXSS/bootup.php on line 109
$uriis assignedin includes/libraries/protect/AntiXSS/bootup.php on line 104
Response Splitting Attacks
Allowing an attacker to set a response header, opens your application to response splitting attacks; effectively allowing an attacker to send any response, he would like.
General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: