Skip to content

ext/session: Validate SameSite cookie attribute against allowed values#21670

Open
jorgsowa wants to merge 1 commit intophp:masterfrom
jorgsowa:fix/session-samesite-validation
Open

ext/session: Validate SameSite cookie attribute against allowed values#21670
jorgsowa wants to merge 1 commit intophp:masterfrom
jorgsowa:fix/session-samesite-validation

Conversation

@jorgsowa
Copy link
Copy Markdown
Contributor

@jorgsowa jorgsowa commented Apr 8, 2026

Applied validation of samesite option in ini, setcookie(), session_set_cookie_params(). Four values are allowed Strict, Lax, None, or empty string. Doc: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Set-Cookie#samesitesamesite-value

We can apply similar validation to other values, as a few values are vulnerable to CRLF Injection. I'm not sure it's a security issue, so I will just publish it in the next PRs.

Extract php_is_valid_samesite_value() in ext/standard/head.c as a
shared validation function that enforces the SameSite whitelist
(Strict, Lax, None, or empty string) with case-insensitive matching.

Apply validation in both setcookie()/setrawcookie() (replacing the
existing TODO comment) and the session.cookie_samesite INI handler.
Previously arbitrary strings including CRLF sequences were accepted
and appended verbatim into the Set-Cookie header.
@jorgsowa jorgsowa force-pushed the fix/session-samesite-validation branch from 4bcb52d to d1d79ac Compare April 8, 2026 09:52
Comment on lines 1 to 5
--TEST--
Test session_set_cookie_params() function : variation
Test session_set_cookie_params() samesite validation
--INI--
session.cookie_samesite=test
session.cookie_samesite=Lax
--EXTENSIONS--
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test where the INI setting has an invalid value? :)


var_dump(ini_get("session.cookie_samesite"));
var_dump(session_set_cookie_params(["samesite" => "nothing"]));
// Valid values
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have these comments be an echo to delimitate the output more easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants