From 6249722589f9a10a4771ef4f7a8cbabbd8935ccc Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Thu, 9 Nov 2023 14:19:59 +0100 Subject: [PATCH] http2: normalize host when there is user info Ticket: 6479 --- rust/src/http2/detect.rs | 62 +++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/rust/src/http2/detect.rs b/rust/src/http2/detect.rs index 99261adc4c..1c595a0cb0 100644 --- a/rust/src/http2/detect.rs +++ b/rust/src/http2/detect.rs @@ -613,13 +613,28 @@ fn http2_lower(value: &[u8]) -> Option> { } // returns a tuple with the value and its size -fn http2_normalize_host(value: &[u8]) -> (Option>, usize) { - match value.iter().position(|&x| x == b':') { +fn http2_normalize_host(value: &[u8]) -> &[u8] { + match value.iter().position(|&x| x == b'@') { Some(i) => { - return (http2_lower(&value[..i]), i); + let value = &value[i+1..]; + match value.iter().position(|&x| x == b':') { + Some(i) => { + return &value[..i]; + } + None => { + return value; + } + } } None => { - return (http2_lower(value), value.len()); + match value.iter().position(|&x| x == b':') { + Some(i) => { + return &value[..i]; + } + None => { + return value; + } + } } } } @@ -632,7 +647,7 @@ pub unsafe extern "C" fn rs_http2_tx_get_host_norm( let r = http2_normalize_host(value); // r is a tuple with the value and its size // this is useful when we only take a substring (before the port) - match r.0 { + match http2_lower(r) { Some(normval) => { // In case we needed some normalization, // the transaction needs to take ownership of this normalized host @@ -640,12 +655,12 @@ pub unsafe extern "C" fn rs_http2_tx_get_host_norm( let idx = tx.escaped.len() - 1; let resvalue = &tx.escaped[idx]; *buffer = resvalue.as_ptr(); //unsafe - *buffer_len = r.1 as u32; + *buffer_len = resvalue.len() as u32; return 1; } None => { - *buffer = value.as_ptr(); //unsafe - *buffer_len = r.1 as u32; + *buffer = r.as_ptr(); //unsafe + *buffer_len = r.len() as u32; return 1; } } @@ -1008,32 +1023,19 @@ mod tests { fn test_http2_normalize_host() { let buf0 = "aBC.com:1234".as_bytes(); let r0 = http2_normalize_host(buf0); - match r0.0 { - Some(r) => { - assert_eq!(r, "abc.com".as_bytes().to_vec()); - } - None => { - panic!("Result should not have been None"); - } - } + assert_eq!(r0, "aBC.com".as_bytes().to_vec()); let buf1 = "oisf.net".as_bytes(); let r1 = http2_normalize_host(buf1); - match r1.0 { - Some(r) => { - panic!("Result should not have been None, not {:?}", r); - } - None => {} - } - assert_eq!(r1.1, "oisf.net".len()); + assert_eq!(r1, "oisf.net".as_bytes().to_vec()); let buf2 = "localhost:3000".as_bytes(); let r2 = http2_normalize_host(buf2); - match r2.0 { - Some(r) => { - panic!("Result should not have been None, not {:?}", r); - } - None => {} - } - assert_eq!(r2.1, "localhost".len()); + assert_eq!(r2, "localhost".as_bytes().to_vec()); + let buf3 = "user:pass@localhost".as_bytes(); + let r3 = http2_normalize_host(buf3); + assert_eq!(r3, "localhost".as_bytes().to_vec()); + let buf4 = "user:pass@localhost:123".as_bytes(); + let r4 = http2_normalize_host(buf4); + assert_eq!(r4, "localhost".as_bytes().to_vec()); } #[test]