]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
http2: normalize host when there is user info
authorPhilippe Antoine <pantoine@oisf.net>
Thu, 9 Nov 2023 13:19:59 +0000 (14:19 +0100)
committerVictor Julien <victor@inliniac.net>
Tue, 14 Nov 2023 18:28:10 +0000 (19:28 +0100)
Ticket: 6479

rust/src/http2/detect.rs

index 99261adc4c41cbfc37f7f60aa5538bef5db2dd8d..1c595a0cb0f82001d8231b2bda84a09ca1687218 100644 (file)
@@ -613,13 +613,28 @@ fn http2_lower(value: &[u8]) -> Option<Vec<u8>> {
 }
 
 // returns a tuple with the value and its size
-fn http2_normalize_host(value: &[u8]) -> (Option<Vec<u8>>, 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]