-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Security tokens error #12392
Comments
I have same problem with this too. v3.0.1 I'm beginner, CSRF looks like easy in Phalcon, but not easy. |
Looks like you've come across the same issue: https://forum.phalconphp.com/discussion/8093/csrf-problems-on-206#C21945 |
@temuri416 yes you are right, here some other examples //some action view in volt
{{ dump(security.checkToken(null, null, false)) }}
{{ security.getSessionToken() }}
{{ security.getToken() }}//here sessionToken was rewrited
{{ security.getSessionToken() }}
{{ security.getToken() }}
{{ dump(security.checkToken(null, null, false)) }}
<form action="" method="post">
<input type="text" name="{{ security.getTokenKey() }}" value="{{ security.getToken() }}">
<input type="submit">
</form>
//ouput
boolean true
aVAza2FRMy9ydzVqdGgvdU9GQTMrQT09
YTBuUlRpMGtBTlYwc0ZVeHdJakc5UT09
YTBuUlRpMGtBTlYwc0ZVeHdJakc5UT09
YTBuUlRpMGtBTlYwc0ZVeHdJakc5UT09
boolean false
//other example
{{ dump(security.checkToken(null, null, false)) }}
{{ dump(security.checkToken()) }}
{{ dump(security.checkToken()) }}
{{ security.getSessionToken() }}//empty!!!
{{ security.getToken() }}//new token was generated
boolean true
boolean true
boolean false
----EMPTY SESSION TOKEN----
YTBuUlRpMGtBTlYwc0ZVeHdJakc5UT09 Thats works fine at first time but an getter modifying another getter it's an strange behavior |
@emiliodeg Could you please provide the Phalcon version, OS, and way you initialized the Security service. I'll try to sort out |
Consider that we have already a token in session e.g. aaa
In this scenario by default session is storing in a file that we read that, write that, and again read that. So it is expectable that after modifying file(session) content in step 2, we read the new content of file in step 3. For better understanding look at the below code:
Same issue in the second example:
And the last example:
At last it is up to you that when and how to use these functions. |
Well it isn't a bug is a wrong way to do a simple work PHP 7.0.14 Sessin service config $di['session'] = function () use ($config) {
$session = new Files(['uniqueId' => $config->session->unique_id]);
$session->start();
return $session;
}; |
If someone needs a more consistent operation here a small change in the logic of the security component namespace PhalconFix;
class Security extends \Phalcon\Security
{
private static $sessionToken = null;
/**
* {@inheritdoc}
*/
public function getSessionToken()
{
if (self::$sessionToken === null) {
self::$sessionToken = parent::getSessionToken();
}
return self::$sessionToken;
}
/**
* {@inheritdoc}
*/
public function getToken()
{
if (self::$sessionToken === null) {
$this->getSessionToken(); //don't lose real session token, setup!
}
return parent::getToken(); //continues normally
}
}
//services setup same as always
$di['security'] = function () {
$security = new Security();
$security->setWorkFactor(12);/*ohm no fluid setters....*/
return $security;
};
//Now works like a charm
//anywhre in your code, here in volt
{{ security.getToken() }}
{{ security.getSessionToken() }}
{{ security.getToken() }}
{{ security.getSessionToken() }}
{{ security.getToken() }}
SVpSaGd2S3E4cUV5ZXczMTZra0d5dz09
UFV1S1c0eWFxc3VOQk90RnJyTUcyQT09
SVpSaGd2S3E4cUV5ZXczMTZra0d5dz09
UFV1S1c0eWFxc3VOQk90RnJyTUcyQT09 //nice work!!! didn't rewrite
SVpSaGd2S3E4cUV5ZXczMTZra0d5dz09 Have a nice new year! |
Actually If I know your mean, you need a new feature e.g. |
Hi @mbrostami I think methods without parameters must have a only one meaning |
I think the problem is just a misunderstanding. The method getToken not only generates a new token but sets it into the current session. This leaves the developer with the responsibility of keeping the previous token before calling this method and calling the checkToken method before that. For me, the request token is only important during the validation (because in the post we are receiving the old one), otherwise is fine that when we call getToken again the session token changes. As soon as we regenerate the token we should start using it as the current session one. I've added a pull request with a change proposal so the checkToken functionality doesn't use the regenerated session but uses the one that was set at the beginning if it existed. |
Well since we already got a consensus about what we should do here this issue is NFR and should aim 3.1.0 with PR #12518. |
This was resolved in this PR #13642. |
Well I think this is an error
getToken function https://github.com/phalcon/cphalcon/blob/master/phalcon/security.zep#L354
getToken() should not change the value of getSessionToken() until the next request
I think this error generates the misunderstanding that makes programmers confuse us also removes the possibility of using the Validator Identical in our forms assigning the accepted value getSessionToken()
What do you think?
The text was updated successfully, but these errors were encountered: