-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathValidationTest.php
621 lines (454 loc) · 23.1 KB
/
ValidationTest.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
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
<?php /** @noinspection JsonEncodingApiUsageInspection */
namespace Tests\Attestation;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Laragear\WebAuthn\Attestation\AttestationObject;
use Laragear\WebAuthn\Attestation\AuthenticatorData;
use Laragear\WebAuthn\Attestation\Formats\None;
use Laragear\WebAuthn\Attestation\Validator\AttestationValidation;
use Laragear\WebAuthn\Attestation\Validator\AttestationValidator;
use Laragear\WebAuthn\Attestation\Validator\Pipes\CheckRelyingPartyHashSame;
use Laragear\WebAuthn\Attestation\Validator\Pipes\CheckUserInteraction;
use Laragear\WebAuthn\Attestation\Validator\Pipes\CredentialIdShouldNotBeDuplicated;
use Laragear\WebAuthn\ByteBuffer;
use Laragear\WebAuthn\Challenge;
use Laragear\WebAuthn\Exceptions\AttestationException;
use Laragear\WebAuthn\Models\WebAuthnCredential;
use Mockery;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\ParameterBag;
use Tests\FakeAuthenticator;
use Tests\Stubs\WebAuthnAuthenticatableUser;
use Tests\TestCase;
use function base64_decode;
use function base64_encode;
use function hex2bin;
use function json_encode;
use function now;
use function session;
use function tap;
/**
* CBOR Encoded strings where done in "cbor.me"
*
* @see https://cbor.me
*/
class ValidationTest extends TestCase
{
protected Request $request;
protected WebAuthnAuthenticatableUser $user;
protected AttestationValidation $validation;
protected AttestationValidator $validator;
protected Challenge $challenge;
protected function setUp(): void
{
parent::setUp();
$this->request = Request::create(
'https://test.app/webauthn/create', 'POST', content: json_encode(FakeAuthenticator::attestationResponse())
);
$this->user = WebAuthnAuthenticatableUser::forceCreate([
'name' => FakeAuthenticator::ATTESTATION_USER['displayName'],
'email' => FakeAuthenticator::ATTESTATION_USER['name'],
'password' => 'test_password',
]);
$this->validator = new AttestationValidator($this->app);
$this->validation = new AttestationValidation($this->user, $this->request);
$this->travelTo(now()->startOfSecond());
$this->challenge = new Challenge(
new ByteBuffer(base64_decode(FakeAuthenticator::ATTESTATION_CHALLENGE)),
60,
false,
['user_uuid' => FakeAuthenticator::ATTESTATION_USER['id']]
);
$this->session(['_webauthn' => $this->challenge]);
$this->request->setLaravelSession($this->app->make('session.store'));
}
protected function validate(): AttestationValidation
{
return $this->validator->send($this->validation)->thenReturn();
}
public function test_validates_attestation_and_instances_webauthn_credential(): void
{
$validation = $this->validator->send($this->validation)->thenReturn();
static::assertInstanceOf(AttestationValidation::class, $validation);
static::assertFalse($validation->credential->exists);
$validation->credential->save();
$this->assertModelExists($validation->credential);
$this->assertDatabaseHas(WebAuthnCredential::class, [
'id' => $validation->credential->id,
'authenticatable_type' => WebAuthnAuthenticatableUser::class,
'authenticatable_id' => 1,
'user_id' => $validation->credential->user_id,
'alias' => null,
'counter' => 0,
'rp_id' => 'localhost',
'origin' => 'http://localhost',
'transports' => null,
'aaguid' => Uuid::NIL,
'attestation_format' => 'none',
'certificates' => null,
'disabled_at' => null,
]);
$key = DB::table('webauthn_credentials')->value('public_key');
static::assertSame($validation->credential->public_key, Crypt::decryptString($key));
}
public function test_validates_attestation_for_scoped_origin(): void
{
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode([
'type' => 'webauthn.create',
'origin' => 'https://scoped.localhost',
'challenge' => $this->challenge->data->toBase64Url()
])
);
$this->request->setJson(new ParameterBag($invalid));
static::assertInstanceOf(AttestationValidation::class, $this->validator->send($this->validation)->thenReturn());
}
public function test_fails_if_challenge_does_not_exists(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Challenge does not exist.');
$this->session(['_webauthn' => null]);
$this->validate();
}
public function test_fails_if_challenge_exists_but_is_expired(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Challenge does not exist.');
$this->travelTo(now()->addMinute()->addSecond());
$this->validate();
}
public function test_challenge_is_pulled_from_session(): void
{
$this->validate();
static::assertNull(session('_webauthn'));
}
public function test_compiling_client_data_json_fails_if_invalid(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Client Data JSON is invalid or malformed.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = 'foo';
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_client_data_json_fails_if_empty(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Client Data JSON is empty.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(json_encode([]));
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_client_data_json_fails_if_type_missing(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Client Data JSON does not contain the [type] key.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(json_encode(['origin' => '', 'challenge' => '']));
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_client_data_json_fails_if_origin_missing(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Client Data JSON does not contain the [origin] key.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(json_encode(['type' => '', 'challenge' => '']));
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_client_data_json_fails_if_challenge_missing(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Client Data JSON does not contain the [challenge] key.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(json_encode(['type' => '', 'origin' => '']));
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_attestation_object_fails_if_invalid(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: CBOR Object is anything but an array.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(hex2bin('1A499602D2')); // 1234567890 in CBOR
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_attestation_object_fails_if_fmt_missing(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Format is missing or invalid.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(hex2bin('A26761747453746D746068617574684461746160'));
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_attestation_object_fails_if_fmt_not_string(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Format is missing or invalid.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(
hex2bin('A363666D74016761747453746D746068617574684461746160')
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_attestation_object_fails_if_attStmt_missing(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Statement is missing or invalid.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(
hex2bin('A263666D74647465737468617574684461746160')
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_attestation_object_fails_if_attStmt_not_array(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Statement is missing or invalid.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(
hex2bin('A363666D746474657374686175746844617461606761747453746D7400')
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_attestation_object_fails_if_authData_missing(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Authenticator Data is missing or invalid.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(
hex2bin('A263666D7464746573746761747453746D7480')
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_attestation_object_fails_if_authData_not_byte_buffer(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Authenticator Data is missing or invalid.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(
hex2bin('A363666D7464746573746761747453746D7481006861757468446174611A001E8480')
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_attestation_object_fails_if_fmt_not_none(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Format name [invalid] is invalid.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(
hex2bin('A363666D7467696E76616C69646761747453746D74A068617574684461746159016749960DE5880E8C687434170F6476605B8FE4AEB9A28632C7995CF3BA831D97634500000000000000000000000000000000000000000020ECFE96F4E099C876F2EA374218CAF33001E97DFDF8FC7F657257FC2E0BC9AF9DA401030339010020590100C3BD9A5E8971F49A2A88A6A161441E61A514BD63E77C1BE40AAAC08D3DFE4070FC37A0B739954A5150AA88A35E562E962B6D77B8EFACBACD90D2C6F93C3C5CBFD0194FA370713C673B1E0B3CEAC4A94B95C5D41EF0E0078309E0CAF6E3F1D10EF8418B4761842AC61F2B7C9F99595076C7BEEFE41E786BC9C013663054A0B3D3F0BE4FEA906696317BE1E2BD2FF299D6FA430E1A762AF69D0F0BC4CAF2FD16AB6EFA685055933FDE65E2C2232C344BE80EEB309975CBE55772887E7ADBFC38E9F68860DB11B466663FCF40C1F7529E274D6687EB237D41B62838540528CE6943664464ADA55B0F510782D5837AF07780BB7A675EF1D3FA29F39D1B472A7B80852143010001')
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_authenticator_data_fails_if_invalid_binary(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Authenticator Data: Invalid input.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(
hex2bin('A363666D74646E6F6E656761747453746D74A06861757468446174614849960DE5880E8C6D')
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_compiling_authenticator_data_fails_if_invalid_length(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: ByteBuffer: Invalid offset or length.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['attestationObject'] = base64_encode(
hex2bin('A363666D74646E6F6E656761747453746D74A068617574684461746159016649960DE5880E8C687434170F6476605B8FE4AEB9A28632C7995CF3BA831D97634500000000000000000000000000000000000000000020ECFE96F4E099C876F2EA374218CAF33001E97DFDF8FC7F657257FC2E0BC9AF9DA401030339010020590100C3BD9A5E8971F49A2A88A6A161441E61A514BD63E77C1BE40AAAC08D3DFE4070FC37A0B739954A5150AA88A35E562E962B6D77B8EFACBACD90D2C6F93C3C5CBFD0194FA370713C673B1E0B3CEAC4A94B95C5D41EF0E0078309E0CAF6E3F1D10EF8418B4761842AC61F2B7C9F99595076C7BEEFE41E786BC9C013663054A0B3D3F0BE4FEA906696317BE1E2BD2FF299D6FA430E1A762AF69D0F0BC4CAF2FD16AB6EFA685055933FDE65E2C2232C344BE80EEB309975CBE55772887E7ADBFC38E9F68860DB11B466663FCF40C1F7529E274D6687EB237D41B62838540528CE6943664464ADA55B0F510782D5837AF07780BB7A675EF1D3FA29F39D1B472A7B808521430100')
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_action_checks_fails_if_not_webauthn_create(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response is not for creating WebAuthn Credentials.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode(['type' => 'invalid', 'origin' => '', 'challenge' => ''])
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_check_challenge_fails_if_challenge_is_empty(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response has an empty challenge.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode(['type' => 'webauthn.create', 'origin' => '', 'challenge' => ''])
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_check_challenge_fails_if_challenge_is_not_equal(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response challenge is not equal.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode(['type' => 'webauthn.create', 'origin' => '', 'challenge' => 'invalid'])
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_check_origin_fails_if_empty(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response has an empty origin.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode(['type' => 'webauthn.create', 'origin' => '', 'challenge' => FakeAuthenticator::ATTESTATION_CHALLENGE])
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_check_origin_fails_if_invalid_host(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response origin is invalid.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode(['type' => 'webauthn.create', 'origin' => 'invalid', 'challenge' => FakeAuthenticator::ATTESTATION_CHALLENGE])
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_check_origin_fails_if_unsecure(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response not made to a secure server (localhost or HTTPS).');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode(['type' => 'webauthn.create', 'origin' => 'http://unsecure.com', 'challenge' => FakeAuthenticator::ATTESTATION_CHALLENGE])
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_rp_id_fails_if_empty(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response has an empty origin.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode([
'type' => 'webauthn.create',
'origin' => '',
'challenge' => FakeAuthenticator::ATTESTATION_CHALLENGE
])
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_rp_id_fails_if_not_equal(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Relying Party ID not scoped to current.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode([
'type' => 'webauthn.create',
'origin' => 'https://otherhost.com',
'challenge' => FakeAuthenticator::ATTESTATION_CHALLENGE
])
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_rp_id_fails_if_not_contained(): void
{
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Relying Party ID not scoped to current.');
$invalid = FakeAuthenticator::attestationResponse();
$invalid['response']['clientDataJSON'] = base64_encode(
json_encode([
'type' => 'webauthn.create',
'origin' => 'https://invalidlocalhost',
'challenge' => FakeAuthenticator::ATTESTATION_CHALLENGE
])
);
$this->request->setJson(new ParameterBag($invalid));
$this->validate();
}
public function test_rp_id_fails_if_hash_not_same(): void
{
$this->app->when(CheckRelyingPartyHashSame::class)
->needs(ConfigContract::class)
->give(static function (): Repository {
return tap(new Repository())->set('webauthn.relying_party.id', 'https://otherhost.com');
});
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response has different Relying Party ID hash.');
$this->validate();
}
public function test_check_user_interaction_fails_if_user_not_present(): void
{
$this->app->resolving(CheckUserInteraction::class, function (): void {
$this->validation->attestationObject = new AttestationObject(
$auth = Mockery::mock(AuthenticatorData::class),
new None([], $auth),
'none',
);
$auth->expects('wasUserAbsent')->andReturnTrue();
});
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response did not have the user present.');
$this->validate();
}
public function test_check_user_interaction_fails_if_user_verification_was_required(): void
{
$this->challenge->verify = true;
$this->app->resolving(CheckUserInteraction::class, function (): void {
$this->validation->attestationObject = new AttestationObject(
$auth = Mockery::mock(AuthenticatorData::class),
new None([], $auth),
'none',
);
$auth->expects('wasUserAbsent')->andReturnFalse();
$auth->expects('wasUserNotVerified')->andReturnTrue();
});
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Response did not verify the user.');
$this->validate();
}
public function test_credential_duplicate_check_fails_if_already_exists(): void
{
$this->app->resolving(CredentialIdShouldNotBeDuplicated::class, static function (): void {
DB::table('webauthn_credentials')->insert([
'id' => FakeAuthenticator::CREDENTIAL_ID,
'authenticatable_type' => WebAuthnAuthenticatableUser::class,
'authenticatable_id' => 1,
'user_id' => 'e8af6f703f8042aa91c30cf72289aa07',
'counter' => 0,
'rp_id' => 'localhost',
'origin' => 'http://localhost',
'aaguid' => Uuid::NIL,
'attestation_format' => 'none',
'public_key' => 'test_key',
'updated_at' => now(),
'created_at' => now(),
]);
});
$this->expectException(AttestationException::class);
$this->expectExceptionMessage('Attestation Error: Credential ID already exists in the database.');
$this->validate();
}
}