Skip to content

Commit 29eb822

Browse files
committed
ext/session: fix cookie_lifetime overflow causing INI value desync
When session.cookie_lifetime was set to a value larger than maxcookie, OnUpdateCookieLifetime returned SUCCESS without updating the internal long value, causing ini_get() string and PS(cookie_lifetime) to go out of sync. Now the value is properly clamped to maxcookie with both the string and internal long updated consistently, and a warning is emitted.
1 parent dc7f6a8 commit 29eb822

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

ext/session/session.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,10 @@ static PHP_INI_MH(OnUpdateCookieLifetime)
711711
php_error_docref(NULL, E_WARNING, "CookieLifetime cannot be negative");
712712
return FAILURE;
713713
} else if (v > maxcookie) {
714+
php_error_docref(NULL, E_WARNING, "CookieLifetime value too large, value was set to the maximum of " ZEND_LONG_FMT, maxcookie);
715+
zend_long *p = ZEND_INI_GET_ADDR();
716+
*p = maxcookie;
717+
entry->value = zend_long_to_str(maxcookie);
714718
return SUCCESS;
715719
}
716720

ext/session/tests/gh16290.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ session
66
<?php include('skipif.inc'); ?>
77
--FILE--
88
<?php
9+
ob_start();
910
session_set_cookie_params(PHP_INT_MAX, '/', null, false, true);
1011
echo "DONE";
12+
ob_end_flush();
1113
?>
12-
--EXPECT--
14+
--EXPECTF--
15+
Warning: session_set_cookie_params(): CookieLifetime value too large, value was set to the maximum of %d in %s on line %d
1316
DONE
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
session.cookie_lifetime overflow value is clamped
3+
--EXTENSIONS--
4+
session
5+
--SKIPIF--
6+
<?php include('skipif.inc'); ?>
7+
--FILE--
8+
<?php
9+
10+
ob_start();
11+
12+
// Set a valid value first
13+
ini_set("session.cookie_lifetime", 100);
14+
var_dump(ini_get("session.cookie_lifetime"));
15+
16+
// Set an overflow value - should succeed with warning, value clamped
17+
ini_set("session.cookie_lifetime", PHP_INT_MAX);
18+
$val = (int) ini_get("session.cookie_lifetime");
19+
var_dump($val < PHP_INT_MAX); // clamped, not PHP_INT_MAX
20+
var_dump($val > 0); // positive
21+
22+
// Valid values still work after clamping
23+
ini_set("session.cookie_lifetime", 200);
24+
var_dump(ini_get("session.cookie_lifetime"));
25+
26+
ob_end_flush();
27+
?>
28+
--EXPECTF--
29+
string(3) "100"
30+
31+
Warning: ini_set(): CookieLifetime value too large, value was set to the maximum of %d in %s on line %d
32+
bool(true)
33+
bool(true)
34+
string(3) "200"

0 commit comments

Comments
 (0)