fix(webdav): fall back to Depth:1 recursion when PROPFIND infinity returns HTTP 500
ci / verify (pull_request) Successful in 30s
ci / verify (pull_request) Successful in 30s
Some WebDAV servers (Nextcloud, ownCloud, Apache mod_dav with infinite depth disabled) reject Depth:infinity PROPFIND with HTTP 500/403/501. list_files now retries with a recursive Depth:1 traversal of child collections, preserving full relative paths for year-folder filtering and dedup keys. BIT-33518 Paperclip-Ref: BIT-33518
This commit is contained in:
1 parent
f55629212c
commit
e1ddd2a293
2 files changed
+216
-3
No files matched your search
@@ -4,9 +4,11 @@ from forwarder.webdav.client import (
|
||||
MIN_WEBDAV_FILE_BYTES,
|
||||
WebdavConfig,
|
||||
WebdavFile,
|
||||
_parse_propfind_collections,
|
||||
_parse_propfind_response,
|
||||
build_webdav_root,
|
||||
get_webdav_message_key,
|
||||
list_files,
|
||||
normalize_webdav_folder_path,
|
||||
parse_webdav_message_key,
|
||||
webdav_bytes_below_min_size,
|
||||
@@ -232,3 +234,138 @@ def test_parse_propfind_response():
|
||||
assert not webdav_file_matches_min_year(invoice, 2025)
|
||||
photo = next(item for item in files if item.name == "photo.jpg")
|
||||
assert webdav_file_matches_min_year(photo, 2025)
|
||||
|
||||
|
||||
def _propfind_response(folder_href: str, entries: list[dict]) -> bytes:
|
||||
"""Build a minimal DAV multistatus body for tests."""
|
||||
parts = [
|
||||
b'<?xml version="1.0" encoding="utf-8"?>',
|
||||
b'<D:multistatus xmlns:D="DAV:">',
|
||||
]
|
||||
for entry in entries:
|
||||
is_collection = entry.get("collection", False)
|
||||
href = entry["href"].encode()
|
||||
parts.append(b" <D:response>")
|
||||
parts.append(b" <D:href>" + href + b"</D:href>")
|
||||
parts.append(b" <D:propstat><D:prop>")
|
||||
if is_collection:
|
||||
parts.append(b" <D:resourcetype><D:collection/></D:resourcetype>")
|
||||
else:
|
||||
parts.append(b" <D:resourcetype/>")
|
||||
parts.append(b" <D:getetag>" + entry.get("etag", b'"x"') + b"</D:getetag>")
|
||||
parts.append(
|
||||
b" <D:getlastmodified>"
|
||||
+ entry.get("modified", b"Mon, 01 Jan 2024 10:00:00 GMT")
|
||||
+ b"</D:getlastmodified>"
|
||||
)
|
||||
parts.append(
|
||||
b" <D:getcontentlength>"
|
||||
+ str(entry.get("size", 5000)).encode()
|
||||
+ b"</D:getcontentlength>"
|
||||
)
|
||||
parts.append(b" </D:prop></D:propstat>")
|
||||
parts.append(b" </D:response>")
|
||||
parts.append(b"</D:multistatus>")
|
||||
return b"\n".join(parts)
|
||||
|
||||
|
||||
ROOT_URL = "https://cloud.example.com/remote.php/dav/files/user/Faturas"
|
||||
|
||||
|
||||
def _make_config() -> WebdavConfig:
|
||||
return WebdavConfig(
|
||||
base_url="https://cloud.example.com/remote.php/dav/files/user",
|
||||
folder_path="/Faturas",
|
||||
user="user",
|
||||
password="secret",
|
||||
verify_ssl=False,
|
||||
)
|
||||
|
||||
|
||||
def test_parse_propfind_collections_skips_root():
|
||||
body = _propfind_response(
|
||||
ROOT_URL + "/",
|
||||
[
|
||||
{"href": ROOT_URL + "/", "collection": True},
|
||||
{"href": ROOT_URL + "/2025/", "collection": True},
|
||||
{"href": ROOT_URL + "/invoice.pdf"},
|
||||
],
|
||||
)
|
||||
children = _parse_propfind_collections(ROOT_URL, body)
|
||||
assert children == [ROOT_URL + "/2025/"]
|
||||
|
||||
|
||||
def test_list_files_falls_back_to_depth_one_on_http_500(monkeypatch):
|
||||
"""Depth:infinity returning HTTP 500 triggers recursive Depth:1 traversal."""
|
||||
import httpx
|
||||
import forwarder.webdav.client as webdav_client
|
||||
|
||||
root_body = _propfind_response(
|
||||
ROOT_URL + "/",
|
||||
[
|
||||
{"href": ROOT_URL + "/", "collection": True},
|
||||
{"href": ROOT_URL + "/2025/", "collection": True},
|
||||
{"href": ROOT_URL + "/invoice.pdf", "etag": b'"abc"'},
|
||||
],
|
||||
)
|
||||
sub_body = _propfind_response(
|
||||
ROOT_URL + "/2025/",
|
||||
[
|
||||
{"href": ROOT_URL + "/2025/", "collection": True},
|
||||
{"href": ROOT_URL + "/2025/receipt.pdf", "etag": b'"def"', "size": 9000},
|
||||
],
|
||||
)
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
depth = request.headers.get("Depth")
|
||||
url = str(request.url).rstrip("/")
|
||||
if depth == "infinity":
|
||||
return httpx.Response(500, text="infinite depth disabled")
|
||||
if url == ROOT_URL and depth == "1":
|
||||
return httpx.Response(207, content=root_body)
|
||||
if url == ROOT_URL + "/2025" and depth == "1":
|
||||
return httpx.Response(207, content=sub_body)
|
||||
return httpx.Response(404)
|
||||
|
||||
transport = httpx.MockTransport(handler)
|
||||
|
||||
def _fake_client(config, *, timeout=60.0):
|
||||
return httpx.Client(
|
||||
transport=transport,
|
||||
auth=(config.user, config.password),
|
||||
headers={"User-Agent": "Bizdocs-Forwarder/1.0"},
|
||||
)
|
||||
|
||||
monkeypatch.setattr(webdav_client, "_client", _fake_client)
|
||||
|
||||
files = list_files(_make_config())
|
||||
paths = sorted(item.relative_path for item in files)
|
||||
assert paths == ["2025/receipt.pdf", "invoice.pdf"]
|
||||
receipt = next(item for item in files if item.name == "receipt.pdf")
|
||||
assert receipt.size == 9000
|
||||
assert webdav_file_matches_min_year(receipt, 2025)
|
||||
|
||||
|
||||
def test_list_files_still_raises_on_persistent_500(monkeypatch):
|
||||
"""A 500 that is not resolved by Depth:1 fallback surfaces as WebdavError."""
|
||||
import httpx
|
||||
import forwarder.webdav.client as webdav_client
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
return httpx.Response(500, text="server error")
|
||||
|
||||
transport = httpx.MockTransport(handler)
|
||||
|
||||
def _fake_client(config, *, timeout=60.0):
|
||||
return httpx.Client(
|
||||
transport=transport,
|
||||
auth=(config.user, config.password),
|
||||
headers={"User-Agent": "Bizdocs-Forwarder/1.0"},
|
||||
)
|
||||
|
||||
monkeypatch.setattr(webdav_client, "_client", _fake_client)
|
||||
|
||||
import pytest
|
||||
|
||||
with pytest.raises(webdav_client.WebdavError):
|
||||
list_files(_make_config())
|
||||
Reference in new issue
Block a user