Skip to content

fix: skip invalid UTF-8 headers instead of panicking #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion examples/t/awssig.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use strict;

use Test::More;

use Socket qw/ CRLF /;

BEGIN { use FindBin; chdir($FindBin::Bin); }

use lib 'lib';
Expand All @@ -21,7 +23,7 @@ use Test::Nginx;
select STDERR; $| = 1;
select STDOUT; $| = 1;

my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(1)
my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(2)
->write_file_expand('nginx.conf', <<"EOF");

%%TEST_GLOBALS%%
Expand Down Expand Up @@ -70,4 +72,12 @@ $t->run();
like(http_get('/'), qr/x-authorization: AWS4.*Credential=my-access-key/i,
'awssig header');

like(http(
'GET / HTTP/1.0' . CRLF
. 'Foo: foo' . CRLF
. "Bar: \xFF\xFE\x80\x81" . CRLF
. "Host: localhost" . CRLF . CRLF
), qr/x-authorization: AWS4.*Credential=my-access-key/,
'awssig invalid header ignored');

###############################################################################
40 changes: 28 additions & 12 deletions src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,20 +465,36 @@ impl<'a> Iterator for NgxListIterator<'a> {
type Item = (&'a str, &'a str);

fn next(&mut self) -> Option<Self::Item> {
let part = self.part.as_mut()?;
if self.i >= part.arr.len() {
if let Some(next_part_raw) = unsafe { part.raw.next.as_ref() } {
// loop back
*part = next_part_raw.into();
self.i = 0;
} else {
self.part = None;
return None;
loop {
let part = self.part.as_mut()?;
if self.i >= part.arr.len() {
if let Some(next_part_raw) = unsafe { part.raw.next.as_ref() } {
// loop back
*part = next_part_raw.into();
self.i = 0;
} else {
self.part = None;
return None;
}
}
let header = &part.arr[self.i];
self.i += 1;

let key_bytes = header.key.as_ref();
let value_bytes = header.value.as_ref();

match (
std::str::from_utf8(key_bytes),
std::str::from_utf8(value_bytes),
) {
(Ok(key), Ok(value)) => {
return Some((key, value));
}
_ => {
continue;
}
}
}
let header = &part.arr[self.i];
self.i += 1;
Some((header.key.to_str(), header.value.to_str()))
}
}

Expand Down
Loading