server icon indicating copy to clipboard operation
server copied to clipboard

[Bug]: CSRF check failed

Open Boc-chi-no opened this issue 3 years ago • 34 comments

⚠️ This issue respects the following points: ⚠️

Bug description

I am getting frequent HTTP 412 errors, it says {"message": "CSRF check failed"} I think everything I've configured is reasonable, since the upgrade to V26 it started to appear, recently upgraded to V27 the problem has become more frequent! According to my experiments and observations I found that this problem may have something to do with the layout's head tag, I use the data-requesttoken returned to me by the page's head to make a request will generate a 412 error, But when I use "/csrftoken" to get the token, making the request works! I have observed that this problem has been around for a long time and has not been resolved on the internet, so I hope to receive your further attention I'd like to make a suggestion if it's possible to catch a 412 error on the front-end and then Call /csrftoke to refresh the CSRF Token and immediately re-request it?

https://github.com/nextcloud/server/blob/master/core/templates/layout.base.php#L3 https://github.com/nextcloud/server/blob/master/core/Controller/CSRFTokenController.php#L57

Steps to reproduce

1.Clocking a page 2.Accessing an interface without the @NoCSRFRequired annotation

Expected behavior

The request was successful

Installation method

Community Manual installation with Archive

Nextcloud Server version

27

Operating system

Debian/Ubuntu

PHP engine version

PHP 8.0

Web server

Nginx

Database engine version

MySQL

Is this bug present after an update or on a fresh install?

Upgraded to a MAJOR version (ex. 22 to 23)

Are you using the Nextcloud Server Encryption module?

Encryption is Disabled

What user-backends are you using?

  • [X] Default user-backend (database)
  • [ ] LDAP/ Active Directory
  • [X] SSO - SAML
  • [ ] Other

Configuration report

{
    "system": {
        "instanceid": "***REMOVED SENSITIVE VALUE***",
        "passwordsalt": "***REMOVED SENSITIVE VALUE***",
        "secret": "***REMOVED SENSITIVE VALUE***",
        "memcache.local": "\\OC\\Memcache\\APCu",
        "memcache.distributed": "\\OC\\Memcache\\Redis",
        "memcache.locking": "\\OC\\Memcache\\Redis",
        "redis": {
            "host": "***REMOVED SENSITIVE VALUE***",
            "port": 6379
        },
        "trusted_domains": [
            "xxxxxxx:xxxxx",
        ],
        "datadirectory": "***REMOVED SENSITIVE VALUE***",
        "dbtype": "mysql",
        "version": "27.1.0.7",
        "overwrite.cli.url": "https:\/\/xxxxxx:xxxx\/xxxxxx\/",
        "overwriteprotocol": "https",
        "allow_local_remote_servers": "true",
        "dbname": "***REMOVED SENSITIVE VALUE***",
        "dbhost": "***REMOVED SENSITIVE VALUE***",
        "dbport": "",
        "dbtableprefix": "oc_",
        "mysql.utf8mb4": true,
        "dbuser": "***REMOVED SENSITIVE VALUE***",
        "dbpassword": "***REMOVED SENSITIVE VALUE***",
        "installed": true,
        "twofactor_enforced": "true",
        "tempdirectory": "\/data\/nextcloud_tmp\/",
        "default_language": "ja",
        "default_locale": "jp-JA",
        "twofactor_enforced_groups": [
            "admin",
        ],
        "twofactor_enforced_excluded_groups": [],
        "mail_from_address": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpmode": "smtp",
        "mail_sendmailmode": "smtp",
        "mail_domain": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpauthtype": "LOGIN",
        "mail_smtpauth": 1,
        "mail_smtpsecure": "tls",
        "mail_smtphost": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpport": "587",
        "mail_smtpname": "***REMOVED SENSITIVE VALUE***",
        "mail_smtppassword": "***REMOVED SENSITIVE VALUE***",
        "maintenance": false,
        "updater.release.channel": "stable",
        "theme": "",
        "loglevel": 0,
        "default_phone_region": "JP",
        "app_install_overwrite": [
            "epubreader",
            "scanner"
        ],
    }
}

