Complex classes like Xss often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Xss, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Xss |
||
|
|
|||
| 20 | { |
||
| 21 | |||
| 22 | private $_xss_found = null; |
||
| 23 | private $neverAllowed; |
||
| 24 | private $exploded; |
||
| 25 | private $string; |
||
| 26 | private $attributes; |
||
| 27 | private $javascript; |
||
| 28 | private $html; |
||
| 29 | private $utf7; |
||
| 30 | private $utf8; |
||
| 31 | private $strings; |
||
| 32 | private $system; |
||
| 33 | private $input; |
||
| 34 | private $output; |
||
| 35 | |||
| 36 | |||
| 37 | 6 | function __construct() |
|
| 41 | |||
| 42 | 6 | function init() |
|
| 60 | |||
| 61 | |||
| 62 | function setString($str) |
||
| 66 | |||
| 67 | public function cleanArray($array) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param $str |
||
| 74 | * @return array|mixed |
||
| 75 | */ |
||
| 76 | 6 | public function clean($str) |
|
| 77 | { |
||
| 78 | // reset |
||
| 79 | 6 | $this->_xss_found = null; |
|
| 80 | |||
| 81 | // check for an array of strings |
||
| 82 | 6 | if (\is_array($str)) { |
|
| 83 | 2 | foreach ($str as $key => $value) { |
|
| 84 | 2 | $str[$key] = $this->clean($value); |
|
| 85 | } |
||
| 86 | 2 | return $str; |
|
| 87 | } |
||
| 88 | |||
| 89 | 6 | $this->input = $str; |
|
| 90 | 6 | $this->output = $this->process($str); |
|
| 91 | 6 | return $this->output; |
|
| 92 | } |
||
| 93 | |||
| 94 | 6 | private function process($str) |
|
| 95 | { |
||
| 96 | |||
| 97 | // process |
||
| 98 | do { |
||
| 99 | 6 | $old_str = $str; |
|
| 100 | 6 | $str = $this->_do($str); |
|
| 101 | 6 | } while ($old_str !== $str); |
|
| 102 | |||
| 103 | // keep the old value, if there wasn't any XSS attack |
||
| 104 | 6 | if ($this->_xss_found !== true) { |
|
| 105 | 6 | $str = $this->input; |
|
| 106 | } |
||
| 107 | |||
| 108 | 6 | return $str; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param StringResource $str |
||
| 113 | * |
||
| 114 | * @return mixed |
||
| 115 | */ |
||
| 116 | 6 | private function _do($str) |
|
| 117 | { |
||
| 118 | 6 | $str = (string)$str; |
|
| 119 | 6 | $strInt = (int)$str; |
|
| 120 | 6 | $strFloat = (float)$str; |
|
| 121 | 6 | if (!$str || (string)$strInt === $str || (string)$strFloat === $str) { |
|
| 122 | |||
| 123 | // no xss found |
||
| 124 | 6 | if ($this->_xss_found !== true) { |
|
| 125 | 6 | $this->_xss_found = false; |
|
| 126 | } |
||
| 127 | |||
| 128 | 6 | return $str; |
|
| 129 | } |
||
| 130 | |||
| 131 | // remove the BOM from UTF-8 / UTF-16 / UTF-32 strings |
||
| 132 | 6 | $str = $this->utf8->remove_bom($str); |
|
| 133 | |||
| 134 | // replace the diamond question mark (�) and invalid-UTF8 chars |
||
| 135 | 6 | $str = $this->utf8->replace_diamond_question_mark($str, ''); |
|
| 136 | |||
| 137 | // replace invisible characters with one single space |
||
| 138 | 6 | $str = $this->utf8->remove_invisible_characters($str, true, ' '); |
|
| 139 | |||
| 140 | 6 | $str = $this->utf8->normalize_whitespace($str); |
|
| 141 | 6 | $str = $this->strings->replace($str); |
|
| 142 | |||
| 143 | // decode UTF-7 characters |
||
| 144 | 6 | $str = $this->utf7->repack($str); |
|
| 145 | |||
| 146 | // decode the string |
||
| 147 | 6 | $str = $this->decodeString($str); // RW Partly DONE |
|
| 148 | |||
| 149 | // backup the string (for later comparision) |
||
| 150 | 6 | $str_backup = $str; |
|
| 151 | |||
| 152 | // remove strings that are never allowed |
||
| 153 | 6 | $str = $this->neverAllowed->doNeverAllowed($str); //RW DONE |
|
| 154 | |||
| 155 | // corrects words before the browser will do it |
||
| 156 | 6 | $str = $this->exploded->compactExplodedString($str); //RW DONE |
|
| 157 | |||
| 158 | // remove disallowed javascript calls in links, images etc. |
||
| 159 | 6 | $str = $this->javascript->removeDisallowedJavascript($str); |
|
| 160 | |||
| 161 | // remove evil attributes such as style, onclick and xmlns |
||
| 162 | 6 | $str = $this->attributes->removeEvilAttributes($str); |
|
| 163 | |||
| 164 | // sanitize naughty JavaScript elements |
||
| 165 | 6 | $str = $this->javascript->naughtyJavascript($str); |
|
| 166 | |||
| 167 | // sanitize naughty HTML elements |
||
| 168 | 6 | $str = $this->html->naughtyHtml($str); |
|
| 169 | |||
| 170 | // final clean up |
||
| 171 | // |
||
| 172 | // -> This adds a bit of extra precaution in case something got through the above filters. |
||
| 173 | 6 | $str = $this->neverAllowed->doNeverAllowedAfterwards($str); |
|
| 174 | |||
| 175 | // check for xss |
||
| 176 | 6 | if ($this->_xss_found !== true) { |
|
| 177 | 6 | $this->_xss_found = !($str_backup === $str); |
|
| 178 | } |
||
| 179 | |||
| 180 | 6 | return $str; |
|
| 181 | } |
||
| 182 | |||
| 183 | 6 | public function decodeString($str) |
|
| 184 | { |
||
| 185 | // init |
||
| 186 | 6 | $regExForHtmlTags = '/<\p{L}+.*+/us'; |
|
| 187 | |||
| 188 | 6 | if (strpos($str, '<') !== false && preg_match($regExForHtmlTags, $str, $matches) === 1) { |
|
| 189 | 6 | $str = (string)preg_replace_callback( |
|
| 190 | 6 | $regExForHtmlTags, |
|
| 191 | function ($matches) { |
||
| 192 | 6 | return $this->decodeEntity($matches); |
|
| 193 | 6 | }, |
|
| 194 | 6 | $str |
|
| 195 | ); |
||
| 196 | } else { |
||
| 197 | 6 | $str = $this->utf8->rawurldecode($str); |
|
| 198 | } |
||
| 199 | |||
| 200 | 6 | return $str; |
|
| 201 | } |
||
| 202 | |||
| 203 | 6 | private function decodeEntity(array $match) |
|
| 204 | { |
||
| 205 | // init |
||
| 206 | 6 | $str = $match[0]; |
|
| 207 | |||
| 208 | // protect GET variables without XSS in URLs |
||
| 209 | 6 | if (preg_match_all("/[\?|&]?[\\p{L}0-9_\-\[\]]+\s*=\s*(?<wrapped>\"|\042|'|\047)(?<attr>[^\\1]*?)\\g{wrapped}/ui", $str, $matches)) { |
|
| 210 | 6 | if (isset($matches['attr'])) { |
|
| 211 | 6 | foreach ($matches['attr'] as $matchInner) { |
|
| 212 | 6 | $tmpAntiXss = clone $this; |
|
| 213 | 6 | $urlPartClean = $tmpAntiXss->clean($matchInner); |
|
| 214 | |||
| 215 | 6 | if ($tmpAntiXss->isXssFound() === true) { |
|
| 216 | 6 | $this->_xss_found = true; |
|
| 217 | 6 | $str = \str_replace($matchInner, $this->utf8->rawurldecode($urlPartClean), $str); |
|
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } else { |
||
| 222 | 6 | $str = $this->_entity_decode($this->utf8->rawurldecode($str)); |
|
| 223 | } |
||
| 224 | |||
| 225 | 6 | return $str; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return null |
||
| 230 | */ |
||
| 231 | 6 | public function isXssFound() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Entity-decoding. |
||
| 238 | * |
||
| 239 | * @param StringResource $str |
||
| 240 | * |
||
| 241 | * @return StringResource |
||
| 242 | */ |
||
| 243 | 6 | private function _entity_decode($str) |
|
| 244 | { |
||
| 245 | 6 | static $HTML_ENTITIES_CACHE; |
|
| 246 | |||
| 247 | 6 | $flags = ENT_QUOTES | ENT_HTML5 | ENT_DISALLOWED | ENT_SUBSTITUTE; |
|
| 248 | |||
| 249 | // decode |
||
| 250 | 6 | $str = html_entity_decode($str, $flags); |
|
| 251 | |||
| 252 | |||
| 253 | // decode-again, for e.g. HHVM or miss configured applications ... |
||
| 254 | 6 | if (preg_match_all('/(?<html_entity>&[A-Za-z]{2,}[;]{0})/', $str, $matches)) { |
|
| 255 | if ($HTML_ENTITIES_CACHE === null) { |
||
| 256 | |||
| 257 | // links: |
||
| 258 | // - http://dev.w3.org/html5/html-author/charref |
||
| 259 | // - http://www.w3schools.com/charsets/ref_html_entities_n.asp |
||
| 260 | $entitiesSecurity = [ |
||
| 261 | '�' => '', |
||
| 262 | '�' => '', |
||
| 263 | '' => '', |
||
| 264 | '' => '', |
||
| 265 | '>⃒' => '', |
||
| 266 | '' => '', |
||
| 267 | '' => '', |
||
| 268 | '­' => '', |
||
| 269 | '­' => '', |
||
| 270 | '­' => '', |
||
| 271 | ':' => ':', |
||
| 272 | ':' => ':', |
||
| 273 | ':' => ':', |
||
| 274 | '(' => '(', |
||
| 275 | '(' => '(', |
||
| 276 | '(' => '(', |
||
| 277 | ')' => ')', |
||
| 278 | ')' => ')', |
||
| 279 | ')' => ')', |
||
| 280 | '?' => '?', |
||
| 281 | '?' => '?', |
||
| 282 | '?' => '?', |
||
| 283 | '/' => '/', |
||
| 284 | '/' => '/', |
||
| 285 | '/' => '/', |
||
| 286 | ''' => '\'', |
||
| 287 | ''' => '\'', |
||
| 288 | ''' => '\'', |
||
| 289 | ''' => '\'', |
||
| 290 | ''' => '\'', |
||
| 291 | '\' => '\'', |
||
| 292 | '\' => '\\', |
||
| 293 | '\' => '\\', |
||
| 294 | ',' => ',', |
||
| 295 | ',' => ',', |
||
| 296 | ',' => ',', |
||
| 297 | '.' => '.', |
||
| 298 | '.' => '.', |
||
| 299 | '"' => '"', |
||
| 300 | '"' => '"', |
||
| 301 | '"' => '"', |
||
| 302 | '"' => '"', |
||
| 303 | '`' => '`', |
||
| 304 | '`' => '`', |
||
| 305 | '`' => '`', |
||
| 306 | '`' => '`', |
||
| 307 | '.' => '.', |
||
| 308 | '=' => '=', |
||
| 309 | '=' => '=', |
||
| 310 | '=' => '=', |
||
| 311 | '&newline;' => "\n", |
||
| 312 | '
' => "\n", |
||
| 313 | ' ' => "\n", |
||
| 314 | '&tab;' => "\t", |
||
| 315 | '	' => "\t", |
||
| 316 | '	' => "\t", |
||
| 317 | ]; |
||
| 318 | |||
| 319 | $HTML_ENTITIES_CACHE = \array_merge( |
||
| 320 | $entitiesSecurity, |
||
| 321 | \array_flip(\get_html_translation_table(HTML_ENTITIES, $flags)), |
||
| 322 | \array_flip($this->_get_data('entities_fallback')) |
||
| 323 | ); |
||
| 324 | } |
||
| 325 | |||
| 326 | $search = []; |
||
| 327 | $replace = []; |
||
| 328 | foreach ($matches['html_entity'] as $match) { |
||
| 329 | $match .= ';'; |
||
| 330 | if (isset($HTML_ENTITIES_CACHE[$match])) { |
||
| 331 | $search[$match] = $match; |
||
| 332 | $replace[$match] = $HTML_ENTITIES_CACHE[$match]; |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | if (\count($replace) > 0) { |
||
| 337 | $str = \str_replace($search, $replace, $str); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | 6 | return $str; |
|
| 342 | } |
||
| 343 | |||
| 344 | private function _get_data($file) |
||
| 349 | |||
| 350 | public function cleanString($str) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param $str |
||
| 357 | * @return array|mixed |
||
| 358 | */ |
||
| 359 | 3 | public function cleanUrl($str) |
|
| 360 | { |
||
| 384 | |||
| 385 | function result() |
||
| 393 | } |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.