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).
13 * This library is PHP4 and PHP5 compatible.
19 * By default, when you include this file csrf-magic will automatically check
20 * and exit if the CSRF token is invalid. This will defer executing
21 * csrf_check() until you're ready. You can also pass false as a parameter to
22 * that function, in which case the function will not exit but instead return
23 * a boolean false if the CSRF check failed. This allows for tighter integration
26 $GLOBALS['csrf']['defer'] = false;
29 * Callback function to execute when there's the CSRF check fails and
30 * $fatal == true (see csrf_check). This will usually output an error message
33 $GLOBALS['csrf']['callback'] = 'csrf_callback';
36 * Whether or not to include our JavaScript library which also rewrites
37 * AJAX requests on this domain. Set this to the web path. This setting only works
38 * with supported JavaScript libraries in Internet Explorer; see README.txt for
39 * a list of supported libraries.
41 $GLOBALS['csrf']['rewrite-js'] = false;
44 * A secret key used when hashing items. Please generate a random string and
45 * place it here. If you change this value, all previously generated tokens
46 * will become invalid.
48 $GLOBALS['csrf']['secret'] = '';
51 * Whether or not to use IP addresses when binding a user to a token. This is
52 * less reliable and less secure than sessions, but is useful when you need
53 * to give facilities to anonymous users and do not wish to maintain a database
56 $GLOBALS['csrf']['allow-ip'] = true;
59 * If this information is available, set this to a unique identifier (it
60 * can be an integer or a unique username) for the current "user" of this
61 * application. The token will then be globally valid for all of that user's
62 * operations, but no one else. This requires that 'secret' be set.
64 $GLOBALS['csrf']['user'] = false;
67 * This is an arbitrary secret value associated with the user's session. This
68 * will most probably be the contents of a cookie, as an attacker cannot easily
69 * determine this information. Warning: If the attacker knows this value, they
70 * can easily spoof a token. This is a generic implementation; sessions should
73 * Why would you want to use this? Lets suppose you have a squid cache for your
74 * website, and the presence of a session cookie bypasses it. Let's also say
75 * you allow anonymous users to interact with the website; submitting forms
76 * and AJAX. Previously, you didn't have any CSRF protection for anonymous users
77 * and so they never got sessions; you don't want to start using sessions either,
78 * otherwise you'll bypass the Squid cache. Setup a different cookie for CSRF
79 * tokens, and have Squid ignore that cookie for get requests, for anonymous
80 * users. (If you haven't guessed, this scheme was(?) used for MediaWiki).
82 $GLOBALS['csrf']['key'] = false;
85 * The name of the magic CSRF token that will be placed in all forms, i.e.
86 * the contents of <input type="hidden" name="$name" value="CSRF-TOKEN" />
88 $GLOBALS['csrf']['input-name'] = '__csrf_magic';
91 * Whether or not CSRF Magic should be allowed to start a new session in order
92 * to determine the key.
94 $GLOBALS['csrf']['auto-session'] = true;
97 * Whether or not csrf-magic should produce XHTML style tags.
99 $GLOBALS['csrf']['xhtml'] = true;
104 * Rewrites <form> on the fly to add CSRF tokens to them. This can also
105 * inject our JavaScript library.
107 function csrf_ob_handler($buffer, $flags) {
108 $token = csrf_get_token();
109 $name = $GLOBALS['csrf']['input-name'];
110 $endslash = $GLOBALS['csrf']['xhtml'] ?
' /' : '';
111 $input = "<input type='hidden' name='$name' value=\"$token\"$endslash>";
112 $buffer = preg_replace('#(<form[^>]*method\s*=\s*["\']post["\'][^>]*>)#i', '$1' . $input, $buffer);
113 if ($js = $GLOBALS['csrf']['rewrite-js']) {
114 $buffer = preg_replace(
116 '<script type="text/javascript">'.
117 'var csrfMagicToken = "'.$token.'";'.
118 'var csrfMagicName = "'.$name.'";</script>'.
119 '<script src="'.$js.'" type="text/javascript"></script>$1',
127 * Checks if this is a post request, and if it is, checks if the nonce is valid.
128 * @param bool $fatal Whether or not to fatally error out if there is a problem.
129 * @return True if check passes or is not necessary, false if failure.
131 function csrf_check($fatal = true) {
132 if ($_SERVER['REQUEST_METHOD'] !== 'POST') return true;
134 $name = $GLOBALS['csrf']['input-name'];
137 if (!isset($_POST[$name])) break;
138 // we don't regenerate a token and check it because some token creation
139 // schemes are volatile.
140 if (!csrf_check_token($_POST[$name])) break;
143 if ($fatal && !$ok) {
144 $callback = $GLOBALS['csrf']['callback'];
152 * Retrieves a valid token for a particular context.
154 function csrf_get_token() {
155 $secret = csrf_get_secret();
157 // These are "strong" algorithms that don't require per se a secret
158 if (session_id()) return 'sid:' . sha1($secret . session_id());
159 if ($GLOBALS['csrf']['key']) return 'key:' . sha1($secret . $GLOBALS['csrf']['key']);
160 // These further algorithms require a server-side secret
161 if ($secret === '') return 'invalid';
162 if ($GLOBALS['csrf']['user'] !== false) {
163 return 'user:' . sha1($secret . $GLOBALS['csrf']['user']);
165 if ($GLOBALS['csrf']['allow-ip']) {
166 // :TODO: Harden this against proxy-spoofing attacks
167 return 'ip:' . sha1($secret . $_SERVER['IP_ADDRESS']);
172 function csrf_callback() {
173 echo "<html><body>CSRF check failed. Please enable cookies.</body></html>
178 * Checks if a token is valid.
180 function csrf_check_token($token) {
181 if (strpos($token, ':') === false) return false;
182 list($type, $value) = explode(':', $token, 2);
183 $secret = csrf_get_secret();
186 return $value === sha1($secret . session_id());
188 if (!$GLOBALS['csrf']['key']) return false;
189 return $value === sha1($secret . $GLOBALS['csrf']['key']);
190 // We could disable these 'weaker' checks if 'key' was set, but
191 // that doesn't make me feel good then about the cookie-based
194 if ($GLOBALS['csrf']['secret'] === '') return false;
195 if ($GLOBALS['csrf']['user'] === false) return false;
196 return $value === sha1($secret . $GLOBALS['csrf']['user']);
198 if ($GLOBALS['csrf']['secret'] === '') return false;
199 // do not allow IP-based checks if the username is set
200 if ($GLOBALS['csrf']['user'] !== false) return false;
201 if (!$GLOBALS['csrf']['allow-ip']) return false;
202 return $value === sha1($secret . $_SERVER['IP_ADDRESS']);
208 * Sets a configuration value.
210 function csrf_conf($key, $val) {
211 if (!isset($GLOBALS['csrf'][$key])) {
212 trigger_error('No such configuration ' . $key, E_USER_WARNING
);
215 $GLOBALS['csrf'][$key] = $val;
219 * Starts a session if we're allowed to.
221 function csrf_start() {
222 if ($GLOBALS['csrf']['auto-session'] && !session_id()) {
228 * Retrieves the secret, and generates one if necessary.
230 function csrf_get_secret() {
231 if ($GLOBALS['csrf']['secret']) return $GLOBALS['csrf']['secret'];
232 $dir = dirname(__FILE__
);
233 $file = $dir . '/csrf-secret.php';
235 if (file_exists($file)) {
239 if (is_writable($dir)) {
240 for ($i = 0; $i < 32; $i++
) $secret .= '\\x' . dechex(mt_rand(32, 126));
241 $fh = fopen($file, 'w');
242 fwrite($fh, '<?php $secret = "'.$secret.'";' . PHP_EOL
);
249 // Initialize our handler
250 ob_start('csrf_ob_handler');
251 // Load user configuration
252 if (function_exists('csrf_startup')) csrf_startup();
254 if (!$GLOBALS['csrf']['defer']) csrf_check();