List of activated Apps

Enabled:
  - activity: 2.19.0
  - admin_audit: 1.17.0
  - bruteforcesettings: 2.7.0
  - circles: 27.0.1
  - cloud_federation_api: 1.10.0
  - comments: 1.17.0
  - contacts: 5.4.2
  - contactsinteraction: 1.8.0
  - dashboard: 7.7.0
  - dav: 1.27.0
  - deck: 1.11.0
  - duplicatefinder: 1.0.5
  - external: 5.2.1
  - extract: 1.3.6
  - federatedfilesharing: 1.17.0
  - federation: 1.17.0
  - files: 1.22.0
  - files_3dmodelviewer: 0.0.12
  - files_accesscontrol: 1.17.1
  - files_external: 1.19.0
  - files_fulltextsearch: 27.0.1
  - files_mindmap: 0.0.29
  - files_pdfviewer: 2.8.0
  - files_reminders: 1.0.0
  - files_rightclick: 1.6.0
  - files_sharing: 1.19.0
  - files_trashbin: 1.17.0
  - files_versions: 1.20.0
  - fulltextsearch: 27.0.1
  - fulltextsearch_elasticsearch: 27.0.2
  - health: 1.6.2
  - impersonate: 1.14.0
  - logreader: 2.12.0
  - lookup_server_connector: 1.15.0
  - metadata: 0.19.0
  - ncdownloader: 1.0.20
  - nextcloud_announcements: 1.16.0
  - notes: 4.8.1
  - notifications: 2.15.0
  - oauth2: 1.15.1
  - password_policy: 1.17.0
  - photos: 2.3.0
  - previewgenerator: 5.3.0
  - privacy: 1.11.0
  - provisioning_api: 1.17.0
  - quota_warning: 1.17.0
  - recognize: 4.3.2
  - recommendations: 1.6.0
  - related_resources: 1.2.0
  - richdocuments: 8.2.0
  - scanner: 0.2.3
  - serverinfo: 1.17.0
  - settings: 1.9.0
  - sharebymail: 1.17.0
  - side_menu: 3.10.3
  - support: 1.10.0
  - survey_client: 1.15.0
  - suspicious_login: 5.0.0
  - systemtags: 1.17.0
  - text: 3.8.0
  - theming: 2.2.0
  - theming_customcss: 1.14.0
  - twofactor_backupcodes: 1.16.0
  - twofactor_totp: 9.0.0
  - twofactor_webauthn: 1.2.0
  - updatenotification: 1.17.0
  - user_saml: 5.2.2
  - user_status: 1.7.0
  - video_converter: 1.0.6
  - viewer: 2.1.0
  - weather_status: 1.7.0
  - workflowengine: 2.9.0
Disabled:
  - encryption: 2.15.0
  - firstrunwizard: 2.16.0 (installed 2.10.0)
  - user_ldap: 1.17.0

Nextcloud Signing status

No response

Nextcloud Logs

