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: narrow the validation of cookies to match RFC6265 #167

Merged
merged 6 commits into from
Oct 1, 2024

Conversation

bewinsnw
Copy link
Contributor

fixes #165. Previously, validation on serialization used the field-content regexp from RFC7230 to perform all checks. However, that is the regexp for the cookie header as a whole, but each individual part of a cookie has a tighter restriction.

In the bug report #165 I demonstrated that whitespace in names and values was invalid but allowed by the field-content pattern. In this code I started by adding tests for those two cases, then added the expressions derived from the RFC; the relevant portions of the RFC have been inlined as comments.

Removing the field-content regexp unearthed that as well as those two it was being used to validate domain names and paths. Paths are fairly unrestricted (being any ascii character except control characters and semicolon) but the code was wrong here too - it allowed all 8-bit characters not just 7-bit characters.

Domain names have a well recognised pattern, there are only a couple of gotchas: RFC6265 explicitly says RFC1123 applies, so the leading character of a label is allowed to be a digit; and while I wondered for a moment if this should allow things like [::1] the domain matching section of https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3 teaches that domain values are not ip addresses.

fixes jshttp#165. Previously, validation on serialization used the field-content
regexp from RFC7230 to perform all checks. However, that is the regexp
for the cookie header as a whole, but each individual part of a cookie
has a tighter restriction.

In the bug report jshttp#165 I demonstrated that whitespace in names and values
was invalid but allowed by the field-content pattern. In this code I
started by adding tests for those two cases, then added the expressions
derived from the RFC; the relevant portions of the RFC have been inlined
as comments.

Removing the field-content regexp unearthed that as well as those two
it was being used to validate domain names and paths. Paths are fairly
unrestricted (being any ascii character except control characters and
semicolon) but the code was wrong here too - it allowed all 8-bit characters
not just 7-bit characters.

Domain names have a well recognised pattern, there are only a couple of
gotchas: RFC6265 explicitly says RFC1123 applies, so the leading character
of a label is allowed to be a digit; and while I wondered for a moment
if this should allow things like [::1] the domain matching section of
https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3
teaches that domain values are _not_ ip addresses.
@bewinsnw bewinsnw changed the title fix: narrow the validation cookies to match RFC6265 fix: narrow the validation of cookies to match RFC6265 Jul 17, 2024
index.js Outdated Show resolved Hide resolved
@wesleytodd
Copy link
Member

Thanks, this VERY well structured from the comments to the new regular expressions, great work. CI is passing, but I want to wait a bit and see if anyone else on the project as opinions on this. My one review comment is not intended to block, so if you feel strongly about that form of regex please tell me.

The regexp should be in the same order as the RFC grammar, for ease
of checking.
Copy link
Member

@IamLizu IamLizu left a comment

Choose a reason for hiding this comment

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

LGTM 👍

index.js Show resolved Hide resolved
index.js Show resolved Hide resolved
@wesleytodd
Copy link
Member

Hey, can we fix up the lint issues? After that I think we can land this and update in both express v4 and v5

@bewinsnw
Copy link
Contributor Author

Hey, can we fix up the lint issues? After that I think we can land this and update in both express v4 and v5

🤦 that's what I get for doing those last two commits in the online editor. Fixed, thanks

@bewinsnw
Copy link
Contributor Author

I think that coveralls just glitched out. I can see at https://coveralls.io/builds/68891763 that the coveralls run was successful, but it looks like it never reported back to github. Just needs a kick.

hdtmccallie added a commit to hdtmccallie/jshttp-cookie that referenced this pull request Oct 1, 2024
…C validations

These tests are to help illustrate vulnerabilities in the cookie name, domain and path strings. The lack of filtering delimiters like semicolon and comma is the major concern but other characters can present security problems as well.

[PR jshttp#167][1] fixes these vulnerabilities and tests should pass with those new validations added.

[1]: jshttp#167
@blakeembrey blakeembrey merged commit e100428 into jshttp:master Oct 1, 2024
26 checks passed
hdtmccallie added a commit to hdtmccallie/jshttp-cookie that referenced this pull request Oct 2, 2024
…ations

These tests better align with the proper RFC rules for the cookie attributes of `name`, `domain`, and `path`.

These test are meant to be implemented along with [PR jshttp#167][1] that adds more fine-grained validations based on RFC rules.

[1]: jshttp#167
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

incorrect validation regexes in serialize
4 participants