WordPress / WordPress/Requests

Cookie::format_for_[header|set_cookie](): unexpected behaviour for key/value when empty

Open
#743 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Status: triage
Dominant language
PHP
Stars
3.6k
Forks
500
Avg merge
2d 21h
Merged PRs (30d)
5

Description

Summary

Discovered while writing tests, see #740

The Cookie class takes the required $name and $value` parameters. These parameters are accepted "blindly". The only validation being done is checking that a string has been passed.

This can lead to surprising - and possibly incorrect - results when those cookies are formatted.

Given the following code sample
$cookie = new Cookie('', '');
var_dump($cookie->format_for_header()); // (string) '='
var_dump($cookie->format_for_set_cookie()); // (string) '='

$cookie = new Cookie('key-only', '');
var_dump($cookie->format_for_header()); // (string) 'key-only='
var_dump($cookie->format_for_set_cookie()); // (string) 'key-only='

$cookie = new Cookie('', 'value-only');
var_dump($cookie->format_for_header()); // (string) '=value-only'
var_dump($cookie->format_for_set_cookie()); // (string) '=value-only'
I'd expect the following behaviour
  • For both an empty key as well as an empty value, I'd expect the return to be an empty string (or an Exception).
  • For key only, I think the behaviour is correct.
  • For value only, I suspect the return value should be value-only (without the =).

Alternatively, I can imagine that a non-empty value for the key should be regarded as invalid and rejected with an exception when the Cookie is being constructed.

Additional context

This may need some research into RFCs on how cookies should be formatted.

Tested against develop branch?

  • I have verified the issue still exists in the develop branch of Requests.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the Cookie constructor and the format_for_header() and format_for_set_cookie() methods, then review the tests referenced by #740 and the relevant cookie-formatting RFCs. The behavior is not yet settled between empty output, an exception, or omitting the separator, so completion requires an agreed rule plus tests covering empty keys and values on the develop branch.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.