1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Enalean, 2011. All Rights Reserved. |
4
|
|
|
* |
5
|
|
|
* This file is a part of Tuleap. |
6
|
|
|
* |
7
|
|
|
* Tuleap is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* Tuleap is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with Tuleap. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
require_once('common/include/Codendi_HTMLPurifier.class.php'); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class CSRFSynchronizerToken |
25
|
|
|
* |
26
|
|
|
* This class deals with a CSRF countermeasure. Usage: |
27
|
|
|
* |
28
|
|
|
* In a html form, add a token: |
29
|
|
|
* <pre> |
30
|
|
|
* echo '<form ...>'; |
31
|
|
|
* $token = new CSRFSynchronizerToken('/path/of/my/page'); |
32
|
|
|
* echo $token->fetchHTMLInput(); |
33
|
|
|
* </pre> |
34
|
|
|
* |
35
|
|
|
* Then in the target page, which deals with sensitive data, check that the token is valid |
36
|
|
|
* <pre> |
37
|
|
|
* $request = HTTPRequest::instance(); |
38
|
|
|
* $token = new CSRFSynchronizerToken('/path/of/my/page'); |
39
|
|
|
* $token->check(); |
40
|
|
|
* // ... continue in a safe way |
41
|
|
|
* </pre> |
42
|
|
|
* |
43
|
|
|
* That's it! |
44
|
|
|
*/ |
45
|
|
|
class CSRFSynchronizerToken { |
46
|
|
|
|
47
|
|
|
const PREF_NAME_PREFIX = 'synchronizer_token_'; |
48
|
|
|
|
49
|
|
|
const DEFAULT_TOKEN_NAME = 'challenge'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string a pseudorandom generated token |
53
|
|
|
*/ |
54
|
|
|
protected $token; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string the url the token is refering to |
58
|
|
|
*/ |
59
|
|
|
protected $url; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string the name of the token (to retrieve in the request) |
63
|
|
|
*/ |
64
|
|
|
protected $token_name; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Generate a token for the $url |
68
|
|
|
* |
69
|
|
|
* Generate a challenge token to prevent CSRF attacks. |
70
|
|
|
* |
71
|
|
|
* The pseudorandom generated token must be put in an hidden field in a form. |
72
|
|
|
* If the form as method=POST it is better. Use this for sensitive server-side operations (admin, preferences, ...) |
73
|
|
|
* |
74
|
|
|
* /!\ using this method for anonymous user is just silly (although it doesn't raise any error) |
75
|
|
|
* |
76
|
|
|
* @see https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29 |
77
|
|
|
* |
78
|
|
|
* @param string $url The url of the page (take it as a 'salt'). The token is uniq for (url, user session) |
79
|
|
|
* @param string $token_name The name of the token in the request. default is 'challenge' |
80
|
|
|
* |
81
|
|
|
* @return void |
|
|
|
|
82
|
|
|
*/ |
83
|
|
|
public function __construct($url, $token_name = self::DEFAULT_TOKEN_NAME) { |
84
|
|
|
$this->url = $url; |
85
|
|
|
$this->token_name = $token_name; |
86
|
|
|
|
87
|
|
|
//generation of the token |
88
|
|
|
$salt = $this->url . $this->getUser()->getSessionHash(); |
89
|
|
|
$this->token = $this->getUser()->getPreference(self::PREF_NAME_PREFIX . $salt); |
90
|
|
|
if (!$this->token) { |
91
|
|
|
$this->token = md5(uniqid(rand(), true) . $salt); |
92
|
|
|
$this->getUser()->setPreference(self::PREF_NAME_PREFIX . $salt, $this->token); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check that a challenge token is valid. |
98
|
|
|
* @see Constructor |
99
|
|
|
* |
100
|
|
|
* @param string $token The token to check against what is stored in the user session |
101
|
|
|
* |
102
|
|
|
* @return bool true if token valid, false otherwise |
103
|
|
|
*/ |
104
|
|
|
public function isValid($token) { |
105
|
|
|
return $this->token === $token; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Redirect to somewhere else if the token in request is not valid |
110
|
|
|
* |
111
|
|
|
* @param Codendi_Request $request The request object, if null then use HTTPRequest |
112
|
|
|
* @param string $redirect_to Url to be redirected to in case of error. if null then use $url instead. Default is null |
113
|
|
|
* |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function check($redirect_to = null, $request = null) { |
117
|
|
|
if (!$request) { |
118
|
|
|
$request = HTTPRequest::instance(); |
119
|
|
|
} |
120
|
|
|
if (!$request->existAndNonEmpty($this->token_name) || !$this->isValid($request->get($this->token_name))) { |
121
|
|
|
$GLOBALS['Response']->addFeedback('error', $GLOBALS['Language']->getText('global', 'error_synchronizertoken')); |
122
|
|
|
$GLOBALS['Response']->redirect($redirect_to === null ? $this->url : $redirect_to); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Fetch HTML input (hidden) to be included in a form to protect the user against CSRF |
128
|
|
|
* |
129
|
|
|
* @return string html |
130
|
|
|
*/ |
131
|
|
|
public function fetchHTMLInput() { |
132
|
|
|
$hp = Codendi_HTMLPurifier::instance(); |
133
|
|
|
return '<input type="hidden" name="'. $hp->purify($this->token_name, CODENDI_PURIFIER_CONVERT_HTML) .'" value="'. $hp->purify($this->token, CODENDI_PURIFIER_CONVERT_HTML) .'" />'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string The token |
138
|
|
|
*/ |
139
|
|
|
public function getToken() { |
140
|
|
|
return $this->token; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string The token name |
145
|
|
|
*/ |
146
|
|
|
public function getTokenName() { |
147
|
|
|
return $this->token_name; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return PFUser |
152
|
|
|
*/ |
153
|
|
|
protected function getUser() { |
154
|
|
|
return UserManager::instance()->getCurrentUser(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
?> |
158
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.