Skip to content

ext/session: fix cookie_lifetime overflow - #21704

Merged
Girgias merged 5 commits into
php:masterfrom
jorgsowa:fix/session-cookie-lifetime-overflow
Apr 20, 2026
Merged

ext/session: fix cookie_lifetime overflow#21704
Girgias merged 5 commits into
php:masterfrom
jorgsowa:fix/session-cookie-lifetime-overflow

Conversation

@jorgsowa

@jorgsowajorgsowa commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

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.

Edit:
Added validation of value of maxcookie, only numeric strings are allowed.

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.

@GirgiasGirgias left a comment

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.

While at it could you fix the way we parse the string as well? As this probably allows non numeric strings and float strings.

Comment threadext/session/session.c Outdated
@jorgsowa
jorgsowa marked this pull request as ready for review April 11, 2026 09:43
@jorgsowa
jorgsowa requested a review from GirgiasApril 11, 2026 09:46

@GirgiasGirgias left a comment

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.

Getting there, minor comments. Thanks for tackling this :)

Comment threadext/session/session.c Outdated
Comment threadext/session/session.c Outdated
Comment threadext/session/session.c Outdated
Comment threadext/session/session.c Outdated
Comment on lines 723 to 729
} 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;
}

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.

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 if (lval < 0 || lval > maxcookie) {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The 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.

Comment threadext/session/session.c Outdated
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");

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.

The usual error message is something along the line of must be of type int.

Copy link
Copy Markdown
ContributorAuthor

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.md in #21761

@jorgsowa
jorgsowaforce-pushed the fix/session-cookie-lifetime-overflow branch from b99050e to 83991b6CompareApril 14, 2026 21:36
@jorgsowa
jorgsowa requested a review from GirgiasApril 18, 2026 18:22
@Girgias
Girgias merged commit 383ff8c into php:masterApr 20, 2026
19 checks passed
Girgias pushed a commit that referenced this pull request May 24, 2026
Add NEWS entries for three ext/session changes that were never documented:
- SameSite cookie validation (GH-21670)
- session.cookie_lifetime improved parsing (GH-21704)
- Recursive GC cleanup for nested session directories (GH-21491)
@jorgsowa
jorgsowa deleted the fix/session-cookie-lifetime-overflow branch June 3, 2026 21:39
Sign up for freeto 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

@jorgsowa@Girgias