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_object_files_helper.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, 19 minutes ago History
test_object_files_helper.py · 4.7 KB · 133 lines
1"""``_files``: per-object file storage, identical in every execution mode."""
2import pytest
3
4import object_files
5from object_runtime_helpers import ObjectFileStore
6from runtime_modes import MODES, configure, run_object, write_object
7
8CRUD = """
9def GET(request):
10 out = {}
11 _files.put("a.txt", b"hello")
12 _files.put("a.txt", b"hello2") # overwrite is allowed
13 out["get"] = _files.get("a.txt").decode()
14 out["list"] = [[f["name"], f["size"]] for f in _files.list()]
15 out["keys"] = sorted(_files.list()[0])
16 out["dir"] = str(_files.files_dir)
17 _files.delete("a.txt")
18 _files.delete("a.txt") # deleting a missing file is silent
19 out["after"] = _files.list()
20 try:
21 _files.get("a.txt")
22 except FileNotFoundError:
23 out["missing"] = "FileNotFoundError"
24 return out
25"""
26
27
28@pytest.mark.parametrize("mode", MODES)
29def test_crud_shapes_in_every_mode(mode, monkeypatch, tmp_path):
30 data_dir, root = configure(monkeypatch, tmp_path)
31 write_object(root, "basics_filer", CRUD)
32 result = run_object(mode, root, "basics_filer")
33 assert result.ok, result.error
34 out = result.result
35 assert out["get"] == "hello2"
36 assert out["list"] == [["a.txt", 6]]
37 assert out["keys"] == ["modified", "name", "size"]
38 assert out["dir"] == str(data_dir / "files" / "basics_filer")
39 assert out["after"] == []
40 assert out["missing"] == "FileNotFoundError"
41
42
43BAD_NAMES = ["../x", "a/../../x", "/etc/passwd", "a\x00b", "", "..", "sub/../../x"]
44
45TRAVERSAL = """
46BAD = %r
47def GET(request):
48 out = {}
49 for name in BAD:
50 for op in ("put", "get", "delete"):
51 try:
52 if op == "put":
53 _files.put(name, b"x")
54 else:
55 getattr(_files, op)(name)
56 out[op + repr(name)] = "allowed"
57 except ValueError:
58 pass
59 except FileNotFoundError:
60 out[op + repr(name)] = "notfound"
61 return out
62""" % (BAD_NAMES,)
63
64
65@pytest.mark.parametrize("mode", MODES)
66def test_traversal_names_refused_in_every_mode(mode, monkeypatch, tmp_path):
67 data_dir, root = configure(monkeypatch, tmp_path)
68 write_object(root, "basics_evil", TRAVERSAL)
69 result = run_object(mode, root, "basics_evil")
70 assert result.ok, result.error
71 assert result.result == {}, result.result
72 assert not (tmp_path / "x").exists() and not (data_dir / "x").exists()
73
74
75def test_symlink_escape_refused(tmp_path):
76 store = ObjectFileStore("basics_sym", tmp_path)
77 outside = tmp_path / "outside"
78 outside.mkdir()
79 (outside / "secret.txt").write_bytes(b"secret")
80 (store.files_dir / "link").symlink_to(outside, target_is_directory=True)
81 (store.files_dir / "flink").symlink_to(outside / "secret.txt")
82 with pytest.raises(ValueError):
83 store.get("link/secret.txt")
84 with pytest.raises(ValueError):
85 store.put("link/new.txt", b"x")
86 with pytest.raises(ValueError):
87 store.get("flink")
88 with pytest.raises(ValueError):
89 store.put("flink", b"x")
90 assert (outside / "secret.txt").read_bytes() == b"secret"
91 assert not (outside / "new.txt").exists()
92 assert all(f["name"] not in ("flink", "link/secret.txt") for f in store.list())
93
94
95def test_size_limit(monkeypatch, tmp_path):
96 store = ObjectFileStore("basics_big", tmp_path)
97 monkeypatch.setenv("DBBASIC_MAX_OBJECT_FILE_BYTES", "10")
98 store.put("ok.bin", b"x" * 10)
99 with pytest.raises(ValueError):
100 store.put("big.bin", b"x" * 11)
101 assert store.exists("ok.bin") and not store.exists("big.bin")
102 with pytest.raises(TypeError):
103 store.put("s.txt", "text")
104
105
106def test_files_are_the_http_api_files(tmp_path):
107 ObjectFileStore("basics_shared", tmp_path).put("n.txt", b"data")
108 data, _ = object_files.read_object_file("basics_shared", "n.txt", tmp_path)
109 assert data == b"data"
110
111
112@pytest.mark.parametrize("mode", MODES)
113def test_objects_cannot_see_each_others_files(mode, monkeypatch, tmp_path):
114 data_dir, root = configure(monkeypatch, tmp_path)
115 write_object(root, "basics_one", """
116def GET(request):
117 _files.put("mine.txt", b"one-secret")
118 return {"list": [f["name"] for f in _files.list()]}
119""")
120 write_object(root, "basics_two", """
121def GET(request):
122 out = {"list": [f["name"] for f in _files.list()]}
123 for probe in ("mine.txt", "../basics_one/mine.txt", "../../files/basics_one/mine.txt"):
124 try:
125 out[probe] = _files.get(probe).decode()
126 except (FileNotFoundError, ValueError):
127 out[probe] = None
128 return out
129""")
130 assert run_object(mode, root, "basics_one").result == {"list": ["mine.txt"]}
131 out = run_object(mode, root, "basics_two").result
132 assert out["list"] == []
133 assert not any(v for k, v in out.items() if k != "list")