comparison h3_absolute_redirect.t @ 1877:dc0bda44044c

Tests: HTTP/3 tests for absolute_redirect and Location.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 10 Nov 2022 18:14:32 +0400
parents
children 8b74936ff2ac
comparison
equal deleted inserted replaced
1876:74cb9454a13e 1877:dc0bda44044c
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for absolute_redirect directive and Location escaping.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19 use Test::Nginx::HTTP3;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 eval { require Crypt::Misc; die if $Crypt::Misc::VERSION < 0.067; };
27 plan(skip_all => 'CryptX version >= 0.067 required') if $@;
28
29 my $t = Test::Nginx->new()->has(qw/http http_v3 proxy rewrite/)
30 ->has_daemon('openssl')->plan(23);
31
32 $t->write_file_expand('nginx.conf', <<'EOF');
33
34 %%TEST_GLOBALS%%
35
36 daemon off;
37
38 events {
39 }
40
41 http {
42 %%TEST_GLOBALS_HTTP%%
43
44 absolute_redirect off;
45
46 ssl_certificate_key localhost.key;
47 ssl_certificate localhost.crt;
48
49 server {
50 listen 127.0.0.1:%%PORT_8980_UDP%% quic;
51 server_name on;
52
53 absolute_redirect on;
54 error_page 400 /return301;
55
56 location / { }
57
58 location /auto/ {
59 proxy_pass http://127.0.0.1:8080;
60 }
61
62 location "/auto sp/" {
63 proxy_pass http://127.0.0.1:8080;
64 }
65
66 location /return301 {
67 return 301 /redirect;
68 }
69
70 location /return301/name {
71 return 301 /redirect;
72 server_name_in_redirect on;
73 }
74
75 location /return301/port {
76 return 301 /redirect;
77 port_in_redirect off;
78 }
79
80 location /i/ {
81 alias %%TESTDIR%%/;
82 }
83 }
84
85 server {
86 listen 127.0.0.1:%%PORT_8980_UDP%% quic;
87 server_name off;
88
89 location / { }
90
91 location /auto/ {
92 proxy_pass http://127.0.0.1:8080;
93 }
94
95 location "/auto sp/" {
96 proxy_pass http://127.0.0.1:8080;
97 }
98
99 location '/auto "#%<>?\^`{|}/' {
100 proxy_pass http://127.0.0.1:8080;
101 }
102
103 location /return301 {
104 return 301 /redirect;
105 }
106
107 location /i/ {
108 alias %%TESTDIR%%/;
109 }
110 }
111 }
112
113 EOF
114
115 $t->write_file('openssl.conf', <<EOF);
116 [ req ]
117 default_bits = 2048
118 encrypt_key = no
119 distinguished_name = req_distinguished_name
120 [ req_distinguished_name ]
121 EOF
122
123 my $d = $t->testdir();
124
125 foreach my $name ('localhost') {
126 system('openssl req -x509 -new '
127 . "-config $d/openssl.conf -subj /CN=$name/ "
128 . "-out $d/$name.crt -keyout $d/$name.key "
129 . ">>$d/openssl.out 2>&1") == 0
130 or die "Can't create certificate for $name: $!\n";
131 }
132
133 mkdir($t->testdir() . '/dir');
134 mkdir($t->testdir() . '/dir sp');
135
136 $t->run();
137
138 ###############################################################################
139
140 my $p = port(8980);
141
142 like(get('on', '/dir'), qr!Location: https://on:$p/dir/\x0d?$!m, 'directory');
143 like(get('on', '/i/dir'), qr!Location: https://on:$p/i/dir/\x0d?$!m,
144 'directory alias');
145
146 like(get('on', '/dir%20sp'), qr!Location: https://on:$p/dir%20sp/\x0d?$!m,
147 'directory escaped');
148 like(get('on', '/dir%20sp?a=b'),
149 qr!Location: https://on:$p/dir%20sp/\?a=b\x0d?$!m,
150 'directory escaped args');
151
152 like(get('on', '/auto'), qr!Location: https://on:$p/auto/\x0d?$!m, 'auto');
153 like(get('on', '/auto?a=b'), qr!Location: https://on:$p/auto/\?a=b\x0d?$!m,
154 'auto args');
155
156 like(get('on', '/auto%20sp'), qr!Location: https://on:$p/auto%20sp/\x0d?$!m,
157 'auto escaped');
158 like(get('on', '/auto%20sp?a=b'),
159 qr!Location: https://on:$p/auto%20sp/\?a=b\x0d?$!m,
160 'auto escaped args');
161
162 like(get('on', '/return301'), qr!Location: https://on:$p/redirect\x0d?$!m,
163 'return');
164
165 like(get('host', '/return301/name'), qr!Location: https://on:$p/redirect\x0d?!m,
166 'server_name_in_redirect on');
167 like(get('host', '/return301'), qr!Location: https://host:$p/redirect\x0d?$!m,
168 'server_name_in_redirect off - using host');
169 my $ph = IO::Socket::INET->new("127.0.0.1:$p")->peerhost();
170 like(get('.', '/return301'), qr!Location: https://$ph:$p/redirect\x0d?$!m,
171 'invalid host - using local sockaddr');
172 like(get('host', '/return301/port'), qr!Location: https://host/redirect\x0d?$!m,
173 'port_in_redirect off');
174
175 like(get('off', '/dir'), qr!Location: /dir/\x0d?$!m, 'off directory');
176 like(get('off', '/i/dir'), qr!Location: /i/dir/\x0d?$!m, 'off directory alias');
177
178 like(get('off', '/dir%20sp'), qr!Location: /dir%20sp/\x0d?$!m,
179 'off directory escaped');
180 like(get('off', '/dir%20sp?a=b'), qr!Location: /dir%20sp/\?a=b\x0d?$!m,
181 'off directory escaped args');
182
183 like(get('off', '/auto'), qr!Location: /auto/\x0d?$!m, 'off auto');
184 like(get('off', '/auto?a=b'), qr!Location: /auto/\?a=b\x0d?$!m,
185 'off auto args');
186
187 like(get('off', '/auto%20sp'), qr!Location: /auto%20sp/\x0d?$!m,
188 'auto escaped');
189 like(get('off', '/auto%20sp?a=b'), qr!Location: /auto%20sp/\?a=b\x0d?$!m,
190 'auto escaped args');
191
192 like(get('off', '/return301'), qr!Location: /redirect\x0d?$!m, 'off return');
193
194 # per RFC 3986, these characters are not allowed unescaped:
195 # %00-%1F, %7F-%FF, " ", """, "<", ">", "\", "^", "`", "{", "|", "}"
196 # additionally, all characters in ESCAPE_URI: "?", "%", "#"
197
198 SKIP: {
199 skip 'win32', 1 if $^O eq 'MSWin32';
200
201 like(get('off', '/auto%20%22%23%25%3C%3E%3F%5C%5E%60%7B%7C%7D'),
202 qr!Location: /auto%20%22%23%25%3C%3E%3F%5C%5E%60%7B%7C%7D/\x0d?$!m,
203 'auto escaped strict');
204
205 }
206
207 ###############################################################################
208
209 sub get {
210 my ($host, $uri) = @_;
211
212 my $s = Test::Nginx::HTTP3->new();
213 my $sid = $s->new_stream({ host => $host, path => $uri });
214 my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
215
216 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
217 return 'Location: ' . $frame->{headers}->{'location'};
218 }
219
220 ###############################################################################