Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): add disclaimer about parsing set-cookie #213

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Parse a HTTP `Cookie` header string and returning an object of all cookie name-v
The `str` argument is the string representing a `Cookie` header value and `options` is an
optional object containing additional parsing options.

**NOTE:** This method does not parse the `set-cookie` header. It parses the `cookie` header. This means you cannot do `cookie.parse(cookie.serialize('name', 'value'))`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
**NOTE:** This method does not parse the `set-cookie` header. It parses the `cookie` header. This means you cannot do `cookie.parse(cookie.serialize('name', 'value'))`.
**Note:** This method does not parse the `Set-Cookie` header.

It already says it parses the Cookie header twice in the above paragraph, would prefer to keep the note shorter so someone skimming actually reads it.

Copy link
Member

Choose a reason for hiding this comment

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

lmao I went in the opposite direction with my suggestion.

Copy link
Member

@jonchurch jonchurch Nov 20, 2024

Choose a reason for hiding this comment

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

I'd split the difference

⚠️ Note: This method does not parse the Set-Cookie header.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am personally in favor of a more detailed description. Honestly I would prefer to talk about the difference and even show a detailed example (on top of what your suggestion is below). The larger we make the block warning for this the more likely someone is to see it IMO. Replying here since this is the one with the discussion, but I lean toward @jonchurch's direction below.

Copy link
Member

@jonchurch jonchurch Nov 20, 2024

Choose a reason for hiding this comment

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

The example can be improved, as that code works fine.

serialize w/ no options essentially makes a valid Cookie string as it just sets the cookie name and value.
The real trouble is when folks use the options. Specifically the flag options like HttpOnly, Secure, Partitioned which aren't key value pairs like a cookie header.

I think we should give folks a little more info so they understand the failure state and risk.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
**NOTE:** This method does not parse the `set-cookie` header. It parses the `cookie` header. This means you cannot do `cookie.parse(cookie.serialize('name', 'value'))`.
> ⚠️ **NOTE:** This method does not parse the `set-cookie` header. It parses the `cookie` header. This means you cannot safely parse a `set-cookie` header, or the output of `serialize`.
>
> E.g. `cookie.parse(cookie.serialize('name', 'value', { httpOnly: true, secure: true, partitioned: true }))` will drop the `httpOnly, secure, partitioned` values entirely. Parsing `set-cookie` header values will result in unexpected results!


```js
const cookies = cookie.parse("foo=bar; equation=E%3Dmc%5E2");
// { foo: 'bar', equation: 'E=mc^2' }
Expand Down