Skip to content

fix(webdav): recognize the Files root by backend user ID - #279

Open
claucambra wants to merge 3 commits into
mainfrom
work/dav-root-backend-user-id
Open

claucambra wants to merge 3 commits into
mainfrom
work/dav-root-backend-user-id

Conversation

@claucambra

@claucambra claucambra commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

NextcloudKit used the login name to recognize the WebDAV Files root. When the login name differed from the backend user ID, the parser returned the root as an ordinary directory. It also missed the root on servers installed in a subfolder.

Use the backend user ID and installation path to recognize the root and build its URL. Sessions without a backend user ID still fall back to the login name.

Consumer impact

For affected accounts, the root response now uses the existing .__NC_ROOT__ marker and points to the user's DAV Files root. The response carries the server's ocId and fileId.

Desktop can retain an older cache row for the same root after updating NextcloudKit. That row can take precedence when newly discovered remote files directly under the root inherit the Keep Downloaded setting. Handling those cached rows is a Desktop follow-up.

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI.

Assisted-by: Codex:GPT-6
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>

@i2h3 i2h3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The core fix is correct. The new test passes locally in an iOS simulator, and CI is green. A few points below. None of them blocks the change itself.

Should fix

1. Root is still missed when Nextcloud runs in a subfolder (Sources/NextcloudKit/Models/NKDataFileXML.swift:282, 326-328)

  • What this is: A server can live at a subfolder URL, for example https://host/nextcloud. The server puts that webroot at the start of every href.
  • Problem: rootFiles is always /remote.php/dav/files/, with no webroot. So parentPath == rootFiles never matches on these servers. A probe test with urlBase https://example.invalid/nextcloud and href /nextcloud/remote.php/dav/files/bob/ returns the root as fileName=bob, serverUrl=https://example.invalid/nextcloud/remote.php/dav/files. That is the same symptom this PR fixes. The bug existed before this PR.
  • Fix: Add the encoded webroot to rootFiles, and build the root URL with the existing NKDav helper:
    let rootFiles = (URLComponents(string: nkSession.urlBase)?.percentEncodedPath ?? "") + "/" + nkSession.dav + "/files/"
    …
    file.serverUrl = NKDav.homeURLStringNoSlash(urlBase: nkSession.urlBase, userId: davUserId)
    Then add one test row with urlBase https://example.invalid/nextcloud.
  • If we skip it: Subfolder installs keep getting the root back as an ordinary folder named after the user.

2. Client apps are not warned that the root changes (PR description; affects NextcloudFileProviderKit)

  • What this is: For the affected accounts, the root entry changes. Before, it was a folder named <userId> at …/dav/files. Now it is .__NC_ROOT__ at …/dav/files/<userId>. AGENTS.md asks PRs to warn about changes in behavior that consumers see.
  • Problem: The desktop File Provider stored the old root as a row under its real ocId. Nothing removes that row. After the update the desktop also writes a new .rootContainer row. The old row still answers itemMetadata(locatedAtRemoteUrl: davFilesUrl) and rootItemMetadata. Example: login alice@example.com, user ID alice, synced on the old version and then updated. The user pins the root ("keep downloaded"), but new files at the root do not inherit the pin. On a fresh install they do. This is not worse than before, because pinning the root failed outright on the old version. Files iOS keys its rows by ocId, so it is not affected.
  • Fix: Add a short consumer note to the PR description. When the desktop bumps NextcloudKit, add a one-time cleanup there that deletes rows where path == Account.webDavFilesUrlSuffix AND ocId != rootContainer. This PR needs no code change.
  • If we skip it: Users who update the desktop client see confusing "keep downloaded" behavior at the root.

Nice to have

3. The login-name fallback has no test (NKDataFileXML.swift:283, FilesRootResponseTests.swift)

  • What this is: userId.isEmpty ? user : userId keeps the old behavior for sessions without a user ID.
  • Problem: Both test rows set a user ID. If someone shortens the line to nkSession.userId, the test still passes. iOS and desktop always set the user ID, so the risk today is small.
  • Fix: Add one row with user alice, userId "", href /remote.php/dav/files/alice/, and expect the root marker.

4. SPDX header has no author name (Tests/NextcloudKitUnitTests/FilesRootResponseTests.swift:2)

  • What this is: AGENTS.md asks for <YEAR> <Author Name> in the SPDX header.
  • Problem: The line says 2026 Nextcloud contributors. The REUSE check still passes.
  • Fix: // SPDX-FileCopyrightText: 2026 Claudio Cambra

Verdict: Fine to merge once the consumer note (2) is in the description. 1 can go into this PR or come right after it.

@i2h3 i2h3 added the bug Something isn't working label Nov 26, 2026
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
Assisted-by: Codex:GPT-6
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
Assisted-by: Codex:GPT-6
@claucambra

Copy link
Copy Markdown
Contributor Author

Thanks @i2h3 I addressed your comments.

@i2h3 i2h3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks! All four points from my earlier review are addressed. The subfolder root works, the description has a consumer note, the empty user ID fallback is tested, and the SPDX header names you. All 9 cases of identifiesFilesRoot(user:webroot:) pass locally.

The red "Build and Test" check is a simulator hang, not a test failure (xctest (7016) encountered an error (The test runner hung before establishing connection.)). A re-run of the failed job should clear it.

Approving. The notes below are optional, so pick what you like for this PR or later.

1. Webroots with encoded characters still miss the root (NKDataFileXML.swift:282, FilesRootResponseTests.swift:12)
rootFiles takes the webroot from percentEncodedPath, but the server writes the webroot into hrefs unencoded. remote.php builds OC::$WEBROOT . '/remote.php/…' from the decoded SCRIPT_NAME, and Sabre only encodes the part after the base URI. For a webroot /nextcloud install the href starts with /nextcloud install/… while rootFiles is /nextcloud%20install/…, so the root comes back as an ordinary folder. The /nextcloud%20install test row sends an href a real server never emits, so it passes anyway. Plain webroots like /nextcloud work, and nothing regresses.
Fix: use URLComponents(string: nkSession.urlBase)?.path ?? "" instead of percentEncodedPath, and set the test's hrefPrefix to "/nextcloud install".

2. Consumer note misses accounts with an encoded user ID (PR description)
For a user ID such as john doe or one with an apostrophe, the root also changes, even when the login equals the user ID and there is no subfolder. One sentence in "Consumer impact" would make sure the Desktop follow-up covers these accounts too.

3. Test does not pin the user ID check (FilesRootResponseTests.swift)
Every entry whose parent is the Files collection is named after the session user, so if parentPath == rootFiles { without the name check would still pass. Real servers only list your own root there, so the risk is low. One extra <d:response> at …/files/other-user/ that keeps fileName == "other-user" would cover it.

4. IPv6 server addresses (existed before this PR, separate fix) (NKCommon.swift:403-413)
getHostName rebuilds the origin from URL.host, which drops the brackets. https://[::1]:8080/nc becomes https://::1:8080, so every serverUrl from the parser has an invalid prefix and the desktop root fixup never matches. Building the origin with URLComponents and .string keeps the brackets.

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

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants