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

0 downloads

dbbasic-object-server/tests/test_webdav.py
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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_webdav.py · 18.7 KB · 437 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_put_to_an_existing_name_replaces_the_bytes_and_keeps_the_record(tmp_path, monkeypatch):
192 data_dir = dav_env(tmp_path, monkeypatch)
193 token = key_for(data_dir)
194 dav("/dav/files/a.txt", token, method="PUT", body=b"one")
195 [before] = records(data_dir)
196 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"second version")
197 assert status == 204
198 [after] = records(data_dir)
199 assert after["id"] == before["id"]
200 assert after["size"] == "14"
201 status, _, body = dav("/dav/files/a.txt", token)
202 assert body == b"second version"
203
204
205def test_conditional_writes_refuse_to_clobber(tmp_path, monkeypatch):
206 """If-None-Match: * is "create only"; a stale If-Match is someone else's
207 newer copy. Without these a client can silently overwrite."""
208 data_dir = dav_env(tmp_path, monkeypatch)
209 token = key_for(data_dir)
210 dav("/dav/files/a.txt", token, method="PUT", body=b"one")
211 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"x", headers=[("if-none-match", "*")])
212 assert status == 412
213 status, headers, _ = dav("/dav/files/a.txt", token)
214 etag = headers[b"etag"].decode()
215 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"two", headers=[("if-match", '"stale"')])
216 assert status == 412
217 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"two", headers=[("if-match", etag)])
218 assert status == 204
219
220
221def test_move_renames_and_will_not_replace_without_overwrite(tmp_path, monkeypatch):
222 data_dir = dav_env(tmp_path, monkeypatch)
223 token = key_for(data_dir)
224 dav("/dav/files/a.txt", token, method="PUT", body=b"aaa")
225 dav("/dav/files/b.md", token, method="PUT", body=b"bbb")
226
227 status, _, _ = dav(
228 "/dav/files/a.txt", token, method="MOVE",
229 headers=[("destination", "http://desk/dav/files/b.md"), ("overwrite", "F")],
230 )
231 assert status == 412
232
233 status, _, _ = dav(
234 "/dav/files/a.txt", token, method="MOVE", headers=[("destination", "/dav/files/Renamed%20a.md")]
235 )
236 assert status == 201
237 names = sorted(r["filename"] for r in records(data_dir))
238 assert names == ["Renamed a.md", "b.md"]
239 renamed = next(r for r in records(data_dir) if r["filename"] == "Renamed a.md")
240 assert renamed["content_type"] == "text/markdown"
241
242 status, _, _ = dav(
243 "/dav/files/Renamed a.md", token, method="MOVE", headers=[("destination", "/dav/files/b.md")]
244 )
245 assert status == 204
246 [left] = records(data_dir)
247 assert left["filename"] == "b.md" and left["id"] == renamed["id"]
248 status, _, body = dav("/dav/files/b.md", token)
249 assert body == b"aaa"
250
251
252def test_a_rename_that_only_changes_case_is_a_rename(tmp_path, monkeypatch):
253 data_dir = dav_env(tmp_path, monkeypatch)
254 token = key_for(data_dir)
255 dav("/dav/files/readme.txt", token, method="PUT", body=b"r")
256 status, _, _ = dav("/dav/files/readme.txt", token, method="MOVE", headers=[("destination", "/dav/files/README.txt")])
257 assert status == 201
258 assert [r["filename"] for r in records(data_dir)] == ["README.txt"]
259
260
261def test_copy_makes_a_second_file(tmp_path, monkeypatch):
262 data_dir = dav_env(tmp_path, monkeypatch)
263 token = key_for(data_dir)
264 dav("/dav/files/a.txt", token, method="PUT", body=b"same")
265 status, _, _ = dav("/dav/files/a.txt", token, method="COPY", headers=[("destination", "/dav/files/c.txt")])
266 assert status == 201
267 assert sorted(r["filename"] for r in records(data_dir)) == ["a.txt", "c.txt"]
268 status, _, body = dav("/dav/files/c.txt", token)
269 assert body == b"same"
270
271
272def test_delete_removes_the_record_and_the_bytes(tmp_path, monkeypatch):
273 data_dir = dav_env(tmp_path, monkeypatch)
274 token = key_for(data_dir)
275 dav("/dav/files/a.txt", token, method="PUT", body=b"gone soon")
276 [record] = records(data_dir)
277 status, _, _ = dav("/dav/files/a.txt", token, method="DELETE")
278 assert status == 204
279 assert records(data_dir) == []
280 assert not object_user_files.file_path("dan", record["id"], base_dir=data_dir).exists()
281
282
283def test_a_range_is_served_as_206(tmp_path, monkeypatch):
284 data_dir = dav_env(tmp_path, monkeypatch)
285 token = key_for(data_dir)
286 dav("/dav/files/v.bin", token, method="PUT", body=b"0123456789")
287 status, headers, body = dav("/dav/files/v.bin", token, headers=[("range", "bytes=2-5")])
288 assert status == 206
289 assert body == b"2345"
290 assert headers[b"content-range"] == b"bytes 2-5/10"
291 status, headers, _ = dav("/dav/files/v.bin", token, headers=[("range", "bytes=50-")])
292 assert status == 416
293
294
295def test_an_uploaded_page_is_served_sandboxed_as_an_attachment(tmp_path, monkeypatch):
296 """Served from the server's own origin: a stored HTML file must not run
297 as a page there, or uploading it would be stored XSS."""
298 data_dir = dav_env(tmp_path, monkeypatch)
299 token = key_for(data_dir)
300 dav("/dav/files/x.html", token, method="PUT", body=b"<script>alert(1)</script>")
301 status, headers, _ = dav("/dav/files/x.html", token)
302 assert status == 200
303 assert headers[b"content-security-policy"] == b"sandbox"
304 assert headers[b"content-disposition"] == b"attachment"
305 assert headers[b"x-content-type-options"] == b"nosniff"
306
307
308def test_propfind_answers_what_was_asked_and_names_what_is_missing(tmp_path, monkeypatch):
309 data_dir = dav_env(tmp_path, monkeypatch)
310 token = key_for(data_dir)
311 dav("/dav/files/a.txt", token, method="PUT", body=b"abc")
312 body = (
313 b'<?xml version="1.0"?><D:propfind xmlns:D="DAV:" xmlns:Z="urn:x">'
314 b"<D:prop><D:getcontentlength/><Z:quota/></D:prop></D:propfind>"
315 )
316 status, _, xml = dav("/dav/files/a.txt", token, method="PROPFIND", body=body, headers=[("depth", "0")])
317 assert status == 207
318 xml = xml.decode()
319 assert "<D:getcontentlength>3</D:getcontentlength>" in xml
320 assert "404 Not Found" in xml and "quota" in xml
321 assert "getlastmodified" not in xml
322
323 root = listing(token, "/dav/", depth="infinity")
324 assert names_in(root) == ["dav", "files", "a.txt"]
325
326
327def test_a_propfind_with_a_doctype_is_refused(tmp_path, monkeypatch):
328 data_dir = dav_env(tmp_path, monkeypatch)
329 body = b'<?xml version="1.0"?><!DOCTYPE x [<!ENTITY a "aaaa">]><D:propfind xmlns:D="DAV:"/>'
330 status, _, _ = dav("/dav/files/", key_for(data_dir), method="PROPFIND", body=body)
331 assert status == 400
332
333
334def test_folders_and_locks_are_refused_not_faked(tmp_path, monkeypatch):
335 data_dir = dav_env(tmp_path, monkeypatch)
336 token = key_for(data_dir)
337 for method, path in (("MKCOL", "/dav/files/new/"), ("LOCK", "/dav/files/a.txt"), ("UNLOCK", "/dav/files/a.txt")):
338 status, headers, _ = dav(path, token, method=method)
339 assert status == 405, method
340 assert b"PROPFIND" in headers[b"allow"]
341
342
343def test_proppatch_refuses_each_property_with_403(tmp_path, monkeypatch):
344 data_dir = dav_env(tmp_path, monkeypatch)
345 token = key_for(data_dir)
346 dav("/dav/files/a.txt", token, method="PUT", body=b"a")
347 body = (
348 b'<?xml version="1.0"?><D:propertyupdate xmlns:D="DAV:" xmlns:W="urn:schemas-microsoft-com:">'
349 b"<D:set><D:prop><W:Win32LastModifiedTime>x</W:Win32LastModifiedTime></D:prop></D:set>"
350 b"</D:propertyupdate>"
351 )
352 status, _, xml = dav("/dav/files/a.txt", token, method="PROPPATCH", body=body)
353 assert status == 207
354 assert b"403 Forbidden" in xml and b"Win32LastModifiedTime" in xml
355
356
357def test_the_quota_holds_for_new_files_and_for_replacements(tmp_path, monkeypatch):
358 data_dir = dav_env(tmp_path, monkeypatch)
359 monkeypatch.setenv(object_server.USER_FILES_QUOTA_ENV, "10")
360 token = key_for(data_dir)
361 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"12345678")
362 assert status == 201
363 status, _, _ = dav("/dav/files/b.txt", token, method="PUT", body=b"123")
364 assert status == 413
365 # replacing a file frees its old bytes first: 8 -> 10 fits, 8 -> 11 does not
366 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"1234567890")
367 assert status == 204
368 status, _, _ = dav("/dav/files/a.txt", token, method="PUT", body=b"12345678901")
369 assert status == 507
370
371
372def test_a_put_may_exceed_the_general_request_cap_up_to_its_own(tmp_path, monkeypatch):
373 """A file is bigger than an API call; the PUT cap is its own and still a
374 cap, because the body is read into memory whole."""
375 data_dir = dav_env(tmp_path, monkeypatch)
376 monkeypatch.setenv(object_server.MAX_REQUEST_BYTES_ENV, "16")
377 monkeypatch.setenv(object_server.WEBDAV_MAX_FILE_BYTES_ENV, "64")
378 token = key_for(data_dir)
379 status, _, _ = dav("/dav/files/a.bin", token, method="PUT", body=b"x" * 40)
380 assert status == 201
381 status, _, _ = dav("/dav/files/b.bin", token, method="PUT", body=b"x" * 65)
382 assert status == 413
383 # everything else keeps the general cap
384 body, content_type = multipart_body("file", "c.txt", b"x" * 40)
385 status, _, _ = request(
386 "/api/files", method="POST", body=body,
387 headers=[("authorization", f"Bearer {token}"), ("content-type", content_type)],
388 )
389 assert status == 413
390
391
392def test_the_files_folder_listing_is_valid_xml(tmp_path, monkeypatch):
393 import xml.etree.ElementTree as ET
394
395 data_dir = dav_env(tmp_path, monkeypatch)
396 token = key_for(data_dir)
397 dav("/dav/files/a <&> b.txt", token, method="PUT", body=b"odd name")
398 root = ET.fromstring(listing(token))
399 hrefs = [e.text for e in root.iter("{DAV:}href")]
400 assert "/dav/files/a%20%3C%26%3E%20b.txt" in hrefs
401 assert json.dumps(names_in(listing(token))) # escaped, still readable
402
403
404def test_someone_elses_public_file_is_not_in_your_folder(tmp_path, monkeypatch):
405 """The policy lets everyone read a public file, but it is still the
406 owner's: in your folder it would be yours to rename or overwrite."""
407 data_dir = dav_env(tmp_path, monkeypatch)
408 dan, eve = key_for(data_dir, "dan"), key_for(data_dir, "eve")
409 body, content_type = multipart_body("file", "flyer.pdf", b"%PDF", extra=[("is_public", "true")])
410 eve_session, _ = create_identity_session({"user_id": "eve"})
411 status, _, uploaded = request(
412 "/api/files", method="POST", body=body,
413 headers=[("authorization", f"Bearer {eve_session}"), ("content-type", content_type)],
414 )
415 assert status == 201 and uploaded["file"]["is_public"] == "true"
416 status, _, _ = raw_request(f"/api/files/{uploaded['file']['id']}", headers=[("authorization", f"Bearer {dan}")])
417 assert status == 200 # dan may read it...
418 assert names_in(listing(dan)) == ["files"] # ...but it is not in his folder
419 assert "flyer.pdf" in names_in(listing(eve))
420
421
422def test_the_folder_reports_the_files_quota_as_its_free_space(tmp_path, monkeypatch):
423 """Without it a desktop shows its own disk's free space (davfs2 said
424 508 GiB on a desk with a 100MB quota), and a copy fails part way."""
425 data_dir = dav_env(tmp_path, monkeypatch)
426 monkeypatch.setenv(object_server.USER_FILES_QUOTA_ENV, "1000")
427 token = key_for(data_dir)
428 dav("/dav/files/a.bin", token, method="PUT", body=b"x" * 300)
429 body = (
430 b'<?xml version="1.0"?><D:propfind xmlns:D="DAV:"><D:prop>'
431 b"<D:quota-available-bytes/><D:quota-used-bytes/></D:prop></D:propfind>"
432 )
433 status, _, xml = dav("/dav/files/", token, method="PROPFIND", body=body, headers=[("depth", "0")])
434 assert status == 207
435 assert b"<D:quota-used-bytes>300</D:quota-used-bytes>" in xml
436 assert b"<D:quota-available-bytes>700</D:quota-available-bytes>" in xml
437 assert b"quota" not in listing(token, depth="0").encode() # not in allprop