xref: /unit/test/test_static_chroot.py (revision 1966:ed8aeb33140b)
1import os
2from pathlib import Path
3
4import pytest
5
6from unit.applications.proto import TestApplicationProto
7
8
9class TestStaticChroot(TestApplicationProto):
10    prerequisites = {'features': ['chroot']}
11
12    @pytest.fixture(autouse=True)
13    def setup_method_fixture(self, temp_dir):
14        os.makedirs(temp_dir + '/assets/dir')
15        Path(temp_dir + '/assets/index.html').write_text('0123456789')
16        Path(temp_dir + '/assets/dir/file').write_text('blah')
17
18        test = Path(__file__)
19        self.test_path = '/' + test.parent.name + '/' + test.name
20
21        self._load_conf(
22            {
23                "listeners": {"*:7080": {"pass": "routes"}},
24                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
25            }
26        )
27
28    def update_action(self, share, chroot):
29        return self.conf(
30            {"share": share, "chroot": chroot}, 'routes/0/action',
31        )
32
33    def get_custom(self, uri, host):
34        return self.get(
35            url=uri, headers={'Host': host, 'Connection': 'close'}
36        )['status']
37
38    def test_static_chroot(self, temp_dir):
39        assert self.get(url='/dir/file')['status'] == 200, 'default chroot'
40        assert self.get(url='/index.html')['status'] == 200, 'default chroot 2'
41
42        assert 'success' in self.conf(
43            {
44                "share": temp_dir + "/assets$uri",
45                "chroot": temp_dir + "/assets/dir",
46            },
47            'routes/0/action',
48        ), 'configure chroot'
49
50        assert self.get(url='/dir/file')['status'] == 200, 'chroot'
51        assert self.get(url='/index.html')['status'] == 403, 'chroot 403 2'
52        assert self.get(url='/file')['status'] == 403, 'chroot 403'
53
54    def test_share_chroot_array(self, temp_dir):
55        assert 'success' in self.conf(
56            {
57                "share": ["/blah", temp_dir + "/assets$uri"],
58                "chroot": temp_dir + "/assets/dir",
59            },
60            'routes/0/action',
61        ), 'configure share array'
62        assert self.get(url='/dir/file')['status'] == 200, 'share array'
63
64        assert 'success' in self.update_action(
65            ["/blah", temp_dir + '/assets$uri'], temp_dir + '/assets/$host'
66        )
67        assert self.get_custom('/dir/file', 'dir') == 200, 'array variable'
68
69        assert 'success' in self.conf(
70            {
71                "share": ["/blah", "/blah2"],
72                "chroot": temp_dir + "/assets/dir",
73            },
74            'routes/0/action',
75        ), 'configure share array bad'
76        assert self.get()['status'] != 200, 'share array bad'
77
78    def test_static_chroot_permission(self, is_su, temp_dir):
79        if is_su:
80            pytest.skip('does\'t work under root')
81
82        os.chmod(temp_dir + '/assets/dir', 0o100)
83
84        assert 'success' in self.conf(
85            {
86                "share": temp_dir + "/assets$uri",
87                "chroot": temp_dir + "/assets/dir",
88            },
89            'routes/0/action',
90        ), 'configure chroot'
91
92        assert self.get(url='/dir/file')['status'] == 200, 'chroot'
93
94    def test_static_chroot_empty(self, temp_dir):
95        assert 'success' in self.conf(
96            {"share": temp_dir + "/assets$uri", "chroot": ""},
97            'routes/0/action',
98        ), 'configure chroot empty absolute'
99
100        assert (
101            self.get(url='/dir/file')['status'] == 200
102        ), 'chroot empty absolute'
103
104        assert 'success' in self.conf(
105            {"share": ".$uri", "chroot": ""}, 'routes/0/action',
106        ), 'configure chroot empty relative'
107
108        assert (
109            self.get(url=self.test_path)['status'] == 200
110        ), 'chroot empty relative'
111
112    def test_static_chroot_relative(self, is_su, temp_dir):
113        if is_su:
114            pytest.skip('does\'t work under root')
115
116        assert 'success' in self.conf(
117            {"share": temp_dir + "/assets$uri", "chroot": "."},
118            'routes/0/action',
119        ), 'configure relative chroot'
120
121        assert self.get(url='/dir/file')['status'] == 403, 'relative chroot'
122
123        assert 'success' in self.conf(
124            {"share": ".$uri"}, 'routes/0/action',
125        ), 'configure relative share'
126
127        assert self.get(url=self.test_path)['status'] == 200, 'relative share'
128
129        assert 'success' in self.conf(
130            {"share": ".$uri", "chroot": "."}, 'routes/0/action',
131        ), 'configure relative'
132
133        assert self.get(url=self.test_path)['status'] == 200, 'relative'
134
135    def test_static_chroot_varibales(self, temp_dir):
136        assert 'success' in self.update_action(
137            temp_dir + '/assets$uri', temp_dir + '/assets/$host'
138        )
139        assert self.get_custom('/dir/file', 'dir') == 200
140
141        assert 'success' in self.update_action(
142            temp_dir + '/assets$uri', temp_dir + '/assets/${host}'
143        )
144        assert self.get_custom('/dir/file', 'dir') == 200
145
146    def test_static_chroot_varibales_buildin_start(self, temp_dir):
147        assert 'success' in self.update_action(
148            temp_dir + '/assets/dir/$host', '$uri/assets/dir'
149        )
150
151        assert self.get_custom(temp_dir, 'file') == 200
152
153    def test_static_chroot_varibales_buildin_mid(self, temp_dir):
154        assert 'success' in self.update_action(
155            temp_dir + '/assets$uri', temp_dir + '/$host/dir'
156        )
157
158        assert self.get_custom('/dir/file', 'assets') == 200
159
160    def test_static_chroot_varibales_buildin_end(self, temp_dir):
161        assert 'success' in self.update_action(
162            temp_dir + '/assets$uri', temp_dir + '/assets/$host'
163        )
164
165        assert self.get_custom('/dir/file', 'dir') == 200
166
167    def test_static_chroot_slash(self, temp_dir):
168        assert 'success' in self.conf(
169            {
170                "share": temp_dir + "/assets$uri",
171                "chroot": temp_dir + "/assets/dir/",
172            },
173            'routes/0/action',
174        ), 'configure chroot slash end'
175
176        assert self.get(url='/dir/file')['status'] == 200, 'slash end'
177        assert self.get(url='/dirxfile')['status'] == 403, 'slash end bad'
178
179        assert 'success' in self.conf(
180            {
181                "share": temp_dir + "/assets$uri",
182                "chroot": temp_dir + "/assets/dir",
183            },
184            'routes/0/action',
185        ), 'configure chroot no slash end'
186
187        assert self.get(url='/dir/file')['status'] == 200, 'no slash end'
188
189        assert 'success' in self.conf(
190            {
191                "share": temp_dir + "/assets$uri",
192                "chroot": temp_dir + "/assets/dir/",
193            },
194            'routes/0/action',
195        ), 'configure chroot slash end 2'
196
197        assert self.get(url='/dir/file')['status'] == 200, 'slash end 2'
198        assert self.get(url='/dirxfile')['status'] == 403, 'slash end 2 bad'
199
200        assert 'success' in self.conf(
201            {
202                "share": temp_dir + "///assets/////$uri",
203                "chroot": temp_dir + "//assets////dir///",
204            },
205            'routes/0/action',
206        ), 'configure chroot multiple slashes'
207
208        assert self.get(url='/dir/file')['status'] == 200, 'multiple slashes'
209
210    def test_static_chroot_invalid(self, temp_dir):
211        assert 'error' in self.conf(
212            {"share": temp_dir, "chroot": True}, 'routes/0/action',
213        ), 'configure chroot error'
214        assert 'error' in self.conf(
215            {"share": temp_dir, "symlinks": "True"}, 'routes/0/action',
216        ), 'configure symlink error'
217        assert 'error' in self.conf(
218            {"share": temp_dir, "mount": "True"}, 'routes/0/action',
219        ), 'configure mount error'
220
221        assert 'error' in self.update_action(
222            temp_dir + '/assets$uri', temp_dir + '/assets/d$r$uri'
223        )
224        assert 'error' in self.update_action(
225            temp_dir + '/assets$uri', temp_dir + '/assets/$$uri'
226        )
227