-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.php
53 lines (41 loc) · 1.04 KB
/
do.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
require_once('common.php');
$location = 'form';
$token = '';
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
$location = 'xhr';
}
if ($location === 'xhr') {
if (!isset($_SERVER['HTTP_X_CSRF_HEADER'])) {
http_response_code(403);
die('No csrf header');
}
$token = $_SERVER['HTTP_X_CSRF_HEADER'];
} else {
if (!isset($_POST['csrf_token'])) {
http_response_code(403);
die('No csrf body');
}
$token = $_POST['csrf_token'];
}
$parts = explode('-', $token);
if (count($parts) !== 4) {
http_response_code(403);
die('Token is malformed');
}
$computed = hash_hmac('sha256', $parts[0].'-'.$parts[1].'-'.$parts[2], $password);
if (!hash_equals($computed, $parts[3])) {
http_response_code(403);
die('Token is invalid');
}
if (time() > $parts[2]) {
http_response_code(403);
die('Token is expired');
}
if (session_id() !== $parts[1]) {
http_response_code(403);
die('Incorrect user id');
}
setcookie('CSRF-TOKEN', generateToken($password));
?>
Request Succeeded