8000 minor #46825 [HttpFoundation] Add session ID regex comment (BrokenSou… · symfony/symfony@99f2ecc · GitHub
[go: up one dir, main page]

Skip to content

Commit 99f2ecc

Browse files
committed
minor #46825 [HttpFoundation] Add session ID regex comment (BrokenSourceCode)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [HttpFoundation] Add session ID regex comment | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | License | MIT A comment intended to explain the session ID regular expression. Related links: - #46777 - #46790 Commits ------- 4908090 [HttpFoundation] Add session ID regex comment
2 parents dd33509 + 4908090 commit 99f2ecc

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)
0