Skip to content

Commit 6de6e9a

Browse files
authored
fix: accept a negative Set-Cookie Max-Age attribute (#5571)
* fix: accept a negative Set-Cookie Max-Age attribute Signed-off-by: arshiya tabasum <arshi@bugqore.com> * fix: check only the Max-Age remainder for non-DIGIT characters Signed-off-by: arshiya tabasum <arshi@bugqore.com> * revert Max-Age step 1 comment to the pinned spec wording Signed-off-by: arshiya tabasum <arshi@bugqore.com> --------- Signed-off-by: arshiya tabasum <arshi@bugqore.com>
1 parent e609acb commit 6de6e9a

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎lib/web/cookies/parse.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,16 @@ function parseUnparsedAttributes (unparsedAttributes, cookieAttributeList = {})
189189
// 1. If the first character of the attribute-value is not a DIGIT or a
190190
// "-" character, ignore the cookie-av.
191191
const charCode = attributeValue.charCodeAt(0)
192+
const startsWithDigit = charCode >= 48 && charCode <= 57
193+
const startsWithSignedDigit = attributeValue[0] === '-' && attributeValue.length > 1
192194

193-
if ((charCode < 48 || charCode > 57) && attributeValue[0] !== '-') {
195+
if (!startsWithDigit && !startsWithSignedDigit) {
194196
return parseUnparsedAttributes(unparsedAttributes, cookieAttributeList)
195197
}
196198

197199
// 2. If the remainder of attribute-value contains a non-DIGIT
198200
// character, ignore the cookie-av.
199-
if (!/^\d+$/.test(attributeValue)) {
201+
if (/[^\d]/.test(attributeValue.slice(1))) {
200202
return parseUnparsedAttributes(unparsedAttributes, cookieAttributeList)
201203
}
202204

‎test/cookie/cookies.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,22 @@ test('Set-Cookie parser', () => {
465465
name: 'Space',
466466
value: 'Cat',
467467
secure: true,
468-
httpOnly: true
468+
httpOnly: true,
469+
maxAge: -1
469470
}])
470471

472+
for (const maxAge of ['-', '--1', '-1a', '+1', '']) {
473+
headers = new Headers({
474+
'set-cookie': `Space=Cat; Secure; HttpOnly; Max-Age=${maxAge}`
475+
})
476+
assert.deepEqual(getSetCookies(headers), [{
477+
name: 'Space',
478+
value: 'Cat',
479+
secure: true,
480+
httpOnly: true
481+
}])
482+
}
483+
471484
headers = new Headers({
472485
'set-cookie': 'Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land'
473486
})

0 commit comments

Comments
 (0)