Skip to content

Commit 76db52f

Browse files
WebAuthn.sol: simplify contains to startsWith (#41)
1 parent 0204d05 commit 76db52f

File tree

4 files changed

+169
-209
lines changed

4 files changed

+169
-209
lines changed

lcov.info

Lines changed: 129 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -428,168 +428,159 @@ BRH:39
428428
end_of_record
429429
TN:
430430
SF:src/WebAuthn.sol
431-
FN:12,WebAuthn.contains
432-
FNDA:11,WebAuthn.contains
433-
DA:17,11
434-
DA:17,11
435-
DA:17,11
436-
DA:18,11
437-
DA:18,11
438-
DA:18,11
439-
DA:20,11
440-
DA:20,11
441-
DA:21,11
442-
DA:21,11
443-
DA:23,11
444-
DA:23,11
445-
DA:23,176
446-
DA:23,176
447-
DA:24,168
448-
DA:24,168
449-
DA:24,168
450-
BRDA:24,0,0,167
451-
BRDA:24,0,1,1
452-
DA:25,1
453-
DA:25,1
454-
DA:28,167
455-
DA:28,167
456-
BRDA:28,1,0,165
457-
BRDA:28,1,1,2
458-
DA:29,2
459-
DA:29,2
460-
DA:33,8
461-
DA:33,8
462-
FN:44,WebAuthn.checkAuthFlags
431+
FN:12,WebAuthn.startsWith
432+
FNDA:6,WebAuthn.startsWith
433+
DA:16,6
434+
DA:16,6
435+
DA:16,6
436+
DA:17,6
437+
DA:17,6
438+
DA:17,6
439+
DA:19,6
440+
DA:19,6
441+
DA:20,6
442+
DA:20,6
443+
DA:22,6
444+
DA:22,6
445+
DA:22,212
446+
DA:22,212
447+
DA:23,209
448+
DA:23,209
449+
BRDA:23,0,0,208
450+
BRDA:23,0,1,1
451+
DA:24,1
452+
DA:24,1
453+
DA:27,208
454+
DA:27,208
455+
BRDA:27,1,0,206
456+
BRDA:27,1,1,2
457+
DA:28,2
458+
DA:28,2
459+
DA:32,3
460+
DA:32,3
461+
FN:43,WebAuthn.checkAuthFlags
463462
FNDA:9,WebAuthn.checkAuthFlags
464-
DA:49,9
465-
DA:49,9
466-
DA:49,9
467-
BRDA:49,2,0,8
468-
BRDA:49,2,1,1
469-
DA:50,1
470-
DA:50,1
471-
DA:57,8
472-
DA:57,8
473-
DA:58,2
474-
DA:58,2
475-
BRDA:56,3,0,7
476-
BRDA:56,3,1,1
477-
DA:60,1
478-
DA:60,1
479-
DA:65,7
480-
DA:65,7
481-
DA:65,7
482-
BRDA:65,4,0,1
483-
BRDA:65,4,1,1
484-
DA:66,2
485-
DA:66,2
486-
DA:66,2
487-
BRDA:66,5,0,1
488-
BRDA:66,5,1,1
489-
DA:67,1
490-
DA:67,1
491-
DA:71,6
492-
DA:71,6
493-
FN:124,WebAuthn.verifySignature
463+
DA:48,9
464+
DA:48,9
465+
DA:48,9
466+
BRDA:48,2,0,8
467+
BRDA:48,2,1,1
468+
DA:49,1
469+
DA:49,1
470+
DA:56,8
471+
DA:56,8
472+
DA:57,5
473+
DA:57,5
474+
BRDA:55,3,0,7
475+
BRDA:55,3,1,1
476+
DA:59,1
477+
DA:59,1
478+
DA:64,7
479+
DA:64,7
480+
DA:64,7
481+
BRDA:64,4,0,1
482+
BRDA:64,4,1,1
483+
DA:65,2
484+
DA:65,2
485+
DA:65,2
486+
BRDA:65,5,0,1
487+
BRDA:65,5,1,1
488+
DA:66,1
489+
DA:66,1
490+
DA:70,6
491+
DA:70,6
492+
FN:123,WebAuthn.verifySignature
494493
FNDA:10,WebAuthn.verifySignature
495-
DA:138,10
496-
DA:138,10
497-
DA:138,10
498-
DA:139,9
499-
DA:139,9
500-
BRDA:137,6,0,6
501-
BRDA:137,6,1,4
502-
DA:141,4
503-
DA:141,4
494+
DA:135,10
495+
DA:135,10
496+
DA:135,10
497+
DA:136,9
498+
DA:136,9
499+
BRDA:134,6,0,6
500+
BRDA:134,6,1,4
501+
DA:138,4
502+
DA:138,4
503+
DA:144,6
504+
DA:144,6
505+
DA:144,6
506+
DA:145,6
504507
DA:145,6
505508
DA:145,6
506-
DA:146,6
507-
DA:146,6
508-
BRDA:146,7,0,5
509-
BRDA:146,7,1,1
510-
DA:147,1
511-
DA:147,1
512-
DA:151,5
513-
DA:151,5
514-
DA:151,5
515-
DA:152,5
516-
DA:152,5
517-
DA:152,5
518-
DA:158,5
519-
DA:158,5
520-
BRDA:158,8,0,3
521-
BRDA:158,8,1,2
522-
DA:159,2
523-
DA:159,2
524-
DA:163,3
525-
DA:163,3
526-
DA:163,3
527-
DA:164,3
528-
DA:164,3
529-
DA:164,3
530-
DA:168,3
531-
DA:168,3
532-
DA:168,3
509+
DA:150,6
510+
DA:150,6
511+
BRDA:150,7,0,3
512+
BRDA:150,7,1,3
513+
DA:151,3
514+
DA:151,3
515+
DA:155,3
516+
DA:155,3
517+
DA:155,3
518+
DA:156,3
519+
DA:156,3
520+
DA:156,3
521+
DA:160,3
522+
DA:160,3
523+
DA:160,3
533524
FNF:3
534525
FNH:3
535-
LF:32
536-
LH:32
537-
BRF:18
538-
BRH:18
526+
LF:29
527+
LH:29
528+
BRF:16
529+
BRH:16
539530
end_of_record
540531
TN:
541532
SF:src/utils/Base64URL.sol
542533
FN:7,Base64URL.encode
543-
FNDA:1799,Base64URL.encode
544-
DA:8,1799
545-
DA:8,1799
546-
DA:8,1799
547-
DA:9,1799
548-
DA:9,1799
549-
DA:9,1799
550-
DA:12,1799
551-
DA:12,1799
552-
DA:13,1799
553-
DA:13,1799
554-
DA:13,1799
555-
DA:13,1795
556-
BRDA:13,0,0,518
534+
FNDA:1800,Base64URL.encode
535+
DA:8,1800
536+
DA:8,1800
537+
DA:8,1800
538+
DA:9,1800
539+
DA:9,1800
540+
DA:9,1800
541+
DA:12,1800
542+
DA:12,1800
543+
DA:13,1800
544+
DA:13,1800
545+
DA:13,1800
546+
DA:13,1796
547+
BRDA:13,0,0,519
557548
BRDA:13,0,1,1281
558-
DA:13,518
549+
DA:13,519
559550
DA:14,1281
560551
DA:14,1281
561552
DA:14,1281
562553
DA:14,1277
563554
BRDA:14,1,0,1281
564555
BRDA:14,1,1,511
565556
DA:14,511
566-
DA:16,1799
567-
DA:16,1799
568-
DA:16,1799
569-
DA:17,1799
570-
DA:17,1799
571-
DA:17,1799
572-
DA:19,1799
573-
DA:19,1799
574-
DA:19,526644
575-
DA:19,526644
576-
DA:20,524845
577-
DA:20,524845
557+
DA:16,1800
558+
DA:16,1800
559+
DA:16,1800
560+
DA:17,1800
561+
DA:17,1800
562+
DA:17,1800
563+
DA:19,1800
564+
DA:19,1800
565+
DA:19,526651
566+
DA:19,526651
567+
DA:20,524851
568+
DA:20,524851
578569
BRDA:20,2,0,1248
579-
BRDA:20,2,1,523597
570+
BRDA:20,2,1,523603
580571
DA:21,1248
581572
DA:21,1248
582-
DA:22,523597
583-
DA:22,523597
573+
DA:22,523603
574+
DA:22,523603
584575
BRDA:22,3,0,176191
585-
BRDA:22,3,1,347406
576+
BRDA:22,3,1,347412
586577
DA:23,176191
587578
DA:23,176191
588-
DA:25,347406
589-
DA:25,347406
590-
DA:29,1799
591-
DA:29,1799
592-
DA:29,1799
579+
DA:25,347412
580+
DA:25,347412
581+
DA:29,1800
582+
DA:29,1800
583+
DA:29,1800
593584
FNF:1
594585
FNH:1
595586
LF:14

src/WebAuthn.sol

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,23 @@ import "./P256.sol";
88
* Helper library for external contracts to verify WebAuthn signatures.
99
**/
1010
library WebAuthn {
11-
/// Checks whether substr occurs in str starting at a given byte offset.
12-
function contains(
13-
string memory substr,
14-
string memory str,
15-
uint256 location
11+
/// Checks whether prefix occurs in the beginning of str.
12+
function startsWith(
13+
string memory prefix,
14+
string memory str
1615
) internal pure returns (bool) {
17-
bytes memory substrBytes = bytes(substr);
16+
bytes memory prefixBytes = bytes(prefix);
1817
bytes memory strBytes = bytes(str);
1918

20-
uint256 substrLen = substrBytes.length;
19+
uint256 prefixLen = prefixBytes.length;
2120
uint256 strLen = strBytes.length;
2221

23-
for (uint256 i = 0; i < substrLen; i++) {
24-
if (location + i >= strLen) {
22+
for (uint256 i = 0; i < prefixLen; i++) {
23+
if (i >= strLen) {
2524
return false;
2625
}
2726

28-
if (substrBytes[i] != strBytes[location + i]) {
27+
if (prefixBytes[i] != strBytes[i]) {
2928
return false;
3029
}
3130
}
@@ -126,8 +125,6 @@ library WebAuthn {
126125
bytes memory authenticatorData,
127126
bool requireUserVerification,
128127
string memory clientDataJSON,
129-
uint256 challengeLocation,
130-
uint256 responseTypeLocation,
131128
uint256 r,
132129
uint256 s,
133130
uint256 x,
@@ -142,20 +139,15 @@ library WebAuthn {
142139
}
143140

144141
// Check that response is for an authentication assertion
145-
string memory responseType = '"type":"webauthn.get"';
146-
if (!contains(responseType, clientDataJSON, responseTypeLocation)) {
147-
return false;
148-
}
149-
150-
// Check that challenge is in the clientDataJSON
142+
// and that the challenge is in the clientDataJSON
143+
// as per https://www.w3.org/TR/webauthn-2/#clientdatajson-serialization
151144
string memory challengeB64url = Base64URL.encode(challenge);
152-
string memory challengeProperty = string.concat(
153-
'"challenge":"',
145+
string memory prefix = string.concat(
146+
'{"type":"webauthn.get","challenge":"',
154147
challengeB64url,
155148
'"'
156149
);
157-
158-
if (!contains(challengeProperty, clientDataJSON, challengeLocation)) {
150+
if (!startsWith(prefix, clientDataJSON)) {
159151
return false;
160152
}
161153

0 commit comments

Comments
 (0)