[Bug]: Late failed chunk PUT deletes a successful retry, causing final MOVE 400

Đang mở
#66,477 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
backend, database

Hướng nghiên cứu

Start with apps/dav/lib/Upload/UploadFolder.php, especially UploadFolder::createFile() and its exception cleanup, then inspect the storage behavior of File::put() and related upload tests. Reproduce the interleaving described in the issue and add a regression test showing that a failed PUT leaves a successful same-name retry available for assembly. Done means the regression passes while cleanup still handles partial data across storage implementations.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Summary

A late failure in OCA\DAV\Upload\UploadFolder::createFile() can delete the final chunk already committed by another request retrying the same chunk name.

The retry returns 201 Created. When the original incomplete request eventually fails, its catch block looks up the chunk by name and deletes it. The final MOVE .../.file then returns 400 Bad Request because the stored chunks no longer add up to OC-Total-Length.

Verification scope
  • Reproduced with real WebDAV requests on a downstream image based on official nextcloud:34.0.2-apache.
  • Reproduced directly against Apache, without Traefik in the request path.
  • A follow-up test sent the successful retry, final MOVE, and content download through Traefik HTTPS. The original incomplete request was sent directly to Apache to control its failure timing.
  • Inspected official v34.0.5, v35.0.2, v34.0.6rc1, v35.0.3rc1, and current master on 2026-11-08. They retain this cleanup logic.
  • A focused PHP regression harness loading the unmodified UploadFolder.php from both v34.0.5 and v35.0.2 also fails with “Late failure deleted the successful retry”. This harness simulates the interleaving using a Directory test double.
  • A complete deployment of the latest patch releases has not been tested. The live instance also includes an unrelated downstream Directory::createFile() change to release upload locks in a finally block. Please distinguish the live reproduction from the focused newer-source regression tests.
Steps to reproduce

Use a disposable test account and an HTTP Apache upstream accessible from the test machine. Configure the origin as a trusted Nextcloud URL. No reverse proxy is required.

  1. Create a unique authenticated DAV upload collection.
  2. Start PUT A to chunk 1, declaring a 2 MiB Content-Length but initially sending only 8 KiB. Keep the connection open.
  3. Send PUT B to the same chunk URL with a complete 2 MiB payload. Observe 201 Created.
  4. End PUT A's request body early. Observe 400 Bad Request.
  5. Send MOVE <upload>/.file with OC-Total-Length: 2097152 and a Destination header.

Minimal Python 3 reproduction, using only the standard library. Set NC_URL to the local HTTP origin, for example http://localhost:8080, and NC_USER to a disposable test account. The password is prompted without echo. Only the uniquely named synthetic paths are cleaned up.

import base64
import getpass
import http.client
import os
import secrets
import socket
import time
from urllib.parse import quote, urlsplit

origin = os.environ["NC_URL"].rstrip("/")
url = urlsplit(origin)
assert url.scheme == "http", "Use the direct local Apache HTTP upstream"
user = quote(os.environ["NC_USER"], safe="")
password = getpass.getpass("Test account password/app password: ")
auth = "Basic " + base64.b64encode(
    (os.environ["NC_USER"] + ":" + password).encode()
).decode()
session = "retry-repro-" + secrets.token_hex(8)
upload = url.path.rstrip("/") + "/remote.php/dav/uploads/" + user + "/" + session
target = url.path.rstrip("/") + "/remote.php/dav/files/" + user + "/" + session + ".bin"
destination = url.scheme + "://" + url.netloc + target
data = b"B" * (2 * 1024 * 1024)

def request(method, path, body=None, extra=None):
    conn = http.client.HTTPConnection(url.hostname, url.port or 80, timeout=40)
    headers = {
        "Host": url.netloc,
        "Authorization": auth,
        "X-Requested-With": "XMLHttpRequest",
    }
    headers.update(extra or {})
    conn.request(method, path, body=body, headers=headers)
    response = conn.getresponse()
    result = response.status, response.read()
    conn.close()
    return result

