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

Allow integers as cookie names #847

Merged
merged 4 commits into from
Jun 3, 2024

Conversation

nosilver4u
Copy link
Contributor

Fixes #845 so that an exception isn't thrown for legitimate numeric cookie names.

Changes for both the constructor and parse() methods:

  • Modifies the check(s) for $name, to allow both string and integer cookie names.
  • Changes the exception thrown to indicate a string OR int is permitted.
  • Updates doc blocks to allow cookie names to be integers.
  • Modifies unit tests for $name parameter to allow strings and integers, and to validate the updated exception messages.

I have kept Cookie->name as a string, and cast $name to a string within the constructor, just in case some (external) code would be doing a strict comparison against the name, and expecting a string value. If that's not desirable, I can revert that change.

Pull Request Type

  • I have checked there is no other PR open for the same change.

This is a:

  • Bug fix
  • New feature
  • Documentation improvement
  • Code quality improvement

Context

Currently, multiple plugins, and possibly even WP core are throwing errors if someone has a cookie with a numeric name. Would love to see this issue go away, so that folks don't have to resort to deleting cookies in the dev tools of their browser.

Detailed Description

This issue typically comes up in plugins that are executing 'async' requests, such that the existing cookies ($_COOKIES) are copied into the 'cookies' parameter for a GET/POST request. One could sanitize the numeric key names at that point, but PHP will cast the indices back to integers, and then an exception will still be thrown.

Quality assurance

  • This change does NOT contain a breaking change (fix or feature that would cause existing functionality to change).
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added unit tests to accompany this PR.
  • The (new/existing) tests cover this PR 100%.
  • I have (manually) tested this code to the best of my abilities.
  • My code follows the style guidelines of this project.

@nosilver4u nosilver4u marked this pull request as ready for review December 4, 2023 18:26
@schlessera
Copy link
Member

schlessera commented Dec 11, 2023

It looks like our cookie parsing has been too loose in terms of what it expects.

However, instead of now adding integers and still not properly reflecting the RFC specification, I'd suggest we take a close look at what the RFC for cookies actually states and implement that instead (which will include integers, but also other characters that should work but currently don't).

In section 4.1.1 of the RFC 6265, the syntax sepcification states the following:

  • a cookie-pair is a cookie-name followed by = followed by a cookie-value
  • a cookie-name is a token as defined in RFC 2616, section 2.2

Then, in section 2.2 of RFC 2616, we have the following specification:

  • a token is a series of <any CHAR except CTLs or separators>
  • a CHAR is <any US-ASCII character (octets 0 - 127)>
  • a CTL is <any US-ASCII control character (octets 0 - 31) and DEL (127)>
  • a separator is "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <"> | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT
  • a SP is <US-ASCII SP, space (32)>
  • a HT is <US-ASCII HT, horizontal-tab (9)>
  • a <"> is <US-ASCII double-quote mark (34)>

The above provides specific rules on how to parse a cookie for validity. I suggest we add something like InputValidator::is_valid_rfc2616_token() that we then use to replace the current is_string() validation check in the cookie constructor.

@jrfnl
Copy link
Member

jrfnl commented Dec 11, 2023

PR #849 has been opened for the input validation functionality. Once that is accepted, solving this should be more straight forward.

@schlessera schlessera added this to the 2.1.0 milestone Feb 12, 2024
@jrfnl
Copy link
Member

jrfnl commented Feb 12, 2024

@nosilver4u PR #849 has now been merged, which should make it more straight-forward and stable to make the change you are proposing. Would you like to update your PR to start using the method as introduced in #849 ? Or do you expect us to get your PR to a mergable state ?

@nosilver4u
Copy link
Contributor Author

@jrfnl Thanks for the update, I'll take a look later this week and see what I can do!

@nosilver4u
Copy link
Contributor Author

I updated the validation to use the new InputValidator::is_valid_rfc2616_token() method from #849. One thing that could potentially be improved would be to use some of the test data written by @schlessera in the constructor/parse tests. However, I wasn't sure if it was appropriate to re-use the IsValidRfc2616TokenTest::dataInvalidValues() method. For example, would it make sense to add a dataInvalidNameValues() method to the ConstructorTest class that is basically just a wrapper for IsValidRfc2616TokenTest::dataInvalidValues()?

@jrfnl jrfnl force-pushed the 845-allow-integer-cookie-names branch from e5518e0 to bd7cb69 Compare June 3, 2024 10:13
@jrfnl
Copy link
Member

jrfnl commented Jun 3, 2024

@nosilver4u I've just rebased the PR without changes to allow the PR to get a running (and hopefully passing!) build.

@schlessera
Copy link
Member

Tests are failing right now, we're already looking into this and will push a change soon.

@schlessera schlessera force-pushed the 845-allow-integer-cookie-names branch from 873b93e to 71a2668 Compare June 3, 2024 10:50
@jrfnl jrfnl force-pushed the 845-allow-integer-cookie-names branch 2 times, most recently from ae6d654 to bb82ab0 Compare June 3, 2024 11:08
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

Looks good to me now.

Thanks @nosilver4u for bringing this to our attention and getting this PR set up!

@jrfnl
Copy link
Member

jrfnl commented Jun 3, 2024

I have kept Cookie->name as a string, and cast $name to a string within the constructor, just in case some (external) code would be doing a strict comparison against the name, and expecting a string value. If that's not desirable, I can revert that change.

That is a correct change as the cookie name should always be a string, though with the current PR, an allowance is added for it being passed as an integer. The name should still be a string for further processing though.

@jrfnl
Copy link
Member

jrfnl commented Jun 3, 2024

However, I wasn't sure if it was appropriate to re-use the IsValidRfc2616TokenTest::dataInvalidValues() method. For example, would it make sense to add a dataInvalidNameValues() method to the ConstructorTest class that is basically just a wrapper for IsValidRfc2616TokenTest::dataInvalidValues()?

Well, the tests for the InputValidator::is_valid_rfc2616_token() method don't need to be duplicated, but we do need to make sure that method is used to do the input validation. Commit bb82ab0 fixes this up.

@schlessera
Copy link
Member

@nosilver4u Unless you have any other feedback or objections, we'll merge this as it currently is in the coming days. Thanks for your work on this!

@jrfnl jrfnl mentioned this pull request Jun 3, 2024
5 tasks
@nosilver4u
Copy link
Contributor Author

I've verified my local test still works with the changes, so it all looks good to me!

nosilver4u and others added 4 commits June 3, 2024 20:33
Modify data validation checks, exception messages, and docblocks to allow cookie names to be integers. This affects both the constructor and the parse() method.
Also modified unit tests to allow strings and integers, and to validate the updated exception messages.
Updated the Cookie token/name validation to use the new InputValidator::is_valid_rfc2616_token() method, updated the exception messages, and modified the test cases to look for the updated exceptions.
The tests did now test that anything but integers as well as strings were rejected, but did not test that an input with a valid type, but an invalid value was also rejected.

Fixed now.
@jrfnl jrfnl force-pushed the 845-allow-integer-cookie-names branch from 2259f6f to a83a4da Compare June 3, 2024 18:33
@jrfnl
Copy link
Member

jrfnl commented Jun 3, 2024

Thanks for testing @nosilver4u !

I've rebased to get rid of the backmerge and get a passing build again. Will merge once the build has passed.

@nosilver4u
Copy link
Contributor Author

Thanks for testing @nosilver4u !

I've rebased to get rid of the backmerge and get a passing build again. Will merge once the build has passed.

Yeah, sorry for the noise, I was just trying to get my repo up to date and didn't realize it was pushing that upstream!

@jrfnl
Copy link
Member

jrfnl commented Jun 3, 2024

CodeCov is having trouble. Merging now anyway.

@jrfnl jrfnl merged commit 5c4e905 into WordPress:develop Jun 3, 2024
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cookies with numeric/integer names cause exception.
3 participants