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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
from pathlib import Path
import pytest
from unit.applications.proto import ApplicationProto
client = ApplicationProto()
@pytest.fixture(autouse=True)
def setup_method_fixture(temp_dir):
Path(f'{temp_dir}/assets').mkdir()
for ext in ['.xml', '.mp4', '.php', '', '.txt', '.html', '.png']:
Path(f'{temp_dir}/assets/file{ext}').write_text(ext, encoding='utf-8')
Path(f'{temp_dir}/assets/index.html').write_text('index', encoding='utf-8')
assert 'success' in client.conf(
{
"listeners": {
"*:8080": {"pass": "routes"},
"*:8081": {"pass": "routes"},
},
"routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}],
"applications": {},
}
)
def action_update(conf):
assert 'success' in client.conf(conf, 'routes/0/action')
def check_body(http_url, body):
resp = client.get(url=http_url)
assert resp['status'] == 200, 'status'
assert resp['body'] == body, 'body'
def test_static_types_basic(temp_dir):
action_update({"share": f'{temp_dir}/assets$uri'})
check_body('/index.html', 'index')
check_body('/file.xml', '.xml')
action_update(
{"share": f'{temp_dir}/assets$uri', "types": "application/xml"}
)
check_body('/file.xml', '.xml')
action_update(
{"share": f'{temp_dir}/assets$uri', "types": ["application/xml"]}
)
check_body('/file.xml', '.xml')
action_update({"share": f'{temp_dir}/assets$uri', "types": [""]})
assert client.get(url='/file.xml')['status'] == 403, 'no mtype'
def test_static_types_wildcard(temp_dir):
action_update(
{"share": f'{temp_dir}/assets$uri', "types": ["application/*"]}
)
check_body('/file.xml', '.xml')
assert client.get(url='/file.mp4')['status'] == 403, 'app * mtype mp4'
action_update({"share": f'{temp_dir}/assets$uri', "types": ["video/*"]})
assert client.get(url='/file.xml')['status'] == 403, 'video * mtype xml'
check_body('/file.mp4', '.mp4')
def test_static_types_negation(temp_dir):
action_update(
{"share": f'{temp_dir}/assets$uri', "types": ["!application/xml"]}
)
assert client.get(url='/file.xml')['status'] == 403, 'forbidden negation'
check_body('/file.mp4', '.mp4')
# sorting negation
action_update(
{
"share": f'{temp_dir}/assets$uri',
"types": ["!video/*", "image/png", "!image/jpg"],
}
)
assert client.get(url='/file.mp4')['status'] == 403, 'negation sort mp4'
check_body('/file.png', '.png')
assert client.get(url='/file.jpg')['status'] == 403, 'negation sort jpg'
def test_static_types_regex(temp_dir):
action_update(
{
"share": f'{temp_dir}/assets$uri',
"types": ["~text/(html|plain)"],
}
)
assert client.get(url='/file.php')['status'] == 403, 'regex fail'
check_body('/file.html', '.html')
check_body('/file.txt', '.txt')
def test_static_types_case(temp_dir):
action_update(
{"share": f'{temp_dir}/assets$uri', "types": ["!APpliCaTiOn/xMl"]}
)
check_body('/file.mp4', '.mp4')
assert (
client.get(url='/file.xml')['status'] == 403
), 'mixed case xml negation'
action_update({"share": f'{temp_dir}/assets$uri', "types": ["vIdEo/mp4"]})
assert client.get(url='/file.mp4')['status'] == 200, 'mixed case'
assert (
client.get(url='/file.xml')['status'] == 403
), 'mixed case video negation'
action_update({"share": f'{temp_dir}/assets$uri', "types": ["vIdEo/*"]})
check_body('/file.mp4', '.mp4')
assert (
client.get(url='/file.xml')['status'] == 403
), 'mixed case video * negation'
def test_static_types_fallback(temp_dir):
assert 'success' in client.conf(
[
{
"match": {"destination": "*:8081"},
"action": {"return": 200},
},
{
"action": {
"share": f'{temp_dir}/assets$uri',
"types": ["!application/x-httpd-php"],
"fallback": {"proxy": "http://127.0.0.1:8081"},
}
},
],
'routes',
), 'configure fallback proxy route'
check_body('/file.php', '')
check_body('/file.mp4', '.mp4')
def test_static_types_index(temp_dir):
action_update(
{"share": f'{temp_dir}/assets$uri', "types": "application/xml"}
)
check_body('/', 'index')
check_body('/file.xml', '.xml')
assert client.get(url='/index.html')['status'] == 403, 'forbidden mtype'
assert client.get(url='/file.mp4')['status'] == 403, 'forbidden mtype'
def test_static_types_custom_mime(temp_dir):
assert 'success' in client.conf(
{
"listeners": {"*:8080": {"pass": "routes"}},
"routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}],
"applications": {},
"settings": {
"http": {"static": {"mime_types": {"test/mime-type": ["file"]}}}
},
}
)
action_update({"share": f'{temp_dir}/assets$uri', "types": [""]})
assert client.get(url='/file')['status'] == 403, 'forbidden custom mime'
action_update(
{"share": f'{temp_dir}/assets$uri', "types": ["test/mime-type"]}
)
check_body('/file', '')
|