Note: security & performance considerations are discussed below.
Changed concept of "cookie authentication". Currently, the token auth is stored insecurely in the piwik_auth cookie. When this cookie is in a request to Piwik, this token auth is used to authenticate the request. The authentication is also delegated to the Auth implementation. This PR changes the strategy.
Now, the session stores information related to who the session is for (including IP address, user agent and a randomly generated secret). The piwik_auth cookie now contains a md5 hash of this randomly generated string + the last time a user's password was changed.
If the cookie is present in a request, Piwik core will use
SessionAuth, and bypass plugin based authentication altogether. SessionAuth will check that the session has been authenticated already, check that the request using the session is from the same place that initiated the session, and that the password hasn't changed since.
ts_password_modified. This column holds the last time the user's password was changed. This column is used to automatically invalidate sessions after a user's password is changed (so we don't have to iterate over sessions or anything).
There should be no BC breaks. Plugins do not have to call
Login::initAuthenticationFromCookie() anymore, but existing implementations don't have to be modified.
What info is compromised if the piwik_auth cookie is compromised?
Would an attacker be able to use the piwik_auth cookie to gain access to Piwik?
What would an attacker gain if the server side session variables were somehow compromised?
The only way I can think of for an attacker to gain access to a session, is for the attacker to:
Since we're using a weak hash in SessionAuth, there's no real overhead to this solution.
Since sessions are not invalidated manually (eg, by iterating over every session), there is no overhead added to the change password workflow, either.
Keeping SessionAuth in core also allows plugin authentication to be more costly if required. Once a session is established, plugin based authentication won't happen again.
Some ideas for making Piwik even more secure (not necessarily related to this PR):
Common::generateUniqId()'s use of md5 & uniqid w/
random_bytes()(there's are polyfills for PHP 5.*, eg, https://github.com/symfony/polyfill). Would prevent attackers from being able to guess what new token auths would be.
I removed the session secret, so now the piwik_auth cookie is empty. It is, however, required (at the moment anyway). In FrontController we check if this cookie is sent and if so, we use SessionAuth instead of the normal Auth implementation.
We can't use the PIWIK_SESSID cookie (which is the PHP session ID) since in the UI there is always a session, even when you're logged out. I believe this is for form nonces (like the one used in the login form), which are stored in sessions.
I thought about checking if the session is authenticated (ie, the 'user.name' variable exists in the session), but that would mean always doing session_start(), and to my understanding, we don't want to do that for, eg, every API request, just for requests from a browser.
Haven't done an actual test if it works but looked at the code and left a few comments. Overall looks good but need to make sure everything still works as before.