Skip to content

fix(files_external): incorrect content when copying files on SFTP storage - #66672

Draft
joshtrichards wants to merge 3 commits into
masterfrom
jtr/fix-SFTPtruncateCopy
Draft

joshtrichards wants to merge 3 commits into
masterfrom
jtr/fix-SFTPtruncateCopy

Conversation

@joshtrichards

@joshtrichards joshtrichards commented Nov 16, 2026 •

Copy link
Copy Markdown
Member
  • Resolves: #na

Summary

Previously:

  • Copying a shorter file over an existing file left old trailing bytes.
  • Copying an empty file over an existing file reported success but left the destination untouched.
  • Copying an empty file to a new location reported success but created nothing.
  • A failed overwrite could leave new content mixed with old content.

Changes:

  • Write the first chunk without an explicit offset so phpseclib truncates an existing destination before writing.
  • Write an empty string for empty sources, creating or truncating the destination and reporting write failures.
  • Avoid truncating the source when the normalized paths match. This does not detect aliases such as symlinks or hard links.
  • Test coverage for SFTP::copy().

A failed copy can still leave a partial destination, but it will no longer be mixed with old trailing bytes. The overall copy() operation also remains non-atomic.

These have been around since #40183. I was surprised I couldn't find any relevant bug reports, but only same-mount copies use this path and ordinary non-empty copies to new filenames work, so much routine copying wouldn’t expose either bug.

TODO

  • Backport?

Checklist

AI (if applicable)

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

Previously:

- Copying a shorter file over an existing file left old trailing bytes.
- Copying an empty file over an existing file reported success but left the destination untouched.
- Copying an empty file to a new location reported success but created nothing.
- A failed overwrite could leave new content mixed with old content.

Changes:

- Write the first chunk without an explicit offset so phpseclib truncates an existing destination before writing.
- Write an empty string for empty sources, creating or truncating the destination and reporting write failures.
- Avoid truncating the source when the normalized paths match. This does not detect aliases such as symlinks or hard links.

A failed copy can still leave a partial destination, but it will no longer be mixed with old trailing bytes. The overall copy() operation also remains non-atomic.

Assisted-by: Copilot:gpt-6.1-terra

Signed-off-by: Josh <josh.t.richards@gmail.com>
Exercise copies around chunk boundaries and verify the resulting
destination contents and transfer offsets.

Cover unavailable source sizes, read and write failures on the first
and later chunks, and failure to write an empty destination. Add
integration cases for overwrites, empty files, and self-copy.

Assisted-by: Copilot:gpt-6.1-terra

Signed-off-by: Josh <josh.t.richards@gmail.com>
phpseclib's get() can return a string shorter than requested, including an empty string, when it reaches EOF or receives an SFTP status response. The current loop accepts these reads, writes the returned data, and advances by a full chunk. It can therefore report success with incomplete content.

Request and verify the expected remaining length for each chunk,
accounting for the final chunk being smaller than COPY_CHUNK_SIZE.

This also reports failure if the source shrinks during copying. It does not guarantee a consistent snapshot if the source changes without changing size, and failure after an earlier write can leave a partial destination.

The check is inexpensive and prevents silent success in the noted cases.

This has been around since #40183. I was surprised I couldn't find any relevant bug reports, but only same-mount copies use this path and ordinary non-empty copies to new filenames work, so much routine copying wouldn’t expose either bug.

Assisted-by: Copilot:gpt-6.1-sol

Signed-off-by: Josh <josh.t.richards@gmail.com>
@github-actions

Copy link
Copy Markdown
Contributor

Possible performance regression detected

Show Output
8 queries added

