Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_webdav.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 18 minutes ago History
test_webdav.py · 20.6 KB · 476 lines
1"""WebDAV (/dav/files/): a person's files as a folder a desktop can mount.
2
3What these guard, and why it would matter:
4
5- Only the key's owner's files are listed, and only an API key opens the
6 folder: a mounted folder is a standing connection, so the wrong files or a
7 browser cookie riding along would be a quiet, durable leak.
8- Writes go through the same quota, permission and record paths as
9 /api/files: a second way in that skipped one would be a hole with a
10 convenient shape.
11- Refusals are refusals (MKCOL, LOCK): a desktop told a folder or a lock
12 exists when it does not loses work later instead of failing now.
13"""
14
15import base64
16import json
17
18import pytest
19from test_object_server import create_identity_session, raw_request, request
20from test_object_user_files import files_env, multipart_body
21
22import object_api_keys
23import object_server
24import object_user_files
25import object_webdav
26
27
28def dav_env(tmp_path, monkeypatch):
29 data_dir = files_env(tmp_path, monkeypatch)
30 monkeypatch.setenv(object_server.WEBDAV_ENABLED_ENV, "true")
31 # enforce the files policy for real (as the desk does), so these tests
32 # exercise the owner row filter rather than the admin-token fallback
33 monkeypatch.setenv(object_server.PERMISSION_UNREADY_ENFORCEMENT_ENV, "true")
34 return data_dir
35
36
37def key_for(data_dir, user_id="dan"):
38 _, token = object_api_keys.create_api_key(user_id, "desk", base_dir=data_dir)
39 return token
40
41
42def basic(token, user="anyone"):
43 raw = base64.b64encode(f"{user}:{token}".encode()).decode()
44 return [("authorization", f"Basic {raw}")]
45
46
47def dav(path, token, method="GET", body=b"", headers=()):
48 return raw_request(path, method=method, body=body, headers=basic(token) + list(headers))
49
50
51def listing(token, path="/dav/files/", depth="1"):
52 status, _, body = dav(path, token, method="PROPFIND", headers=[("depth", depth)])
53 assert status == 207, body
54 return body.decode()
55
56
57def names_in(xml):
58 import re
59
60 return re.findall(r"<D:displayname>([^<]*)</D:displayname>", xml)
61
62
63def records(data_dir):
64 lines = (data_dir / "collections" / "files" / "records.tsv").read_text().splitlines()
65 head = lines[0].split("\t")
66 return [dict(zip(head, line.split("\t"), strict=False)) for line in lines[1:] if line.strip()]
67
68
69# --- the pure half ---------------------------------------------------------
70
71
72def test_a_filename_is_made_safe_for_a_path_not_trusted():
73 assert object_webdav.safe_name("a/b\\c.txt") == "a_b_c.txt"
74 assert object_webdav.safe_name("bad\x00\x1fname") == "badname"
75 assert object_webdav.safe_name("..") == "unnamed"
76 assert object_webdav.safe_name("") == "unnamed"
77 assert len(object_webdav.safe_name("x" * 400)) == 255
78
79
80def test_duplicate_names_get_a_suffix_and_the_oldest_keeps_the_plain_name():
81 folder = object_webdav.folder_names(
82 [
83 {"id": "b", "filename": "notes.txt", "created_at": "2026-01-02T00:00:00Z"},
84 {"id": "a", "filename": "notes.txt", "created_at": "2026-01-01T00:00:00Z"},
85 {"id": "c", "filename": "NOTES.txt", "created_at": "2026-01-03T00:00:00Z"},
86 {"id": "d", "filename": "README", "created_at": "2026-01-01T00:00:00Z"},
87 {"id": "e", "filename": "README", "created_at": "2026-01-02T00:00:00Z"},
88 ]
89 )
90 assert folder["notes.txt"]["id"] == "a"
91 assert folder["notes (2).txt"]["id"] == "b"
92 assert folder["NOTES (3).txt"]["id"] == "c" # case-insensitive, like the desktops
93 assert folder["README (2)"]["id"] == "e"
94
95
96def test_paths_outside_the_two_level_tree_do_not_exist():
97 assert object_webdav.split_path("/dav") == ("root", None)
98 assert object_webdav.split_path("/dav/files/") == ("files", None)
99 assert object_webdav.split_path("/dav/files/a b.txt") == ("file", "a b.txt")
100 for path in ("/dav/other/", "/dav/files/a/b", "/davx"):
101 with pytest.raises(object_webdav.DavRequestError):
102 object_webdav.split_path(path)
103
104
105def test_ranges_serve_one_slice_and_refuse_what_cannot_be_served():
106 assert object_webdav.parse_range("bytes=0-3", 10) == (0, 3)
107 assert object_webdav.parse_range("bytes=5-", 10) == (5, 9)
108 assert object_webdav.parse_range("bytes=-4", 10) == (6, 9)
109 assert object_webdav.parse_range("bytes=2-99", 10) == (2, 9)
110 assert object_webdav.parse_range("bytes=0-1,4-5", 10) is None # whole file
111 with pytest.raises(object_webdav.DavRequestError):
112 object_webdav.parse_range("bytes=10-", 10)
113
114
115def test_a_destination_is_read_as_a_path_whatever_host_it_names():
116 assert object_webdav.destination_path("http://proxy:9/dav/files/a%20b.txt") == "/dav/files/a b.txt"
117 assert object_webdav.destination_path("/dav/files/x") == "/dav/files/x"
118
119
120# --- the surface -----------------------------------------------------------
121
122
123def test_the_folder_is_off_unless_enabled(tmp_path, monkeypatch):
124 data_dir = files_env(tmp_path, monkeypatch)
125 status, _, _ = dav("/dav/files/", key_for(data_dir), method="PROPFIND")
126 assert status == 404
127
128
129def test_without_an_api_key_the_folder_asks_for_one(tmp_path, monkeypatch):
130 """A password or a session cookie must not open it: a password here would
131 be a login without the login's rate limits, and a cookie would let any
132 page the person visits write to their files."""
133 dav_env(tmp_path, monkeypatch)
134 status, headers, _ = raw_request("/dav/files/", method="PROPFIND")
135 assert status == 401
136 assert headers[b"www-authenticate"].startswith(b"Basic")
137
138 status, _, _ = dav("/dav/files/", "not-a-key", method="PROPFIND")
139 assert status == 401
140
141 session, _ = create_identity_session({"user_id": "dan"})
142 status, _, _ = raw_request(
143 "/dav/files/", method="PROPFIND", headers=[("cookie", f"dbbasic_session={session}")]
144 )
145 assert status == 401
146 status, _, _ = raw_request(
147 "/dav/files/", method="PROPFIND", headers=[("authorization", f"Bearer {session}")]
148 )
149 assert status == 401
150
151
152def test_options_says_class_1_without_asking_for_a_key(tmp_path, monkeypatch):
153 dav_env(tmp_path, monkeypatch)
154 status, headers, _ = raw_request("/dav/files/", method="OPTIONS")
155 assert status == 200
156 assert headers[b"dav"] == b"1"
157 assert b"LOCK" not in headers[b"allow"]
158
159
160def test_put_creates_a_file_that_the_files_api_also_serves(tmp_path, monkeypatch):
161 data_dir = dav_env(tmp_path, monkeypatch)
162 token = key_for(data_dir)
163 status, _, _ = dav("/dav/files/hello.txt", token, method="PUT", body=b"hi there")
164 assert status == 201
165
166 [record] = records(data_dir)
167 assert record["filename"] == "hello.txt"
168 assert record["owner_id"] == "dan"
169 assert record["size"] == "8"
170 assert record["content_type"] == "text/plain"
171
172 status, _, body = raw_request(f"/api/files/{record['id']}", headers=[("authorization", f"Bearer {token}")])
173 assert status == 200 and body == b"hi there"
174 assert "hello.txt" in names_in(listing(token))
175
176
177def test_only_the_key_owners_files_are_in_the_folder(tmp_path, monkeypatch):
178 data_dir = dav_env(tmp_path, monkeypatch)
179 dan, eve = key_for(data_dir, "dan"), key_for(data_dir, "eve")
180 dav("/dav/files/dans.txt", dan, method="PUT", body=b"d")
181 dav("/dav/files/eves.txt", eve, method="PUT", body=b"e")
182
183 assert names_in(listing(dan)) == ["files", "dans.txt"]
184 status, _, _ = dav("/dav/files/eves.txt", dan)
185 assert status == 404
186 status, _, _ = dav("/dav/files/eves.txt", dan, method="DELETE")
187 assert status == 404
188 assert len(records(data_dir)) == 2
189
190
191def test_a_broken_policy_is_a_503_never_an_empty_folder(tmp_path, monkeypatch):
192 """A sync client that sees an empty listing deletes its copies. With the policy
193 file broken the folder answered 207 with no files and 404 for each one."""
194 data_dir = dav_env(tmp_path, monkeypatch)
195 token = key_for(data_dir)
196 dav("/dav/files/keep.txt", token, method="PUT", body=b"keep")
197 assert "keep.txt" in names_in(listing(token))
198 (data_dir / "permissions" / "policy.json").write_text("{ not json")
199 status, _, body = dav("/dav/files/", token, method="PROPFIND", headers=[("depth", "1")])
200 assert status == 503 and b"keep.txt" not in body
201 status, _, body = dav("/dav/files/keep.txt", token)
202 assert status == 503 and body != b"keep"
203 status, _, _ = dav("/dav/files/keep.txt", token, method="DELETE")
204 assert status == 503
205 assert len(records(data_dir)) == 1
206
207
208def test_an_unreadable_files_collection_is_a_503_never_an_empty_folder(tmp_path, monkeypatch):
209 data_dir = dav_env(tmp_path, monkeypatch)
210 token = key_for(data_dir)
211 dav("/dav/files/keep.txt", token, method="PUT", body=b"keep")
212 path = data_dir / "collections" / "files" / "records.tsv"
213 path.rename(path.with_name("records.tsv.bak"))
214 path.mkdir()
215 status, _, body = dav("/dav/files/", token, method="PROPFIND", headers=[("depth", "1")])
216 assert status == 503 and b"<D:multistatus" not in body
217
218
219def test_an_unreadable_keys_file_is_a_503_not_a_sign_in_prompt(tmp_path, monkeypatch):
220 """A 401 here makes a desktop forget the saved key; the store is down, not the key."""
221 data_dir = dav_env(tmp_path, monkeypatch)
222 token = key_for(data_dir)
223 path = object_api_keys.api_keys_path(data_dir)
224 path.rename(path.with_name(path.name + ".bak"))
225 path.mkdir()
226 status, headers, _ = dav("/dav/files/", token, method="PROPFIND", headers=[("depth", "1")])
227 assert status == 503 and headers[b"retry-after"] == b"2"
228
229
230def test_a_put_to_an_existing_name_replaces_the_bytes_and_keeps_the_record(tmp_path, monkeypatch):
231 data_dir = dav_env(tmp_path, monkeypatch)
232 token = key_for(data_dir)
233 dav("/dav/files/a.txt", token, method="PUT", body=b"one")
234 [before] = records(data_dir)
235 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"second version")
236 assert status == 204
237 [after] = records(data_dir)
238 assert after["id"] == before["id"]
239 assert after["size"] == "14"
240 status, _, body = dav("/dav/files/a.txt", token)
241 assert body == b"second version"
242
243
244def test_conditional_writes_refuse_to_clobber(tmp_path, monkeypatch):
245 """If-None-Match: * is "create only"; a stale If-Match is someone else's
246 newer copy. Without these a client can silently overwrite."""
247 data_dir = dav_env(tmp_path, monkeypatch)
248 token = key_for(data_dir)
249 dav("/dav/files/a.txt", token, method="PUT", body=b"one")
250 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"x", headers=[("if-none-match", "*")])
251 assert status == 412
252 status, headers, _ = dav("/dav/files/a.txt", token)
253 etag = headers[b"etag"].decode()
254 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"two", headers=[("if-match", '"stale"')])
255 assert status == 412
256 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"two", headers=[("if-match", etag)])
257 assert status == 204
258
259
260def test_move_renames_and_will_not_replace_without_overwrite(tmp_path, monkeypatch):
261 data_dir = dav_env(tmp_path, monkeypatch)
262 token = key_for(data_dir)
263 dav("/dav/files/a.txt", token, method="PUT", body=b"aaa")
264 dav("/dav/files/b.md", token, method="PUT", body=b"bbb")
265
266 status, _, _ = dav(
267 "/dav/files/a.txt", token, method="MOVE",
268 headers=[("destination", "http://desk/dav/files/b.md"), ("overwrite", "F")],
269 )
270 assert status == 412
271
272 status, _, _ = dav(
273 "/dav/files/a.txt", token, method="MOVE", headers=[("destination", "/dav/files/Renamed%20a.md")]
274 )
275 assert status == 201
276 names = sorted(r["filename"] for r in records(data_dir))
277 assert names == ["Renamed a.md", "b.md"]
278 renamed = next(r for r in records(data_dir) if r["filename"] == "Renamed a.md")
279 assert renamed["content_type"] == "text/markdown"
280
281 status, _, _ = dav(
282 "/dav/files/Renamed a.md", token, method="MOVE", headers=[("destination", "/dav/files/b.md")]
283 )
284 assert status == 204
285 [left] = records(data_dir)
286 assert left["filename"] == "b.md" and left["id"] == renamed["id"]
287 status, _, body = dav("/dav/files/b.md", token)
288 assert body == b"aaa"
289
290
291def test_a_rename_that_only_changes_case_is_a_rename(tmp_path, monkeypatch):
292 data_dir = dav_env(tmp_path, monkeypatch)
293 token = key_for(data_dir)
294 dav("/dav/files/readme.txt", token, method="PUT", body=b"r")
295 status, _, _ = dav("/dav/files/readme.txt", token, method="MOVE", headers=[("destination", "/dav/files/README.txt")])
296 assert status == 201
297 assert [r["filename"] for r in records(data_dir)] == ["README.txt"]
298
299
300def test_copy_makes_a_second_file(tmp_path, monkeypatch):
301 data_dir = dav_env(tmp_path, monkeypatch)
302 token = key_for(data_dir)
303 dav("/dav/files/a.txt", token, method="PUT", body=b"same")
304 status, _, _ = dav("/dav/files/a.txt", token, method="COPY", headers=[("destination", "/dav/files/c.txt")])
305 assert status == 201
306 assert sorted(r["filename"] for r in records(data_dir)) == ["a.txt", "c.txt"]
307 status, _, body = dav("/dav/files/c.txt", token)
308 assert body == b"same"
309
310
311def test_delete_removes_the_record_and_the_bytes(tmp_path, monkeypatch):
312 data_dir = dav_env(tmp_path, monkeypatch)
313 token = key_for(data_dir)
314 dav("/dav/files/a.txt", token, method="PUT", body=b"gone soon")
315 [record] = records(data_dir)
316 status, _, _ = dav("/dav/files/a.txt", token, method="DELETE")
317 assert status == 204
318 assert records(data_dir) == []
319 assert not object_user_files.file_path("dan", record["id"], base_dir=data_dir).exists()
320
321
322def test_a_range_is_served_as_206(tmp_path, monkeypatch):
323 data_dir = dav_env(tmp_path, monkeypatch)
324 token = key_for(data_dir)
325 dav("/dav/files/v.bin", token, method="PUT", body=b"0123456789")
326 status, headers, body = dav("/dav/files/v.bin", token, headers=[("range", "bytes=2-5")])
327 assert status == 206
328 assert body == b"2345"
329 assert headers[b"content-range"] == b"bytes 2-5/10"
330 status, headers, _ = dav("/dav/files/v.bin", token, headers=[("range", "bytes=50-")])
331 assert status == 416
332
333
334def test_an_uploaded_page_is_served_sandboxed_as_an_attachment(tmp_path, monkeypatch):
335 """Served from the server's own origin: a stored HTML file must not run
336 as a page there, or uploading it would be stored XSS."""
337 data_dir = dav_env(tmp_path, monkeypatch)
338 token = key_for(data_dir)
339 dav("/dav/files/x.html", token, method="PUT", body=b"<script>alert(1)</script>")
340 status, headers, _ = dav("/dav/files/x.html", token)
341 assert status == 200
342 assert headers[b"content-security-policy"] == b"sandbox"
343 assert headers[b"content-disposition"] == b"attachment"
344 assert headers[b"x-content-type-options"] == b"nosniff"
345
346
347def test_propfind_answers_what_was_asked_and_names_what_is_missing(tmp_path, monkeypatch):
348 data_dir = dav_env(tmp_path, monkeypatch)
349 token = key_for(data_dir)
350 dav("/dav/files/a.txt", token, method="PUT", body=b"abc")
351 body = (
352 b'<?xml version="1.0"?><D:propfind xmlns:D="DAV:" xmlns:Z="urn:x">'
353 b"<D:prop><D:getcontentlength/><Z:quota/></D:prop></D:propfind>"
354 )
355 status, _, xml = dav("/dav/files/a.txt", token, method="PROPFIND", body=body, headers=[("depth", "0")])
356 assert status == 207
357 xml = xml.decode()
358 assert "<D:getcontentlength>3</D:getcontentlength>" in xml
359 assert "404 Not Found" in xml and "quota" in xml
360 assert "getlastmodified" not in xml
361
362 root = listing(token, "/dav/", depth="infinity")
363 assert names_in(root) == ["dav", "files", "a.txt"]
364
365
366def test_a_propfind_with_a_doctype_is_refused(tmp_path, monkeypatch):
367 data_dir = dav_env(tmp_path, monkeypatch)
368 body = b'<?xml version="1.0"?><!DOCTYPE x [<!ENTITY a "aaaa">]><D:propfind xmlns:D="DAV:"/>'
369 status, _, _ = dav("/dav/files/", key_for(data_dir), method="PROPFIND", body=body)
370 assert status == 400
371
372
373def test_folders_and_locks_are_refused_not_faked(tmp_path, monkeypatch):
374 data_dir = dav_env(tmp_path, monkeypatch)
375 token = key_for(data_dir)
376 for method, path in (("MKCOL", "/dav/files/new/"), ("LOCK", "/dav/files/a.txt"), ("UNLOCK", "/dav/files/a.txt")):
377 status, headers, _ = dav(path, token, method=method)
378 assert status == 405, method
379 assert b"PROPFIND" in headers[b"allow"]
380
381
382def test_proppatch_refuses_each_property_with_403(tmp_path, monkeypatch):
383 data_dir = dav_env(tmp_path, monkeypatch)
384 token = key_for(data_dir)
385 dav("/dav/files/a.txt", token, method="PUT", body=b"a")
386 body = (
387 b'<?xml version="1.0"?><D:propertyupdate xmlns:D="DAV:" xmlns:W="urn:schemas-microsoft-com:">'
388 b"<D:set><D:prop><W:Win32LastModifiedTime>x</W:Win32LastModifiedTime></D:prop></D:set>"
389 b"</D:propertyupdate>"
390 )
391 status, _, xml = dav("/dav/files/a.txt", token, method="PROPPATCH", body=body)
392 assert status == 207
393 assert b"403 Forbidden" in xml and b"Win32LastModifiedTime" in xml
394
395
396def test_the_quota_holds_for_new_files_and_for_replacements(tmp_path, monkeypatch):
397 data_dir = dav_env(tmp_path, monkeypatch)
398 monkeypatch.setenv(object_server.USER_FILES_QUOTA_ENV, "10")
399 token = key_for(data_dir)
400 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"12345678")
401 assert status == 201
402 status, _, _ = dav("/dav/files/b.txt", token, method="PUT", body=b"123")
403 assert status == 413
404 # replacing a file frees its old bytes first: 8 -> 10 fits, 8 -> 11 does not
405 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"1234567890")
406 assert status == 204
407 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"12345678901")
408 assert status == 507
409
410
411def test_a_put_may_exceed_the_general_request_cap_up_to_its_own(tmp_path, monkeypatch):
412 """A file is bigger than an API call; the PUT cap is its own and still a
413 cap, because the body is read into memory whole."""
414 data_dir = dav_env(tmp_path, monkeypatch)
415 monkeypatch.setenv(object_server.MAX_REQUEST_BYTES_ENV, "16")
416 monkeypatch.setenv(object_server.WEBDAV_MAX_FILE_BYTES_ENV, "64")
417 token = key_for(data_dir)
418 status, _, _ = dav("/dav/files/a.bin", token, method="PUT", body=b"x" * 40)
419 assert status == 201
420 status, _, _ = dav("/dav/files/b.bin", token, method="PUT", body=b"x" * 65)
421 assert status == 413
422 # everything else keeps the general cap
423 body, content_type = multipart_body("file", "c.txt", b"x" * 40)
424 status, _, _ = request(
425 "/api/files", method="POST", body=body,
426 headers=[("authorization", f"Bearer {token}"), ("content-type", content_type)],
427 )
428 assert status == 413
429
430
431def test_the_files_folder_listing_is_valid_xml(tmp_path, monkeypatch):
432 import xml.etree.ElementTree as ET
433
434 data_dir = dav_env(tmp_path, monkeypatch)
435 token = key_for(data_dir)
436 dav("/dav/files/a <&> b.txt", token, method="PUT", body=b"odd name")
437 root = ET.fromstring(listing(token))
438 hrefs = [e.text for e in root.iter("{DAV:}href")]
439 assert "/dav/files/a%20%3C%26%3E%20b.txt" in hrefs
440 assert json.dumps(names_in(listing(token))) # escaped, still readable
441
442
443def test_someone_elses_public_file_is_not_in_your_folder(tmp_path, monkeypatch):
444 """The policy lets everyone read a public file, but it is still the
445 owner's: in your folder it would be yours to rename or overwrite."""
446 data_dir = dav_env(tmp_path, monkeypatch)
447 dan, eve = key_for(data_dir, "dan"), key_for(data_dir, "eve")
448 body, content_type = multipart_body("file", "flyer.pdf", b"%PDF", extra=[("is_public", "true")])
449 eve_session, _ = create_identity_session({"user_id": "eve"})
450 status, _, uploaded = request(
451 "/api/files", method="POST", body=body,
452 headers=[("authorization", f"Bearer {eve_session}"), ("content-type", content_type)],
453 )
454 assert status == 201 and uploaded["file"]["is_public"] == "true"
455 status, _, _ = raw_request(f"/api/files/{uploaded['file']['id']}", headers=[("authorization", f"Bearer {dan}")])
456 assert status == 200 # dan may read it...
457 assert names_in(listing(dan)) == ["files"] # ...but it is not in his folder
458 assert "flyer.pdf" in names_in(listing(eve))
459
460
461def test_the_folder_reports_the_files_quota_as_its_free_space(tmp_path, monkeypatch):
462 """Without it a desktop shows its own disk's free space (davfs2 said
463 508 GiB on a desk with a 100MB quota), and a copy fails part way."""
464 data_dir = dav_env(tmp_path, monkeypatch)
465 monkeypatch.setenv(object_server.USER_FILES_QUOTA_ENV, "1000")
466 token = key_for(data_dir)
467 dav("/dav/files/a.bin", token, method="PUT", body=b"x" * 300)
468 body = (
469 b'<?xml version="1.0"?><D:propfind xmlns:D="DAV:"><D:prop>'
470 b"<D:quota-available-bytes/><D:quota-used-bytes/></D:prop></D:propfind>"
471 )
472 status, _, xml = dav("/dav/files/", token, method="PROPFIND", body=body, headers=[("depth", "0")])
473 assert status == 207
474 assert b"<D:quota-used-bytes>300</D:quota-used-bytes>" in xml
475 assert b"<D:quota-available-bytes>700</D:quota-available-bytes>" in xml
476 assert b"quota" not in listing(token, depth="0").encode() # not in allprop