1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (!defined("__IN_SYMPHONY__")) { |
|
|
|
|
4
|
|
|
die("You cannot directly access this file."); |
|
|
|
|
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @package toolkit |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The `XSRF` class provides protection for mitigating XRSF/CSRF attacks. |
13
|
|
|
* |
14
|
|
|
* @since Symphony 2.4 |
15
|
|
|
* @author Rich Adams, http://richadams.me |
16
|
|
|
*/ |
17
|
|
|
class XSRF |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Return's the location of the XSRF tokens in the Session |
21
|
|
|
* |
22
|
|
|
* @return string|null |
23
|
|
|
*/ |
24
|
|
|
public static function getSessionToken() |
25
|
|
|
{ |
26
|
|
|
$token = null; |
27
|
|
|
|
28
|
|
|
if (isset($_SESSION[__SYM_COOKIE_PREFIX__]['xsrf-token'])) { |
|
|
|
|
29
|
|
|
$token = $_SESSION[__SYM_COOKIE_PREFIX__]['xsrf-token']; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (is_array($token)) { |
33
|
|
|
$token = key($token); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return is_null($token) ? null : $token; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Adds a token to the Session |
41
|
|
|
* |
42
|
|
|
* @param array $token |
43
|
|
|
*/ |
44
|
|
|
public static function setSessionToken($token = array()) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$_SESSION[__SYM_COOKIE_PREFIX__]['xsrf-token'] = $token; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Removes the token from the Session |
51
|
|
|
* |
52
|
|
|
* @param string $token |
53
|
|
|
*/ |
54
|
|
|
public static function removeSessionToken($token = null) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if (is_null($token)) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$_SESSION[__SYM_COOKIE_PREFIX__]['xsrf-token'] = null; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Generates nonce to a desired `$length` using `openssl` where available, |
65
|
|
|
* falling back to using `/dev/urandom` and a microtime implementation |
66
|
|
|
* otherwise |
67
|
|
|
* |
68
|
|
|
* @param integer $length optional. By default, 30. |
69
|
|
|
* @return string |
70
|
|
|
* base64 encoded, url safe |
71
|
|
|
*/ |
72
|
|
|
public static function generateNonce($length = 30) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$random = null; |
75
|
|
|
if ($length < 1) { |
76
|
|
|
throw new Exception('$length must be greater than 0'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Use the new PHP 7 random_bytes call, if available |
80
|
|
|
if (!$random && function_exists('random_bytes')) { |
|
|
|
|
81
|
|
|
$random = random_bytes($length); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Get some random binary data from open ssl, if available |
85
|
|
|
if (!$random && function_exists('openssl_random_pseudo_bytes')) { |
86
|
|
|
$random = openssl_random_pseudo_bytes($length); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Fallback to /dev/urandom |
90
|
|
|
if (!$random && is_readable('/dev/urandom')) { |
91
|
|
|
if (($handle = @fopen('/dev/urandom', 'rb')) !== false) { |
92
|
|
|
$random = @fread($handle, $length); |
93
|
|
|
@fclose($handle); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Fallback if no random bytes were found |
98
|
|
|
if (!$random) { |
99
|
|
|
$random = microtime(); |
100
|
|
|
|
101
|
|
|
for ($i = 0; $i < 1000; $i += $length) { |
102
|
|
|
$random = sha1(microtime() . $random); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Convert to base64 |
107
|
|
|
$random = base64_encode($random); |
108
|
|
|
|
109
|
|
|
// Replace unsafe chars |
110
|
|
|
$random = strtr($random, '+/', '-_'); |
111
|
|
|
$random = str_replace('=', '', $random); |
112
|
|
|
|
113
|
|
|
// Truncate the string to specified lengh |
114
|
|
|
$random = substr($random, 0, $length); |
115
|
|
|
|
116
|
|
|
return $random; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Creates the form input to use to house the token |
121
|
|
|
* |
122
|
|
|
* @return XMLElement |
123
|
|
|
*/ |
124
|
|
|
public static function formToken() |
125
|
|
|
{ |
126
|
|
|
// <input type="hidden" name="xsrf" value=" . self::getToken() . " /> |
127
|
|
|
$obj = new XMLElement("input"); |
|
|
|
|
128
|
|
|
$obj->setAttribute("type", "hidden"); |
|
|
|
|
129
|
|
|
$obj->setAttribute("name", "xsrf"); |
|
|
|
|
130
|
|
|
$obj->setAttribute("value", self::getToken()); |
|
|
|
|
131
|
|
|
return $obj; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* This is the nonce used to stop CSRF/XSRF attacks. It's stored in the user session. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public static function getToken() |
140
|
|
|
{ |
141
|
|
|
$token = self::getSessionToken(); |
142
|
|
|
if (is_null($token)) { |
143
|
|
|
$nonce = self::generateNonce(); |
144
|
|
|
self::setSessionToken($nonce); |
|
|
|
|
145
|
|
|
|
146
|
|
|
// Handle old tokens (< 2.6.0) |
147
|
|
|
} elseif (is_array($token)) { |
|
|
|
|
148
|
|
|
$nonce = key($token); |
149
|
|
|
self::setSessionToken($nonce); |
150
|
|
|
|
151
|
|
|
// New style tokens |
152
|
|
|
} else { |
153
|
|
|
$nonce = $token; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $nonce; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* This will determine if a token is valid. |
161
|
|
|
* |
162
|
|
|
* @param string $xsrf |
163
|
|
|
* The token to validate |
164
|
|
|
* @return boolean |
165
|
|
|
*/ |
166
|
|
|
public static function validateToken($xsrf) |
167
|
|
|
{ |
168
|
|
|
$token = self::getSessionToken(); |
169
|
|
|
|
170
|
|
|
return $token === $xsrf; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* This will validate a request has a good token. |
175
|
|
|
* |
176
|
|
|
* @throws SymphonyErrorPage |
177
|
|
|
* @param boolean $silent |
178
|
|
|
* If true, this function will return false if the request fails, |
179
|
|
|
* otherwise it will throw an Exception. By default this function |
180
|
|
|
* will thrown an exception if the request is invalid. |
181
|
|
|
* @return false|void |
182
|
|
|
*/ |
183
|
|
|
public static function validateRequest($silent = false) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
// Only care if we have a POST request. |
186
|
|
|
if (count($_POST) > 0) { |
187
|
|
|
if (!self::validateToken($_POST["xsrf"])) { |
|
|
|
|
188
|
|
|
// Token was invalid, show an error page. |
189
|
|
|
if (!$silent) { |
190
|
|
|
self::throwXSRFException(); |
191
|
|
|
} else { |
192
|
|
|
return false; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* The error function that's thrown if the token is invalid. |
200
|
|
|
* |
201
|
|
|
* @throws SymphonyErrorPage |
202
|
|
|
*/ |
203
|
|
|
public static function throwXSRFException() |
204
|
|
|
{ |
205
|
|
|
$msg = |
206
|
|
|
__('Request was rejected for having an invalid cross-site request forgery token.') |
207
|
|
|
. '<br/><br/>' . |
208
|
|
|
__('Please go back and try again.'); |
209
|
|
|
throw new SymphonyErrorPage($msg, __('Access Denied'), 'generic', array(), Page::HTTP_STATUS_FORBIDDEN); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return's the location of the XSRF tokens in the Session |
214
|
|
|
* |
215
|
|
|
* @deprecated This function will be removed in Symphony 3.0. Use |
216
|
|
|
* `getSessionToken()` instead. |
217
|
|
|
* @return string|null |
218
|
|
|
*/ |
219
|
|
|
public static function getSession() |
220
|
|
|
{ |
221
|
|
|
if (Symphony::Log()) { |
222
|
|
|
Symphony::Log()->pushDeprecateWarningToLog('XSRF::getSession()', 'XSRF::getSessionToken()'); |
223
|
|
|
} |
|
|
|
|
224
|
|
|
return self::getSessionToken(); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.