Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/web/cookies/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,16 @@ function parseUnparsedAttributes (unparsedAttributes, cookieAttributeList = {})
// 1. If the first character of the attribute-value is not a DIGIT or a
// "-" character, ignore the cookie-av.
const charCode = attributeValue.charCodeAt(0)
const startsWithDigit = charCode >= 48 && charCode <= 57
const startsWithSignedDigit = attributeValue[0] === '-' && attributeValue.length > 1

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

// 2. If the remainder of attribute-value contains a non-DIGIT
// character, ignore the cookie-av.
if (!/^\d+$/.test(attributeValue)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the following changes to ensure closer adherence to the specifications.

Suggested change
if (!/^\d+$/.test(attributeValue)) {
if (/[^\d]/.test(attributeValue.slice(1))) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, that reads much closer to the spec. I took the remainder check as suggested and also tightened step 1 to the current 6265bis wording (a "-" followed by a DIGIT). Without that, slice(1) lets a bare Max-Age=- and an empty value slip past step 2 and parse to NaN/0, so validating the sign up front keeps them ignored while step 2 stays the clean remainder check you wanted. The existing edge cases and the -1 case all still pass. Pushed in 4403160.

if (/[^\d]/.test(attributeValue.slice(1))) {
return parseUnparsedAttributes(unparsedAttributes, cookieAttributeList)
}

Expand Down
15 changes: 14 additions & 1 deletion test/cookie/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,22 @@ test('Set-Cookie parser', () => {
name: 'Space',
value: 'Cat',
secure: true,
httpOnly: true
httpOnly: true,
maxAge: -1
}])

for (const maxAge of ['-', '--1', '-1a', '+1', '']) {
headers = new Headers({
'set-cookie': `Space=Cat; Secure; HttpOnly; Max-Age=${maxAge}`
})
assert.deepEqual(getSetCookies(headers), [{
name: 'Space',
value: 'Cat',
secure: true,
httpOnly: true
}])
}

headers = new Headers({
'set-cookie': 'Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land'
})
Expand Down