Repository navigation
Sessions: validate ids, opt-in query parameter, expiry, regenerate() - #14
Merged
Merged
Conversation
- A request may only bring a session id in the format PHP generates that has a stored session. The handlers implement SessionUpdateTimestampHandlerInterface and switch on session.use_strict_mode when a session starts. - The PSID query parameter is only read after SessionHandler::setQueryParameterEnabled (true). Off by default. - The log names where a session id came from, not the id. - DbSessionHandler treats a row older than session.gc_maxlifetime as absent and removes it; gc() uses the lifetime it is given. - Session::regenerate () gives the active session a new id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DqZUi2UiBJP6rZoCSRcdpE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes
Session id validation
SessionHandler::isValidSessionId()accepts ids in the format PHP generates: 22 to 256 characters out ofA-Za-z0-9,-.Session::connect($id)), the session cookie or the query parameter that is not in that format is ignored; the request gets a new session.SessionHandlerandDbSessionHandlerimplementSessionUpdateTimestampHandlerInterface(validateId,updateTimestamp), andstart()switches onsession.use_strict_mode. An id is only kept when it has a stored session; otherwise PHP generates a new one.DbSessionHandlerchecks for a row that has not expired (the same lookupread()uses, one query for both).SessionHandlerwraps PHP's own save handler and can only check for a stored session when that handler isfiles; with another save handler only the format is checked.updateTimestamp()writes the session, so a session without data is still stored on its first request, as before.Query parameter is opt-in
PSIDquery parameter is only read after\Neuron\SessionHandlers\SessionHandler::setQueryParameterEnabled (true);. It is off by default. The cookie still takes precedence.Logging
Expiry (
DbSessionHandler)read()treats a row whoseset_timeis older thansession.gc_maxlifetimeas absent and removes it.gc($maxlifetime)deletes rows older than the lifetime it is given, instead of a fixed 10 minutes.New method
Session::regenerate()callssession_regenerate_id(true)when a session is active (and the cookie can still be sent) and returns whether the id changed. The session data is kept. Meant for login code.Compatibility
PSIDquery parameter must callSessionHandler::setQueryParameterEnabled (true)before the session starts. Without it the parameter is ignored and such a request starts a new, empty session.Session::connect($id)must now belong to a stored session; it can no longer be used to create a session with a chosen id.DbSessionHandleris now exactlysession.gc_maxlifetimeseconds since the last request (PHP default: 1440). Before, a row was removed by garbage collection once it was 10 minutes old, whenever garbage collection happened to run. Applications that want a specific lifetime should setsession.gc_maxlifetimebefore the session starts.session.use_strict_modeis switched on bySessionHandler::start().read()also selectingset_time.Tests
tests/SessionHandlerTest.php,tests/DbSessionHandlerTest.phpandtests/SessionTest.phpcover the id format, the source resolution and the opt-in switch, the log messages, the expiry decision and the queries against a stub database. They do not start a session or send headers.🤖 Generated with Claude Code
https://claude.ai/code/session_01DqZUi2UiBJP6rZoCSRcdpE