original = None
try:
    status, body = request("MKCOL", upload, extra={
        "Destination": destination, "Content-Length": "0",
    })
    assert status == 201, (status, body)
    original = socket.create_connection((url.hostname, url.port or 80), timeout=40)
    headers = (
        f"PUT {upload}/1 HTTP/1.1\r\n"
        f"Host: {url.netloc}\r\nAuthorization: {auth}\r\n"
        f"Content-Length: {len(data)}\r\nConnection: close\r\n\r\n"
    )
    original.sendall(headers.encode() + b"A" * 8192)
    time.sleep(1)
    print("Complete retry:", request("PUT", upload + "/1", data)[0])
    original.shutdown(socket.SHUT_WR)
    reply = b""
    while True:
        block = original.recv(65536)
        if not block:
            break
        reply += block
    print("Original incomplete PUT:", reply.split(b"\r\n", 1)[0].decode())
    print("Assembly:", request("MOVE", upload + "/.file", extra={
        "Destination": destination, "OC-Total-Length": str(len(data)),
    }))
finally:
    if original is not None:
        original.close()
    request("DELETE", upload)
    request("DELETE", target)
Actual behavior

Observed before changing UploadFolder cleanup, with a 2 MiB synthetic payload

Complete retry: 201
Original incomplete PUT: HTTP/1.1 400 Bad Request
Assembly: 400
Sabre\DAV\Exception\BadRequest
Chunks on server do not sum up to 2097152 but to 0 bytes

In a browser large-file upload we also observed successful chunk retries followed several minutes later by failures of the older PUTs, and finally MOVE 400.

Expected behavior

Failure cleanup must only remove data belonging to the failed attempt. A chunk already committed by a successful retry must remain available for final assembly.

Relevant implementation and local experiment

UploadFolder::createFile() catches the exception from the underlying Directory and, if a child with the requested name exists, deletes that child without establishing that the child belongs to the failed attempt. File::put() already manages per-attempt partial-file cleanup on the local storage path used here.

Removing that outer name-based deletion while allowing the original exception to propagate changed the same real HTTP test to

Complete retry: 201
Original incomplete PUT: HTTP/1.1 400 Bad Request
Assembly: 201
Download: 2097152 bytes, exactly matching the complete retry payload
SHA-256: 7995dfceebfb9fa8972d361d53d899f6e268ecd6d823b5737198041fabc02e20

This local experiment establishes the cleanup race on our storage path. Any upstream fix should also account for storage implementations with different partial-file behavior.

The affected method is present in these official sources.

Environment
  • Live server version tested: 34.0.2, Apache-based downstream Docker image
  • PHP: 8.5.9
  • OS inside container: Debian GNU/Linux 13
  • Database configuration: PostgreSQL (dbtype=pgsql)
  • File locking: enabled, Redis
  • Primary data: local filesystem exposed through a bind mount
  • Server-side encryption: disabled
  • Browser reproduction: Chrome, authenticated upload
  • Upload settings during the affected browser test: 16 MiB chunks, maximum parallel count 2
  • Existing recovery administrator used for the successful synthetic HTTP test. A separate temporary ordinary-user probe timed out during initial setup and is not counted as a successful reproduction.
  • Full configuration, credentials, private hostnames, user identifiers, and user files are intentionally omitted.
Related reports searched

#65408 concerns public-share final assembly being interrupted and the web uploader deleting the session. #63071 concerns interrupted assembly and destination/filecache inconsistency. This report isolates an earlier server-side chunk PUT failure deleting a successful same-name retry before assembly.

Additional information

AI assistance was used to investigate the source, organize the reproduction, run the controlled tests, and draft this report. The HTTP results and before/after content comparison were observed on the affected instance. Newer releases were checked using source inspection and a focused regression harness as described above.

Ngôn ngữ chính
PHP
Star
37.5k
Fork
5.4k
Merge trung bình
1 ngày 13 giờ
Pull request đã merge (30 ngày)
639

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của nextcloud/server

Tất cả issue của nextcloud/server

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.