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

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

Open
1 task done
jrfnl opened this issue May 24, 2022 · 0 comments
Open
1 task done

Comments

@jrfnl
Copy link
Member

jrfnl commented May 24, 2022

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.
@jrfnl jrfnl changed the title Cookie::format_for_[header|set_cookie](): unexpected behaviour for key/value is empty Cookie::format_for_[header|set_cookie](): unexpected behaviour for key/value when empty May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant