Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/interceptors/csrf-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import type { CancelableAxiosInstance } from '../client.ts'
import type { InterceptorErrorHandler } from './index.ts'

import { emit } from '@nextcloud/event-bus'
import { generateUrl } from '@nextcloud/router'
import { isAxiosError } from 'axios'

Expand Down Expand Up @@ -35,6 +36,7 @@ export function onCsrfTokenError(axios: CancelableAxiosInstance): InterceptorErr
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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


return axios({
...config,
Expand Down