forked from nginx/nginx-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_disable_symlinks.t
288 lines (217 loc) · 7.55 KB
/
http_disable_symlinks.t
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/perl
# (C) Andrey Belov
# Tests for disable_symlinks directive.
###############################################################################
use warnings;
use strict;
use Test::More;
use POSIX;
use Cwd qw/ realpath /;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
my $t = Test::Nginx->new()->has(qw/http rewrite symlink/)
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080;
server_name s1;
location /on/ {
disable_symlinks on;
}
location /not_owner/ {
disable_symlinks if_not_owner;
}
location /try_on/ {
disable_symlinks on;
try_files $uri $uri.html =404;
}
location /try_not_owner/ {
disable_symlinks if_not_owner;
try_files $uri $uri.txt =404;
}
location /if_on/ {
disable_symlinks on;
if (-f $request_filename) {
return 204;
}
}
location /if_not_owner/ {
disable_symlinks if_not_owner;
if (-f $request_filename) {
return 204;
}
}
location /complex/1/ {
disable_symlinks on;
alias %%TESTDIR%%/./cached/../;
}
location /complex/2/ {
disable_symlinks on;
alias %%TESTDIR%%//./cached/..//;
}
location /complex/3/ {
disable_symlinks on;
alias ///%%TESTDIR%%//./cached/..//;
}
location ~ (.+/)tail$ {
disable_symlinks on;
alias %%TESTDIR%%/$1;
}
location ~ (.+/)tailowner$ {
disable_symlinks if_not_owner;
alias %%TESTDIR%%/$1;
}
location ~ (.+/)tailoff$ {
disable_symlinks off;
alias %%TESTDIR%%/$1;
}
location /dir {
disable_symlinks on;
try_files $uri/ =404;
}
location /from {
disable_symlinks on from=$document_root;
location /from/wo_slash {
alias %%TESTDIR%%/dirlink;
}
location /from/with_slash/ {
alias %%TESTDIR%%/dirlink/;
}
location ~ ^/from/exact/(.+)$ {
alias %%TESTDIR%%/$1;
}
}
}
server {
listen 127.0.0.1:8080;
server_name s2;
open_file_cache max=16 inactive=60s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
open_file_cache_errors on;
location /cached-off/ {
disable_symlinks off;
alias %%TESTDIR%%/cached/;
}
location /cached-on/ {
disable_symlinks on;
alias %%TESTDIR%%/cached/;
}
location /cached-if-not-owner/ {
disable_symlinks if_not_owner;
alias %%TESTDIR%%/cached/;
}
location / {
disable_symlinks off;
}
}
}
EOF
my $uid = getuid();
my ($extfile) = grep { -f && !-l && $uid != (stat())[4] }
('/etc/resolv.conf', '/etc/protocols', '/etc/host.conf');
plan(skip_all => 'no external file found')
if !defined $extfile;
$t->try_run('no disable_symlinks')->plan(28);
my $d = $t->testdir();
mkdir("$d/on");
mkdir("$d/not_owner");
mkdir("$d/try_on");
mkdir("$d/try_not_owner");
mkdir("$d/if_on");
mkdir("$d/if_not_owner");
mkdir("$d/cached");
$t->write_file("empty.html", "");
symlink("empty.html", "$d/link");
symlink($extfile, "$d/link2");
$t->write_file("on/empty.html", "");
symlink("empty.html", "$d/on/link");
symlink($extfile, "$d/on/link2");
$t->write_file("not_owner/empty.html", "");
symlink("empty.html", "$d/not_owner/link");
symlink($extfile, "$d/not_owner/link2");
$t->write_file("try_on/try.html", "LOCAL TRY");
symlink($extfile, "$d/try_on/try");
$t->write_file("try_not_owner/try.html", "LOCAL TRY");
symlink($extfile, "$d/try_not_owner/try");
symlink("try.html", "$d/try_not_owner/try.txt");
$t->write_file("if_on/empty.html", "");
symlink("empty.html", "$d/if_on/link");
symlink($extfile, "$d/if_on/link2");
$t->write_file("if_not_owner/empty.html", "");
symlink("empty.html", "$d/if_not_owner/link");
symlink($extfile, "$d/if_not_owner/link2");
mkdir("$d/dir");
$t->write_file("dir/empty.html", "");
symlink("dir", "$d/dirlink");
symlink($extfile, "$d/cached/link");
###############################################################################
SKIP: {
skip 'cannot test under symlink', 25 if $d ne realpath($d) or $^O eq 'netbsd';
like(http_get_host('s1', '/link'), qr!200 OK!, 'static (off, same uid)');
like(http_get_host('s1', '/link2'), qr!200 OK!, 'static (off, other uid)');
like(http_get_host('s1', '/on/link'), qr!403 Forbidden!,
'static (on, same uid)');
like(http_get_host('s1', '/on/link2'), qr!403 Forbidden!,
'static (on, other uid)');
like(http_get_host('s1', '/not_owner/link'), qr!200 OK!,
'static (if_not_owner, same uid)');
like(http_get_host('s1', '/not_owner/link2'), qr!403 Forbidden!,
'static (if_not_owner, other uid)');
like(http_get_host('s1', '/try_on/try'), qr/LOCAL TRY/,
'try_files (on)');
like(http_get_host('s1', '/try_not_owner/try'), qr/LOCAL TRY/,
'try_files (if_not_owner)');
like(http_get_host('s1', '/if_on/link'), qr!403 Forbidden!,
'if (on, same uid)');
like(http_get_host('s1', '/if_on/link2'), qr!403 Forbidden!,
'if (on, other uid)');
like(http_get_host('s1', '/if_not_owner/link'), qr!204 No Content!,
'if (if_not_owner, same uid)');
like(http_get_host('s1', '/if_not_owner/link2'), qr!403 Forbidden!,
'if (if_not_owner, other uid)');
like(http_get_host('s2', '/cached-off/link'), qr!200 OK!,
'open_file_cache (pass 1)');
like(http_get_host('s2', '/cached-on/link'), qr!403 Forbidden!,
'open_file_cache (pass 2)');
like(http_get_host('s2', '/cached-off/link'), qr!200 OK!,
'open_file_cache (pass 3)');
like(http_get_host('s2', '/cached-if-not-owner/link'), qr!403 Forbidden!,
'open_file_cache (pass 4)');
like(http_get_host('s2', '/cached-off/link'), qr!200 OK!,
'open_file_cache (pass 5)');
like(http_get('/complex/1/empty.html'), qr!200 OK!, 'complex root 1');
like(http_get('/complex/2/empty.html'), qr!200 OK!, 'complex root 2');
like(http_get('/complex/3/empty.html'), qr!200 OK!, 'complex root 3');
# workaround for freebsd 8: we use O_EXEC instead of O_SEARCH (since there
# is no O_SEARCH), and O_DIRECTORY does nothing. setting the 'x' bit
# tests to pass as openat() will correctly fail with ENOTDIR
chmod(0700, "$d/link");
my $rc = $^O eq 'darwin' ? 200 : 404;
like(http_get('/link/tail'), qr!40[34] !, 'file with trailing /, on');
like(http_get('/link/tailowner'), qr!404 !, 'file with trailing /, owner');
like(http_get('/link/tailoff'), qr!$rc !, 'file with trailing /, off');
like(http_get('/dirlink'), qr!404 !, 'directory without /');
like(http_get('/dirlink/'), qr!404 !, 'directory with trailing /');
} # SKIP: cannot test under symlink
like(http_get('/from/wo_slash/empty.html'), qr!200 OK!, '"from=" without /');
like(http_get('/from/with_slash/empty.html'), qr!200 OK!, '"from=" with /');
like(http_get('/from/exact/link'), qr!200 OK!, '"from=" exact match');
###############################################################################
sub http_get_host {
my ($host, $url) = @_;
return http(<<EOF);
GET $url HTTP/1.0
Host: $host
EOF
}
###############################################################################