Skip to content

Commit 8046b0d

Browse files
committed
fix(parse): fix auth param regex pattern
1 parent 05c038d commit 8046b0d

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

_abnf.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
capture,
32
either,
43
maybe,
54
namedCapture,
@@ -42,7 +41,7 @@ const obsText = /[\x80-\xFF]/;
4241
const HTAB = /\t/;
4342
const qdtext = either(HTAB, SP, "\x21", /[\x23-\x5B/, /[\x5D-\x7E]/, obsText);
4443
const VCHAR = /[\x21-\x7E]/;
45-
const quotedPair = sequence("\\", either(HTAB, SP, VCHAR), obsText);
44+
const quotedPair = sequence("\\", either(HTAB, SP, VCHAR, obsText));
4645

4746
const quotedString = sequence(
4847
DQUOTE,
@@ -55,13 +54,16 @@ const authParam = sequence(
5554
BWS,
5655
"=",
5756
BWS,
58-
capture(either(token, quotedString)),
57+
either(
58+
namedCapture("token", token),
59+
namedCapture("quotedString", quotedString),
60+
),
5961
);
6062

6163
if (import.meta.main) {
6264
console.log("challenge:", challenge);
6365
console.log("element: ", element);
64-
console.log("authParam: ", authParam);
66+
console.log("authParam: ", optimize(authParam).toRegExp());
6567
console.log("token: ", optimize(token).toRegExp());
6668
console.log("token68: ", optimize(token68).toRegExp());
6769
console.log(

auth_param.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@
1919
"header": "a=a,b=b, c=c , d=d , e=e ",
2020
"expected": { "a": "a", "b": "b", "c": "c", "d": "d", "e": "e" }
2121
},
22+
{
23+
"name": "all char",
24+
"header": "a=abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
25+
"expected": {
26+
"a": "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
27+
}
28+
},
29+
{
30+
"name": "quoted-string",
31+
"header": "a=\"a\", b=\"b\"",
32+
"expected": { "a": "\"a\"", "b": "\"b\"" }
33+
},
34+
{
35+
"name": "quoted-pair",
36+
"header": "a=\"\\a\", b=\"\\\\\"",
37+
"expected": { "a": "\"a\"", "b": "\"\\\"" }
38+
},
2239
{
2340
"name": "duplicate key",
2441
"header": "a=a,a=a",
@@ -33,5 +50,15 @@
3350
"name": "invalid format",
3451
"header": "YWxhZGRpbjpvcGVuc2VzYW1l a=a",
3552
"must_fail": true
53+
},
54+
{
55+
"name": "invalid quoted-string",
56+
"header": "a=\u0000",
57+
"must_fail": true
58+
},
59+
{
60+
"name": "invalid quoted-pair",
61+
"header": "a=\"\u0000\"",
62+
"must_fail": true
3663
}
3764
]

parse.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,17 @@ type ParsedGroups = {
5858
};
5959

6060
const reAuthParam =
61-
/^(?<key>[!#$%&'*+.^_`|~\dA-Za-z-]+)[ \t]*=[ \t]*(?<value>[!#$%&'*+.^_`|~\dA-Za-z-]+|"(?:\t| |!|[\x23-\x5B/, /[\x5D-\x7E]|[\x80-\xFF]|\\(?:\t| |[\x21-\x7E])[\x80-\xFF])*")$/;
61+
/^(?<key>[\w!#$%&'*+.^`|~-]+)[\t ]*=[\t ]*(?:(?<token>[\w!#$%&'*+.^`|~-]+)|(?<quotedString>"(?:\t| |!|[ \x23-\x5B\x5D-\x7E]|[\x80-\xFF]|\\(?:\t| |[\x21-\x7E]|[\x80-\xFF]))*"))$/;
62+
63+
type AuthParamGroups =
64+
& { key: string }
65+
& ({ token: string; quotedString: never } | {
66+
token: never;
67+
quotedString: string;
68+
});
6269

6370
/** Parse string into {@link AuthParam}.
71+
* @throws {SyntaxError} It the input is invalid [auth-param](https://www.rfc-editor.org/rfc/rfc9110.html#section-11.2-5).
6472
* @throws {Error} If the auth param key is duplicated.
6573
*/
6674
export function parseAuthParams(input: string): AuthParams {
@@ -71,7 +79,12 @@ export function parseAuthParams(input: string): AuthParams {
7179

7280
if (!result || !result.groups) throw SyntaxError(Msg.InvalidSyntax);
7381

74-
return [result.groups.key, result.groups.value] as const;
82+
const groups = result.groups as AuthParamGroups;
83+
const value = isString(groups.token)
84+
? groups.token
85+
: groups.quotedString.replace(/\\(.)/g, "$1");
86+
87+
return [groups.key, value] as const;
7588
});
7689

7790
const duplicates = duplicate(

0 commit comments

Comments
 (0)