forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
374 lines (302 loc) · 11.1 KB
/
auth.php
File metadata and controls
374 lines (302 loc) · 11.1 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* @group pluggable
* @group auth
*/
class Tests_Auth extends WP_UnitTestCase {
protected $user;
protected static $_user;
protected static $user_id;
protected static $wp_hasher;
/**
* action hook
*/
protected $nonce_failure_hook = 'wp_verify_nonce_failed';
public static function wpSetUpBeforeClass( $factory ) {
self::$_user = $factory->user->create_and_get(
array(
'user_login' => 'password-tests',
)
);
self::$user_id = self::$_user->ID;
require_once( ABSPATH . WPINC . '/class-phpass.php' );
self::$wp_hasher = new PasswordHash( 8, true );
}
function setUp() {
parent::setUp();
$this->user = clone self::$_user;
wp_set_current_user( self::$user_id );
}
function test_auth_cookie_valid() {
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
$this->assertEquals( self::$user_id, wp_validate_auth_cookie( $cookie, 'auth' ) );
}
function test_auth_cookie_invalid() {
// 3600 or less and +3600 may occur in wp_validate_auth_cookie(),
// as an ajax test may have defined DOING_AJAX, failing the test.
$cookie = wp_generate_auth_cookie( self::$user_id, time() - 7200, 'auth' );
$this->assertEquals( false, wp_validate_auth_cookie( $cookie, 'auth' ), 'expired cookie' );
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
$this->assertEquals( false, wp_validate_auth_cookie( $cookie, 'logged_in' ), 'wrong auth scheme' );
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
list($a, $b, $c) = explode( '|', $cookie );
$cookie = $a . '|' . ( $b + 1 ) . '|' . $c;
$this->assertEquals( false, wp_validate_auth_cookie( self::$user_id, 'auth' ), 'altered cookie' );
}
function test_auth_cookie_scheme() {
// arbitrary scheme name
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'foo' );
$this->assertEquals( self::$user_id, wp_validate_auth_cookie( $cookie, 'foo' ) );
// wrong scheme name - should fail
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'foo' );
$this->assertEquals( false, wp_validate_auth_cookie( $cookie, 'bar' ) );
}
/**
* @ticket 23494
*/
function test_password_trimming() {
$passwords_to_test = array(
'a password with no trailing or leading spaces',
'a password with trailing spaces ',
' a password with leading spaces',
' a password with trailing and leading spaces ',
);
foreach ( $passwords_to_test as $password_to_test ) {
wp_set_password( $password_to_test, $this->user->ID );
$authed_user = wp_authenticate( $this->user->user_login, $password_to_test );
$this->assertInstanceOf( 'WP_User', $authed_user );
$this->assertEquals( $this->user->ID, $authed_user->ID );
}
}
/**
* Test wp_hash_password trims whitespace
*
* This is similar to test_password_trimming but tests the "lower level"
* wp_hash_password function
*
* @ticket 24973
*/
function test_wp_hash_password_trimming() {
$password = ' pass with leading whitespace';
$this->assertTrue( wp_check_password( 'pass with leading whitespace', wp_hash_password( $password ) ) );
$password = 'pass with trailing whitespace ';
$this->assertTrue( wp_check_password( 'pass with trailing whitespace', wp_hash_password( $password ) ) );
$password = ' pass with whitespace ';
$this->assertTrue( wp_check_password( 'pass with whitespace', wp_hash_password( $password ) ) );
$password = "pass with new line \n";
$this->assertTrue( wp_check_password( 'pass with new line', wp_hash_password( $password ) ) );
$password = "pass with vertial tab o_O\x0B";
$this->assertTrue( wp_check_password( 'pass with vertial tab o_O', wp_hash_password( $password ) ) );
}
/**
* @ticket 29217
*/
function test_wp_verify_nonce_with_empty_arg() {
$this->assertFalse( wp_verify_nonce( '' ) );
$this->assertFalse( wp_verify_nonce( null ) );
}
/**
* @ticket 29542
*/
function test_wp_verify_nonce_with_integer_arg() {
$this->assertFalse( wp_verify_nonce( 1 ) );
}
/**
* @ticket 24030
*/
function test_wp_nonce_verify_failed() {
$nonce = substr( md5( uniqid() ), 0, 10 );
$count = did_action( $this->nonce_failure_hook );
wp_verify_nonce( $nonce, 'nonce_test_action' );
$this->assertEquals( ( $count + 1 ), did_action( $this->nonce_failure_hook ) );
}
/**
* @ticket 24030
*/
function test_wp_nonce_verify_success() {
$nonce = wp_create_nonce( 'nonce_test_action' );
$count = did_action( $this->nonce_failure_hook );
wp_verify_nonce( $nonce, 'nonce_test_action' );
$this->assertEquals( $count, did_action( $this->nonce_failure_hook ) );
}
/**
* @ticket 36361
*/
public function test_check_admin_referer_with_no_action_triggers_doing_it_wrong() {
$this->setExpectedIncorrectUsage( 'check_admin_referer' );
// A valid nonce needs to be set so the check doesn't die()
$_REQUEST['_wpnonce'] = wp_create_nonce( -1 );
$result = check_admin_referer();
$this->assertSame( 1, $result );
unset( $_REQUEST['_wpnonce'] );
}
/**
* @ticket 36361
*/
public function test_check_ajax_referer_with_no_action_triggers_doing_it_wrong() {
$this->setExpectedIncorrectUsage( 'check_ajax_referer' );
// A valid nonce needs to be set so the check doesn't die()
$_REQUEST['_wpnonce'] = wp_create_nonce( -1 );
$result = check_ajax_referer();
$this->assertSame( 1, $result );
unset( $_REQUEST['_wpnonce'] );
}
function test_password_length_limit() {
$limit = str_repeat( 'a', 4096 );
wp_set_password( $limit, self::$user_id );
// phpass hashed password
$this->assertStringStartsWith( '$P$', $this->user->data->user_pass );
$user = wp_authenticate( $this->user->user_login, 'aaaaaaaa' );
// Wrong Password
$this->assertInstanceOf( 'WP_Error', $user );
$user = wp_authenticate( $this->user->user_login, $limit );
$this->assertInstanceOf( 'WP_User', $user );
$this->assertEquals( self::$user_id, $user->ID );
// one char too many
$user = wp_authenticate( $this->user->user_login, $limit . 'a' );
// Wrong Password
$this->assertInstanceOf( 'WP_Error', $user );
wp_set_password( $limit . 'a', self::$user_id );
$user = get_user_by( 'id', self::$user_id );
// Password broken by setting it to be too long.
$this->assertEquals( '*', $user->data->user_pass );
$user = wp_authenticate( $this->user->user_login, '*' );
$this->assertInstanceOf( 'WP_Error', $user );
$user = wp_authenticate( $this->user->user_login, '*0' );
$this->assertInstanceOf( 'WP_Error', $user );
$user = wp_authenticate( $this->user->user_login, '*1' );
$this->assertInstanceOf( 'WP_Error', $user );
$user = wp_authenticate( $this->user->user_login, 'aaaaaaaa' );
// Wrong Password
$this->assertInstanceOf( 'WP_Error', $user );
$user = wp_authenticate( $this->user->user_login, $limit );
// Wrong Password
$this->assertInstanceOf( 'WP_Error', $user );
$user = wp_authenticate( $this->user->user_login, $limit . 'a' );
// Password broken by setting it to be too long.
$this->assertInstanceOf( 'WP_Error', $user );
}
/**
* @ticket 32429
*/
function test_user_activation_key_is_checked() {
global $wpdb;
$key = wp_generate_password( 20, false );
$wpdb->update(
$wpdb->users,
array(
'user_activation_key' => strtotime( '-1 hour' ) . ':' . self::$wp_hasher->HashPassword( $key ),
),
array(
'ID' => $this->user->ID,
)
);
// A valid key should be accepted
$check = check_password_reset_key( $key, $this->user->user_login );
$this->assertNotWPError( $check );
$this->assertInstanceOf( 'WP_User', $check );
$this->assertSame( $this->user->ID, $check->ID );
// An invalid key should be rejected
$check = check_password_reset_key( 'key', $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
// An empty key should be rejected
$check = check_password_reset_key( '', $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
// A truncated key should be rejected
$partial = substr( $key, 0, 10 );
$check = check_password_reset_key( $partial, $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
}
/**
* @ticket 32429
*/
function test_expired_user_activation_key_is_rejected() {
global $wpdb;
$key = wp_generate_password( 20, false );
$wpdb->update(
$wpdb->users,
array(
'user_activation_key' => strtotime( '-48 hours' ) . ':' . self::$wp_hasher->HashPassword( $key ),
),
array(
'ID' => $this->user->ID,
)
);
// An expired but otherwise valid key should be rejected
$check = check_password_reset_key( $key, $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
}
/**
* @ticket 32429
*/
function test_empty_user_activation_key_fails_key_check() {
// An empty user_activation_key should not allow any key to be accepted
$check = check_password_reset_key( 'key', $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
// An empty user_activation_key should not allow an empty key to be accepted
$check = check_password_reset_key( '', $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
}
/**
* @ticket 32429
*/
function test_legacy_user_activation_key_is_rejected() {
global $wpdb;
// A legacy user_activation_key is one without the `time()` prefix introduced in WordPress 4.3.
$key = wp_generate_password( 20, false );
$wpdb->update(
$wpdb->users,
array(
'user_activation_key' => self::$wp_hasher->HashPassword( $key ),
),
array(
'ID' => $this->user->ID,
)
);
// A legacy user_activation_key should not be accepted
$check = check_password_reset_key( $key, $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
// An empty key with a legacy user_activation_key should be rejected
$check = check_password_reset_key( '', $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
}
/**
* @ticket 32429
* @ticket 24783
*/
function test_plaintext_user_activation_key_is_rejected() {
global $wpdb;
// A plaintext user_activation_key is one stored before hashing was introduced in WordPress 3.7.
$key = wp_generate_password( 20, false );
$wpdb->update(
$wpdb->users,
array(
'user_activation_key' => $key,
),
array(
'ID' => $this->user->ID,
)
);
// A plaintext user_activation_key should not allow an otherwise valid key to be accepted
$check = check_password_reset_key( $key, $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
// A plaintext user_activation_key should not allow an empty key to be accepted
$check = check_password_reset_key( '', $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
}
/**
* Ensure users can log in using both their username and their email address.
*
* @ticket 9568
*/
function test_log_in_using_email() {
$user_args = array(
'user_login' => 'johndoe',
'user_email' => 'mail@example.com',
'user_pass' => 'password',
);
$this->factory->user->create( $user_args );
$this->assertInstanceOf( 'WP_User', wp_authenticate( $user_args['user_email'], $user_args['user_pass'] ) );
$this->assertInstanceOf( 'WP_User', wp_authenticate( $user_args['user_login'], $user_args['user_pass'] ) );
}
}