| [ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * This filter attempts to make HTML safe for viewing. IT IS NOT PERFECT. If 4 * you enable HTML viewing, you are opening a security hole. With the current 5 * state of the web, I believe that the best we can do is to make sure that 6 * people *KNOW* HTML is a security hole, clean up what we can, and leave it 7 * at that. 8 * 9 * $Horde: framework/Text_Filter/Filter/xss.php,v 1.1.2.3 2006/02/09 16:40:41 jan Exp $ 10 * 11 * Copyright 2004-2006 Jan Schneider <jan@horde.org> 12 * 13 * See the enclosed file COPYING for license information (LGPL). If you 14 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 15 * 16 * @author Jan Schneider <jan@horde.org> 17 * @since Horde 3.1 18 * @package Horde_Text 19 */ 20 class Text_Filter_xss extends Text_Filter { 21 22 /** 23 * Filter parameters. 24 * 25 * @var array 26 */ 27 var $_params = array('body_only' => true, 28 'replace' => 'XSSCleaned', 29 'strip_styles' => true); 30 31 /** 32 * Returns a hash with replace patterns. 33 * 34 * @return array Patterns hash. 35 */ 36 function getPatterns() 37 { 38 $patterns = array(); 39 40 /* Removes HTML comments (including some scripts & styles). */ 41 if ($this->_params['strip_styles']) { 42 $patterns['/<!--.*?-->/s'] = ''; 43 } 44 45 /* Change space entities to space characters. */ 46 $patterns['/&#(x0*20|0*32);?/i'] = ' '; 47 48 /* Nuke non-printable characters (a play in three acts). */ 49 50 /* Rule 1). Remove all control characters. */ 51 //$data = preg_replace('/[\x00-\x08\x0e-\x1f]/', '', $data); 52 53 /* Rule 1). If we have a semicolon, it is deterministically detectable 54 * and fixable, without introducing collateral damage. */ 55 $patterns['/&#x?0*([9A-D]|1[0-3]);/i'] = ' '; 56 57 /* Rule 2). Hex numbers (usually having an x prefix) are also 58 * deterministic, even if we don't have the semi. Note that some 59 * browsers will treat &#a or �a as a hex number even without the x 60 * prefix; hence /x?/ which will cover those cases in this rule. */ 61 $patterns['/&#x?0*[9A-D]([^0-9A-F]|$)/i'] = ' \\1'; 62 63 /* Rule 3). Decimal numbers without trailing semicolons. The problem 64 * is that some browsers will interpret 
a as "\na", some as 65 * "Ċ" so we have to clean the 
 to be safe for the "\na" case 66 * at the expense of mangling a valid entity in other cases. (Solution 67 * for valid HTML authors: always use the semicolon.) */ 68 $patterns['/�*(9|1[0-3])([^0-9]|$)/i'] = ' \\2'; 69 70 /* Remove overly long numeric entities. */ 71 $patterns['/&#x?0*[0-9A-F]{6,};?/i'] = ' '; 72 73 /* Remove everything outside of and including the <body> tag. */ 74 if ($this->_params['body_only']) { 75 $patterns['/.*<body[^>]*>/si'] = ''; 76 $patterns['/<\/body>.*/si'] = ''; 77 } 78 79 /* Get all attribute="javascript:foo()" tags. This is essentially the 80 * regex /(=|url\()("?)[^>]*script:/ but expanded to catch camouflage 81 * with spaces and entities. */ 82 $preg = '/((�*61;?|�*3D;?|=)|' . 83 '((u|�*85;?|�*55;?|�*117;?|�*75;?)\s*' . 84 '(r|�*82;?|�*52;?|�*114;?|�*72;?)\s*' . 85 '(l|�*76;?|�*4c;?|�*108;?|�*6c;?)\s*' . 86 '(\()))\s*' . 87 '(�*34;?|�*22;?|"|�*39;?|�*27;?|\')?' . 88 '[^>]*\s*' . 89 '(s|�*83;?|�*53;?|�*115;?|�*73;?)\s*' . 90 '(c|�*67;?|�*43;?|�*99;?|�*63;?)\s*' . 91 '(r|�*82;?|�*52;?|�*114;?|�*72;?)\s*' . 92 '(i|�*73;?|�*49;?|�*105;?|�*69;?)\s*' . 93 '(p|�*80;?|�*50;?|�*112;?|�*70;?)\s*' . 94 '(t|�*84;?|�*54;?|�*116;?|�*74;?)\s*' . 95 '(:|�*58;?|�*3a;?)/i'; 96 $patterns[$preg] = '\1\8' . $this->_params['replace']; 97 98 /* Get all on<foo>="bar()". NEVER allow these. */ 99 $patterns['/([\s"\']+' . 100 '(o|�*79;?|�*4f;?|�*111;?|�*6f;?)' . 101 '(n|�*78;?|�*4e;?|�*110;?|�*6e;?)' . 102 '\w+)\s*=/i'] = '\1' . $this->_params['replace'] . '='; 103 104 /* Remove all scripts since they might introduce garbage if they are 105 * not quoted properly. */ 106 $patterns['|<script[^>]*>.*?</script>|is'] = '<' . $this->_params['replace'] . '_script />'; 107 108 /* Get all tags that might cause trouble - <object>, <embed>, <base>, 109 * etc. Meta refreshes and iframes, too. */ 110 $malicious = array( 111 '/<([^>a-z]*)' . 112 '(s|�*83;?|�*53;?|�*115;?|�*73;?)\s*' . 113 '(c|�*67;?|�*43;?|�*99;?|�*63;?)\s*' . 114 '(r|�*82;?|�*52;?|�*114;?|�*72;?)\s*' . 115 '(i|�*73;?|�*49;?|�*105;?|�*69;?)\s*' . 116 '(p|�*80;?|�*50;?|�*112;?|�*70;?)\s*' . 117 '(t|�*84;?|�*54;?|�*116;?|�*74;?)\s*/i', 118 119 '/<([^>a-z]*)' . 120 '(e|�*69;?|�*45;?|�*101;?|�*65;?)\s*' . 121 '(m|�*77;?|�*4d;?|�*109;?|�*6d;?)\s*' . 122 '(b|�*66;?|�*42;?|�*98;?|�*62;?)\s*' . 123 '(e|�*69;?|�*45;?|�*101;?|�*65;?)\s*' . 124 '(d|�*68;?|�*44;?|�*100;?|�*64;?)\s*/i', 125 126 '/<([^>a-z]*)' . 127 '(x|�*88;?|�*58;?|�*120;?|�*78;?)\s*' . 128 '(m|�*77;?|�*4d;?|�*109;?|�*6d;?)\s*' . 129 '(l|�*76;?|�*4c;?|�*108;?|�*6c;?)\s*/i', 130 131 '/<([^>a-z]*)' . 132 '(b|�*66;?|�*42;?|�*98;?|�*62;?)\s*' . 133 '(a|�*65;?|�*41;?|�*97;?|�*61;?)\s*' . 134 '(s|�*83;?|�*53;?|�*115;?|�*73;?)\s*' . 135 '(e|�*69;?|�*45;?|�*101;?|�*65;?)\s*' . 136 '[^line]/i', 137 138 '/<([^>a-z]*)' . 139 '(m|�*77;?|�*4d;?|�*109;?|�*6d;?)\s*' . 140 '(e|�*69;?|�*45;?|�*101;?|�*65;?)\s*' . 141 '(t|�*84;?|�*54;?|�*116;?|�*74;?)\s*' . 142 '(a|�*65;?|�*41;?|�*97;?|�*61;?)\s*/i', 143 144 '/<([^>a-z]*)' . 145 '(j|�*74;?|�*4a;?|�*106;?|�*6a;?)\s*' . 146 '(a|�*65;?|�*41;?|�*97;?|�*61;?)\s*' . 147 '(v|�*86;?|�*56;?|�*118;?|�*76;?)\s*' . 148 '(a|�*65;?|�*41;?|�*97;?|�*61;?)\s*/i', 149 150 '/<([^>a-z]*)' . 151 '(o|�*79;?|�*4f;?|�*111;?|�*6f;?)\s*' . 152 '(b|�*66;?|�*42;?|�*98;?|�*62;?)\s*' . 153 '(j|�*74;?|�*4a;?|�*106;?|�*6a;?)\s*' . 154 '(e|�*69;?|�*45;?|�*101;?|�*65;?)\s*' . 155 '(c|�*67;?|�*43;?|�*99;?|�*63;?)\s*' . 156 '(t|�*84;?|�*54;?|�*116;?|�*74;?)\s*/i', 157 158 '/<([^>a-z]*)' . 159 '(i|�*73;?|�*49;?|�*105;?|�*69;?)\s*' . 160 '(f|�*70;?|�*46;?|�*102;?|�*66;?)\s*' . 161 '(r|�*82;?|�*52;?|�*114;?|�*72;?)\s*' . 162 '(a|�*65;?|�*41;?|�*97;?|�*61;?)\s*' . 163 '(m|�*77;?|�*4d;?|�*109;?|�*6d;?)\s*' . 164 '(e|�*69;?|�*45;?|�*101;?|�*65;?)\s*/i'); 165 166 foreach ($malicious as $pattern) { 167 $patterns[$pattern] = '<' . $this->_params['replace'] . '_tag'; 168 } 169 170 /* Comment out style/link tags. */ 171 if ($this->_params['strip_styles']) { 172 $patterns['/\s+style\s*=/i'] = ' ' . $this->_params['replace'] . '='; 173 $patterns['|<style[^>]*>(?:\s*<\!--)*|i'] = '<!--'; 174 $patterns['|(?:-->\s*)*</style>|i'] = '-->'; 175 $patterns['|(<link[^>]*>)|i'] = '<!-- $1 -->'; 176 } 177 178 /* A few other matches. */ 179 $patterns['|<([^>]*)&{.*}([^>]*)>|'] = '<&{;}\3>'; 180 $patterns['|<([^>]*)mocha:([^>]*)>|i'] = '<\1' . $this->_params['replace'] . ':\2>'; 181 $patterns['|<([^>]*)binding:([^>]*)>|i'] = '<\1' . $this->_params['replace'] . ':\2>'; 182 183 return array('regexp' => $patterns); 184 } 185 186 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |