From 8ca33edee9dfa76626272b03bee42dd41ad5dd43 Mon Sep 17 00:00:00 2001 From: andreinknv <261684394+andreinknv@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:04:06 -0500 Subject: [PATCH] fix(sip): keep `;` inside quoted parameter values RFC 3261 lets a parameter value be a quoted-string (gen-value), and a quoted-string may contain `;`. parse_params split at every `;`, so a Kamailio registrar that stores the observed flow answers a REGISTER over TCP or TLS with a Contact carrying `;received="sip:192.0.2.10:47522;transport=tls"`, which was split into `received="sip:192.0.2.10:47522` and `transport=tls"`. The second part failed as "unknown transport: tls\"", the whole response was refused and the stream connection closed, so the client never registered. UDP answers carry no `;transport=` in that value and were not affected. Split only outside quoted strings, honouring quoted-pairs, and refuse a quoted value that never closes. Add a regression test with a complete 200 OK. --- src/sip/headers/typed/contact.rs | 43 ++++++++++++++++++++++++++++++++ src/sip/uri.rs | 33 +++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/sip/headers/typed/contact.rs b/src/sip/headers/typed/contact.rs index 1181a145..2f4119c1 100644 --- a/src/sip/headers/typed/contact.rs +++ b/src/sip/headers/typed/contact.rs @@ -253,6 +253,49 @@ mod tests { assert!(contacts[0].params.iter().any(|param| matches!(param, Param::Other(name, Some(value)) if name.value().eq_ignore_ascii_case("+sip.instance") && value.value() == "\"\""))); } + #[test] + fn quoted_contact_params_may_contain_semicolons() { + // A Kamailio registrar with `received_avp` answers a REGISTER over + // TCP or TLS with the observed flow as a quoted URI, which has its + // own `;transport=` (RFC 3261 contact-extension / gen-value). + let response: Response = concat!( + "SIP/2.0 200 OK\r\n", + "Via: SIP/2.0/TLS 192.0.2.10:47522;rport=47522;received=192.0.2.10;alias;branch=z9hG4bK-test\r\n", + "From: ;tag=from-tag\r\n", + "To: ;tag=to-tag\r\n", + "CSeq: 2 REGISTER\r\n", + "Call-ID: test-call-id@example.com\r\n", + "Contact: ;q=1;expires=600;received=\"sip:192.0.2.10:47522;transport=tls\";+sip.instance=\"\";reg-id=1\r\n", + "Content-Length: 0\r\n", + "\r\n" + ) + .try_into() + .unwrap(); + + let contacts = response.typed_contact_headers().unwrap(); + assert_eq!(contacts.len(), 1); + let contact = &contacts[0]; + assert_eq!(contact.expires(), Some(600)); + let received = contact.params.iter().find_map(|param| match param { + Param::Received(value) => Some(value.value()), + _ => None, + }); + assert_eq!(received, Some("\"sip:192.0.2.10:47522;transport=tls\"")); + let reg_id = contact.params.iter().find_map(|param| match param { + Param::Other(name, Some(value)) if name.value() == "reg-id" => Some(value.value()), + _ => None, + }); + assert_eq!(reg_id, Some("1")); + + // A quoted value that never closes is still refused. + let unterminated = ";received=\"sip:192.0.2.1;transport=tls"; + assert!(Contact::parse(unterminated).is_err()); + + // The parsed header prints back to the same header. + let printed = contact.to_string(); + assert_eq!(&Contact::parse(&printed).unwrap(), contact); + } + #[test] fn record_style_contact_without_brackets_splits_header_params() { let contact = Contact::parse("sip:alice@restsend.com;expires=120").unwrap(); diff --git a/src/sip/uri.rs b/src/sip/uri.rs index 6b506cf2..76d9135e 100644 --- a/src/sip/uri.rs +++ b/src/sip/uri.rs @@ -714,6 +714,37 @@ fn split_at_first_semicolon(s: &str) -> (&str, Option<&str>) { } } +/// Split parameters at `;` outside quoted strings. RFC 3261 `gen-value` +/// may be a `quoted-string`, which may contain `;`: a registrar's +/// `received="sip:192.0.2.1:5061;transport=tls"` Contact parameter is one +/// parameter, not two. +fn split_unquoted_semicolons(s: &str) -> Result, Error> { + let mut parts = Vec::new(); + let mut start = 0; + let mut in_quotes = false; + let mut escaped = false; + for (i, ch) in s.char_indices() { + if escaped { + escaped = false; + continue; + } + match ch { + '\\' if in_quotes => escaped = true, + '"' => in_quotes = !in_quotes, + ';' if !in_quotes => { + parts.push(&s[start..i]); + start = i + 1; + } + _ => {} + } + } + if in_quotes || escaped { + return Err(Error::ParseError("unterminated quoted parameter".into())); + } + parts.push(&s[start..]); + Ok(parts) +} + pub fn parse_params(s: &str) -> Result, Error> { let s = s.trim(); if s.is_empty() { @@ -724,7 +755,7 @@ pub fn parse_params(s: &str) -> Result, Error> { return Err(Error::ParseError("empty parameter".into())); } let mut params = Vec::new(); - for part in s.split(';') { + for part in split_unquoted_semicolons(s)? { let part = part.trim(); if part.is_empty() { return Err(Error::ParseError("empty parameter".into()));