[TikiWiki-commits] [Git][tikiwiki/tiki][master] [FIX] Samesite http header for cookies wasn't obeyed for the PHPSESSID_CSRF cookie

"Rodriguez Nyiringabo \(@rodriguezny\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <6a1d598f340ce_381c03bd0371bd@gitlab-sidekiq-low-urgency-cpu-bound-v2-57bc5b685d-9dqh9.mail>

Rodriguez Nyiringabo pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
a9f6b922 by Benoit Grégoire at 2026-06-01T09:47:48+00:00
[FIX] Samesite http header for cookies wasn't obeyed for the PHPSESSID_CSRF cookie
---
* Changes samesite from strict to lax

* [FIX] Samesite http header for cookies wasn't obeyed for the PHPSESSID_CSRF cookie

See merge request tikiwiki/tiki!10203

- - - - -


4 changed files:

- lib/CookieConsent/CookieConsentLib.php
- lib/setup/cookies.php
- lib/tiki-js.js
- lib/tikiaccesslib.php


Changes:

=====================================
lib/CookieConsent/CookieConsentLib.php
=====================================
@@ -303,7 +303,13 @@ class CookieConsentLib
                 // See note at the begining of fucntion.  As far as I can tell, this is unreachable code - benoitg - 2026-06-25
                 $_SESSION['tiki_cookie_jar'][$name] = $value;
             } else {
-                setcookie($name, $value, $expire, $path, $domain, $secure);
+                $options = [
+                    'expires' => $expire,
+                    'path' => $path,
+                    'domain' => $domain,
+                    'secure' => $secure
+                ];
+                setcookie_obeySetCookieParams($name, $value, $options);
             }
         }
     }


=====================================
lib/setup/cookies.php
=====================================
@@ -30,6 +30,25 @@ if (isset($_SESSION['tiki_cookie_jar'])) {
 
 $smarty->assign_by_ref('cookie', $_COOKIE);
 
+/** This function is meant to replace PHPs low-level https://www.php.net/manual/en/function.setcookie.php function.  It only implements the 3 parameter version.
+ *
+ * the reason it exists is that:
+ *
+ * It is completely silly that setcookie is slightly different from the array returned by session_get_cookie_params.
+ * It is also completely silly that it will not obey smaesite in session_set_cookie_params if you pass lifetime explicitly.  Maybe it disregards other parameters in some forms, I did not check.
+ * benoitg -2025-05-12.
+ *
+ *
+ */
+function setcookie_obeySetCookieParams(string $name, string $value = "", array $options = []): bool
+{
+    $sessionParams = session_get_cookie_params();
+    // Yes, it is completely silly that the options parameter of setcookie is slightly different from the array returned by session_get_cookie_params().
+    $sessionParams['expires'] = $sessionParams['lifetime'];
+    unset($sessionParams['lifetime']);
+    $finalOptions = array_merge($sessionParams, $options);
+    return setcookie($name, $value, $finalOptions);
+}
 /**
  * This seems to be the mirror function of CookieConsentLib::setCookieSection(), but
  * I am not 100% sure since CookieConsentLib has it's own setCookie() method - benoitg - 2026-03-26


=====================================
lib/tiki-js.js
=====================================
@@ -622,7 +622,7 @@ function setCookieBrowser(name, value, section, expires, path, domain, secure) {
     }
     else {
         document.cookie = name + "=" + encodeURIComponent(value) + ((expires) ? "; expires=" + expires.toGMTString() : "")
-        + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
+        + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "" + "; samesite=Lax");
     }
 }
 


=====================================
lib/tikiaccesslib.php
=====================================
@@ -411,8 +411,7 @@ class TikiAccessLib extends TikiLib
         $cipherTicket = $this->encryptCsrfTicket($this->ticket);
 
         // you can set even if is the same value
-        $session_params = session_get_cookie_params();
-        setcookie(session_name() . '_CSRF', $cipherTicket, 0, $session_params['path'], $session_params['domain'], $session_params['secure'], true);
+        setcookie_obeySetCookieParams(session_name() . '_CSRF', $cipherTicket);
     }
 
 



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/a9f6b9229a43e5154ce51299fdd6b5f7073341a5

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/a9f6b9229a43e5154ce51299fdd6b5f7073341a5
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.