xref: /unit/test/test_static_symlink.py (revision 1971:3410f9d2a662)
1import os
2from pathlib import Path
3
4import pytest
5from unit.applications.proto import TestApplicationProto
6
7
8class TestStaticSymlink(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/dir')
14        Path(temp_dir + '/assets/index.html').write_text('0123456789')
15        Path(temp_dir + '/assets/dir/file').write_text('blah')
16
17        self._load_conf(
18            {
19                "listeners": {"*:7080": {"pass": "routes"}},
20                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
21            }
22        )
23
24    def test_static_symlink(self, temp_dir, skip_alert):
25        skip_alert(r'opening.*failed')
26
27        os.symlink(temp_dir + '/assets/dir', temp_dir + '/assets/link')
28
29        assert self.get(url='/dir')['status'] == 301, 'dir'
30        assert self.get(url='/dir/file')['status'] == 200, 'file'
31        assert self.get(url='/link')['status'] == 301, 'symlink dir'
32        assert self.get(url='/link/file')['status'] == 200, 'symlink file'
33
34        assert 'success' in self.conf(
35            {"share": temp_dir + "/assets$uri", "follow_symlinks": False},
36            'routes/0/action',
37        ), 'configure symlink disable'
38
39        assert self.get(url='/link/file')['status'] == 403, 'symlink disabled'
40
41        assert 'success' in self.conf(
42            {"share": temp_dir + "/assets$uri", "follow_symlinks": True},
43            'routes/0/action',
44        ), 'configure symlink enable'
45
46        assert self.get(url='/link/file')['status'] == 200, 'symlink enabled'
47
48    def test_static_symlink_two_blocks(self, temp_dir, skip_alert):
49        skip_alert(r'opening.*failed')
50
51        os.symlink(temp_dir + '/assets/dir', temp_dir + '/assets/link')
52
53        assert 'success' in self.conf(
54            [
55                {
56                    "match": {"method": "HEAD"},
57                    "action": {
58                        "share": temp_dir + "/assets$uri",
59                        "follow_symlinks": False,
60                    },
61                },
62                {
63                    "match": {"method": "GET"},
64                    "action": {
65                        "share": temp_dir + "/assets$uri",
66                        "follow_symlinks": True,
67                    },
68                },
69            ],
70            'routes',
71        ), 'configure two options'
72
73        assert self.get(url='/link/file')['status'] == 200, 'block enabled'
74        assert self.head(url='/link/file')['status'] == 403, 'block disabled'
75
76    def test_static_symlink_chroot(self, temp_dir, skip_alert):
77        skip_alert(r'opening.*failed')
78
79        os.symlink(
80            temp_dir + '/assets/dir/file', temp_dir + '/assets/dir/dir/link'
81        )
82
83        assert self.get(url='/dir/dir/link')['status'] == 200, 'default chroot'
84
85        assert 'success' in self.conf(
86            {
87                "share": temp_dir + "/assets$uri",
88                "chroot": temp_dir + "/assets/dir/dir",
89            },
90            'routes/0/action',
91        ), 'configure chroot'
92
93        assert self.get(url='/dir/dir/link')['status'] == 404, 'chroot'
94