xref: /unit/test/test_static_chroot.py (revision 2144:b14caaedca5e)
1import os
2from pathlib import Path
3
4import pytest
5from unit.applications.proto import TestApplicationProto
6
7
8class TestStaticChroot(TestApplicationProto):
9    prerequisites = {'features': ['chroot']}
10
11    @pytest.fixture(autouse=True)
12    def setup_method_fixture(self, temp_dir):
13        os.makedirs(temp_dir + '/assets/dir')
14        Path(temp_dir + '/assets/index.html').write_text('0123456789')
15        Path(temp_dir + '/assets/dir/file').write_text('blah')
16
17        self.test_path = '/' + os.path.relpath(Path(__file__))
18
19        self._load_conf(
20            {
21                "listeners": {"*:7080": {"pass": "routes"}},
22                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
23            }
24        )
25
26    def update_action(self, share, chroot):
27        return self.conf(
28            {"share": share, "chroot": chroot},
29            'routes/0/action',
30        )
31
32    def get_custom(self, uri, host):
33        return self.get(url=uri, headers={'Host': host, 'Connection': 'close'})[
34            'status'
35        ]
36
37    def test_static_chroot(self, temp_dir):
38        assert self.get(url='/dir/file')['status'] == 200, 'default chroot'
39        assert self.get(url='/index.html')['status'] == 200, 'default chroot 2'
40
41        assert 'success' in self.update_action(
42            temp_dir + "/assets$uri", temp_dir + "/assets/dir"
43        )
44
45        assert self.get(url='/dir/file')['status'] == 200, 'chroot'
46        assert self.get(url='/index.html')['status'] == 403, 'chroot 403 2'
47        assert self.get(url='/file')['status'] == 403, 'chroot 403'
48
49    def test_share_chroot_array(self, temp_dir):
50        assert 'success' in self.update_action(
51            ["/blah", temp_dir + "/assets$uri"], temp_dir + "/assets/dir"
52        )
53        assert self.get(url='/dir/file')['status'] == 200, 'share array'
54
55        assert 'success' in self.update_action(
56            ["/blah", temp_dir + '/assets$uri'], temp_dir + '/assets/$host'
57        )
58        assert self.get_custom('/dir/file', 'dir') == 200, 'array variable'
59
60        assert 'success' in self.update_action(
61            ["/blah", "/blah2"], temp_dir + "/assets/dir"
62        )
63        assert self.get()['status'] != 200, 'share array bad'
64
65    def test_static_chroot_permission(self, is_su, temp_dir):
66        if is_su:
67            pytest.skip('does\'t work under root')
68
69        os.chmod(temp_dir + '/assets/dir', 0o100)
70
71        assert 'success' in self.update_action(
72            temp_dir + "/assets$uri", temp_dir + "/assets/dir"
73        ), 'configure chroot'
74
75        assert self.get(url='/dir/file')['status'] == 200, 'chroot'
76
77    def test_static_chroot_empty(self, temp_dir):
78        assert 'success' in self.update_action(temp_dir + "/assets$uri", "")
79        assert self.get(url='/dir/file')['status'] == 200, 'empty absolute'
80
81        assert 'success' in self.update_action(".$uri", "")
82        assert self.get(url=self.test_path)['status'] == 200, 'empty relative'
83
84    def test_static_chroot_relative(self, is_su, temp_dir):
85        if is_su:
86            pytest.skip('does\'t work under root')
87
88        assert 'success' in self.update_action(temp_dir + "/assets$uri", ".")
89        assert self.get(url='/dir/file')['status'] == 403, 'relative chroot'
90
91        assert 'success' in self.conf({"share": ".$uri"}, 'routes/0/action')
92        assert self.get(url=self.test_path)['status'] == 200, 'relative share'
93
94        assert 'success' in self.update_action(".$uri", ".")
95        assert self.get(url=self.test_path)['status'] == 200, 'relative'
96
97    def test_static_chroot_variables(self, temp_dir):
98        assert 'success' in self.update_action(
99            temp_dir + '/assets$uri', temp_dir + '/assets/$host'
100        )
101        assert self.get_custom('/dir/file', 'dir') == 200
102
103        assert 'success' in self.update_action(
104            temp_dir + '/assets$uri', temp_dir + '/assets/${host}'
105        )
106        assert self.get_custom('/dir/file', 'dir') == 200
107
108    def test_static_chroot_variables_buildin_start(self, temp_dir):
109        assert 'success' in self.update_action(
110            temp_dir + '/assets/dir/$host', '$uri/assets/dir'
111        )
112        assert self.get_custom(temp_dir, 'file') == 200
113
114    def test_static_chroot_variables_buildin_mid(self, temp_dir):
115        assert 'success' in self.update_action(
116            temp_dir + '/assets$uri', temp_dir + '/$host/dir'
117        )
118        assert self.get_custom('/dir/file', 'assets') == 200
119
120    def test_static_chroot_variables_buildin_end(self, temp_dir):
121        assert 'success' in self.update_action(
122            temp_dir + '/assets$uri', temp_dir + '/assets/$host'
123        )
124        assert self.get_custom('/dir/file', 'dir') == 200
125
126    def test_static_chroot_slash(self, temp_dir):
127        assert 'success' in self.update_action(
128            temp_dir + "/assets$uri", temp_dir + "/assets/dir/"
129        )
130        assert self.get(url='/dir/file')['status'] == 200, 'slash end'
131        assert self.get(url='/dirxfile')['status'] == 403, 'slash end bad'
132
133        assert 'success' in self.update_action(
134            temp_dir + "/assets$uri", temp_dir + "/assets/dir"
135        )
136        assert self.get(url='/dir/file')['status'] == 200, 'no slash end'
137
138        assert 'success' in self.update_action(
139            temp_dir + "/assets$uri", temp_dir + "/assets/dir/"
140        )
141        assert self.get(url='/dir/file')['status'] == 200, 'slash end 2'
142        assert self.get(url='/dirxfile')['status'] == 403, 'slash end 2 bad'
143
144        assert 'success' in self.update_action(
145            temp_dir + "///assets/////$uri", temp_dir + "//assets////dir///"
146        )
147        assert self.get(url='/dir/file')['status'] == 200, 'multiple slashes'
148
149    def test_static_chroot_invalid(self, temp_dir):
150        assert 'error' in self.conf(
151            {"share": temp_dir, "chroot": True},
152            'routes/0/action',
153        ), 'configure chroot error'
154        assert 'error' in self.conf(
155            {"share": temp_dir, "symlinks": "True"},
156            'routes/0/action',
157        ), 'configure symlink error'
158        assert 'error' in self.conf(
159            {"share": temp_dir, "mount": "True"},
160            'routes/0/action',
161        ), 'configure mount error'
162
163        assert 'error' in self.update_action(
164            temp_dir + '/assets$uri', temp_dir + '/assets/d$r$uri'
165        )
166        assert 'error' in self.update_action(
167            temp_dir + '/assets$uri', temp_dir + '/assets/$$uri'
168        )
169