-
Notifications
You must be signed in to change notification settings - Fork 788
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
[MERGED] Security Enhancements (Session, Cookie, CSRF) & Encryption. #693
Conversation
- Encryption for user_id in cookie instead of plain - Remove remember_me token from database when deleting the cookie - Configure session cookie using setcookie() - prevent session concurrency - CSRF tokens(per session)
This is excellent! Thank you so much, please allow some time for proper testing. Have a wonderful day! |
If I'm not mistaken there is no change to the SQL installation file which should have a Some of the functions use this field when it doesn't seem to exist, such as:
But yes the rest seems excellent! |
@panique I do have some, but, I am not an Expert though. Just some knowledge about Authentication, Authorization, and some famous attacks and how you can defect yourself against these attacks in PHP. I've digging into frameworks like CakePHP, Laravel, Symphony & Yii to see how those developers take care of these security issues and figure out a suitable solution for your application. @josh-bridge Yes. I did change it while i was testing the app, I don't know how i forgot to commit it :) |
@OmarElgabry as kind of a novice at PHP and web security in general, for my project I've added a changePassword page, would this also need to be protected against CSRF? If so, would I make a new csrf_token on the password form? Here is the changePassword fork if you wish to view it :) |
Also this error is shown on the login page
Is this because |
CSRF attack is when a hacker tricks the user to make a request to a website. If the hacker knows the user's old password, then the hacker can login, and change it by himself. But as your part of defensive security mechanism, It don't see any problems if you do add it too to change password forms. |
Use mater branch because it always contain the stable version. |
This was an error I received after committing your code, not from the HUGE branch itself (I am using the master branch)
This is the line that is causing the issue, XSSFilter doesn't appear to exist? At least it doesn't come up when I google the function |
I apologise, I see you didn't write it yourself, but it seems to be a by-product of having to commit your code to the Anyway, all is well now! Thanks! Edit: I see that the Filter class is defined in the panique develop branch, but since I was using the Master Branch with files replaced with the ones you edited, it still had parts of the develop code that I was unaware was different! |
[testing] Security Enhancements(Session, Cookie, CSRF) & Encryption.
@OmarElgabry Thank you very much, this is a very very good pull request, and it's extremely useful for the project! I've just merged it into develop branch and everything runs fine from first try. You code is beautiful and well documented, you've added something very great to this project! I've for sure put your Github name into the changelog and the Readme! By the way, the feature that the user can only be logged in from one location/browser might not be optimal for some users, so I'll try to build a switch there, allowing the developer to set in configs that users can indeed be logged in from multiple locations. In the time of always-on-applications and typical smartphones + desktop + tablet-use-cases it might be a security risk we'll have to accept to make some things run fine for the end-user. Very interesting topic! Have a great time and thank you again! |
Thank you for the appreciation. Just one note, When I added CSRF token in login, and editUsername form, It was just for demonstration, but definitely you(or whoever is going to use HUGE) needs to add this token to every critical actions like edit email, passwords, register, delete/edit user, ..etc.(Although I would recommend to send it with every POST, and even critical GET requests). For the session concurrency, Yes, Maybe you can give them the option to enable/disable this feature. This line of code checks for session concurrency for every request. Thank you! |
(personal notice) TODO add CSRF token logic into every relevant POST request |
It seems there are many changes, so I'll explain everything here:
1 . CSRF Tokens
Referring to #416, CSRF tokens are implemented per session, means there is a unique token for the current user. This token will be expired after certain duration(1 day).
Another solution is per-form, but generating a new token for each form could report a false CSRF attack. That's likely to happen when you open more than one form at the same time, because one of them will have a token that's not same as the session.
I added Instruction on how to use this class in Csrf.php. Also I added two samples in login form
LoginController::login()
, and in change user name formLoginController::editUsername_action()
.2 . Encryption & Decryption
It's likely that you will need encryption and decryption in your application. This is needed in case you want to expose something like user_id in cookie. Instead of using plain user_id, it's better to encrypt it.
3 . Session Settings
Referring to #186, Securing session could be done through different approaches.
4 . Session Concurrency
Referring to #282. It's a perfectly valid situation if you have two users tries to login using the same user credentials. Allowing access your application with the same user credentials(maybe hacked account) from different devices could be also vulnerable to attacks like DDOS attacks.
So, a good solution is to allow users to login from different devices with the same credentials, but also both of them can't be logged in at the same time.
deleteCookie()
, Cookie token needs to be cleared in database as well as in user browser.loginWithCookie()
,explode()
may return be split into 2 strings instead of 3, So, as i know this snippetlist ($user_id, $token, $hash) = explode(':', $cookie);
may trigger an error.LoginController::logout()
I addedexit()
at the end instead of typingexit()
every time you call this method.NOTE Everything is documented, please check the comments in the code. I hope everything was clear.