{"reqId":"7pguVp7wVcp1j7oblyn6","level":0,"time":"2023-09-26T02:54:55+00:00","remoteAddr":"xxxx","user":"xxxx","app":"no app in context","method":"GET","url":"/nextcloud/apps/xxxxxxxxxxxx","message":"CSRF check failed","userAgent":"xxxxxxxxxxxx","version":"27.1.0.7","exception":{"Exception":"OC\\AppFramework\\Middleware\\Security\\Exceptions\\CrossSiteRequestForgeryException","Message":"CSRF check failed","Code":412,"Trace":[{"file":"/xxxxx/wwwroot/nextcloud/nextcloud/lib/private/AppFramework/Middleware/MiddlewareDispatcher.php","line":96,"function":"beforeController","class":"OC\\AppFramework\\Middleware\\Security\\SecurityMiddleware","type":"->"},{"file":"/xxxxx/wwwroot/nextcloud/nextcloud/lib/private/AppFramework/Http/Dispatcher.php","line":129,"function":"beforeController","class":"OC\\AppFramework\\Middleware\\MiddlewareDispatcher","type":"->"},{"file":"/xxxxx/wwwroot/nextcloud/nextcloud/lib/private/AppFramework/App.php","line":183,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"/xxxxx/wwwroot/nextcloud/nextcloud/lib/private/Route/Router.php","line":315,"function":"main","class":"OC\\AppFramework\\App","type":"::"},{"file":"/xxxxx/wwwroot/nextcloud/nextcloud/lib/base.php","line":1068,"function":"match","class":"OC\\Route\\Router","type":"->"},{"file":"/xxxxx/wwwroot/nextcloud/nextcloud/index.php","line":36,"function":"handleRequest","class":"OC","type":"::"}],"File":"/xxxxx/wwwroot/nextcloud/nextcloud/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php","Line":224,"message":"CSRF check failed","exception":{},"CustomMessage":"CSRF check failed"}}

Additional info

Nextcloud 27.1.0

Boc-chi-no avatar Sep 26 '23 03:09 Boc-chi-no

I have the same issue, the message appears each second in the nextcloud.log file just exhausting it:

[no app in context] Debug: OC\AppFramework\Middleware\Security\Exceptions\CrossSiteRequestForgeryException: CSRF check failed at <<closure>>

0. /var/www/html/nextcloud/lib/private/AppFramework/Middleware/MiddlewareDispatcher.php line 96
   OC\AppFramework\Middleware\Security\SecurityMiddleware->beforeController()
1. /var/www/html/nextcloud/lib/private/AppFramework/Http/Dispatcher.php line 129
   OC\AppFramework\Middleware\MiddlewareDispatcher->beforeController()
2. /var/www/html/nextcloud/lib/private/AppFramework/App.php line 183
   OC\AppFramework\Http\Dispatcher->dispatch()
3. /var/www/html/nextcloud/lib/private/Route/Router.php line 315
   OC\AppFramework\App::main()
4. /var/www/html/nextcloud/lib/base.php line 1068
   OC\Route\Router->match()
5. /var/www/html/nextcloud/index.php line 36
   OC::handleRequest()

GET /apps/files/api/v1/stats
from ***.***.***.*** by username at 2023-10-21T11:54:33+03:00

oleua avatar Oct 21 '23 08:10 oleua

Receiving the same on 27.1, unable to login or reset password.

  Debug    no app in context  OC\AppFramework\Middleware\Security\Exceptions\CrossSiteRequestForgeryException: CSRF check failed at                              2023-11-10T22:48:24+00:00
                              .../Middleware/Security/SecurityMiddleware.php line 224
                              0. .../Middleware/MiddlewareDispatcher.php line 96
                                 OC\AppFramework\Middleware\Security\SecurityMiddleware->beforeController(
                                   ["OC\\Core\\Controller\\WebAuthnController"],
                                   "startAuthentication"
                                 )
                              1. lib/private/AppFramework/Http/Dispatcher.php line 129
                                 OC\AppFramework\Middleware\MiddlewareDispatcher->beforeController(
                                   ["OC\\Core\\Controller\\WebAuthnController"],
                                   "startAuthentication"
                                 )
                              2. lib/private/AppFramework/App.php line 183
                                 OC\AppFramework\Http\Dispatcher->dispatch(["OC\\Core\\Controller\\WebAuthnController"], "startAuthentication")
                              3. lib/private/Route/Router.php line 315
                                 OC\AppFramework\App::main(
                                   "OC\\Core\\Controller\\WebAuthnController",
                                   "startAuthentication",
                                   ["OC\\AppFramework\\DependencyInjection\\DIContainer"],
                                   ["core.WebAuthn.startAuthentication"]
                                 )
                              4. lib/base.php line 1068
                                 OC\Route\Router->match("\/login\/webauthn\/start")
                              5. index.php line 36
                                 OC::handleRequest(

                                 )

markoftw avatar Nov 10 '23 22:11 markoftw

Same here. I have "CSRF check failed" when I log out. And it does not log me out Server 28.0.2.5 php 8.2 Mariadb 10.6.14

with occ log:watch

  Debug    no app in context  OC\AppFramework\Middleware\Security\Exceptions\CrossSiteRequestForgeryException: CSRF check failed at                               2024-02-22T13:31:58+00:00
                              .../Middleware/Security/SecurityMiddleware.php line 219

                              0. .../Middleware/MiddlewareDispatcher.php line 96
                                 OC\AppFramework\Middleware\Security\SecurityMiddleware->beforeController(

                                 )
                              1. lib/private/AppFramework/Http/Dispatcher.php line 129
                                 OC\AppFramework\Middleware\MiddlewareDispatcher->beforeController(

                                 )
                              2. lib/private/AppFramework/App.php line 184                                                                                                                                                    OC\AppFramework\Http\Dispatcher->dispatch(                                                                                                                  
                                 )
                              3. lib/private/Route/Router.php line 315
                                 OC\AppFramework\App::main(

                                 )
                              4. lib/base.php line 1069
                                 OC\Route\Router->match(

                                 )
                              5. index.php line 39
                                 OC::handleRequest(

                                 )

And in log : {"reqId":"m7yIDjXDYl1bavgrqgD5","level":0,"time":"2024-02-22T13:42:15+00:00","remoteAddr":"80.125.52.13","user":"gab","app":"no app in context","method":"GET","url":"/logout?requesttoken=DeZBHtvacVl4jpRucZBplSIqPnZ%2Bp5Aj%2B%2BJ1pFdt3rE%3D%3ASp81dK%2BdJDgSzeddFtIawm5%2BdEMdzKhV1LMU9BkrjOM%3D","message":"CSRF check failed","userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0","version":"28.0.2.5","exception":{"Exception":"OC\\AppFramework\\Middleware\\Security\\Exceptions\\CrossSiteRequestForgeryException","Message":"CSRF check failed","Code":412,"Trace":[{"file":"/var/www/nextcloud/lib/private/AppFramework/Middleware/MiddlewareDispatcher.php","line":96,"function":"beforeController","class":"OC\\AppFramework\\Middleware\\Security\\SecurityMiddleware","type":"->"},{"file":"/var/www/nextcloud/lib/private/AppFramework/Http/Dispatcher.php","line":129,"function":"beforeController","class":"OC\\AppFramework\\Middleware\\MiddlewareDispatcher","type":"->"},{"file":"/var/www/nextcloud/lib/private/AppFramework/App.php","line":184,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"/var/www/nextcloud/lib/private/Route/Router.php","line":315,"function":"main","class":"OC\\AppFramework\\App","type":"::"},{"file":"/var/www/nextcloud/lib/base.php","line":1069,"function":"match","class":"OC\\Route\\Router","type":"->"},{"file":"/var/www/nextcloud/index.php","line":39,"function":"handleRequest","class":"OC","type":"::"}],"File":"/var/www/nextcloud/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php","Line":219,"message":"CSRF check failed","exception":{},"CustomMessage":"CSRF check failed"}}

Korsani avatar Feb 22 '24 13:02 Korsani

Same here, NC 28.0.3, issue comes and goes. Access via IOS app works, but not with browsers.

rcjcarr avatar Mar 11 '24 19:03 rcjcarr

My problem was resolved, and I was attempting to address another issue concerning slow access to Nextcloud. Upon checking the log, I discovered numerous occurrences of "GuzzleHttp\Exception\ConnectException: cURL error 28: Connection timed out after 10001 milliseconds (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://xxx.xxx.xxx/ocm-provider/ " I realized that this issue was associated with federated cloud. Therefore, I removed all federated cloud connections, which restored Nextcloud's performance to normal and inadvertently resolved the problem. Although I fail to comprehend the connection between these two issues.

