Skip to content

Commit c12a20c

Browse files
exebaSebastiano Degantvdijen
committed
Allow "Secure" cookie attribute via HTTP on localhost (#2483)
* allow secure cookie on localhost * allow ipv4 & ipv6 loopback addresses * Remove excess parenthesis --------- Co-authored-by: Sebastiano Degan <sebastiano@localhost.localdomain> Co-authored-by: Tim van Dijen <tvdijen@gmail.com>
1 parent 2b6f97f commit c12a20c

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

docs/simplesamlphp-changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ See the [upgrade notes](https://simplesamlphp.org/docs/stable/simplesamlphp-upgr
1010
Released TBD
1111

1212
* Fix auth state AuthnInstant (#2478)
13+
* Allow "Secure" cookie attribute via HTTP on localhost (#2483)
1314

1415
## Version 2.3.8
1516

src/SimpleSAML/SessionHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function getCookieParams(): array
164164
'lifetime' => $config->getOptionalInteger('session.cookie.lifetime', 0),
165165
'path' => $config->getOptionalString('session.cookie.path', '/'),
166166
'domain' => $config->getOptionalString('session.cookie.domain', null),
167-
'secure' => $config->getOptionalBoolean('session.cookie.secure', $httpUtils->isHTTPS()),
167+
'secure' => $config->getOptionalBoolean('session.cookie.secure', $httpUtils->isSecureCookieAllowed()),
168168
'samesite' => $config->getOptionalString('session.cookie.samesite', null),
169169
'httponly' => true,
170170
];

src/SimpleSAML/SessionHandlerPHP.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ public function getCookieSessionId(): ?string
190190
$session_cookie_params = session_get_cookie_params();
191191

192192
$httpUtils = new Utils\HTTP();
193-
if ($session_cookie_params['secure'] && !$httpUtils->isHTTPS()) {
194-
throw new Error\Exception('Session start with secure cookie not allowed on http.');
193+
if ($session_cookie_params['secure'] && !$httpUtils->isSecureCookieAllowed()) {
194+
throw new Error\Exception('Session start with secure cookie not allowed on http (except on localhost).');
195195
}
196196

197197
@session_start();
@@ -322,9 +322,9 @@ public function setCookie(string $sessionName, ?string $sessionID, ?array $cooki
322322
}
323323

324324
$httpUtils = new Utils\HTTP();
325-
if ($cookieParams['secure'] && !$httpUtils->isHTTPS()) {
325+
if ($cookieParams['secure'] && !$httpUtils->isSecureCookieAllowed()) {
326326
throw new Error\CannotSetCookie(
327-
'Setting secure cookie on plain HTTP is not allowed.',
327+
'Setting secure cookie on plain HTTP (except on localhost) is not allowed.',
328328
Error\CannotSetCookie::SECURE_COOKIE,
329329
);
330330
}

src/SimpleSAML/Utils/HTTP.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,15 +1099,15 @@ public function setCookie(string $name, ?string $value, ?array $params = null, b
10991099
$params = $default_params;
11001100
}
11011101

1102-
// Do not set secure cookie if not on HTTPS
1103-
if ($params['secure'] && !$this->isHTTPS()) {
1102+
// Do not set secure cookie if not on HTTPS or localhost
1103+
if ($params['secure'] && !$this->isSecureCookieAllowed()) {
11041104
if ($throw) {
11051105
throw new Error\CannotSetCookie(
1106-
'Setting secure cookie on plain HTTP is not allowed.',
1106+
'Setting secure cookie on plain HTTP (except on localhost) is not allowed.',
11071107
Error\CannotSetCookie::SECURE_COOKIE,
11081108
);
11091109
}
1110-
Logger::warning('Error setting cookie: setting secure cookie on plain HTTP is not allowed.');
1110+
Logger::warning('Error setting cookie: setting secure cookie on plain HTTP (except on localhost) is not allowed.');
11111111
return;
11121112
}
11131113

@@ -1164,6 +1164,17 @@ public function setCookie(string $name, ?string $value, ?array $params = null, b
11641164
}
11651165

11661166

1167+
/**
1168+
* Check if "Secure" attribute on cookies is supported
1169+
*
1170+
* @return boolean True "Secure" attribute can be set, false otherwise.
1171+
*/
1172+
public function isSecureCookieAllowed(): bool
1173+
{
1174+
return $this->isHTTPS() || in_array($this->getSelfHost(), ['localhost', '127.0.0.1', '::1'], true);
1175+
}
1176+
1177+
11671178
/**
11681179
* Submit a POST form to a specific destination.
11691180
*

0 commit comments

Comments
 (0)