Skip to content

Conversation

@ChristophWurst
Copy link
Member

The CSP nonce is based on the CSRF token. This token does not change,
unless you log in (or out). In case of the session data being lost,
e.g. because php gets rid of old sessions, a new CSRF token is gen-
erated. While this is fine in theory, it actually caused some annoying
problems where the browser restored a tab and Nextcloud js was blocked
due to an outdated nonce.
The main problem here is that, while processing the request, we write
out security headers relatively early. At that point the CSRF token
is known/generated and transformed into a CSP nonce. During this request,
however, we also log the user in because the session information was
lost. At that point we also refresh the CSRF token, which eventually
causes the browser to block any scripts as the nonce in the header
does not match the one which is used to include scripts.
This patch adds a flag to indicate whether the CSRF token should be
refreshed or not. It is assumed that refreshing is only necessary
if we want to re-generate the session id too. To my knowledge, this
case only happens on fresh logins, not when we recover from a deleted
session file.

@LukasReschke @rullzer this is the problem I've discussed with you a few times but never really found the problem. Now that I know how to reproduce it was relatively easy to attach a debugger and see what's happening inside our login magic.

Steps to reproduce:

  • (install the mail app – could be possible to reproduce this with other apps too, haven't tried it)
  • log in
  • wait for all request to have finished
  • delete php's session file
  • refresh the page

Before, all scripts were blocked. You couldn't even see the apps menu on the top left. On this branch all scripts are executed as expected.

Fixes nextcloud/mail#322

The CSP nonce is based on the CSRF token. This token does not change,
unless you log in (or out). In case of the session data being lost,
e.g. because php gets rid of old sessions, a new CSRF token is gen-
erated. While this is fine in theory, it actually caused some annoying
problems where the browser restored a tab and Nextcloud js was blocked
due to an outdated nonce.
The main problem here is that, while processing the request, we write
out security headers relatively early. At that point the CSRF token
is known/generated and transformed into a CSP nonce. During this request,
however, we also log the user in because the session information was
lost. At that point we also refresh the CSRF token, which eventually
causes the browser to block any scripts as the nonce in the header
does not match the one which is used to include scripts.
This patch adds a flag to indicate whether the CSRF token should be
refreshed or not. It is assumed that refreshing is only necessary
if we want to re-generate the session id too. To my knowledge, this
case only happens on fresh logins, not when we recover from a deleted
session file.

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Copy link
Member

@MorrisJobke MorrisJobke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and works 👍

@nickvergessen
Copy link
Member

Is this the same issue I see with the tasks app from time to time, when I see only placeholders and then have to do two hard refreshes before it works again?

@ChristophWurst
Copy link
Member Author

Probably, yes!

@MorrisJobke
Copy link
Member

I have seen this with the spreed app as well. :/

Copy link
Member

@rullzer rullzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. But I'd like @LukasReschke to have a look as well

@jancborchardt
Copy link
Member

Nice! Fixes the Mail app issue for me 👍

Copy link
Member

@skjnldsv skjnldsv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works fine!!

Copy link
Member

@LukasReschke LukasReschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@LukasReschke LukasReschke merged commit bab313d into master Sep 7, 2017
@LukasReschke LukasReschke deleted the fix/session-timeout-refresh-csrf-token branch September 7, 2017 17:52
@MorrisJobke
Copy link
Member

@ChristophWurst Mind to open the backport PR?

@ChristophWurst
Copy link
Member Author

@ChristophWurst Mind to open the backport PR?

Will take care of it ASAP!

@ChristophWurst
Copy link
Member Author

@ChristophWurst Mind to open the backport PR?

-> #6440

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants