# HG changeset patch # User Sergey Kandaurov # Date 1571394814 -10800 # Node ID d8684b300d227279668a3a0dbf908906f1717e75 # Parent b6699ffd9ddded807b1a9187b893125bd21cd992 Tests: ignore_invalid_headers, underscores_in_headers directives. diff --git a/h2_headers.t b/h2_headers.t --- a/h2_headers.t +++ b/h2_headers.t @@ -3,8 +3,7 @@ # (C) Sergey Kandaurov # (C) Nginx, Inc. -# Tests for HTTP/2 protocol with headers. -# various HEADERS compression/encoding, see hpack() for mode details. +# Tests for HTTP/2 headers. ############################################################################### @@ -24,7 +23,7 @@ use Test::Nginx::HTTP2; select STDERR; $| = 1; select STDOUT; $| = 1; -my $t = Test::Nginx->new()->has(qw/http http_v2 proxy rewrite/)->plan(93) +my $t = Test::Nginx->new()->has(qw/http http_v2 proxy rewrite/)->plan(97) ->write_file_expand('nginx.conf', <<'EOF'); %%TEST_GLOBALS%% @@ -103,6 +102,22 @@ http { http2_max_header_size 64; } + + server { + listen 127.0.0.1:8086 http2; + server_name localhost; + + underscores_in_headers on; + add_header X-Sent-Foo $http_x_foo always; + } + + server { + listen 127.0.0.1:8087 http2; + server_name localhost; + + ignore_invalid_headers off; + add_header X-Sent-Foo $http_x_foo always; + } } EOF @@ -951,6 +966,54 @@ is($frame->{code}, 1, 'newline in reques ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; is($frame->{headers}->{'x-referer'}, 'see-this', 'after invalid header name'); +# other invalid header name characters as seen with ':' result in RST_STREAM + +$s = Test::Nginx::HTTP2->new(); +$sid = $s->new_stream({ headers => [ + { name => ':method', value => 'GET', mode => 0 }, + { name => ':scheme', value => 'http', mode => 0 }, + { name => ':path', value => '/', mode => 0 }, + { name => ':authority', value => 'localhost', mode => 1 }, + { name => 'x:foo', value => "x-bar", mode => 2 }, + { name => 'referer', value => "see-this", mode => 1 }]}); +$frames = $s->read(all => [{ type => 'RST_STREAM' }]); + +($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames; +is($frame->{sid}, $sid, 'colon in header name - RST_STREAM sid'); +is($frame->{code}, 1, 'colon in header name - RST_STREAM code'); + +# header name with underscore - underscores_in_headers on + +$s = Test::Nginx::HTTP2->new(port(8086)); +$sid = $s->new_stream({ headers => [ + { name => ':method', value => 'GET', mode => 0 }, + { name => ':scheme', value => 'http', mode => 0 }, + { name => ':path', value => '/', mode => 0 }, + { name => ':authority', value => 'localhost', mode => 1 }, + { name => 'x_foo', value => "x-bar", mode => 2 }, + { name => 'referer', value => "see-this", mode => 1 }]}); +$frames = $s->read(all => [{ type => 'HEADERS' }]); + +($frame) = grep { $_->{type} eq "HEADERS" } @$frames; +is($frame->{headers}->{'x-sent-foo'}, 'x-bar', + 'underscore in header name - underscores_in_headers'); + +# header name with underscore - ignore_invalid_headers off + +$s = Test::Nginx::HTTP2->new(port(8087)); +$sid = $s->new_stream({ headers => [ + { name => ':method', value => 'GET', mode => 0 }, + { name => ':scheme', value => 'http', mode => 0 }, + { name => ':path', value => '/', mode => 0 }, + { name => ':authority', value => 'localhost', mode => 1 }, + { name => 'x_foo', value => "x-bar", mode => 2 }, + { name => 'referer', value => "see-this", mode => 1 }]}); +$frames = $s->read(all => [{ type => 'HEADERS' }]); + +($frame) = grep { $_->{type} eq "HEADERS" } @$frames; +is($frame->{headers}->{'x-sent-foo'}, 'x-bar', + 'underscore in header name - ignore_invalid_headers'); + # missing mandatory request header ':scheme' $s = Test::Nginx::HTTP2->new(); diff --git a/ignore_invalid_headers.t b/ignore_invalid_headers.t new file mode 100644 --- /dev/null +++ b/ignore_invalid_headers.t @@ -0,0 +1,171 @@ +#!/usr/bin/perl + +# (C) Sergey Kandaurov +# (C) Nginx, Inc. + +# Tests for ignore_invalid_headers, underscores_in_headers directives. + +############################################################################### + +use warnings; +use strict; + +use Test::More; + +use Socket qw/ CRLF /; +use MIME::Base64 qw/ encode_base64 decode_base64 /; + +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 proxy/)->plan(9) + ->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + server { + listen 127.0.0.1:8080; + server_name localhost; + + ignore_invalid_headers off; + + location / { + proxy_pass http://127.0.0.1:8085; + } + + location /v { + add_header X-Cookie $http_cookie; + } + } + + server { + listen 127.0.0.1:8081; + server_name localhost; + + location / { + proxy_pass http://127.0.0.1:8085; + } + } + + server { + listen 127.0.0.1:8082; + server_name localhost; + + underscores_in_headers on; + + location / { + proxy_pass http://127.0.0.1:8085; + } + } +} + +EOF + +$t->write_file('index.html', ''); +$t->write_file('v', ''); +$t->run_daemon(\&http_daemon); +$t->run()->waitforsocket('127.0.0.1:' . port(8085)); + +############################################################################### + +my $us = 'GET / HTTP/1.0' . CRLF + . 'x_foo: x-bar' . CRLF . CRLF; +my $us2 = 'GET / HTTP/1.0' . CRLF + . '_foo: x-bar' . CRLF . CRLF; +my $bad = 'GET / HTTP/1.0' . CRLF + . 'x.foo: x-bar' . CRLF . CRLF; +my $bad2 = 'GET / HTTP/1.0' . CRLF + . '.foo: x-bar' . CRLF . CRLF; + +# ignore_invalid_headers off; + +like(get($us, 8080), qr/x-bar/, 'off - underscore'); +like(get($us2, 8080), qr/x-bar/, 'off - underscore first'); +like(get($bad, 8080), qr/x-bar/, 'off - bad'); +like(get($bad2, 8080), qr/x-bar/, 'off - bad first'); + +# ignore_invalid_headers off; headers parsing post 8f55cb5c7e79 + +TODO: { +local $TODO = 'not yet' unless $t->has_version('1.17.5'); + +unlike(http('GET /v HTTP/1.0' . CRLF + . 'Host: localhost' . CRLF + . 'coo: foo' . CRLF + . ': x-bar' . CRLF . CRLF), qr/x-bar/, 'off - several'); + +} + +# ignore_invalid_headers on; + +unlike(get($us, 8081), qr/x-bar/, 'on - underscore'); +unlike(get($us2, 8081), qr/x-bar/, 'on - underscore first'); + +# ignore_invalid_headers on; underscores_in_headers on; + +like(get($us, 8082), qr/x-bar/, 'underscores_in_headers'); +like(get($us2, 8082), qr/x-bar/, 'underscores_in_headers - first'); + +############################################################################### + +sub get { + my ($msg, $port) = @_; + + my $s = IO::Socket::INET->new('127.0.0.1:' . port($port)) or die; + my ($headers) = http($msg, socket => $s) =~ /X-Headers: (\w+)/; + decode_base64($headers); +} + +############################################################################### + +sub http_daemon { + my $once = 1; + my $server = IO::Socket::INET->new( + Proto => 'tcp', + LocalHost => '127.0.0.1:' . port(8085), + Listen => 5, + Reuse => 1 + ) + or die "Can't create listening socket: $!\n"; + + local $SIG{PIPE} = 'IGNORE'; + + while (my $client = $server->accept()) { + $client->autoflush(1); + + my $headers = ''; + my $uri = ''; + + while (<$client>) { + $headers .= $_; + last if (/^\x0d?\x0a?$/); + } + + $headers = encode_base64($headers, ""); + + print $client <