Skip to content

Commit 728d2bc

Browse files
committed
feat(addr): remove error when parsing ip from headers, as there can be bad values coming from clients
1 parent 0a92a51 commit 728d2bc

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

src/api/log.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ impl Log {
106106
for forwarded_ip in forwarded_ips {
107107
match forwarded_ip.parse::<Addr>() {
108108
Ok(addr) => ips.push(addr.addr),
109-
Err(()) => {
110-
log::error!("cannot parse ip address {}, skipping", forwarded_ip);
111-
}
109+
Err(()) => (),
112110
}
113111
}
114112
}
@@ -123,9 +121,7 @@ impl Log {
123121

124122
match ip.parse::<Addr>() {
125123
Ok(ip) => ips.push(ip.addr),
126-
Err(()) => {
127-
log::error!("cannot parse ip address {}, skipping", ip);
128-
}
124+
Err(()) => (),
129125
}
130126
}
131127
}

src/http/addr.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ impl FromStr for Addr {
2727
type Err = ();
2828

2929
fn from_str(s: &str) -> Result<Self, Self::Err> {
30-
if let Ok(addr) = s.parse::<IpAddr>() {
30+
let trimmed = s.trim_matches(|c| c == '\0' || c == '\n' || c == '\r' || c == '\t' || c == ' ');
31+
32+
if let Ok(addr) = trimmed.parse::<IpAddr>() {
3133
Ok(Self { addr, port: None })
32-
} else if let Ok(sock) = s.parse::<SocketAddr>() {
34+
} else if let Ok(sock) = trimmed.parse::<SocketAddr>() {
3335
Ok(Self {
3436
addr: sock.ip(),
3537
port: Some(sock.port()),
@@ -60,6 +62,15 @@ mod tests {
6062
assert_eq!(addr.addr.to_string(), "127.0.0.1");
6163
}
6264

65+
#[test]
66+
fn test_ipv4_with_null() {
67+
let addr = "127.0.0.1\0".parse::<Addr>().unwrap();
68+
69+
assert!(addr.addr.is_ipv4());
70+
assert!(addr.port.is_none());
71+
assert_eq!(addr.addr.to_string(), "127.0.0.1");
72+
}
73+
6374
#[test]
6475
fn test_ipv4_with_port() {
6576
let addr = "127.0.0.1:8080".parse::<Addr>().unwrap();

src/http/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ mod trusted_proxies;
77
#[cfg(not(target_arch = "wasm32"))]
88
pub mod ffi;
99

10+
pub use addr::Addr;
1011
pub use header::Header;
1112
pub use query::sanitize_url;
1213
pub use query::PathAndQueryWithSkipped;
1314
pub use request::Request;
1415
pub use trusted_proxies::TrustedProxies;
15-
pub use addr::Addr;

src/http/request.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ impl Request {
277277

278278
match ip.parse::<Addr>() {
279279
Ok(ip) => ips.push(ip.addr),
280-
Err(()) => {
281-
log::error!("-cannot parse ip address {}, skipping", ip);
282-
}
280+
Err(()) => (),
283281
}
284282
}
285283
}
@@ -289,9 +287,7 @@ impl Request {
289287

290288
match ip.parse::<Addr>() {
291289
Ok(ip) => ips.push(ip.addr),
292-
Err(()) => {
293-
log::error!("cannot parse ip address {}, skipping", ip);
294-
}
290+
Err(()) => (),
295291
}
296292
}
297293

0 commit comments

Comments
 (0)