Skip to content

Commit de27273

Browse files
committed
ngx::http::NgxListIterator: ngx_str_t items are NgxStr, not str
The ngx_str_items in the header name and value are often untrusted input, and may not have utf-8 contents. The use of ngx_str_t::to_str in this iterator will panic when the contents are not utf-8. So, instead of yielding a pair of strs here, yield a pair of &NgxStr, which is like ngx_str_t but with more methods.
1 parent 95424ad commit de27273

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

examples/awssig.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,16 @@ http_request_handler!(awssigv4_header_handler, |request: &mut Request| {
286286
// Copy only headers that will be used to sign the request
287287
let mut headers = HeaderMap::new();
288288
for (name, value) in request.headers_in_iterator() {
289-
if name.to_lowercase() == "host" {
290-
headers.insert(http::header::HOST, value.parse().unwrap());
289+
if let Ok(name) = name.to_str() {
290+
if name.to_lowercase() == "host" {
291+
if let Ok(value) = http::HeaderValue::from_bytes(value.as_bytes()) {
292+
headers.insert(http::header::HOST, value);
293+
} else {
294+
return core::Status::NGX_DECLINED;
295+
}
296+
}
297+
} else {
298+
return core::Status::NGX_DECLINED;
291299
}
292300
}
293301
headers.insert("X-Amz-Date", datetime_now.parse().unwrap());
@@ -313,12 +321,11 @@ http_request_handler!(awssigv4_header_handler, |request: &mut Request| {
313321
request.add_header_in("authorization", signature.as_str());
314322
request.add_header_in("X-Amz-Date", datetime_now.as_str());
315323

316-
// done signing, let's print values we have in request.headers_out, request.headers_in
317324
for (name, value) in request.headers_out_iterator() {
318-
ngx_log_debug_http!(request, "headers_out {}: {}", name, value);
325+
ngx_log_debug_http!(request, "headers_out {name}: {value}",);
319326
}
320327
for (name, value) in request.headers_in_iterator() {
321-
ngx_log_debug_http!(request, "headers_in {}: {}", name, value);
328+
ngx_log_debug_http!(request, "headers_in {name}: {value}",);
322329
}
323330

324331
core::Status::NGX_OK

src/http/request.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl<'a> Iterator for NgxListIterator<'a> {
462462
// something like pub struct Header(ngx_table_elt_t);
463463
// then header would have key and value
464464

465-
type Item = (&'a str, &'a str);
465+
type Item = (&'a NgxStr, &'a NgxStr);
466466

467467
fn next(&mut self) -> Option<Self::Item> {
468468
let part = self.part.as_mut()?;
@@ -478,7 +478,12 @@ impl<'a> Iterator for NgxListIterator<'a> {
478478
}
479479
let header = &part.arr[self.i];
480480
self.i += 1;
481-
Some((header.key.to_str(), header.value.to_str()))
481+
unsafe {
482+
Some((
483+
NgxStr::from_ngx_str(header.key),
484+
NgxStr::from_ngx_str(header.value),
485+
))
486+
}
482487
}
483488
}
484489

0 commit comments

Comments
 (0)