Project

General

Profile

Download (14 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/**
4
 * @file
5
 *
6
 * csrf-magic is a PHP library that makes adding CSRF-protection to your
7
 * web applications a snap. No need to modify every form or create a database
8
 * of valid nonces; just include this file at the top of every
9
 * web-accessible page (or even better, your common include file included
10
 * in every page), and forget about it! (There are, of course, configuration
11
 * options for advanced users).
12
 *
13
 * This library is PHP4 and PHP5 compatible.
14
 */
15

    
16
include_once('phpsessionmanager.inc');
17

    
18
if (!is_array($GLOBALS['csrf'])) {
19
	$GLOBALS['csrf'] = [];
20
}
21

    
22
// CONFIGURATION:
23

    
24
/**
25
 * By default, when you include this file csrf-magic will automatically check
26
 * and exit if the CSRF token is invalid. This will defer executing
27
 * csrf_check() until you're ready.  You can also pass false as a parameter to
28
 * that function, in which case the function will not exit but instead return
29
 * a boolean false if the CSRF check failed. This allows for tighter integration
30
 * with your system.
31
 */
32
$GLOBALS['csrf']['defer'] = false;
33

    
34
/**
35
 * This is the amount of seconds you wish to allow before any token becomes
36
 * invalid; the default is two hours, which should be more than enough for
37
 * most websites.
38
 */
39
$GLOBALS['csrf']['expires'] = 7200;
40

    
41
/**
42
 * Callback function to execute when there's the CSRF check fails and
43
 * $fatal == true (see csrf_check). This will usually output an error message
44
 * about the failure.
45
 */
46
$GLOBALS['csrf']['callback'] = 'csrf_callback';
47

    
48
/**
49
 * Whether or not to include our JavaScript library which also rewrites
50
 * AJAX requests on this domain. Set this to the web path. This setting only works
51
 * with supported JavaScript libraries in Internet Explorer; see README.txt for
52
 * a list of supported libraries.
53
 */
54
$GLOBALS['csrf']['rewrite-js'] = false;
55

    
56
/**
57
 * A secret key used when hashing items. Please generate a random string and
58
 * place it here. If you change this value, all previously generated tokens
59
 * will become invalid.
60
 */
61
$GLOBALS['csrf']['secret'] = '';
62
// nota bene: library code should use csrf_get_secret() and not access
63
// this global directly
64

    
65
/**
66
 * Set this to false to disable csrf-magic's output handler, and therefore,
67
 * its rewriting capabilities. If you're serving non HTML content, you should
68
 * definitely set this false.
69
 */
70
$GLOBALS['csrf']['rewrite'] = true;
71

    
72
/**
73
 * Whether or not to use IP addresses when binding a user to a token. This is
74
 * less reliable and less secure than sessions, but is useful when you need
75
 * to give facilities to anonymous users and do not wish to maintain a database
76
 * of valid keys.
77
 */
78
$GLOBALS['csrf']['allow-ip'] = true;
79

    
80
/**
81
 * If this information is available, use the cookie by this name to determine
82
 * whether or not to allow the request. This is a shortcut implementation
83
 * very similar to 'key', but we randomly set the cookie ourselves.
84
 */
85
$GLOBALS['csrf']['cookie'] = '__csrf_cookie';
86

    
87
/**
88
 * If this information is available, set this to a unique identifier (it
89
 * can be an integer or a unique username) for the current "user" of this
90
 * application. The token will then be globally valid for all of that user's
91
 * operations, but no one else. This requires that 'secret' be set.
92
 */
93
$GLOBALS['csrf']['user'] = false;
94

    
95
/**
96
 * This is an arbitrary secret value associated with the user's session. This
97
 * will most probably be the contents of a cookie, as an attacker cannot easily
98
 * determine this information. Warning: If the attacker knows this value, they
99
 * can easily spoof a token. This is a generic implementation; sessions should
100
 * work in most cases.
101
 *
102
 * Why would you want to use this? Lets suppose you have a squid cache for your
103
 * website, and the presence of a session cookie bypasses it. Let's also say
104
 * you allow anonymous users to interact with the website; submitting forms
105
 * and AJAX. Previously, you didn't have any CSRF protection for anonymous users
106
 * and so they never got sessions; you don't want to start using sessions either,
107
 * otherwise you'll bypass the Squid cache. Setup a different cookie for CSRF
108
 * tokens, and have Squid ignore that cookie for get requests, for anonymous
109
 * users. (If you haven't guessed, this scheme was(?) used for MediaWiki).
110
 */
111
$GLOBALS['csrf']['key'] = false;
112

    
113
/**
114
 * The name of the magic CSRF token that will be placed in all forms, i.e.
115
 * the contents of <input type="hidden" name="$name" value="CSRF-TOKEN" />
116
 */
117
$GLOBALS['csrf']['input-name'] = '__csrf_magic';
118

    
119
/**
120
 * Set this to false if your site must work inside of frame/iframe elements,
121
 * but do so at your own risk: this configuration protects you against CSS
122
 * overlay attacks that defeat tokens.
123
 */
124
$GLOBALS['csrf']['frame-breaker'] = true;
125

    
126
/**
127
 * Whether or not CSRF Magic should be allowed to start a new session in order
128
 * to determine the key.
129
 */
130
$GLOBALS['csrf']['auto-session'] = true;
131

    
132
/**
133
 * Whether or not csrf-magic should produce XHTML style tags.
134
 */
135
$GLOBALS['csrf']['xhtml'] = true;
136

    
137
// FUNCTIONS:
138

    
139
// Don't edit this!
140
$GLOBALS['csrf']['version'] = '1.0.4';
141

    
142
/**
143
 * Rewrites <form> on the fly to add CSRF tokens to them. This can also
144
 * inject our JavaScript library.
145
 */
146
function csrf_ob_handler($buffer, $flags) {
147
    // Even though the user told us to rewrite, we should do a quick heuristic
148
    // to check if the page is *actually* HTML. We don't begin rewriting until
149
    // we hit the first <html tag.
150
    static $is_html = false;
151
    if (!$is_html) {
152
        // not HTML until proven otherwise
153
        if (stripos($buffer, '<html') !== false) {
154
            $is_html = true;
155
        } else {
156
            return $buffer;
157
        }
158
    }
159
    $tokens = csrf_get_tokens();
160
    $name = $GLOBALS['csrf']['input-name'];
161
    $endslash = $GLOBALS['csrf']['xhtml'] ? ' /' : '';
162
    $input = "<input type='hidden' name='$name' value=\"$tokens\"$endslash>";
163
    $buffer = preg_replace('#(<form[^>]*method\s*=\s*["\']post["\'][^>]*>)#i', '$1' . $input, $buffer);
164
    if ($GLOBALS['csrf']['frame-breaker']) {
165
        $buffer = str_ireplace('</head>', '<script type="text/javascript">if (top != self) {top.location.href = self.location.href;}</script></head>', $buffer);
166
    }
167
    if ($js = $GLOBALS['csrf']['rewrite-js']) {
168
        $buffer = str_ireplace(
169
            '</head>',
170
            '<script type="text/javascript">'.
171
                'var csrfMagicToken = "'.$tokens.'";'.
172
                'var csrfMagicName = "'.$name.'";</script>'.
173
            '<script src="'.$js.'" type="text/javascript"></script></head>',
174
            $buffer
175
        );
176
        $script = '<script type="text/javascript">CsrfMagic.end();</script>';
177
        $buffer = str_ireplace('</body>', $script . '</body>', $buffer, $count);
178
        if (!$count) {
179
            $buffer .= $script;
180
        }
181
    }
182
    return $buffer;
183
}
184

    
185
/**
186
 * Checks if this is a post request, and if it is, checks if the nonce is valid.
187
 * @param bool $fatal Whether or not to fatally error out if there is a problem.
188
 * @return True if check passes or is not necessary, false if failure.
189
 */
190
function csrf_check($fatal = true) {
191
    if ($_SERVER['REQUEST_METHOD'] !== 'POST') return true;
192
    csrf_start();
193
    $name = $GLOBALS['csrf']['input-name'];
194
    $ok = false;
195
    $tokens = '';
196
    do {
197
        if (!isset($_POST[$name])) break;
198
        // we don't regenerate a token and check it because some token creation
199
        // schemes are volatile.
200
        $tokens = $_POST[$name];
201
        if (!csrf_check_tokens($tokens)) break;
202
        $ok = true;
203
    } while (false);
204
    if ($fatal && !$ok) {
205
        $callback = $GLOBALS['csrf']['callback'];
206
        if (trim($tokens, 'A..Za..z0..9:;,') !== '') $tokens = 'hidden';
207
        $callback($tokens);
208
        phpsession_end();
209
        exit;
210
    }
211
    return $ok;
212
}
213

    
214
/**
215
 * Retrieves a valid token(s) for a particular context. Tokens are separated
216
 * by semicolons.
217
 */
218
function csrf_get_tokens() {
219
    $has_cookies = !empty($_COOKIE);
220

    
221
    // $ip implements a composite key, which is sent if the user hasn't sent
222
    // any cookies. It may or may not be used, depending on whether or not
223
    // the cookies "stick"
224
    $secret = csrf_get_secret();
225
    if (!$has_cookies && $secret) {
226
        // :TODO: Harden this against proxy-spoofing attacks
227
        $IP_ADDRESS = (isset($_SERVER['IP_ADDRESS']) ? $_SERVER['IP_ADDRESS'] : $_SERVER['REMOTE_ADDR']);
228
        $ip = ';ip:' . csrf_hash($IP_ADDRESS);
229
    } else {
230
        $ip = '';
231
    }
232
    csrf_start();
233

    
234
    // These are "strong" algorithms that don't require per se a secret
235
    if (session_id()) return 'sid:' . csrf_hash(session_id()) . $ip;
236
    if ($GLOBALS['csrf']['cookie']) {
237
        $val = csrf_generate_secret();
238
        setcookie($GLOBALS['csrf']['cookie'], $val);
239
        return 'cookie:' . csrf_hash($val) . $ip;
240
    }
241
    if ($GLOBALS['csrf']['key']) return 'key:' . csrf_hash($GLOBALS['csrf']['key']) . $ip;
242
    // These further algorithms require a server-side secret
243
    if (!$secret) return 'invalid';
244
    if ($GLOBALS['csrf']['user'] !== false) {
245
        return 'user:' . csrf_hash($GLOBALS['csrf']['user']);
246
    }
247
    if ($GLOBALS['csrf']['allow-ip']) {
248
        return ltrim($ip, ';');
249
    }
250
    return 'invalid';
251
}
252

    
253
function csrf_flattenpost($data) {
254
    $ret = array();
255
    foreach($data as $n => $v) {
256
        $ret = array_merge($ret, csrf_flattenpost2(1, $n, $v));
257
    }
258
    return $ret;
259
}
260
function csrf_flattenpost2($level, $key, $data) {
261
    if(!is_array($data)) return array($key => $data);
262
    $ret = array();
263
    foreach($data as $n => $v) {
264
        $nk = $level >= 1 ? $key."[$n]" : "[$n]";
265
        $ret = array_merge($ret, csrf_flattenpost2($level+1, $nk, $v));
266
    }
267
    return $ret;
268
}
269

    
270
/**
271
 * @param $tokens is safe for HTML consumption
272
 */
273
function csrf_callback($tokens) {
274
    // (yes, $tokens is safe to echo without escaping)
275
    header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
276
    $data = '';
277
    foreach (csrf_flattenpost($_POST) as $key => $value) {
278
        if ($key == $GLOBALS['csrf']['input-name']) continue;
279
        $data .= '<input type="hidden" name="'.htmlspecialchars($key).'" value="'.htmlspecialchars($value).'" />';
280
    }
281
    echo "<html><head><title>CSRF check failed</title></head>
282
        <body>
283
        <p>CSRF check failed. Your form session may have expired, or you may not have
284
        cookies enabled.</p>
285
        <form method='post' action=''>$data<input type='submit' value='Try again' /></form>
286
        <p>Debug: $tokens</p></body></html>
287
";
288
}
289

    
290
/**
291
 * Checks if a composite token is valid. Outward facing code should use this
292
 * instead of csrf_check_token()
293
 */
294
function csrf_check_tokens($tokens) {
295
    if (is_string($tokens)) $tokens = explode(';', $tokens);
296
    foreach ($tokens as $token) {
297
        if (csrf_check_token($token)) return true;
298
    }
299
    return false;
300
}
301

    
302
/**
303
 * Checks if a token is valid.
304
 */
305
function csrf_check_token($token) {
306
    if (strpos($token, ':') === false) return false;
307
    list($type, $value) = explode(':', $token, 2);
308
    if (strpos($value, ',') === false) return false;
309
    list($x, $time) = explode(',', $token, 2);
310
    $time = intval($time);
311
    if ($GLOBALS['csrf']['expires']) {
312
        if (time() > $time + $GLOBALS['csrf']['expires']) return false;
313
    }
314
    switch ($type) {
315
        case 'sid':
316
            return $value === csrf_hash(session_id(), $time);
317
        case 'cookie':
318
            $n = $GLOBALS['csrf']['cookie'];
319
            if (!$n) return false;
320
            if (!isset($_COOKIE[$n])) return false;
321
            return $value === csrf_hash($_COOKIE[$n], $time);
322
        case 'key':
323
            if (!$GLOBALS['csrf']['key']) return false;
324
            return $value === csrf_hash($GLOBALS['csrf']['key'], $time);
325
        // We could disable these 'weaker' checks if 'key' was set, but
326
        // that doesn't make me feel good then about the cookie-based
327
        // implementation.
328
        case 'user':
329
            if (!csrf_get_secret()) return false;
330
            if ($GLOBALS['csrf']['user'] === false) return false;
331
            return $value === csrf_hash($GLOBALS['csrf']['user'], $time);
332
        case 'ip':
333
            if (!csrf_get_secret()) return false;
334
            // do not allow IP-based checks if the username is set, or if
335
            // the browser sent cookies
336
            if ($GLOBALS['csrf']['user'] !== false) return false;
337
            if (!empty($_COOKIE)) return false;
338
            if (!$GLOBALS['csrf']['allow-ip']) return false;
339
            $IP_ADDRESS = (isset($_SERVER['IP_ADDRESS']) ? $_SERVER['IP_ADDRESS'] : $_SERVER['REMOTE_ADDR']);
340
            return $value === csrf_hash($IP_ADDRESS, $time);
341
    }
342
    return false;
343
}
344

    
345
/**
346
 * Sets a configuration value.
347
 */
348
function csrf_conf($key, $val) {
349
    if (!isset($GLOBALS['csrf'][$key])) {
350
        trigger_error('No such configuration ' . $key, E_USER_WARNING);
351
        return;
352
    }
353
    $GLOBALS['csrf'][$key] = $val;
354
}
355

    
356
/**
357
 * Starts a session if we're allowed to.
358
 */
359
function csrf_start() {
360
    if ($GLOBALS['csrf']['auto-session'] && !session_id()) {
361
        phpsession_begin();
362
    }
363
}
364

    
365
/**
366
 * Retrieves the secret, and generates one if necessary.
367
 */
368
function csrf_get_secret() {
369
    if ($GLOBALS['csrf']['secret']) return $GLOBALS['csrf']['secret'];
370
    $dir = dirname(__FILE__);
371
    $file = $dir . '/csrf-secret.php';
372
    $secret = '';
373
    if (file_exists($file)) {
374
        include $file;
375
        return $secret;
376
    }
377
    if (is_writable($dir)) {
378
        $secret = csrf_generate_secret();
379
        $fh = fopen($file, 'w');
380
        fwrite($fh, '<?php $secret = "'.$secret.'";' . PHP_EOL);
381
        fclose($fh);
382
        return $secret;
383
    }
384
    return '';
385
}
386

    
387
/**
388
 * Generates a random string as the hash of time, microtime, and mt_rand.
389
 */
390
function csrf_generate_secret($len = 32) {
391
    $r = '';
392
    for ($i = 0; $i < $len; $i++) {
393
        $r .= chr(mt_rand(0, 255));
394
    }
395
    $r .= time() . microtime();
396
    return sha1($r);
397
}
398

    
399
/**
400
 * Generates a hash/expiry double. If time isn't set it will be calculated
401
 * from the current time.
402
 */
403
function csrf_hash($value, $time = null) {
404
    if (!$time) $time = time();
405
    return sha1(csrf_get_secret() . $value . $time) . ',' . $time;
406
}
407

    
408
// Load user configuration
409
if (function_exists('csrf_startup')) csrf_startup();
410
// Initialize our handler
411
if ($GLOBALS['csrf']['rewrite'])     ob_start('csrf_ob_handler');
412
// Perform check
413
if (!$GLOBALS['csrf']['defer'])      csrf_check();
(2-2/2)