Skip to content

Commit 82dfd93

Browse files
committed
Throw on negative setcookie expiration timestamp
Fixes GH-10765
1 parent aef5250 commit 82dfd93

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ PHP NEWS
130130
. password_hash() will now chain the original RandomException to the ValueError
131131
on salt generation failure. (timwolla)
132132
. Fix GH-10239 (proc_close after proc_get_status always returns -1). (nielsdos)
133+
. Fix GH-10765 (Throw on negative setcookie expiration date). (ilutov)
133134

134135
- Streams:
135136
. Fixed bug #51056: blocking fread() will block even if data is available.

Zend/tests/gh10765_1.phpt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-10765: Throw on negative cookie expiration timestamp
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
8+
try {
9+
setcookie("name", "value", -1);
10+
} catch (Error $e) {
11+
echo $e->getMessage(), "\n";
12+
}
13+
14+
?>
15+
--EXPECT--
16+
setcookie(): "expires" option cannot be negative
17+
--EXPECTHEADERS--

Zend/tests/gh10765_2.phpt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-10765: Throw on negative cookie expiration timestamp
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
8+
try {
9+
setcookie("name", "value", ['expires' => -1]);
10+
} catch (Error $e) {
11+
echo $e->getMessage(), "\n";
12+
}
13+
14+
?>
15+
--EXPECT--
16+
setcookie(): "expires" option cannot be negative
17+
--EXPECTHEADERS--

ext/standard/head.c

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t e
161161

162162
smart_str_appends(&buf, COOKIE_MAX_AGE);
163163
smart_str_append_long(&buf, (zend_long) diff);
164+
} else if (UNEXPECTED(expires < 0)) {
165+
zend_value_error("%s(): \"expires\" option cannot be negative", get_active_function_name());
164166
}
165167
}
166168

0 commit comments

Comments
 (0)