Skip to content

fix(session): do not persist the session user in the PHP session - #9685

Open
kynrek wants to merge 1 commit into
nextcloud:mainfrom
kynrek:fix/direct-editing-csrf-session
Open

kynrek wants to merge 1 commit into
nextcloud:mainfrom
kynrek:fix/direct-editing-csrf-session

Conversation

@kynrek

@kynrek kynrek commented Nov 23, 2026 •

Copy link
Copy Markdown

📝 Summary

When the editor runs without a logged-in user (direct editing, e.g. the Nextcloud Android app), push/sync/save activate the text session's user via IUserSession::setUser(), which writes user_id into the shared PHP session. If two requests of the same editor overlap (a push during an autosave), the second one finds user_id with no auth token behind it, validateSession() logs out and clears the session, and every following request fails with 412 CSRF check failed. The client treats that 412 as an outdated document and shows "The file was overwritten", even with a single editor.

The change uses setVolatileActiveUser() (OCP since 29), so the session user is only active for the current request and the PHP session is not touched. Logged-in requests are unchanged: loginSessionUser() still does nothing for them.

How to verify

  1. Open a Markdown file through direct editing (POST /ocs/v2.php/apps/files/api/v1/directEditing/open, editorId=text) and load the returned URL without credentials.
  2. From that cookie jar, send concurrent push and save requests (script in Direct editing: overlapping push/save logs the editor's PHP session out (CSRF 412), causing a false "The file was overwritten" conflict #9684).
  3. Before: about half of the requests get 412 {"message":"CSRF check failed"} within 20 s. After: none (0 of ~1,400 on our instance), saves still land and are attributed to the editing user.

Tested on a live instance

Nextcloud 35.0.2 / Text 9.0.2 with this patch, two Android phones (Nextcloud Android 35.1.0, direct editing) on a shared note:

Two client-side issues seen during these tests are unrelated to this change and already fixed on main/stable35: after a reconnect, the file is only written on the next edit (#9630), and changes typed just before the app is killed are not pushed after recovery (likely #9589).

🖼️ Screenshots

No UI change. The conflict banner this removes is shown in #9684.

🏚️ Before 🏡 After
Conflict banner after overlapping requests No banner

🚧 TODO

  • Backport to stable35

🏁 Checklist

  • Code is properly formatted (npm run lint / npm run stylelint / composer run cs:check)
  • Sign-off message is added to all commits
  • Tests (unit, integration and/or end-to-end) passing and the changes are covered with tests
  • Documentation (README or documentation) has been updated or is not required

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI tools
  • The AI-generated content was reviewed, comprehended and tested by a human

The root cause analysis, fix, test and this description were drafted with Claude Code. The fix has been running on our 35.0.2 instance (Text 9.0.2) since 2026-11-22, where the race test above went from ~45% failures to 0.

Testing details
  • New tests/unit/Controller/SessionControllerTest.php: without a logged-in user, push/sync/save must call setVolatileActiveUser() (user, then the previous user) and never setUser(); with a logged-in user, neither is called. The not-logged-in cases fail on the old code.
  • Full PHPUnit suite against server master (SQLite, PHP 8.3): 179 tests, 3 skipped (Oracle only), 0 failures.
  • composer run cs:check and composer run psalm: no errors.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q367ozke3eY15mEffZoDEG

@kynrek

kynrek commented Nov 23, 2026

Copy link
Copy Markdown
Author

/backport to stable35

@mejo- mejo- left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the contribution @kynrek. The changes look good to me and indeed fix the described problem.

The PR needs a manual rebase as the unit test file on main branch changed in the meantime. Unfortunately I lack permissions to push directly into your PR. Can you do the rebase yourself or grant me editing permissions to the PR?

When the editor runs without a logged-in user (direct editing, e.g.
from the Android app), push/sync/save activate the text session's user
for the request. Using setUser() wrote that user into the shared PHP
session. When two requests of the same editor overlapped, the second
one found a user_id without an auth token, the server logged the
session out and cleared it, and all following requests failed the CSRF
check with 412. The client treats a 412 as an outdated document and
shows the "file was overwritten" conflict, even with a single editor.

A request-scoped user is all these endpoints need.

Fixes nextcloud#9684

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q367ozke3eY15mEffZoDEG
Signed-off-by: kynrek <35751552+kynrek@users.noreply.github.com>
@kynrek
kynrek force-pushed the fix/direct-editing-csrf-session branch from 7ad71e3 to aa608e7 Compare November 26, 2026 16:09
@kynrek

kynrek commented Nov 26, 2026

Copy link
Copy Markdown
Author

Thanks @mejo-! Rebased onto current main. The only conflict was the new SessionControllerTest.php from #9042: I kept your two tests as they are and added ours below them, with their setup in a helper so it doesn't affect yours. All 8 tests in the file and the full unit suite pass locally.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Direct editing: overlapping push/save logs the editor's PHP session out (CSRF 412), causing a false "The file was overwritten" conflict

2 participants