Boc-chi-no avatar Mar 20 '24 03:03 Boc-chi-no

Same here. I have "CSRF check failed" when I log out. And it does not log me out Server 28.0.2.5

I just experienced this on Server 28.0.3.

I visited the front page where it showed me the menus and chat mentions. I clicked the Log out link. The result was a Nextcloud-style Forbidden screen with a CSRF error. When I returned to the front page again and clicked Log out again, the problem went away that time.

miqrogroove avatar May 08 '24 19:05 miqrogroove

The issue persists on NC 29, if I leave a login idle it will timeout and rather than logout it creates the csrf failure. However the login is not closed as I can change the url and re access the session; despite the fact that the session end should have occurred.

fastSimulations avatar Jul 06 '24 09:07 fastSimulations

Do you have http2 enabled? I have this issue frequently, random things stop working with "Invalid action" error (like clicking on details of a file). Sometimes loading /files/files URL which should show root folder is just empty saying No Files. Refresh fixes it. When checking console, I see CSRF errors. refreshing page solves it. This is happening since like NC27 or so. Around the same time http2 was enabled on our rev proxy

michnovka avatar Aug 22 '24 10:08 michnovka

This is still ongoing, for random actions. Like uploading file.

<?xml version="1.0" encoding="utf-8"?>
<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
  <s:exception>Sabre\DAV\Exception\NotAuthenticated</s:exception>
  <s:message>CSRF check not passed.</s:message>
</d:error>

Refreshing page fixes the issue.

michnovka avatar Sep 02 '24 09:09 michnovka

I'll see this now happening on nextcloud v30 on one instance 😕 I don't see it on another instance nearly configured same (same reverse proxy)

viceice avatar Oct 23 '24 13:10 viceice

OK, i could reduce via disabling text app. So all the failing apps/text/session/<num>/push and apps/text/session/<num>/sync requests are gone and the web ui is usable again

viceice avatar Oct 23 '24 13:10 viceice

I still see a lot errors in my browsers and then empty file lists. they're gone after a few reloads.

url: /remote.php/dav/files/XXXXX/
status: 401
<?xml version="1.0" encoding="utf-8"?>
<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
	<s:exception>Interner Serverfehler</s:exception>
	<s:message>
		Der Server konnte die Anfrage nicht fertig stellen.		Sollte dies erneut auftreten, senden Sie bitte die nachfolgenden technischen Einzelheiten  an Ihren Server-Administrator.		Weitere Details können im Server-Protokoll gefunden werden.			</s:message>

	<s:technical-details>
		<s:remote-address>2001:9e8:f734:5900:199c:8858:7619:aa71</s:remote-address>
		<s:request-id>m8HhXNUyUvsgjV3OGagL</s:request-id>

		</s:technical-details>
</d:error>

viceice avatar Nov 05 '24 14:11 viceice

OK, i could solve it by moving the redis connections to my valkey master node only. It would be really nice if nextcloud can support redis+sentinel

viceice avatar Nov 07 '24 15:11 viceice

I have no federated cloud set up and I still see the issue from time to time.

michnovka avatar Mar 14 '25 10:03 michnovka

We're experiencing this issue as follows:

According to the network log of the Chrome developer tools requests to the endpoint /ocs/v2.php/apps/notifications/api/v2/notifications fail with 412 Precondition Failed just after the endpoint /index.php/csrftoken has been requested.

Interestingly, those faulty requests to the notifications endpoint are missing the HTTP header Requesttoken, which is present in previous successful requests (before the endpoint /index.php/csrftoken has been requested).

sst-yde avatar Mar 19 '25 14:03 sst-yde

We have the exact same behavior as @sst-yde described on our Nextcloud 30.0.6.

Here are some console log from the Firefox client (just changed the domain):

15:38:00,808 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 200  55ms]  
15:38:00,809 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 200  55ms]  
15:38:00,910 GET https://nextcloud.example.com/index.php/apps/theming/image/logo?useSvg=1&v=88 [HTTP/2 200  0ms]  
15:38:00,910 Got notification data, restoring default polling interval. 3 NotificationsApp-DV09BU4a.chunk.mjs:35:70325 
15:38:30,656 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  62ms]  
15:39:00,653 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  110ms]  
15:39:30,669 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  62ms]  
15:40:00,857 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  64ms]  
15:40:31,471 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  92ms]  
15:41:02,153 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  75ms]  
15:41:32,834 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  88ms]  
15:42:03,795 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  55ms]  
15:42:34,194 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  120ms]  
15:43:05,069 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  69ms]  
15:43:35,038 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  79ms]  
15:44:05,040 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  73ms]  
15:44:35,051 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  69ms]  
15:45:05,049 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  65ms]  
15:45:35,580 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  70ms]  
15:46:05,855 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  51ms]  
15:46:36,655 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  71ms]  
15:47:07,050 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  63ms]  
15:47:37,728 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  81ms]  
15:48:07,716 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  66ms]  
15:48:38,319 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  150ms]  
15:49:09,729 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  57ms]  
15:49:40,139 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 304  115ms]  
15:50:00,144 XHRGET https://nextcloud.example.com/index.php/csrftoken  
15:50:01,161 OC.requestToken changed undefined index.js:295:9 
15:50:12,333 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  79ms]  
15:50:12,422 Request to https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications failed because of a CSRF mismatch. Fetching a new token _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19711 
15:50:12,448 XHRGET https://nextcloud.example.com/index.php/csrftoken [HTTP/2 200  0ms]  
15:50:12,451 New request token undefined fetched _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19867 
15:50:12,462 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  56ms]  
15:50:12,526 Request to https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications failed because of a CSRF mismatch. Fetching a new token _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19711 
15:50:12,542 XHRGET https://nextcloud.example.com/index.php/csrftoken [HTTP/2 200  0ms]  
15:50:12,546 New request token undefined fetched _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19867 
15:50:12,556 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  54ms]  
15:50:12,615 Request to https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications failed because of a CSRF mismatch. Fetching a new token _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19711 
15:50:12,624 XHRGET https://nextcloud.example.com/index.php/csrftoken [HTTP/2 200  0ms]  
15:50:12,628 New request token undefined fetched _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19867 
15:50:12,637 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  61ms]  
15:50:12,702 Request to https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications failed because of a CSRF mismatch. Fetching a new token _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19711 
15:50:12,713 XHRGET https://nextcloud.example.com/index.php/csrftoken [HTTP/2 200  0ms]  
15:50:12,717 New request token undefined fetched _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19867 
15:50:12,724 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  54ms]  
15:50:12,782 Request to https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications failed because of a CSRF mismatch. Fetching a new token _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19711 
15:50:12,790 XHRGET https://nextcloud.example.com/index.php/csrftoken [HTTP/2 200  0ms]  
15:50:12,793 New request token undefined fetched _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19867 
15:50:12,800 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  70ms]  
15:50:12,877 Request to https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications failed because of a CSRF mismatch. Fetching a new token _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19711 
15:50:12,890 XHRGET https://nextcloud.example.com/index.php/csrftoken [HTTP/2 200  0ms]  
15:50:12,894 New request token undefined fetched _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19867 
15:50:12,906 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  56ms]  
15:50:12,969 Request to https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications failed because of a CSRF mismatch. Fetching a new token _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19711 
15:50:12,979 XHRGET https://nextcloud.example.com/index.php/csrftoken [HTTP/2 200  0ms]  
15:50:12,982 New request token undefined fetched _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19867 
15:50:12,990 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  59ms]  

b3 avatar Mar 22 '25 15:03 b3

De-activating the Federation app does not change anything here.

