Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/sip/headers/typed/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() == "\"<urn:uuid:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee>\"")));
}

#[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: <sip:1002@example.com>;tag=from-tag\r\n",
"To: <sip:1002@example.com>;tag=to-tag\r\n",
"CSeq: 2 REGISTER\r\n",
"Call-ID: test-call-id@example.com\r\n",
"Contact: <sip:1002@192.0.2.10:47522;transport=TLS>;q=1;expires=600;received=\"sip:192.0.2.10:47522;transport=tls\";+sip.instance=\"<urn:uuid:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee>\";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 = "<sip:bob@example.com>;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();
Expand Down
33 changes: 32 additions & 1 deletion src/sip/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<&str>, 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<Vec<Param>, Error> {
let s = s.trim();
if s.is_empty() {
Expand All @@ -724,7 +755,7 @@ pub fn parse_params(s: &str) -> Result<Vec<Param>, 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()));
Expand Down
Loading