diff --git a/crates/pcap/src/lib.rs b/crates/pcap/src/lib.rs index f5203f002..5ba79249b 100644 --- a/crates/pcap/src/lib.rs +++ b/crates/pcap/src/lib.rs @@ -1176,7 +1176,10 @@ fn dns(msg: &[u8], d: &mut Decoded, label: &'static str, mdns_wire_format: bool) d.dns = Some(out); } -const METHODS: &[&str] = &["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH", "CONNECT", "TRACE"]; +/// HTTP method names are case-sensitive tokens, including extension methods. +fn http_method(method: &str) -> bool { + !method.is_empty() && method.bytes().all(|b| b.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&b)) +} /// Remove `user:password@` from an absolute request target. pub fn strip_userinfo(target: &str) -> String { @@ -1209,7 +1212,7 @@ fn tcp_app(p: &[u8], d: &mut Decoded) { return; } if let (Some(m), Some(t), Some(v)) = fields - && METHODS.contains(&m) + && http_method(m) && !t.is_empty() && !t.bytes().any(|b| b.is_ascii_control()) && version(v) diff --git a/crates/pcap/tests/pcap_http_methods.rs b/crates/pcap/tests/pcap_http_methods.rs new file mode 100644 index 000000000..d2bc17062 --- /dev/null +++ b/crates/pcap/tests/pcap_http_methods.rs @@ -0,0 +1,32 @@ +use craft_anything_formats::pcap::decode; +use craft_anything_testkit::pcap::{ipv4_pkt, tcp}; + +/// WebDAV and extension methods retain the same HTTP evidence as core methods. +#[test] +fn extension_methods_are_http_requests() { + for method in ["PROPFIND", "MKCOL", "LOCK", "REPORT", "M-SEARCH", "custom", "X!#$%&'*+-.^_`|~012"] { + let request = format!("{method} /collection/ HTTP/1.1\r\nHost: dav.example\r\n\r\n"); + let decoded = decode(101, &ipv4_pkt(6, [1; 4], [2; 4], &tcp(100, 80, 0x18, request.as_bytes()))); + let http = decoded.http.unwrap_or_else(|| panic!("missing HTTP: {method}")); + assert_eq!(http.method.as_deref(), Some(method)); + assert_eq!(http.target.as_deref(), Some("/collection/")); + assert_eq!(http.host.as_deref(), Some("dav.example")); + } +} + +/// A method still needs a nonempty ASCII token and a complete valid start line. +#[test] +fn invalid_extension_method_lines_are_not_http() { + for request in [ + " / HTTP/1.1\r\n", + "BAD:METHOD / HTTP/1.1\r\n", + "BAD/METHOD / HTTP/1.1\r\n", + "MÉTHOD / HTTP/1.1\r\n", + "BAD\tMETHOD / HTTP/1.1\r\n", + "PROPFIND / HTTP/1.1", + "PROPFIND / HTTP/no\r\n", + ] { + let decoded = decode(101, &ipv4_pkt(6, [1; 4], [2; 4], &tcp(100, 80, 0x18, request.as_bytes()))); + assert!(decoded.http.is_none(), "{request:?}"); + } +}