= /remote.php/dav/files/test
= /remote.php/dav/files/test/test.txt
= /remote.php/dav/files/test/many_files
= /remote.php/dav/files/test/new_file.txt
≠ /remote.php/dav/files/test/new_file.txt with 8 queries added
  - UPDATE "oc_filecache" SET "size" = :dcValue4 WHERE ("fileid" = :dcValue1) AND ("storage" = :dcValue2) AND (("size" <> :dcValue3) OR ("size" IS NULL))
  - UPDATE "oc_filecache" SET "size" = :dcValue4 WHERE ("fileid" = :dcValue1) AND ("storage" = :dcValue2) AND (("size" <> :dcValue3) OR ("size" IS NULL))
  - SELECT "path" FROM "oc_filecache" WHERE ("storage" = :dcValue1) AND ("fileid" = :dcValue2)
  - SELECT "path" FROM "oc_filecache" WHERE ("storage" = :dcValue1) AND ("fileid" = :dcValue2)
  + SELECT "storage", "path", "mimetype" FROM "oc_filecache" WHERE "fileid" = :dcValue1
  + SELECT "storage_id", "root_id", "user_id", "mount_point", "mount_id", "f"."path", "mount_provider_class" FROM "oc_mounts" "m" INNER JOIN "oc_filecache" "f" ON "m"."root_id" = "f"."fileid" WHERE ("m"."storage_id" = :dcValue1) AND ("f"."storage" = :dcValue2) AND (("f"."fileid" = :dcValue3) OR ("f"."path" = '') OR (("f"."path" || :dcValue4) = SUBSTR(:dcValue5, :dcValue6, LENGTH("f"."path") + :dcValue7))) AND ("user_id" = :dcValue8)
  + SELECT "filecache"."fileid", "storage", "path", "path_hash", "filecache"."parent", "filecache"."name", "mimetype", "mimepart", "size", "mtime", "storage_mtime", "encrypted", "filecache"."etag", "filecache"."permissions", "checksum", "unencrypted_size", "metadata_etag", "creation_time", "upload_time", "meta"."json" AS "meta_json", "meta"."sync_token" AS "meta_sync_token" FROM "oc_filecache" "filecache" LEFT JOIN "oc_filecache_extended" "fe" ON "filecache"."fileid" = "fe"."fileid" LEFT JOIN "oc_files_metadata" "meta" ON "filecache"."fileid" = "meta"."file_id" WHERE ("filecache"."fileid" = :dcValue1) AND ("storage" = :dcValue2)
  + DELETE FROM "oc_filecache" WHERE ("storage" = :dcValue1) AND ("fileid" = :dcValue2)
  + DELETE FROM "oc_filecache_extended" WHERE "fileid" = :dcValue1
  + SELECT "file_id", "json", "sync_token" FROM "oc_files_metadata" WHERE "file_id" IN (:dcValue1)
  + SELECT "file_id", "json", "sync_token" FROM "oc_files_metadata" WHERE "file_id" IN (:dcValue1)
  + DELETE FROM "oc_files_metadata" WHERE "file_id" IN (:dcValue1)
  + DELETE FROM "oc_files_metadata_index" WHERE "file_id" IN (:dcValue1)
  + SELECT "fileid" FROM "oc_filecache" WHERE ("storage" = :dcValue1) AND ("path_hash" = :dcValue2)
  + UPDATE "oc_file_locks" SET "lock" = :dcValue1, "ttl" = :dcValue2 WHERE ("key" = :dcValue3) AND ("lock" = :dcValue4)
  + SELECT "filecache"."fileid", "storage", "path", "path_hash", "filecache"."parent", "filecache"."name", "mimetype", "mimepart", "size", "mtime", "storage_mtime", "encrypted", "filecache"."etag", "filecache"."permissions", "checksum", "unencrypted_size", "metadata_etag", "creation_time", "upload_time", "meta"."json" AS "meta_json", "meta"."sync_token" AS "meta_sync_token" FROM "oc_filecache" "filecache" LEFT JOIN "oc_filecache_extended" "fe" ON "filecache"."fileid" = "fe"."fileid" LEFT JOIN "oc_files_metadata" "meta" ON "filecache"."fileid" = "meta"."file_id" WHERE ("path_hash" = :dcValue1) AND ("storage" = :dcValue2)
= ./occ profiler:list

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants