Skip to content

fix: CSRF Check failed#892

Open
FlyInWind1 wants to merge 1 commit intonextcloud-libraries:mainfrom
FlyInWind1:main
Open

fix: CSRF Check failed#892
FlyInWind1 wants to merge 1 commit intonextcloud-libraries:mainfrom
FlyInWind1:main

Conversation

@FlyInWind1
Copy link

@FlyInWind1 FlyInWind1 commented Mar 2, 2026

fix nextcloud/server#57273

some http request like /remote.php/dav/trashbin/user/trash/ use it own axios instance (example trasbin)
create by getClient function, that not add onCsrfTokenError interceptor and will not update requesttoken itself
图片
图片

but it subscribed csrf-token-update, after emit('csrf-token-update', { token }) it's token will update.

with my commit, when the request has onCsrfTokenError interceptor like /ocs/v2.php/apps/notifications/api/v2/notifications updated token and emit('csrf-token-update', { token }), all axios instance will update it's token

mybe we also need modify getClient function, add onCsrfTokenError interceptor for it

Copy link
Contributor

@ShGKme ShGKme left a comment

Choose a reason for hiding this comment

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

The change makes sense, but to make it complete, we need slightly more changes.

However, I don't see how this fixes the linked issue on the server...

const { data: { token } } = await axios.get(generateUrl('/csrftoken'))
console.debug(`New request token ${token} fetched`)
axios.defaults.headers.requesttoken = token
emit('csrf-token-update', { token })
Copy link
Contributor

Choose a reason for hiding this comment

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

This sets OC.requestToken and updates token for every current csrf-token-update listener. But the new executed code may still initialize CSRF token only from document.head.dataset.requesttoken which is unchanged here, resulting in the error again.

Server has setRequestToken that coverts it, but not in public API:
https://github.com/nextcloud/server/blob/e7c4dbf2cbde841b6f7126e27b22c10f979c6cd7/core/src/OC/requesttoken.ts#L23

IMO, we should:

  1. Move setRequestToken from server to @nextcloud/auth: https://github.com/nextcloud-libraries/nextcloud-auth/blob/main/lib/requesttoken.ts
  2. Use setRequestToken here and on the server

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes!
Maybe not a setRequestToken but updateRequestToken to keep logic about fetching a new token in one place?

Copy link
Author

@FlyInWind1 FlyInWind1 Mar 3, 2026

Choose a reason for hiding this comment

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

To minimal changes, we can add document.head.dataset.requesttoken = e.token after OC.requestToken = e.token

Copy link
Author

Choose a reason for hiding this comment

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

It seems my commit message need change to emit('csrf-token-update', { token }) after /csrftoken. update OC.requestToken not the reason i fixed the problem...

Copy link
Author

Choose a reason for hiding this comment

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

Yes! Maybe not a setRequestToken but updateRequestToken to keep logic about fetching a new token in one place?

You can found it at https://github.com/nextcloud/server/blob/master/core/src/OC/requesttoken.ts#L38

@ShGKme ShGKme requested a review from susnux March 2, 2026 12:59
@ShGKme ShGKme added the bug Something isn't working label Mar 2, 2026
Signed-off-by: FlyInWind <2518509078@qq.com>
@FlyInWind1 FlyInWind1 changed the title fix: set OC.requestToken after /csrftoken fix: CSRF Check failed Mar 3, 2026
@FlyInWind1 FlyInWind1 requested a review from ShGKme March 3, 2026 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: CSRF Check failed, again

3 participants