-
Notifications
You must be signed in to change notification settings - Fork 8k
ext/session: fix cookie_lifetime overflow #21704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
29eb822
2860ac4
070407c
481e9b7
83991b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -706,11 +706,25 @@ static PHP_INI_MH(OnUpdateCookieLifetime) | |
| #else | ||
| const zend_long maxcookie = ZEND_LONG_MAX / 2 - 1; | ||
| #endif | ||
| zend_long v = (zend_long)atol(ZSTR_VAL(new_value)); | ||
| if (v < 0) { | ||
| php_error_docref(NULL, E_WARNING, "CookieLifetime cannot be negative"); | ||
| zend_long lval = 0; | ||
| int oflow = 0; | ||
| uint8_t type = is_numeric_string_ex(ZSTR_VAL(new_value), ZSTR_LEN(new_value), &lval, NULL, false, &oflow, NULL); | ||
| if (UNEXPECTED(type != IS_LONG)) { | ||
| if (oflow != 0) { | ||
| php_error_docref(NULL, E_WARNING, "session.cookie_lifetime must be between 0 and " ZEND_LONG_FMT, maxcookie); | ||
| } else { | ||
| php_error_docref(NULL, E_WARNING, "session.cookie_lifetime must be an integer"); | ||
| } | ||
| return FAILURE; | ||
| } | ||
| if (lval < 0) { | ||
| php_error_docref(NULL, E_WARNING, "session.cookie_lifetime must be between 0 and " ZEND_LONG_FMT, maxcookie); | ||
| return FAILURE; | ||
| } else if (v > maxcookie) { | ||
| } else if (lval > maxcookie) { | ||
| php_error_docref(NULL, E_WARNING, "session.cookie_lifetime must be between 0 and " ZEND_LONG_FMT ", value clamped to maximum", maxcookie); | ||
| zend_long *p = ZEND_INI_GET_ADDR(); | ||
| *p = maxcookie; | ||
| entry->value = zend_long_to_str(maxcookie); | ||
| return SUCCESS; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it was a bug before for it to return SUCCESS, so I would rather have it return FAILURE and warn then silently change behaviour. Effectively just change the prior if condition to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. It changes logic and may break some implementations, but it's fine to me. |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| --TEST-- | ||
| session.cookie_lifetime rejects non-integer values | ||
| --EXTENSIONS-- | ||
| session | ||
| --SKIPIF-- | ||
| <?php include('skipif.inc'); ?> | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| ob_start(); | ||
|
|
||
| ini_set("session.cookie_lifetime", 100); | ||
|
|
||
| // Float strings are rejected | ||
| ini_set("session.cookie_lifetime", "1.5"); | ||
| var_dump(ini_get("session.cookie_lifetime")); | ||
|
|
||
| // Non-numeric strings are rejected | ||
| ini_set("session.cookie_lifetime", "abc"); | ||
| var_dump(ini_get("session.cookie_lifetime")); | ||
|
|
||
| // Negative values are rejected | ||
| ini_set("session.cookie_lifetime", -1); | ||
| var_dump(ini_get("session.cookie_lifetime")); | ||
|
|
||
| // Negative overflow strings are rejected | ||
| ini_set("session.cookie_lifetime", "-99999999999999999999"); | ||
| var_dump(ini_get("session.cookie_lifetime")); | ||
|
|
||
| ob_end_flush(); | ||
| ?> | ||
| --EXPECTF-- | ||
| Warning: ini_set(): session.cookie_lifetime must be an integer in %s on line %d | ||
| string(3) "100" | ||
|
|
||
| Warning: ini_set(): session.cookie_lifetime must be an integer in %s on line %d | ||
| string(3) "100" | ||
|
|
||
| Warning: ini_set(): session.cookie_lifetime must be between 0 and %d in %s on line %d | ||
| string(3) "100" | ||
|
|
||
| Warning: ini_set(): session.cookie_lifetime must be between 0 and %d in %s on line %d | ||
| string(3) "100" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --TEST-- | ||
| session.cookie_lifetime overflow value is clamped | ||
| --EXTENSIONS-- | ||
| session | ||
| --SKIPIF-- | ||
| <?php include('skipif.inc'); ?> | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| ob_start(); | ||
|
|
||
| // Set a valid value first | ||
| ini_set("session.cookie_lifetime", 100); | ||
| var_dump(ini_get("session.cookie_lifetime")); | ||
|
|
||
| // Set an overflow value - should succeed with warning, value clamped | ||
| ini_set("session.cookie_lifetime", PHP_INT_MAX); | ||
| $val = (int) ini_get("session.cookie_lifetime"); | ||
| var_dump($val < PHP_INT_MAX); // clamped, not PHP_INT_MAX | ||
| var_dump($val > 0); | ||
|
|
||
| // Valid values still work after clamping | ||
| ini_set("session.cookie_lifetime", 200); | ||
| var_dump(ini_get("session.cookie_lifetime")); | ||
|
|
||
| ob_end_flush(); | ||
| ?> | ||
| --EXPECTF-- | ||
| string(3) "100" | ||
|
|
||
| Warning: ini_set(): session.cookie_lifetime must be between 0 and %d, value clamped to maximum in %s on line %d | ||
| bool(true) | ||
| bool(true) | ||
| string(3) "200" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usual error message is something along the line of
must be of type int.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied + created an addition to
CODING_CONVENTIONS.mdin #21761