Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 69d8d9a

Browse files
[HttpFoundation] make cookies auto-secure when passing them $secure = null
1 parent 2879baf commit 69d8d9a

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Symfony/Component/HttpFoundation/Cookie.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Cookie
2727
protected $httpOnly;
2828
private $raw;
2929
private $sameSite;
30+
private $secureDefault = false;
3031

3132
const SAMESITE_LAX = 'lax';
3233
const SAMESITE_STRICT = 'strict';
@@ -72,14 +73,14 @@ public static function fromString($cookie, $decode = false)
7273
* @param int|string|\DateTimeInterface $expire The time the cookie expires
7374
* @param string $path The path on the server in which the cookie will be available on
7475
* @param string|null $domain The domain that the cookie is available to
75-
* @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
76+
* @param bool|null $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client or null to set it later using {@see setSecureDefault()}
7677
* @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
7778
* @param bool $raw Whether the cookie value should be sent with no url encoding
7879
* @param string|null $sameSite Whether the cookie will be available for cross-site requests
7980
*
8081
* @throws \InvalidArgumentException
8182
*/
82-
public function __construct(string $name, string $value = null, $expire = 0, ?string $path = '/', string $domain = null, bool $secure = false, bool $httpOnly = true, bool $raw = false, string $sameSite = null)
83+
public function __construct(string $name, string $value = null, $expire = 0, ?string $path = '/', string $domain = null, ?bool $secure = false, bool $httpOnly = true, bool $raw = false, string $sameSite = null)
8384
{
8485
// from PHP source code
8586
if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
@@ -232,7 +233,7 @@ public function getPath()
232233
*/
233234
public function isSecure()
234235
{
235-
return $this->secure;
236+
return $this->secure ?? $this->secureDefault;
236237
}
237238

238239
/**
@@ -274,4 +275,12 @@ public function getSameSite()
274275
{
275276
return $this->sameSite;
276277
}
278+
279+
/**
280+
* @param bool $default The default value of the "secure" flag when it is set to null
281+
*/
282+
public function setSecureDefault(bool $default): void
283+
{
284+
$this->secureDefault = $default;
285+
}
277286
}

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ public function prepare(Request $request)
313313

314314
$this->ensureIEOverSSLCompatibility($request);
315315

316+
if ($request->isSecure()) {
317+
foreach ($headers->getCookies() as $cookie) {
318+
$cookie->setSecureDefault(true);
319+
}
320+
}
321+
316322
return $this;
317323
}
318324

0 commit comments

Comments
 (0)