-
Notifications
You must be signed in to change notification settings - Fork 297
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
Session ID: extend the regex to match possible hash representations #338
Conversation
ping @ArthurHoaro @Cy4n1d3 |
This still does not fix it for me: I'll have to investigate what's going on there - the cookie is being refreshed every page load, ruling an old cookie out. |
|
It looks like your session ID is being considered invalid (which is to be expected, as it does not match the expected format), and either:
In which case, it would be necessary to define a regex that covers all PHP hash settings (or set the regex according to PHP settings -may turn hackish) |
|
Defining a catch-all regex using the maximum length (should 128) would somehow work against the original intent of #306 I think. Hacking the regex together could turn out just as bad however :) |
Working on an update & more robust tests, here are some interesting links:
However, I'm not sure how to reproduce the generation of strings encoded in such a way:
|
Note: PHP 5.6.13 session ID generation code: php-src/session.c, specifically |
I've read the first SO thread yesterday, the second link is pretty interesting. The following was quite interesting, although already a few years old. Might serve as base for considerations? |
I think you're missing the point of this piece of code. It's only used to regenerate a random session ID if something's wrong and avoid any error. Otherwise, session ID is generated by PHP automatically (when the session starts I think). Apparently, session ids can take any form depending on the server configuration but always hash. I've made a few tests. Note that I've updated to PHP 5.6, and:
Which means that our regex is valid, but I suggest that we just remove the length limit in the regex. Plus, [this list] can only get longer. |
I guess you're right, I've thought this function was adding some additional, own security token. I've re-read the code and get the point now. May I ask why you're not using https://secure.php.net/manual/en/function.session-regenerate-id.php to retrieve a new session id? Also, did you try changing |
Here's what I've been imagining to remember ;) The function validating the session id: Valid chars are I've made the changes locally and adjusted (+ made) the unit tests: |
I've come up with the following snippet to generate test values: <?php
function gen_session_hash($function, $bits_per_character)
{
ini_set('session.hash_function', $function);
ini_set('session.hash_bits_per_character', $bits_per_character);
session_start();
$sid = session_id();
session_destroy();
return $sid;
}
$results = array();
foreach (hash_algos() as $algo) {
$results[$algo] = array();
foreach (array(4, 5, 6) as $bpc) {
$results[$algo][$bpc] = gen_session_hash($algo, $bpc);
}
}
var_export($results);
?> It has the advantage of dynamically generating test data for all hash functions embedded by PHP, in the 3 available representations. Time to toy a bit with [EDIT] replaced |
0c7481b
to
10c78d1
Compare
PR updated with:
Todo: replace |
10c78d1
to
7bb89f8
Compare
Testin' time! @Cy4n1d3 was there a reason for moving the session ID regen after the call to @ArthurHoaro I've added a helper class to generate test data -sessions+PHPUnit don't seem to have a strong desire of working side-by-side ;-) |
Session needs to be started before you can regenerate the ID :) |
7bb89f8
to
60a3976
Compare
Updated :) |
Improves shaarli#306 Relates to shaarli#335 & shaarli#336 Duplicated by shaarli#339 Issues: - PHP regenerates the session ID if it is not compliant - the regex checking the session ID does not cover all cases - different algorithms: md5, sha1, sha256, etc. - bit representations: 4, 5, 6 Fix: - `index.php`: - remove `uniqid()` usage - call `session_regenerate_id()` if an invalid cookie is detected - regex: support all possible characters - '[a-zA-Z,-]{2,128}' - tests: add coverage for all algorithms & bit representations See: - http://php.net/manual/en/session.configuration.php#ini.session.hash-function - https://secure.php.net/manual/en/session.configuration.php#ini.session.hash-bits-per-character - http://php.net/manual/en/function.session-id.php - http://php.net/manual/en/function.session-regenerate-id.php - http://php.net/manual/en/function.hash-algos.php Signed-off-by: VirtualTam <[email protected]>
60a3976
to
68bc213
Compare
Session ID: extend the regex to match possible hash representations
Awesome work here guys. Hopefully, this won't bother us anymore. |
[EDITED]
Improves #306
Relates to #335 & #336
Duplicated by #339
Issues:
4, 5, 6
Fix:
index.php
:uniqid()
usagesession_regenerate_id()
if an invalid cookie is detected[a-zA-Z,-]{2,128}
See: