GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||
GeezerVison is showing github.com — fix(cookies): ignore a numeric expires outside the Date range by xia-chao · Pull Request #5832 · nodejs/undici Conversation
Contributor
What this fixes
setCookie(headers, { name: 'Space', value: 'Cat', expires: 1e16 }) // before: Space=Cat; Expires=undefined, undefined undefined NaN undefined:undefined:undefined GMT // after: Space=Cat Fixes #5831. Why
if (cookie.expires != null && cookie.expires.toString() !== 'Invalid Date') { A bad The changeCheck the number itself, and leave the if (cookie.expires != null) { const expiresMs = cookie.expires instanceof Date ? cookie.expires.getTime() : cookie.expires if (typeof expiresMs === 'number' && Number.isFinite(expiresMs) && Math.abs(expiresMs) <= 8640000000000000) { out.push(`Expires=${toIMFDate(cookie.expires)}`) } }
TestsAdded cases in
Reverting only
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
gijs-martens
approved these changes
metcoder95
reviewed
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just add a comment of why that harcoded number
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@ ## main #5832 +/- ## ========================================== - Coverage 93.54% 93.53% -0.02% ========================================== Files 110 110 Lines 39596 39790 +194 ========================================== + Hits 37040 37217 +177 - Misses 2556 2573 +17 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
stringify guarded expires with a string comparison that only works for the
Date form:
if (cookie.expires != null && cookie.expires.toString() !== 'Invalid Date')
A Date whose time value is NaN stringifies to "Invalid Date" and is skipped,
but a number never does: NaN.toString() is "NaN" and 1e16.toString() is
"10000000000000000". The guard therefore always passes for a number outside
the range representable by Date, and toIMFDate reads NaN/undefined from every
getter and splices those tokens into the header value:
setCookie(headers, { name: 'Space', value: 'Cat', expires: 1e16 })
// Space=Cat; Expires=undefined, undefined undefined NaN undefined:undefined:undefined GMT
The emitted value is not a valid IMF-fixdate. The Max-Age branch a few lines
above already tests the type rather than a stringified value.
Treat a number as absent only when it is not a valid time value, keeping the
existing Date behaviour. Name the upper bound so it is clear it is the largest
time value a Date can represent rather than a magic number.
Non-finite numbers reach stringify already coerced to 0 by the unsigned long
long converter in lib/web/cookies/index.js, so they keep serializing as the
epoch; that coercion is a separate concern.
Signed-off-by: Xia Chao <[email protected]>
cd66bb8 to
2bbba02
Compare
There was an error while loading. Please reload this page.
* fix(cookies): keep a cookie named __proto__ in getCookies (#5663) getCookies built its result on a plain object literal, so assigning the __proto__ key reached the Object.prototype setter instead of creating an own property. The assigned value is a string, so the setter is a no-op and the cookie is dropped from the returned record. Reading that key back also returned Object.prototype, which breaks the documented Record<string, string> contract. Use a null-prototype object, as lib/ already does in six other places. Co-authored-by: Carlos Fuentes <[email protected]> (cherry picked from commit b73952a) * fix(cookies): keep an Expires of 0 when serializing a cookie (#5808) The Expires branch tested cookie.expires for truthiness, so a numeric 0 -- the Unix epoch, and the canonical way to say "expire this cookie now" -- dropped the attribute entirely. expires is typed Date | number, and the Max-Age check directly above already tests the type rather than truthiness for the same reason. (cherry picked from commit 6933139) * fix(cookies): ignore a numeric expires outside the Date range (#5832) stringify guarded expires with a string comparison that only works for the Date form: if (cookie.expires != null && cookie.expires.toString() !== 'Invalid Date') A Date whose time value is NaN stringifies to "Invalid Date" and is skipped, but a number never does: NaN.toString() is "NaN" and 1e16.toString() is "10000000000000000". The guard therefore always passes for a number outside the range representable by Date, and toIMFDate reads NaN/undefined from every getter and splices those tokens into the header value: setCookie(headers, { name: 'Space', value: 'Cat', expires: 1e16 }) // Space=Cat; Expires=undefined, undefined undefined NaN undefined:undefined:undefined GMT The emitted value is not a valid IMF-fixdate. The Max-Age branch a few lines above already tests the type rather than a stringified value. Treat a number as absent only when it is not a valid time value, keeping the existing Date behaviour. Name the upper bound so it is clear it is the largest time value a Date can represent rather than a magic number. Non-finite numbers reach stringify already coerced to 0 by the unsigned long long converter in lib/web/cookies/index.js, so they keep serializing as the epoch; that coercion is a separate concern. Signed-off-by: Xia Chao <[email protected]> Co-authored-by: Xia Chao <[email protected]> (cherry picked from commit 78aa1a0) --------- Signed-off-by: Xia Chao <[email protected]> Co-authored-by: Luan Taraschi <[email protected]> Co-authored-by: Carlos Fuentes <[email protected]> Co-authored-by: Ngo Quoc Viet <[email protected]> Co-authored-by: Xia Chao <[email protected]> Co-authored-by: Xia Chao <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
setCookie writes a broken Expires header when expires is an out-of-range number
There was an error while loading. Please reload this page.