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
2102 lines (1728 loc) · 66.3 KB
/
auth.php
File metadata and controls
2102 lines (1728 loc) · 66.3 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @group pluggable
* @group auth
*/
class Tests_Auth extends WP_UnitTestCase {
// Class User values assigned to constants.
const USER_EMAIL = 'test@password.com';
const USER_LOGIN = 'password-user';
const USER_PASS = 'password';
/**
* @var WP_User
*/
protected $user;
/**
* @var WP_User
*/
protected static $_user;
/**
* @var int
*/
protected static $user_id;
/**
* @var PasswordHash
*/
protected static $wp_hasher;
protected static $bcrypt_length_limit = 72;
protected static $phpass_length_limit = 4096;
protected static $password_length_limit = 4096;
/**
* Action hook.
*/
protected $nonce_failure_hook = 'wp_verify_nonce_failed';
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$_user = $factory->user->create_and_get(
array(
'user_login' => self::USER_LOGIN,
'user_email' => self::USER_EMAIL,
'user_pass' => self::USER_PASS,
)
);
self::$user_id = self::$_user->ID;
require_once ABSPATH . WPINC . '/class-phpass.php';
self::$wp_hasher = new PasswordHash( 8, true );
}
public function set_up() {
parent::set_up();
$this->user = clone self::$_user;
wp_set_current_user( self::$user_id );
update_site_option( 'using_application_passwords', 1 );
unset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'], $GLOBALS['wp_rest_application_password_status'], $GLOBALS['wp_rest_application_password_uuid'] );
}
public function tear_down() {
// Cleanup all the global state.
unset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'], $GLOBALS['wp_rest_application_password_status'], $GLOBALS['wp_rest_application_password_uuid'] );
// Cleanup manual auth cookie test.
unset( $_COOKIE[ AUTH_COOKIE ] );
unset( $_COOKIE[ SECURE_AUTH_COOKIE ] );
parent::tear_down();
}
public function test_auth_cookie_valid() {
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
$this->assertSame( self::$user_id, wp_validate_auth_cookie( $cookie, 'auth' ) );
}
public 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->assertFalse( wp_validate_auth_cookie( $cookie, 'auth' ), 'expired cookie' );
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
$this->assertFalse( 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->assertFalse( wp_validate_auth_cookie( self::$user_id, 'auth' ), 'altered cookie' );
}
public function test_auth_cookie_scheme() {
// Arbitrary scheme name.
$cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'foo' );
$this->assertSame( 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->assertFalse( wp_validate_auth_cookie( $cookie, 'bar' ) );
}
/**
* @ticket 21022
*/
public function test_auth_cookie_generated_with_phpass_hash_remains_valid() {
self::set_user_password_with_phpass( 'password', self::$user_id );
$auth_cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
$this->assertSame( self::$user_id, wp_validate_auth_cookie( $auth_cookie, 'auth' ) );
}
/**
* @ticket 21022
*/
public function test_auth_cookie_generated_with_plain_bcrypt_hash_remains_valid() {
self::set_user_password_with_plain_bcrypt( 'password', self::$user_id );
$auth_cookie = wp_generate_auth_cookie( self::$user_id, time() + 3600, 'auth' );
$this->assertSame( self::$user_id, wp_validate_auth_cookie( $auth_cookie, 'auth' ) );
}
/**
* @ticket 23494
*/
public 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->assertNotWPError( $authed_user );
$this->assertInstanceOf( 'WP_User', $authed_user );
$this->assertSame( $this->user->ID, $authed_user->ID );
}
}
/**
* Tests hooking into wp_set_password().
*
* @ticket 57436
* @ticket 61541
*
* @covers ::wp_set_password
*/
public function test_wp_set_password_action() {
$action = new MockAction();
$previous_user_pass = get_user_by( 'id', $this->user->ID )->user_pass;
add_action( 'wp_set_password', array( $action, 'action' ), 10, 3 );
wp_set_password( 'A simple password', $this->user->ID );
$this->assertSame( 1, $action->get_call_count() );
// Check that the old data passed through the hook is correct.
$this->assertSame( $previous_user_pass, $action->get_args()[0][2]->user_pass );
}
/**
* Test wp_hash_password trims whitespace
*
* This is similar to test_password_trimming but tests the "lower level"
* wp_hash_password function
*
* @ticket 24973
*/
public 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 vertical tab o_O\x0B";
$this->assertTrue( wp_check_password( 'pass with vertical tab o_O', wp_hash_password( $password ) ) );
}
/**
* @ticket 21022
*/
public function test_wp_check_password_supports_phpass_hash() {
$password = 'password';
$hash = self::$wp_hasher->HashPassword( $password );
$this->assertTrue( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
}
/**
* Ensure wp_check_password() remains compatible with an increase to the default bcrypt cost.
*
* The test verifies this by reducing the cost used to generate the hash, therefore mimicing a hash
* which was generated prior to the default cost being increased.
*
* Notably the bcrypt cost was increased in PHP 8.4: https://wiki.php.net/rfc/bcrypt_cost_2023 .
*
* @ticket 21022
*/
public function test_wp_check_password_supports_hash_with_increased_bcrypt_cost() {
$password = 'password';
// Reducing the cost mimics an increase to the default cost.
add_filter( 'wp_hash_password_options', array( $this, 'reduce_hash_cost' ) );
$hash = wp_hash_password( $password, PASSWORD_BCRYPT );
remove_filter( 'wp_hash_password_options', array( $this, 'reduce_hash_cost' ) );
$this->assertTrue( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
$this->assertTrue( wp_password_needs_rehash( $hash ) );
}
/**
* Ensure wp_check_password() remains compatible with a reduction of the default bcrypt cost.
*
* The test verifies this by increasing the cost used to generate the hash, therefore mimicing a hash
* which was generated prior to the default cost being reduced.
*
* A reduction of the cost is unlikely to occur but is fully supported.
*
* @ticket 21022
*/
public function test_wp_check_password_supports_hash_with_reduced_bcrypt_cost() {
$password = 'password';
// Increasing the cost mimics a reduction of the default cost.
add_filter( 'wp_hash_password_options', array( $this, 'increase_hash_cost' ) );
$hash = wp_hash_password( $password, PASSWORD_BCRYPT );
remove_filter( 'wp_hash_password_options', array( $this, 'increase_hash_cost' ) );
$this->assertTrue( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
$this->assertTrue( wp_password_needs_rehash( $hash ) );
}
/**
* @ticket 21022
*/
public function test_wp_check_password_supports_wp_hash_with_default_bcrypt_cost() {
$password = 'password';
$hash = wp_hash_password( $password, PASSWORD_BCRYPT );
$this->assertTrue( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
$this->assertFalse( wp_password_needs_rehash( $hash ) );
}
/**
* @ticket 21022
*/
public function test_wp_check_password_supports_plain_bcrypt_hash_with_default_bcrypt_cost() {
$password = 'password';
$hash = password_hash( $password, PASSWORD_BCRYPT );
$this->assertTrue( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
$this->assertTrue( wp_password_needs_rehash( $hash ) );
}
/**
* Ensure wp_check_password() is compatible with Argon2i hashes.
*
* @ticket 21022
*/
public function test_wp_check_password_supports_argon2i_hash() {
if ( ! defined( 'PASSWORD_ARGON2I' ) ) {
$this->markTestSkipped( 'Argon2i is not supported.' );
}
$password = 'password';
$hash = password_hash( trim( $password ), PASSWORD_ARGON2I );
$this->assertTrue( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
}
/**
* Ensure wp_check_password() is compatible with Argon2id hashes.
*
* @requires PHP >= 7.3
*
* @ticket 21022
*/
public function test_wp_check_password_supports_argon2id_hash() {
if ( ! defined( 'PASSWORD_ARGON2ID' ) ) {
$this->markTestSkipped( 'Argon2id is not supported.' );
}
$password = 'password';
$hash = password_hash( trim( $password ), PASSWORD_ARGON2ID );
$this->assertTrue( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
}
/**
* @ticket 21022
*/
public function test_wp_check_password_supports_md5_hash() {
$password = 'password';
$hash = md5( $password );
$this->assertTrue( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
}
/**
* @ticket 21022
*/
public function test_wp_check_password_does_not_support_plain_text() {
$password = 'password';
$hash = $password;
$this->assertFalse( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
}
/**
* @ticket 21022
*
* @dataProvider data_empty_values
* @param mixed $value
*/
public function test_wp_check_password_does_not_support_empty_hash( $value ) {
$password = 'password';
$hash = $value;
$this->assertFalse( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
}
/**
* @ticket 21022
*
* @dataProvider data_empty_values
* @param mixed $value
*/
public function test_wp_check_password_does_not_support_empty_password( $value ) {
$password = $value;
$hash = $value;
$this->assertFalse( wp_check_password( $password, $hash ) );
$this->assertSame( 1, did_filter( 'check_password' ) );
}
public function data_empty_values() {
return array(
// String zero:
array( '0' ),
// Zero-length string:
array( '' ),
// Null byte character:
array( "\0" ),
// Asterisk values:
array( '*' ),
array( '*0' ),
array( '*1' ),
);
}
/**
* @ticket 29217
*/
public function test_wp_verify_nonce_with_empty_arg() {
$this->assertFalse( wp_verify_nonce( '' ) );
$this->assertFalse( wp_verify_nonce( null ) );
}
/**
* @ticket 29542
*/
public function test_wp_verify_nonce_with_integer_arg() {
$this->assertFalse( wp_verify_nonce( 1 ) );
}
/**
* @ticket 24030
*/
public 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->assertSame( ( $count + 1 ), did_action( $this->nonce_failure_hook ) );
}
/**
* @ticket 24030
*/
public 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->assertSame( $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'] );
}
public function test_check_admin_referer_with_default_action_as_string_not_doing_it_wrong() {
// A valid nonce needs to be set so the check doesn't die().
$_REQUEST['_wpnonce'] = wp_create_nonce( '-1' );
$result = check_admin_referer( '-1' );
$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'] );
}
/**
* @ticket 21022
*/
public function test_password_is_hashed_with_bcrypt() {
$password = 'password';
// Set the user password.
wp_set_password( $password, self::$user_id );
// Ensure the password is hashed with bcrypt.
$this->assertStringStartsWith( '$wp$2y$', get_userdata( self::$user_id )->user_pass );
// Authenticate.
$user = wp_authenticate( $this->user->user_login, $password );
// Verify correct password.
$this->assertNotWPError( $user );
$this->assertInstanceOf( 'WP_User', $user );
$this->assertSame( self::$user_id, $user->ID );
}
public function data_passwords(): array {
return array(
array( 'a' ),
array( 'password' ),
array( str_repeat( 'a', self::$password_length_limit ) ),
);
}
/**
* Ensure the hash of the user password remains less than 64 characters in length to account for the old users table schema.
*
* @ticket 21022
* @dataProvider data_passwords
*/
public function test_user_password_against_old_users_table_schema( string $password ) {
// Mimic the schema of the users table prior to WordPress 4.4.
add_filter( 'wp_pre_insert_user_data', array( $this, 'mimic_users_schema_prior_to_44' ) );
$username = 'old-schema-user';
// Create a user.
$user_id = $this->factory()->user->create(
array(
'user_login' => $username,
'user_email' => 'old-schema-user@example.com',
'user_pass' => $password,
)
);
// Check the user can authenticate.
$user = wp_authenticate( $username, $password );
$this->assertNotWPError( $user );
$this->assertInstanceOf( 'WP_User', $user );
$this->assertSame( $user_id, $user->ID, 'User should be able to authenticate' );
$this->assertNotSame( self::$user_id, $user->ID, 'A unique user must be created for this test, the shared fixture must not be used' );
}
/**
* Ensure the hash of the user activation key remains less than 60 characters in length to account for the old users table schema.
*
* @ticket 21022
*/
public function test_user_activation_key_against_old_users_table_schema() {
// Mimic the schema of the users table prior to WordPress 4.4.
add_filter( 'wp_pre_insert_user_data', array( $this, 'mimic_users_schema_prior_to_44' ) );
$username = 'old-schema-user';
// Create a user.
$user_id = $this->factory()->user->create(
array(
'user_login' => $username,
'user_email' => 'old-schema-user@example.com',
)
);
$user = get_userdata( $user_id );
$key = get_password_reset_key( $user );
// A correctly saved key should be accepted.
$check = check_password_reset_key( $key, $user->user_login );
$this->assertNotWPError( $check );
$this->assertInstanceOf( 'WP_User', $check );
$this->assertSame( $user->ID, $check->ID );
$this->assertNotSame( self::$user_id, $user->ID, 'A unique user must be created for this test, the shared fixture must not be used' );
}
/*
* Fake the schema of the users table prior to WordPress 4.4 to mimic sites that are using the
* `DO_NOT_UPGRADE_GLOBAL_TABLES` constant and have not updated the users table schema.
*
* The schema of the wp_users table on wordpress.org has not been updated since the schema was changed in [35638]
* for WordPress 4.4, which means the `user_activation_key` field remains at 60 characters length and the `user_pass`
* field remains at 64 characters length instead of the expected 255. Although this is unlikely to affect other
* sites, this can be accommodated for in the codebase.
*
* Actually altering the database schema during tests will commit the transaction and break subsequent tests, hence
* the use of this filter.
*/
public function mimic_users_schema_prior_to_44( array $data ): array {
if ( isset( $data['user_pass'] ) ) {
$this->assertLessThanOrEqual( 64, strlen( $data['user_pass'] ) );
}
if ( isset( $data['user_activation_key'] ) ) {
$this->assertLessThanOrEqual( 60, strlen( $data['user_activation_key'] ) );
}
return $data;
}
/**
* @ticket 21022
*/
public function test_invalid_password_at_bcrypt_length_limit_is_rejected() {
$limit = str_repeat( 'a', self::$bcrypt_length_limit );
// Set the user password to the bcrypt limit.
wp_set_password( $limit, self::$user_id );
$user = wp_authenticate( $this->user->user_login, 'aaaaaaaa' );
// Wrong password.
$this->assertWPError( $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
}
/**
* @ticket 21022
*/
public function test_invalid_password_beyond_bcrypt_length_limit_is_rejected() {
$limit = str_repeat( 'a', self::$bcrypt_length_limit + 1 );
// Set the user password beyond the bcrypt limit.
wp_set_password( $limit, self::$user_id );
$user = wp_authenticate( $this->user->user_login, 'aaaaaaaa' );
// Wrong password.
$this->assertWPError( $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
}
/**
* @ticket 21022
*/
public function test_valid_password_at_bcrypt_length_limit_is_accepted() {
$limit = str_repeat( 'a', self::$bcrypt_length_limit );
// Set the user password to the bcrypt limit.
wp_set_password( $limit, self::$user_id );
// Authenticate.
$user = wp_authenticate( $this->user->user_login, $limit );
// Correct password.
$this->assertNotWPError( $user );
$this->assertInstanceOf( 'WP_User', $user );
$this->assertSame( self::$user_id, $user->ID );
}
/**
* @ticket 21022
*/
public function test_valid_password_beyond_bcrypt_length_limit_is_accepted() {
$limit = str_repeat( 'a', self::$bcrypt_length_limit + 1 );
// Set the user password beyond the bcrypt limit.
wp_set_password( $limit, self::$user_id );
// Authenticate.
$user = wp_authenticate( $this->user->user_login, $limit );
// Correct password depite its length.
$this->assertNotWPError( $user );
$this->assertInstanceOf( 'WP_User', $user );
$this->assertSame( self::$user_id, $user->ID );
}
/**
* A password beyond 72 bytes will be truncated by bcrypt by default and still be accepted.
*
* This ensures that a truncated password is not accepted by WordPress.
*
* @ticket 21022
*/
public function test_long_truncated_password_is_rejected() {
$at_limit = str_repeat( 'a', self::$bcrypt_length_limit );
$beyond_limit = str_repeat( 'a', self::$bcrypt_length_limit + 1 );
// Set the user password beyond the bcrypt limit.
wp_set_password( $beyond_limit, self::$user_id );
// Authenticate using a truncated password.
$user = wp_authenticate( $this->user->user_login, $at_limit );
// Incorrect password.
$this->assertWPError( $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
}
/**
* @ticket 21022
*/
public function test_setting_password_beyond_bcrypt_length_limit_is_rejected() {
$beyond_limit = str_repeat( 'a', self::$password_length_limit + 1 );
// Set the user password beyond the limit.
wp_set_password( $beyond_limit, self::$user_id );
// Password broken by setting it to be too long.
$user = get_user_by( 'id', self::$user_id );
$this->assertSame( '*', $user->data->user_pass );
// Password is not accepted.
$user = wp_authenticate( $this->user->user_login, $beyond_limit );
$this->assertInstanceOf( 'WP_Error', $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
// Placeholder is not accepted.
$user = wp_authenticate( $this->user->user_login, '*' );
$this->assertInstanceOf( 'WP_Error', $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
}
/**
* @see https://core.trac.wordpress.org/changeset/30466
*/
public function test_invalid_password_at_phpass_length_limit_is_rejected() {
$limit = str_repeat( 'a', self::$phpass_length_limit );
// Set the user password with the old phpass algorithm.
self::set_user_password_with_phpass( $limit, self::$user_id );
// Authenticate.
$user = wp_authenticate( $this->user->user_login, 'aaaaaaaa' );
// Wrong password.
$this->assertInstanceOf( 'WP_Error', $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
}
public function test_valid_password_at_phpass_length_limit_is_accepted() {
$limit = str_repeat( 'a', self::$phpass_length_limit );
// Set the user password with the old phpass algorithm.
self::set_user_password_with_phpass( $limit, self::$user_id );
// Authenticate.
$user = wp_authenticate( $this->user->user_login, $limit );
// Correct password.
$this->assertNotWPError( $user );
$this->assertInstanceOf( 'WP_User', $user );
$this->assertSame( self::$user_id, $user->ID );
}
public function test_too_long_password_at_phpass_length_limit_is_rejected() {
$limit = str_repeat( 'a', self::$phpass_length_limit );
// Set the user password with the old phpass algorithm.
self::set_user_password_with_phpass( $limit, self::$user_id );
// Authenticate with a password that is one character too long.
$user = wp_authenticate( $this->user->user_login, $limit . 'a' );
// Wrong password.
$this->assertInstanceOf( 'WP_Error', $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
}
public function test_too_long_password_beyond_phpass_length_limit_is_rejected() {
// One char too many.
$too_long = str_repeat( 'a', self::$phpass_length_limit + 1 );
// Set the user password with the old phpass algorithm.
self::set_user_password_with_phpass( $too_long, self::$user_id );
$user = get_user_by( 'id', self::$user_id );
// Password broken by setting it to be too long.
$this->assertSame( '*', $user->data->user_pass );
// Password is not accepted.
$user = wp_authenticate( $this->user->user_login, '*' );
$this->assertInstanceOf( 'WP_Error', $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
}
/**
* @dataProvider data_empty_values
* @param mixed $value
*/
public function test_empty_password_is_rejected_by_bcrypt( $value ) {
// Set the user password.
wp_set_password( 'password', self::$user_id );
$user = wp_authenticate( $this->user->user_login, $value );
$this->assertInstanceOf( 'WP_Error', $user );
}
/**
* @dataProvider data_empty_values
* @param mixed $value
*/
public function test_empty_password_is_rejected_by_phpass( $value ) {
// Set the user password with the old phpass algorithm.
self::set_user_password_with_phpass( 'password', self::$user_id );
$user = wp_authenticate( $this->user->user_login, $value );
$this->assertInstanceOf( 'WP_Error', $user );
}
public function test_incorrect_password_is_rejected_by_phpass() {
// Set the user password with the old phpass algorithm.
self::set_user_password_with_phpass( 'password', self::$user_id );
$user = wp_authenticate( $this->user->user_login, 'aaaaaaaa' );
// Wrong password.
$this->assertInstanceOf( 'WP_Error', $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
}
public function test_too_long_password_is_rejected_by_phpass() {
$limit = str_repeat( 'a', self::$phpass_length_limit );
// Set the user password with the old phpass algorithm.
self::set_user_password_with_phpass( 'password', self::$user_id );
$user = wp_authenticate( $this->user->user_login, $limit . 'a' );
// Password broken by setting it to be too long.
$this->assertInstanceOf( 'WP_Error', $user );
$this->assertSame( 'incorrect_password', $user->get_error_code() );
}
/**
* @ticket 45746
*/
public function test_user_activation_key_is_saved() {
$user = get_userdata( $this->user->ID );
$key = get_password_reset_key( $user );
// A correctly saved 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 );
}
/**
* @ticket 32429
*/
public 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' ) . ':' . wp_fast_hash( $key ),
),
array(
'ID' => $this->user->ID,
)
);
clean_user_cache( $this->user );
// 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
*/
public 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' ) . ':' . wp_fast_hash( $key ),
),
array(
'ID' => $this->user->ID,
)
);
clean_user_cache( $this->user );
// An expired but otherwise valid key should be rejected.
$check = check_password_reset_key( $key, $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
$this->assertSame( 'expired_key', $check->get_error_code() );
}
/**
* @ticket 32429
*/
public 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
*/
public 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,
)
);
clean_user_cache( $this->user );
// A legacy user_activation_key should not be accepted.
$check = check_password_reset_key( $key, $this->user->user_login );
$this->assertInstanceOf( 'WP_Error', $check );
$this->assertSame( 'expired_key', $check->get_error_code() );
// 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 );
$this->assertSame( 'invalid_key', $check->get_error_code() );
}
/**
* @ticket 21022
*/
public function test_phpass_user_activation_key_is_allowed() {
global $wpdb;
// A legacy user_activation_key is one hashed using phpass between WordPress 4.3 and 6.8.0.
$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,
)
);
clean_user_cache( $this->user );
// A legacy phpass user_activation_key should remain valid.
$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 empty key with a legacy user_activation_key should be rejected.
$check = check_password_reset_key( '', $this->user->user_login );
$this->assertWPError( $check );
$this->assertSame( 'invalid_key', $check->get_error_code() );
}
/**
* @ticket 21022
*/
public function test_expired_phpass_user_activation_key_is_rejected() {
global $wpdb;
// A legacy user_activation_key is one hashed using phpass between WordPress 4.3 and 6.8.0.
$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,
)
);
clean_user_cache( $this->user );
// A legacy phpass user_activation_key should still be subject to an expiry check.
$check = check_password_reset_key( $key, $this->user->user_login );
$this->assertWPError( $check );
$this->assertSame( 'expired_key', $check->get_error_code() );
// An empty key with a legacy user_activation_key should be rejected.
$check = check_password_reset_key( '', $this->user->user_login );
$this->assertWPError( $check );
$this->assertSame( 'invalid_key', $check->get_error_code() );
}
/**
* @ticket 21022
*/
public function test_user_request_key_handling() {
$request_id = wp_create_user_request( 'test@example.com', 'remove_personal_data' );
$key = wp_generate_user_request_key( $request_id );
// A valid key should be accepted.
$check = wp_validate_user_request_key( $request_id, $key );
$this->assertNotWPError( $check );
$this->assertTrue( $check );
// An invalid key should rejected.
$check = wp_validate_user_request_key( $request_id, 'invalid' );
$this->assertWPError( $check );
$this->assertSame( 'invalid_key', $check->get_error_code() );
// An empty key should be rejected.
$check = wp_validate_user_request_key( $request_id, '' );
$this->assertWPError( $check );
$this->assertSame( 'missing_key', $check->get_error_code() );
}
/**
* @ticket 21022
*/