Issues (21)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

modules/otp.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 *
4
 * 2FA extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Paul Sohier
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace paul999\tfa\modules;
12
13
use OTPAuthenticate\OTPAuthenticate;
14
use OTPAuthenticate\OTPHelper;
15
use phpbb\db\driver\driver_interface;
16
use phpbb\exception\http_exception;
17
use phpbb\request\request_interface;
18
use phpbb\template\template;
19
use phpbb\user;
20
21
class otp extends abstract_module
22
{
23
	/**
24
	 * @var OTPHelper
25
	 */
26
	private $otp_helper;
27
28
	/**
29
	 * @var OTPAuthenticate
30
	 */
31
	private $otp;
32
33
	/**
34
	 * @var request_interface
35
	 */
36
	private $request;
37
38
	/**
39
	 * @var string
40
	 */
41
	private $otp_registration_table;
42
43
	/**
44
	 * OTP constructor.
45
	 *
46
	 * @param driver_interface $db
47
	 * @param user $user
48
	 * @param request_interface $request
49
	 * @param template $template
50
	 * @param string                            $otp_registration_table
51
	 */
52 View Code Duplication
	public function __construct(driver_interface $db, user $user, request_interface $request, template $template, $otp_registration_table)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
	{
54
		$this->otp_helper = new OTPHelper();
55
		$this->otp = new OTPAuthenticate();
56
		$this->db = $db;
57
		$this->user = $user;
58
		$this->request = $request;
59
		$this->template = $template;
60
		$this->otp_registration_table = $otp_registration_table;
61
	}
62
63
	/**
64
	 * Get a language key for this specific module.
65
	 * @return string
66
	 */
67
	public function get_translatable_name()
68
	{
69
		return 'TFA_OTP';
70
	}
71
72
	/**
73
	 * Return the name of the current module
74
	 * This is for internal use only
75
	 * @return string
76
	 */
77
	public function get_name()
78
	{
79
		return 'otp';
80
	}
81
82
	/**
83
	 * Return if this module is enabled by the admin
84
	 * (And all server requirements are met).
85
	 *
86
	 * Do not return false in case a specific user disabled this module,
87
	 * OR if the user is unable to use this specific module,
88
	 * OR if a browser specific item is missing/incorrect.
89
	 * @return boolean
90
	 */
91
	public function is_enabled()
92
	{
93
		return true;
94
	}
95
96
	/**
97
	 * Check if the current user is able to use this module.
98
	 *
99
	 * This means that the user enabled it in the UCP,
100
	 * And has it setup up correctly.
101
	 * This method will be called during login, not during registration/
102
	 *
103
	 * @param int $user_id
104
	 *
105
	 * @return bool
106
	 */
107
	public function is_usable($user_id)
108
	{
109
		return $this->check_table_for_user($this->otp_registration_table, $user_id);
110
	}
111
112
	/**
113
	 * Check if the user can potentially use this.
114
	 * This method is called at registration page.
115
	 *
116
	 * You can, for example, check if the current browser is suitable.
117
	 *
118
	 * @param int|boolean $user_id Use false to ignore user
119
	 *
120
	 * @return bool
121
	 */
122
	public function is_potentially_usable($user_id = false)
123
	{
124
		return true;
125
	}
126
127
	/**
128
	 * Check if the user has any key registered with this module.
129
	 * There should be no check done if the key is usable, it should
130
	 * only return if a key is registered.
131
	 *
132
	 * @param $user_id
133
	 * @return bool
134
	 */
135
	public function key_registered($user_id)
136
	{
137
		return $this->check_table_for_user($this->otp_registration_table, $user_id);
138
	}
139
140
	/**
141
	 * Get the priority for this module.
142
	 * A lower priority means more chance it gets selected as default option
143
	 *
144
	 * There can be only one module with a specific priority!
145
	 * If there is already a module registered with this priority,
146
	 * a Exception might be thrown
147
	 *
148
	 * @return int
149
	 */
150
	public function get_priority()
151
	{
152
		return 15;
153
	}
154
155
	/**
156
	 * Start of the login procedure.
157
	 *
158
	 * @param int $user_id
159
	 *
160
	 * @return array
161
	 */
162
	public function login_start($user_id)
163
	{
164
		return array(
165
			'S_TFA_INCLUDE_HTML'	=> '@paul999_tfa/tfa_otp_authenticate.html',
166
		);
167
	}
168
169
	/**
170
	 * Actual login procedure
171
	 *
172
	 * @param int $user_id
173
	 *
174
	 * @return bool
175
	 */
176
	public function login($user_id)
177
	{
178
		$key = $this->request->variable('authenticate', '');
179
180
		if (empty($key))
181
		{
182
			throw new http_exception(400, 'TFA_NO_KEY_PROVIDED');
183
		}
184
185
		foreach ($this->getRegistrations($user_id) as $registration)
186
		{
187 View Code Duplication
			if ($this->otp->checkTOTP($registration['secret'], $key, 'sha1'))
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
			{
189
				// We found a valid key.
190
				$sql_ary = array(
191
					'last_used' => time(),
192
				);
193
				$sql = 'UPDATE ' . $this->otp_registration_table . ' 
194
					SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . ' 
195
					WHERE 
196
						registration_id = ' . (int) $registration['registration_id'];
197
				$this->db->sql_query($sql);
198
				return true;
199
			}
200
		}
201
		return false;
202
	}
203
204
	/**
205
	 * If this module can add new keys (Or other things)
206
	 *
207
	 * @return boolean
208
	 */
209
	public function can_register()
210
	{
211
		return true;
212
	}
213
214
	/**
215
	 * Start with the registration of a new security key.
216
	 * This page should return a name of a template, and
217
	 * it should assign the required variables for this template.
218
	 *
219
	 * @return string
220
	 */
221
	public function register_start()
222
	{
223
		$secret = $this->otp->generateSecret();
224
		$QR = $this->otp_helper->generateKeyURI('totp', $secret, $this->user->data['username'], generate_board_url(), 0, 'sha1');
225
		$this->template->assign_vars(array(
226
			'TFA_QR_CODE'				=> 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=' . $QR,
227
			'TFA_SECRET'				=> $secret,
228
			'L_TFA_ADD_OTP_KEY_EXPLAIN'	=> $this->user->lang('TFA_ADD_OTP_KEY_EXPLAIN', $secret),
229
			'S_HIDDEN_FIELDS_MODULE'	=> build_hidden_fields(array(
230
				'secret'	=> $secret,
231
			)),
232
		));
233
234
		return 'tfa_otp_ucp_new';
235
	}
236
237
	/**
238
	 * Do the actual registration of a new security key.
239
	 *
240
	 * @throws http_exception
241
	 */
242
	public function register()
243
	{
244
		$secret = $this->request->variable('secret', '');
245
		$otp	= $this->request->variable('register', '');
246
247
		if (!$this->otp->checkTOTP($secret, $otp, 'sha1'))
248
		{
249
			throw new http_exception(400, 'TFA_OTP_INVALID_KEY');
250
		}
251
252
		$sql_ary = array(
253
			'user_id' 		=> $this->user->data['user_id'],
254
			'secret'		=> $secret,
255
			'registered' 	=> time(),
256
			'last_used' 	=> time(),
257
		);
258
259
		$sql = 'INSERT INTO ' . $this->otp_registration_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
260
		$this->db->sql_query($sql);
261
	}
262
263
	/**
264
	 * This method is called to show the UCP page.
265
	 * You can assign template variables to the template, or do anything else here.
266
	 */
267
	public function show_ucp()
268
	{
269
		$this->show_ucp_complete($this->otp_registration_table);
270
	}
271
272
	/**
273
	 * Delete a specific row from the UCP.
274
	 * The data is based on the data provided in show_ucp.
275
	 *
276
	 * @param int $key
277
	 *
278
	 * @return void
279
	 */
280 View Code Duplication
	public function delete($key)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
	{
282
		$sql = 'DELETE FROM ' . $this->otp_registration_table . '
283
			WHERE user_id = ' . (int) $this->user->data['user_id'] . '
284
			AND registration_id =' . (int) $key;
285
286
		$this->db->sql_query($sql);
287
	}
288
289
	/**
290
	 * Select all registration objects from the database
291
	 * @param integer $user_id
292
	 * @return array
293
	 */
294 View Code Duplication
	private function getRegistrations($user_id)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295
	{
296
		$sql = 'SELECT * FROM ' . $this->otp_registration_table . ' WHERE user_id = ' . (int) $user_id;
297
		$result = $this->db->sql_query($sql);
298
		$rows = $this->db->sql_fetchrowset($result);
299
300
		$this->db->sql_freeresult($result);
301
		return $rows;
302
	}
303
}
304