The problem is however very deterministic:

  1. load a page (for instance Personal Settings / Security
  2. wait 12 minutes
  3. the client javascript code throw an error (OC.requestToken changed undefined [core-main.js:1:28342](https://nextcloud.example.com/dist/core-main.js?v=c6f20869-88)
  4. then it started indefinitively
16:53:33,412 OC.requestToken changed undefined
16:53:43,301 GET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  122ms]
16:53:43,429 Request to https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications failed because of a CSRF mismatch. Fetching a new token [_plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19711](https://nextcloud.example.com/apps/notifications/js/_plugin-vue2_normalizer-IVCPCvfq.chunk.mjs)
16:53:43,461 XHRGET https://nextcloud.example.com/index.php/csrftoken [HTTP/2 200  0ms]
16:53:43,467 New request token undefined fetched
16:53:43,481 XHRGET https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications [HTTP/2 412  53ms]
16:53:43,541 Request to https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications failed because of a CSRF mismatch. Fetching a new token _plugin-vue2_normalizer-IVCPCvfq.chunk.mjs:7:19711
16:53:43,553 XHRGET https://nextcloud.example.com/index.php/csrftoken [HTTP/2 200  0ms]
16:53:43,557 New request token undefined fetched

b3 avatar Mar 22 '25 16:03 b3

In our case we (someone from https://www.tetras-libre.fr/) found a workaround and certainly one of the cause of the trouble.

Our installation is done through a docker-compose build on documentation found in the admin_manual.

For nginx we used to have that snippet in /etc/nginx/nginx.conf (certainly based on what was found in the admin_manual at the time of our setup):

        # Adding the cache control header for js and css files
        # Make sure it is BELOW the location ~ \.php(?:$|/) { block
        location ~* \.(?:css|js)$ {
            add_header Cache-Control "public, max-age=7200";
            # Add headers to serve security related headers
            add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
            add_header X-Content-Type-Options nosniff;
            add_header X-Frame-Options "SAMEORIGIN";
            add_header X-XSS-Protection "1; mode=block";
            add_header X-Robots-Tag "noindex, nofollow";
            add_header X-Download-Options noopen;
            add_header X-Permitted-Cross-Domain-Policies none;
            # Optional: Don't log access to assets
            access_log off;
        }

As on can see the Cache-Control header is added and ask client to renew cache every 12 minutes (720s).

As for today in current (the admin_manual) the recommendation is to have Cache-Control with a largely different value for static files:

   # Serve static files
    location ~ \.(?:css|js|mjs|svg|gif|ico|jpg|png|webp|wasm|tflite|map|ogg|flac)$ {
        try_files $uri /index.php$request_uri;
        # HTTP response headers borrowed from Nextcloud `.htaccess`
        add_header Cache-Control                     "public, max-age=15778463$asset_immutable";
        add_header Referrer-Policy                   "no-referrer"       always;
        add_header X-Content-Type-Options            "nosniff"           always;
        add_header X-Frame-Options                   "SAMEORIGIN"        always;
        add_header X-Permitted-Cross-Domain-Policies "none"              always;
        add_header X-Robots-Tag                      "noindex, nofollow" always;
        add_header X-XSS-Protection                  "1; mode=block"     always;
        access_log off;     # Optional: Don't log access to assets
    }

With that kind of values it seems we do not get any 412 error anymore.

Maybe this workaround can help others.

b3 avatar Mar 24 '25 14:03 b3

Well I spoke too soon.

We still have a lot of 412 errors coming on https://nextcloud.example.com/ocs/v2.php/apps/notifications/api/v2/notifications as well as on /index.php/apps/text/session/XXXXXX/push (as well as on sync and save).

b3 avatar Mar 25 '25 17:03 b3

I can confirm this - we still receive 412 Precondition errors too after adjusting the Cache-Control header with a large value.

sst-yde avatar Mar 26 '25 07:03 sst-yde