Skip to content

Commit 8cc779f

Browse files
committed
handle non-ascii header values
1 parent e1128c7 commit 8cc779f

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

runtimes/core/src/api/reqauth/meta.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
use std::str::{self, FromStr};
22

3+
use http::HeaderValue;
4+
5+
pub trait HeaderValueExt {
6+
fn to_utf8_str(&self) -> Result<&str, std::str::Utf8Error>;
7+
}
8+
9+
impl HeaderValueExt for HeaderValue {
10+
// Some header values may contain utf8 characters (e.g authdata) so we use `str::from_utf8`
11+
// rather than using `HeaderValues::to_str` which errors on non-visible ASCII characters.
12+
fn to_utf8_str(&self) -> Result<&str, std::str::Utf8Error> {
13+
std::str::from_utf8(self.as_bytes())
14+
}
15+
}
16+
317
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
418
pub enum MetaKey {
519
TraceParent,
@@ -78,17 +92,15 @@ impl MetaMapMut for reqwest::header::HeaderMap {
7892

7993
impl MetaMap for axum::http::HeaderMap {
8094
fn get_meta(&self, key: MetaKey) -> Option<&str> {
81-
// Some meta values may contain utf8 characters (e.g authdata) so we use `str::from_utf8`
82-
// rather than using `HeaderValues::to_str` which errors on non-visible ASCII characters.
8395
self.get(key.header_key())
84-
.and_then(|val| std::str::from_utf8(val.as_bytes()).ok())
96+
.and_then(|val| val.to_utf8_str().ok())
8597
}
8698

8799
fn meta_values<'a>(&'a self, key: MetaKey) -> Box<dyn Iterator<Item = &'a str> + 'a> {
88100
Box::new(
89101
self.get_all(key.header_key())
90102
.iter()
91-
.filter_map(|v| v.to_str().ok()),
103+
.filter_map(|v| v.to_utf8_str().ok()),
92104
)
93105
}
94106

@@ -113,15 +125,15 @@ impl MetaMap for pingora::http::RequestHeader {
113125
fn get_meta(&self, key: MetaKey) -> Option<&str> {
114126
self.headers
115127
.get(key.header_key())
116-
.and_then(|v| v.to_str().ok())
128+
.and_then(|v| v.to_utf8_str().ok())
117129
}
118130

119131
fn meta_values<'a>(&'a self, key: MetaKey) -> Box<dyn Iterator<Item = &'a str> + 'a> {
120132
Box::new(
121133
self.headers
122134
.get_all(key.header_key())
123135
.iter()
124-
.filter_map(|v| v.to_str().ok()),
136+
.filter_map(|v| v.to_utf8_str().ok()),
125137
)
126138
}
127139

runtimes/core/src/api/schema/header.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::api::reqauth::meta::HeaderValueExt;
12
use crate::api::schema::{JSONPayload, ParseResponse, ToOutgoingRequest, ToResponse};
23
use crate::api::{self, PValue, PValues};
34
use crate::api::{jsonschema, APIResult};
@@ -74,10 +75,10 @@ impl AsStr for &axum::http::header::HeaderName {
7475
}
7576

7677
impl ToHeaderStr for &axum::http::header::HeaderValue {
77-
type Error = axum::http::header::ToStrError;
78+
type Error = std::str::Utf8Error;
7879

7980
fn to_str(&self) -> Result<&str, Self::Error> {
80-
<axum::http::header::HeaderValue>::to_str(self)
81+
<axum::http::header::HeaderValue>::to_utf8_str(self)
8182
}
8283
fn is_empty(&self) -> bool {
8384
<axum::http::header::HeaderValue>::is_empty(self)

runtimes/core/src/trace/protocol.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use std::sync::atomic::{AtomicU64, Ordering};
55

6+
use crate::api::reqauth::meta::HeaderValueExt;
67
use crate::api::{self, PValue};
78
use crate::model::{LogField, LogFieldValue, Request, TraceEventId};
89
use crate::trace::eventbuf::EventBuffer;
@@ -888,7 +889,7 @@ impl EventBuffer {
888889
self.uvarint(headers.len() as u64);
889890
for (k, v) in headers.iter() {
890891
self.str(k.as_str());
891-
self.str(v.to_str().unwrap_or(""));
892+
self.str(v.to_utf8_str().unwrap_or(""));
892893
}
893894
}
894895
}

runtimes/js/src/request_meta.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use chrono::{DateTime, SecondsFormat, Utc};
22
use napi_derive::napi;
33

4-
use encore_runtime_core::model;
4+
use encore_runtime_core::{api::reqauth::meta::HeaderValueExt, model};
55

66
use crate::pvalue::PVals;
77

@@ -149,7 +149,7 @@ fn serialize_headers(
149149
let mut map = Map::with_capacity(headers.len());
150150

151151
for (k, v) in headers {
152-
let Ok(v) = v.to_str() else {
152+
let Ok(v) = v.to_utf8_str() else {
153153
continue;
154154
};
155155

0 commit comments

Comments
 (0)