|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scelus\Escaper; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* All functions are based on the recommendations in the |
|
7
|
|
|
* XSS (Cross Site Scripting) Prevention Cheat Sheet: |
|
8
|
|
|
* https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
class CEscaper |
|
12
|
|
|
{ |
|
13
|
|
|
private $CHARSET; |
|
14
|
|
|
|
|
15
|
9 |
|
public function __construct($encoding = 'UTF-8') { |
|
|
|
|
|
|
16
|
9 |
|
isset($_SESSION['escaper_charset']) |
|
17
|
9 |
|
? $this->CHARSET = $_SESSION['escaper_charset'] |
|
18
|
9 |
|
: $_SESSION['escaper_charset'] = $encoding; |
|
19
|
|
|
|
|
20
|
9 |
|
$this->CHARSET = $_SESSION['escaper_charset']; |
|
21
|
9 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** Sets the used charset for the esacpeHTML and escapeXML function. |
|
24
|
|
|
* |
|
25
|
|
|
* @param $value, the string/value to escape. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function setEncoding($value) { |
|
|
|
|
|
|
29
|
1 |
|
$_SESSION['escaper_charset'] = strip_tags($value); |
|
30
|
1 |
|
$this->CHARSET = $_SESSION['escaper_charset']; |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** Returns the used charset for the esacpeHTML and escapeXML function. |
|
34
|
|
|
* |
|
35
|
|
|
* @return the current charset as a string. |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function getEncoding() { |
|
38
|
2 |
|
return $this->CHARSET; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** Escapes HTML string using htmlspecialchars(). |
|
42
|
|
|
* |
|
43
|
|
|
* @param $string, the untrusted string to escape. |
|
44
|
|
|
* |
|
45
|
|
|
* @return $result, escaped string. |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function escapeHTML($value) { |
|
48
|
2 |
|
$result = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $this->CHARSET); |
|
49
|
2 |
|
$result = preg_replace('/[\/]/', '/', $result); |
|
50
|
|
|
|
|
51
|
2 |
|
return $result; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** Escapes non-alphanumeric characters in an untrusted string for HTML attribute values. |
|
55
|
|
|
* |
|
56
|
|
|
* @param $string, the untrusted string to escape. |
|
57
|
|
|
* |
|
58
|
|
|
* @return $result, escaped string. |
|
|
|
|
|
|
59
|
|
|
*/ |
|
60
|
2 |
View Code Duplication |
public function escapeHTMLattr($value) { |
|
|
|
|
|
|
61
|
|
|
$result = preg_replace_callback("/[\W]/", function($matches) { |
|
62
|
2 |
|
return "&#x" . bin2hex($matches[0]) . ";"; |
|
63
|
2 |
|
}, |
|
64
|
2 |
|
$value); |
|
65
|
2 |
|
return $result; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** Escapes non-alphanumeric characters in an untrusted string for JS input values. |
|
69
|
|
|
* |
|
70
|
|
|
* @param $string, the untrusted string to escape. |
|
71
|
|
|
* |
|
72
|
|
|
* @return $result, escaped string. |
|
|
|
|
|
|
73
|
|
|
*/ |
|
74
|
1 |
View Code Duplication |
public function escapeJs($value) { |
|
|
|
|
|
|
75
|
|
|
$result = preg_replace_callback("/[\W]/", function($matches) { |
|
76
|
1 |
|
return "\\x" . bin2hex($matches[0]); |
|
77
|
1 |
|
}, |
|
78
|
1 |
|
$value); |
|
79
|
|
|
|
|
80
|
1 |
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** Escapes non-alphanumeric characters in an untrusted string for CSS input values. |
|
84
|
|
|
* |
|
85
|
|
|
* @param $string, the untrusted string to escape. |
|
86
|
|
|
* |
|
87
|
|
|
* @return $result, escaped string. |
|
|
|
|
|
|
88
|
|
|
*/ |
|
89
|
|
View Code Duplication |
public function escapeCSS($value) { |
|
|
|
|
|
|
90
|
1 |
|
$result = preg_replace_callback("/[\W]/", function($matches) { |
|
91
|
1 |
|
return "\\" . bin2hex($matches[0]) . " "; |
|
92
|
1 |
|
}, |
|
93
|
1 |
|
$value); |
|
94
|
|
|
|
|
95
|
1 |
|
return $result; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** Escapes data that is to be inserted in a URL not the whole URL itself. |
|
99
|
|
|
* |
|
100
|
|
|
* @param $string, the untrusted string to escape. |
|
101
|
|
|
* |
|
102
|
|
|
* @return, escaped string. |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function escapeUrl($value) { |
|
105
|
1 |
|
return rawurlencode($value); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Aliases to HTML functions for semantic value. |
|
110
|
|
|
* XML escaping is identical to HTML escaping. |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function escapeXml($value) { |
|
113
|
1 |
|
return $this->escapeHTML($value); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
public function escapeXmlAttr($value) { |
|
117
|
1 |
|
return $this->escapeHTMLattr($value); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: