Skip to content

Commit 4908090

Browse files
BrokenSourceCodefabpot
authored andcommitted
[HttpFoundation] Add session ID regex comment
1 parent bf46a8d commit 4908090

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,36 @@ public function start()
153153
}
154154

155155
$sessionId = $_COOKIE[session_name()] ?? null;
156+
/*
157+
* Explanation of the session ID regular expression: `/^[a-zA-Z0-9,-]{22,250}$/`.
158+
*
159+
* ---------- Part 1
160+
*
161+
* The part `[a-zA-Z0-9,-]` is related to the PHP ini directive `session.sid_bits_per_character` defined as 6.
162+
* See https://www.php.net/manual/en/session.configuration.php#ini.session.sid-bits-per-character.
163+
* Allowed values are integers such as:
164+
* - 4 for range `a-f0-9`
165+
* - 5 for range `a-v0-9`
166+
* - 6 for range `a-zA-Z0-9,-`
167+
*
168+
* ---------- Part 2
169+
*
170+
* The part `{22,250}` is related to the PHP ini directive `session.sid_length`.
171+
* See https://www.php.net/manual/en/session.configuration.php#ini.session.sid-length.
172+
* Allowed values are integers between 22 and 256, but we use 250 for the max.
173+
*
174+
* Where does the 250 come from?
175+
* - The length of Windows and Linux filenames is limited to 255 bytes. Then the max must not exceed 255.
176+
* - The session filename prefix is `sess_`, a 5 bytes string. Then the max must not exceed 255 - 5 = 250.
177+
*
178+
* ---------- Conclusion
179+
*
180+
* The parts 1 and 2 prevent the warning below:
181+
* `PHP Warning: SessionHandler::read(): Session ID is too long or contains illegal characters. Only the A-Z, a-z, 0-9, "-", and "," characters are allowed.`
182+
*
183+
* The part 2 prevents the warning below:
184+
* `PHP Warning: SessionHandler::read(): open(filepath, O_RDWR) failed: No such file or directory (2).`
185+
*/
156186
if ($sessionId && $this->saveHandler instanceof AbstractProxy && 'files' === $this->saveHandler->getSaveHandlerName() && !preg_match('/^[a-zA-Z0-9,-]{22,250}$/', $sessionId)) {
157187
// the session ID in the header is invalid, create a new one
158188
session_id(session_create_id());

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy