Skip to content

Commit

Permalink
forwarding bugfix: correctly format non-token values for host, by, an…
Browse files Browse the repository at this point in the history
…d proto
  • Loading branch information
jbr committed Dec 19, 2023
1 parent 67877c7 commit ea52e21
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions forwarding/src/forwarded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ impl std::fmt::Display for Forwarded<'_> {
let mut needs_semi = false;
if let Some(by) = self.by() {
needs_semi = true;
write!(f, "by={by}").unwrap();
write!(f, "by={}", format_value(by))?;
}

if !self.forwarded_for.is_empty() {
Expand All @@ -388,14 +388,14 @@ impl std::fmt::Display for Forwarded<'_> {
f.write_char(';')?;
}
needs_semi = true;
write!(f, "host={host}")?
write!(f, "host={}", format_value(host))?
}

if let Some(proto) = self.proto() {
if needs_semi {
f.write_char(';')?;
}
write!(f, "proto={proto}")?
write!(f, "proto={}", format_value(proto))?
}

Ok(())
Expand Down Expand Up @@ -616,6 +616,15 @@ mod tests {
forwarded.to_string(),
r#"for="quote: \" backslash: \\", for=";proto=https""#
);

let mut forwarded = Forwarded::new();
forwarded.set_host("localhost:8080");
forwarded.set_proto("not:normal"); // handled correctly but should not happen
forwarded.set_by("localhost:8081");
assert_eq!(
forwarded.to_string(),
r#"by="localhost:8081";host="localhost:8080";proto="not:normal""#
);
}

#[test]
Expand Down Expand Up @@ -675,6 +684,7 @@ mod tests {
"proto=https",
"host=example.com",
"for=a,for=b",
r#"by="localhost:8081";host="localhost:8080";proto="not:normal""#,
];
for input in inputs {
let forwarded = Forwarded::parse(input)?;
Expand Down

0 comments on commit ea52e21

Please sign in to comment.