Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class Security |
||
13 | { |
||
14 | |||
15 | |||
16 | /** |
||
17 | * Random Hash for protecting URLs |
||
18 | * |
||
19 | * @var string |
||
20 | * @access protected |
||
21 | */ |
||
22 | protected $_xss_hash = ''; |
||
23 | |||
24 | /** |
||
25 | * List of never allowed strings |
||
26 | * |
||
27 | * @var array |
||
28 | * @access protected |
||
29 | */ |
||
30 | protected $_never_allowed_str = array( |
||
31 | 'document.cookie' => '[removed]', |
||
32 | 'document.write' => '[removed]', |
||
33 | '.parentNode' => '[removed]', |
||
34 | '.innerHTML' => '[removed]', |
||
35 | 'window.location' => '[removed]', |
||
36 | '-moz-binding' => '[removed]', |
||
37 | '<!--' => '<!--', |
||
38 | '-->' => '-->', |
||
39 | '<![CDATA[' => '<![CDATA[', |
||
40 | '<comment>' => '<comment>' |
||
41 | ); |
||
42 | |||
43 | /* never allowed, regex replacement */ |
||
44 | /** |
||
45 | * List of never allowed regex replacement |
||
46 | * |
||
47 | * @var array |
||
48 | * @access protected |
||
49 | */ |
||
50 | protected $_never_allowed_regex = array( |
||
51 | 'javascript\s*:', |
||
52 | 'expression\s*(\(|&\#40;)', // CSS and IE |
||
53 | 'vbscript\s*:', // IE, surprise! |
||
54 | 'Redirect\s+302', |
||
55 | "([\"'])?data\s*:[^\\1]*?base64[^\\1]*?,[^\\1]*?\\1?" |
||
56 | ); |
||
57 | |||
58 | |||
59 | /** |
||
60 | * XSS Clean |
||
61 | * |
||
62 | * Sanitizes data so that Cross Site Scripting Hacks can be |
||
63 | * prevented. This function does a fair amount of work but |
||
64 | * it is extremely thorough, designed to prevent even the |
||
65 | * most obscure XSS attempts. Nothing is ever 100% foolproof, |
||
66 | * of course, but I haven't been able to get anything passed |
||
67 | * the filter. |
||
68 | * |
||
69 | * Note: This function should only be used to deal with data |
||
70 | * upon submission. It's not something that should |
||
71 | * be used for general runtime processing. |
||
72 | * |
||
73 | * This function was based in part on some code and ideas I |
||
74 | * got from Bitflux: http://channel.bitflux.ch/wiki/XSS_Prevention |
||
75 | * |
||
76 | * To help develop this script I used this great list of |
||
77 | * vulnerabilities along with a few other hacks I've |
||
78 | * harvested from examining vulnerabilities in other programs: |
||
79 | * http://ha.ckers.org/xss.html |
||
80 | * |
||
81 | * @param mixed string or array |
||
82 | * @return string |
||
83 | */ |
||
84 | public function xss_clean($str, $is_image = FALSE, $evilAttribute = TRUE) |
||
286 | |||
287 | // -------------------------------------------------------------------- |
||
288 | |||
289 | /* |
||
290 | * Remove Evil HTML Attributes (like evenhandlers and style) |
||
291 | * |
||
292 | * It removes the evil attribute and either: |
||
293 | * - Everything up until a space |
||
294 | * For example, everything between the pipes: |
||
295 | * <a |style=document.write('hello');alert('world');| class=link> |
||
296 | * - Everything inside the quotes |
||
297 | * For example, everything between the pipes: |
||
298 | * <a |style="document.write('hello'); alert('world');"| class="link"> |
||
299 | * |
||
300 | * @param string $str The string to check |
||
301 | * @param boolean $is_image TRUE if this is an image |
||
302 | * @return string The string with the evil attributes removed |
||
303 | */ |
||
304 | protected function _remove_evil_attributes($str, $is_image, $evilAttribute) |
||
336 | |||
337 | |||
338 | |||
339 | // -------------------------------------------------------------------- |
||
340 | |||
341 | /** |
||
342 | * HTML Entities Decode |
||
343 | * |
||
344 | * This function is a replacement for html_entity_decode() |
||
345 | * |
||
346 | * The reason we are not using html_entity_decode() by itself is because |
||
347 | * while it is not technically correct to leave out the semicolon |
||
348 | * at the end of an entity most browsers will still interpret the entity |
||
349 | * correctly. html_entity_decode() does not convert entities without |
||
350 | * semicolons, so we are left with our own little solution here. Bummer. |
||
351 | * |
||
352 | * @param string |
||
353 | * @param string |
||
354 | * @return string |
||
355 | */ |
||
356 | public function entity_decode($str, $charset='UTF-8') |
||
367 | |||
368 | // -------------------------------------------------------------------- |
||
369 | |||
370 | /** |
||
371 | * Filename Security |
||
372 | * |
||
373 | * @param string |
||
374 | * @param bool |
||
375 | * @return string |
||
376 | */ |
||
377 | public function sanitize_filename($str, $relative_path = FALSE) |
||
422 | |||
423 | // ---------------------------------------------------------------- |
||
424 | |||
425 | /** |
||
426 | * Compact Exploded Words |
||
427 | * |
||
428 | * Callback function for xss_clean() to remove whitespace from |
||
429 | * things like j a v a s c r i p t |
||
430 | * |
||
431 | * @param type |
||
432 | * @return type |
||
433 | */ |
||
434 | protected function _compact_exploded_words($matches) |
||
438 | |||
439 | // -------------------------------------------------------------------- |
||
440 | |||
441 | /** |
||
442 | * Sanitize Naughty HTML |
||
443 | * |
||
444 | * Callback function for xss_clean() to remove naughty HTML elements |
||
445 | * |
||
446 | * @param array |
||
447 | * @return string |
||
448 | */ |
||
449 | protected function _sanitize_naughty_html($matches) |
||
460 | |||
461 | // -------------------------------------------------------------------- |
||
462 | |||
463 | /** |
||
464 | * JS Link Removal |
||
465 | * |
||
466 | * Callback function for xss_clean() to sanitize links |
||
467 | * This limits the PCRE backtracks, making it more performance friendly |
||
468 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
||
469 | * PHP 5.2+ on link-heavy strings |
||
470 | * |
||
471 | * @param array |
||
472 | * @return string |
||
473 | */ |
||
474 | View Code Duplication | protected function _js_link_removal($match) |
|
486 | |||
487 | // -------------------------------------------------------------------- |
||
488 | |||
489 | /** |
||
490 | * JS Image Removal |
||
491 | * |
||
492 | * Callback function for xss_clean() to sanitize image tags |
||
493 | * This limits the PCRE backtracks, making it more performance friendly |
||
494 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
||
495 | * PHP 5.2+ on image tag heavy strings |
||
496 | * |
||
497 | * @param array |
||
498 | * @return string |
||
499 | */ |
||
500 | View Code Duplication | protected function _js_img_removal($match) |
|
512 | |||
513 | // -------------------------------------------------------------------- |
||
514 | |||
515 | /** |
||
516 | * Attribute Conversion |
||
517 | * |
||
518 | * Used as a callback for XSS Clean |
||
519 | * |
||
520 | * @param array |
||
521 | * @return string |
||
522 | */ |
||
523 | protected function _convert_attribute($match) |
||
527 | |||
528 | // -------------------------------------------------------------------- |
||
529 | |||
530 | /** |
||
531 | * Filter Attributes |
||
532 | * |
||
533 | * Filters tag attributes for consistency and safety |
||
534 | * |
||
535 | * @param string |
||
536 | * @return string |
||
537 | */ |
||
538 | protected function _filter_attributes($str) |
||
552 | |||
553 | // -------------------------------------------------------------------- |
||
554 | |||
555 | /** |
||
556 | * HTML Entity Decode Callback |
||
557 | * |
||
558 | * Used as a callback for XSS Clean |
||
559 | * |
||
560 | * @param array |
||
561 | * @return string |
||
562 | */ |
||
563 | protected function _decode_entity($match) |
||
567 | |||
568 | // -------------------------------------------------------------------- |
||
569 | |||
570 | /** |
||
571 | * Validate URL entities |
||
572 | * |
||
573 | * Called by xss_clean() |
||
574 | * |
||
575 | * @param string |
||
576 | * @return string |
||
577 | */ |
||
578 | protected function _validate_entities($str) |
||
608 | |||
609 | // ---------------------------------------------------------------------- |
||
610 | |||
611 | /** |
||
612 | * Do Never Allowed |
||
613 | * |
||
614 | * A utility function for xss_clean() |
||
615 | * |
||
616 | * @param string |
||
617 | * @return string |
||
618 | */ |
||
619 | protected function _do_never_allowed($str) |
||
630 | |||
631 | |||
632 | protected function remove_invisible_characters($str, $url_encoded = TRUE) |
||
655 | |||
656 | /** |
||
657 | * Random Hash for protecting URLs |
||
658 | * |
||
659 | * @return string |
||
660 | */ |
||
661 | public function xss_hash() |
||
671 | |||
672 | |||
673 | } |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.