1 | <?php |
||
24 | final class noxss |
||
25 | { |
||
26 | /** |
||
27 | * @var (bool) A flag to indicate if there might be an XSS attack going on |
||
28 | */ |
||
29 | public static $potentialXSS = false; |
||
30 | |||
31 | /** |
||
32 | * @var (array) A container for inputs potentially containing XSS attacks |
||
33 | */ |
||
34 | public static $xss; |
||
35 | |||
36 | /** |
||
37 | * @var (string) buffered output caught by prevent. |
||
38 | */ |
||
39 | public static $output; |
||
40 | |||
41 | /** |
||
42 | * @var (string) Regular expression that matches any input containing quotes, tag start or end delimiters or &. |
||
43 | * I don't know any XSS attack that doesn't require at least one of these characters. |
||
44 | */ |
||
45 | public static $reXSS = '/[\'"<>&]/'; |
||
46 | |||
47 | /** |
||
48 | * @var (array) A list of _SERVER variables sent by client header and thus potential attack vectors, can be set |
||
49 | * by user when needed / used. |
||
50 | */ |
||
51 | public static $xssHeaders = [ 'PHP_AUTH_USER', 'PHP_AUTH_PW' ]; |
||
52 | |||
53 | /** |
||
54 | * @var (int) Minimum length of an input to qualify as a potential XSS attack. |
||
55 | */ |
||
56 | public static $minimumLength = 10; |
||
57 | |||
58 | /** |
||
59 | * @var (array) A list of inputs to ignore, keyed to the input method - GET, POST, COOKIE, SERVER |
||
60 | */ |
||
61 | public static $ignoreList = []; |
||
62 | |||
63 | /** |
||
64 | * This method checks all user inputs ( get/post/cookie variables, client sent headers ) for potential XSS attacks |
||
65 | * If found it flags these and sets self::$potentialXSS to true and starts an output buffer |
||
66 | */ |
||
67 | 1 | public static function detect() |
|
87 | |||
88 | 1 | private static function _gatherXSSInput($input, $method, $name = null) |
|
108 | |||
109 | /** |
||
110 | * This method checks if self::$potentialXSS to see if an XSS attack might be going on. If so |
||
111 | * the output buffer is ended and the output content retrieved. All inputs flagged as potential XSS attacks |
||
112 | * are checked to see if any of these is in the output content _in_unchanged_form_ ! |
||
113 | * If so, there is a vulnerability to XSS which is being exploited ( or at least triggered ) and the only |
||
114 | * safe option is to not sent the output but sent a 400 Bad Request header instead. |
||
115 | * This method doesn't actually send this header but it does throw an exception allowing you to handle it |
||
116 | * any way you see fit |
||
117 | * |
||
118 | * @param callable $f (optional) A method to call when a potential xss attack is detected. Takes one argument: the output |
||
119 | * generated by this request so far. If not set prevent() will just sent a 400 Bad Request header if a potential xss attack |
||
120 | * is detected. |
||
121 | */ |
||
122 | 1 | public static function prevent($f = null) |
|
141 | |||
142 | 1 | private static function _checkForProblems() |
|
162 | |||
163 | public static function ignore($name, $method = 'GET') |
||
167 | } |
||
168 |