Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 6 additions & 4 deletions lib/web/cookies/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,19 @@ function parseUnparsedAttributes (unparsedAttributes, cookieAttributeList = {})
// If the attribute-name case-insensitively matches the string "Max-
// Age", the user agent MUST process the cookie-av as follows.

// 1. If the first character of the attribute-value is not a DIGIT or a
// "-" character, ignore the cookie-av.
// 1. If the first character of the attribute-value is neither a DIGIT,
// nor a "-" character followed by a DIGIT, ignore the cookie-av.

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.

Could you please revert this change? This comment block is intended to preserve the exact wording from the specification.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted, the comment block is back to the original spec wording. The code still does the sign-followed-by-digit check but the comment stays verbatim.

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
Contributor 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