forked from nginx/nginx-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreferer.t
210 lines (167 loc) · 6.58 KB
/
referer.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
#!/usr/bin/perl
# (C) Sergey Kandaurov
# Tests for referer module.
###############################################################################
use warnings;
use strict;
use Test::More;
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 referer rewrite/)->plan(54);
$t->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080;
server_name another;
valid_referers server_names;
return 200 "$host value $invalid_referer";
}
server {
listen 127.0.0.1:8080;
server_name _;
location / {
valid_referers server_names;
return 200 "$host value $invalid_referer";
}
server_name below;
}
server {
listen 127.0.0.1:8080;
server_name localhost ~bar ~^anchoredre$;
location /blocked {
valid_referers blocked www.example.org;
return 200 "value $invalid_referer";
}
location /none {
valid_referers none www.example.org;
return 200 "value $invalid_referer";
}
location /simple {
valid_referers www.example.org;
return 200 "value $invalid_referer";
}
location /regex {
valid_referers ~example.org ~(?-i)example.net;
return 200 "value $invalid_referer";
}
location /regex2 {
valid_referers ~example.org/uri;
return 200 "value $invalid_referer";
}
location /regex3 {
valid_referers ~example.org$;
return 200 "value $invalid_referer";
}
location /uri {
valid_referers www.example.org/uri;
return 200 "value $invalid_referer";
}
location /sn {
valid_referers server_names;
return 200 "value $invalid_referer";
}
location /sn_blocked {
valid_referers blocked server_names;
return 200 "value $invalid_referer";
}
location /wc {
valid_referers *.example.com *.example.org www.example.* example.*;
return 200 "value $invalid_referer";
}
location /long {
valid_referers ~.*;
return 200 "value $invalid_referer";
}
location /wc2 {
valid_referers www.example.*/uri;
return 200 "value $invalid_referer";
}
}
}
EOF
$t->run();
###############################################################################
ok(valid('/simple', 'http://www.example.org'), 'simple');
ok(valid('/simple', 'http://www.example.org/uri'), 'simple uri');
ok(valid('/simple', 'http://www.example.org:' . port(8080) . '/uri'),
'simple port uri');
ok(!valid('/simple', 'localhost'), 'simple invalid');
ok(valid('/simple', 'https://www.example.org'), 'https');
ok(!valid('/simple', 'example.com'), 'no scheme');
ok(!valid('/simple'), 'no none');
ok(valid('/none'), 'none');
ok(!valid('/none', ''), 'none empty');
ok(valid('/blocked', 'www.example.org'), 'blocked');
ok(valid('/blocked', 'www.example.com'), 'blocked 2');
ok(valid('/blocked', 'http://su'), 'blocked short');
ok(valid('/blocked', 'foobar'), 'blocked short no scheme');
ok(valid('/blocked', ''), 'blocked empty');
ok(!valid('/simple', 'foobar'), 'small');
ok(valid('/simple', 'http://www.example.org/' . 'a' x 256), 'long uri');
ok(!valid('/simple', 'http://www.example.' . 'a' x 256), 'long hostname');
ok(!valid('/wc', 'http://example.' . 'a' x 256), 'long hostname wildcard');
ok(valid('/long', 'http://' . 'a' x 255), 'long hostname 255');
ok(valid('/long', 'http://' . 'a' x 256), 'long hostname 256');
ok(!valid('/long', 'http://' . 'a' x 257), 'long hostname 257');
ok(valid('/uri', 'http://www.example.org/uri'), 'uri');
ok(valid('/uri', 'http://www.example.org/urii'), 'uri prefix');
ok(!valid('/uri', 'http://www.example.org/uRi'), 'uri case');
ok(valid('/uri', 'http://www.example.org:' . port(8080) . '/urii'), 'uri port');
ok(!valid('/uri', 'http://www.example.org/ur'), 'uri invalid len');
ok(!valid('/uri', 'http://www.example.org/urd'), 'uri invalid cmp');
ok(valid('/regex', 'http://www.example.org'), 'regex');
ok(valid('/regex', 'http://www.eXample.org'), 'regex caseless');
ok(valid('/regex', 'http://www.example.org/uri'), 'regex uri');
ok(!valid('/regex', 'http://www.example.com'), 'regex mismatch');
ok(!valid('/regex', 'http://www.eXample.net'), 'regex case mismatch');
ok(valid('/regex2', 'http://www.example.org/uri'), 'regex 2 uri');
ok(!valid('/regex2', 'http://www.example.org'), 'regex 2 no uri');
ok(valid('/regex2', 'http://www.example.org/uRI'), 'regex 2 uri caseless');
ok(valid('/regex3', 'https://www.eXample.org'), 'regex https');
ok(valid('/sn', 'http://localhost'), 'server_names');
ok(valid('/sn', 'http://localHost'), 'server_names caseless');
ok(valid('/sn', 'http://localhost/uri'), 'server_names uri');
ok(valid('/sn', 'http://foobar'), 'server_names regex');
ok(valid('/sn', 'http://foobAr'), 'server_names regex caseless');
ok(valid('/sn', 'http://foobAr/uri'), 'server_names regex caseless uri');
ok(valid('/sn', 'http://anchoredre/uri'), 'server_names regex anchored');
ok(valid('/sn', 'http://foobar/uri'), 'server_names regex uri');
ok(!valid('/sn', 'localhost'), 'server_names no scheme');
ok(!valid('/sn', 'foobar'), 'server_names regex no scheme');
ok(valid('/sn_blocked', 'localhost'), 'server_names no scheme blocked');
ok(valid('/wc', 'http://www.example.org'), 'wildcard head');
ok(valid('/wc', 'http://www.example.net'), 'wildcard tail');
ok(valid('/wc2', 'http://www.example.net/uri'), 'wildcard uri');
ok(valid('/wc2', 'http://www.example.net/urii'), 'wildcard uri prefix');
ok(!valid('/wc2', 'http://www.example.net/uRI'), 'wildcard uri case');
ok(valid('/', 'http://another', 'another'), 'server context');
# server_name below valid_referers
ok(valid('/', 'http://below', 'below'), 'server below');
###############################################################################
sub valid {
my ($uri, $referer, $host) = @_;
my $text;
$host = 'localhost' unless defined $host;
unless (defined $referer) {
$text = http_get($uri);
} else {
$text = http(<<EOF);
GET $uri HTTP/1.0
Host: $host
Referer: $referer
EOF
}
$text =~ /value 1/ && return 0;
$text =~ /value/ && return 1;
fail("no valid_referers in $uri");
}
###############################################################################