-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Adds support for the SameSite attribute in cookies. #19104
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
Changes from all commits
a5ab3ba
85310fe
b60cb05
2988969
b78fad6
3e54e4a
38e9039
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,10 @@ class Cookie | |
| protected $secure; | ||
| protected $httpOnly; | ||
| private $raw; | ||
| private $sameSite; | ||
|
|
||
| const SAMESITE_LAX = 'lax'; | ||
| const SAMESITE_STRICT = 'strict'; | ||
|
|
||
| /** | ||
| * Constructor. | ||
|
|
@@ -38,10 +42,11 @@ class Cookie | |
| * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client | ||
| * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol | ||
| * @param bool $raw Whether the cookie value should be sent with no url encoding | ||
| * @param string|null $sameSite Whether the cookie will be available for cross-site requests | ||
| * | ||
| * @throws \InvalidArgumentException | ||
| */ | ||
| public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true, $raw = false) | ||
| public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true, $raw = false, $sameSite = null) | ||
| { | ||
| // from PHP source code | ||
| if (preg_match("/[=,; \t\r\n\013\014]/", $name)) { | ||
|
|
@@ -71,6 +76,12 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom | |
| $this->secure = (bool) $secure; | ||
| $this->httpOnly = (bool) $httpOnly; | ||
| $this->raw = (bool) $raw; | ||
|
|
||
| if (!in_array($sameSite, array(self::SAMESITE_LAX, self::SAMESITE_STRICT, null))) { | ||
| throw new \InvalidArgumentException('The sameSite parameter is not valid.'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see 4223997 |
||
| } | ||
|
|
||
| $this->sameSite = $sameSite; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about validating the domain? I.e. allowed values (
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -108,6 +119,10 @@ public function __toString() | |
| $str .= '; httponly'; | ||
| } | ||
|
|
||
| if (null !== $this->getSameSite()) { | ||
| $str .= '; samesite='.$this->getSameSite(); | ||
| } | ||
|
|
||
| return $str; | ||
| } | ||
|
|
||
|
|
@@ -200,4 +215,14 @@ public function isRaw() | |
| { | ||
| return $this->raw; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the SameSite attribute. | ||
| * | ||
| * @return string|null | ||
| */ | ||
| public function getSameSite() | ||
| { | ||
| return $this->sameSite; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should pass
trueas a third argumentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see 4223997