Skip to content

Commit 6f2fbef

Browse files
Merge pull request #51657 from SBizienFilippiPEReN/cookie-domain
cookie_domain config option for setting cookie on a wider domain
2 parents b69f041 + a14cade commit 6f2fbef

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

config/config.sample.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@
8787
'[2001:db8::1]'
8888
],
8989

90+
/**
91+
* The validity domain for cookies, for example '' (cookies will be sent only
92+
* the domain that defined it, e.g. 'demo.example.org'), 'demo.example.org'
93+
* (cookies will be valid for the domain and all subdomains), ...
94+
*
95+
* Defaults to '' (safe option)
96+
*/
97+
'cookie_domain' => '',
9098

9199
/**
92100
* Where user files are stored. The SQLite database is also stored here, when

lib/base.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ public static function initSession(): void {
393393
$cookie_path = OC::$WEBROOT ? : '/';
394394
ini_set('session.cookie_path', $cookie_path);
395395

396+
// set the cookie domain to the Nextcloud domain
397+
$cookie_domain = self::$config->getValue('cookie_domain', '');
398+
if ($cookie_domain) {
399+
ini_set('session.cookie_domain', $cookie_domain);
400+
}
401+
396402
// Let the session name be changed in the initSession Hook
397403
$sessionName = OC_Util::getInstanceId();
398404

lib/private/Session/CryptoWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function __construct(
5959
[
6060
'expires' => 0,
6161
'path' => $webRoot,
62-
'domain' => '',
62+
'domain' => \OCP\Server::get(\OCP\IConfig::class)->getSystemValueString('cookie_domain'),
6363
'secure' => $secureCookie,
6464
'httponly' => true,
6565
'samesite' => 'Lax',

lib/private/User/Session.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -967,14 +967,15 @@ public function setMagicInCookie($username, $token) {
967967
if ($webRoot === '') {
968968
$webRoot = '/';
969969
}
970+
$domain = $this->config->getSystemValueString('cookie_domain');
970971

971972
$maxAge = $this->config->getSystemValueInt('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
972973
\OC\Http\CookieHelper::setCookie(
973974
'nc_username',
974975
$username,
975976
$maxAge,
976977
$webRoot,
977-
'',
978+
$domain,
978979
$secureCookie,
979980
true,
980981
\OC\Http\CookieHelper::SAMESITE_LAX
@@ -984,7 +985,7 @@ public function setMagicInCookie($username, $token) {
984985
$token,
985986
$maxAge,
986987
$webRoot,
987-
'',
988+
$domain,
988989
$secureCookie,
989990
true,
990991
\OC\Http\CookieHelper::SAMESITE_LAX
@@ -995,7 +996,7 @@ public function setMagicInCookie($username, $token) {
995996
$this->session->getId(),
996997
$maxAge,
997998
$webRoot,
998-
'',
999+
$domain,
9991000
$secureCookie,
10001001
true,
10011002
\OC\Http\CookieHelper::SAMESITE_LAX
@@ -1011,18 +1012,19 @@ public function setMagicInCookie($username, $token) {
10111012
public function unsetMagicInCookie() {
10121013
//TODO: DI for cookies and IRequest
10131014
$secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https';
1015+
$domain = $this->config->getSystemValueString('cookie_domain');
10141016

10151017
unset($_COOKIE['nc_username']); //TODO: DI
10161018
unset($_COOKIE['nc_token']);
10171019
unset($_COOKIE['nc_session_id']);
1018-
setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true);
1019-
setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true);
1020-
setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true);
1020+
setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, $domain, $secureCookie, true);
1021+
setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, $domain, $secureCookie, true);
1022+
setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, $domain, $secureCookie, true);
10211023
// old cookies might be stored under /webroot/ instead of /webroot
10221024
// and Firefox doesn't like it!
1023-
setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true);
1024-
setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true);
1025-
setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true);
1025+
setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', $domain, $secureCookie, true);
1026+
setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', $domain, $secureCookie, true);
1027+
setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', $domain, $secureCookie, true);
10261028
}
10271029

10281030
/**

0 commit comments

Comments
 (0)