summaryrefslogtreecommitdiffhomepage
path: root/test/test_static_symlink.py
blob: de65b85be19fe3e6649af00d49869d59d8056057 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
from pathlib import Path

import pytest
from unit.applications.proto import TestApplicationProto

prerequisites = {'features': {'chroot': True}}


class TestStaticSymlink(TestApplicationProto):
    @pytest.fixture(autouse=True)
    def setup_method_fixture(self, temp_dir):
        os.makedirs(f'{temp_dir}/assets/dir/dir')
        Path(f'{temp_dir}/assets/index.html').write_text('0123456789')
        Path(f'{temp_dir}/assets/dir/file').write_text('blah')

        self._load_conf(
            {
                "listeners": {"*:7080": {"pass": "routes"}},
                "routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}],
            }
        )

    def test_static_symlink(self, temp_dir, skip_alert):
        skip_alert(r'opening.*failed')

        os.symlink(f'{temp_dir}/assets/dir', f'{temp_dir}/assets/link')

        assert self.get(url='/dir')['status'] == 301, 'dir'
        assert self.get(url='/dir/file')['status'] == 200, 'file'
        assert self.get(url='/link')['status'] == 301, 'symlink dir'
        assert self.get(url='/link/file')['status'] == 200, 'symlink file'

        assert 'success' in self.conf(
            {"share": f'{temp_dir}/assets$uri', "follow_symlinks": False},
            'routes/0/action',
        ), 'configure symlink disable'

        assert self.get(url='/link/file')['status'] == 403, 'symlink disabled'

        assert 'success' in self.conf(
            {"share": f'{temp_dir}/assets$uri', "follow_symlinks": True},
            'routes/0/action',
        ), 'configure symlink enable'

        assert self.get(url='/link/file')['status'] == 200, 'symlink enabled'

    def test_static_symlink_two_blocks(self, temp_dir, skip_alert):
        skip_alert(r'opening.*failed')

        os.symlink(f'{temp_dir}/assets/dir', f'{temp_dir}/assets/link')

        assert 'success' in self.conf(
            [
                {
                    "match": {"method": "HEAD"},
                    "action": {
                        "share": f'{temp_dir}/assets$uri',
                        "follow_symlinks": False,
                    },
                },
                {
                    "match": {"method": "GET"},
                    "action": {
                        "share": f'{temp_dir}/assets$uri',
                        "follow_symlinks": True,
                    },
                },
            ],
            'routes',
        ), 'configure two options'

        assert self.get(url='/link/file')['status'] == 200, 'block enabled'
        assert self.head(url='/link/file')['status'] == 403, 'block disabled'

    def test_static_symlink_chroot(self, temp_dir, skip_alert):
        skip_alert(r'opening.*failed')

        os.symlink(
            f'{temp_dir}/assets/dir/file', f'{temp_dir}/assets/dir/dir/link'
        )

        assert self.get(url='/dir/dir/link')['status'] == 200, 'default chroot'

        assert 'success' in self.conf(
            {
                "share": f'{temp_dir}/assets$uri',
                "chroot": f'{temp_dir}/assets/dir/dir',
            },
            'routes/0/action',
        ), 'configure chroot'

        assert self.get(url='/dir/dir/link')['status'] == 404, 'chroot'