Skip to content

Commit

Permalink
uric/contact: fix display name and contact header uri escaping (#762)
Browse files Browse the repository at this point in the history
* uric: fix display name uri escaping

In display names, LWS may not be escaped according to RFC 3261 section
25.1.

* contact: escape Contact header SIP URI

* uri,contact: improve uri escaping of user part

* dialog: do not escape display name in FROM header

The display name is sent as a quoted-string which must not be escaped.
Further, basically all UTF-8 characters are allowed in display names,
so not checking it should be fine for almost all cases.

* uric: remove unused functions
  • Loading branch information
maximilianfridrich authored May 2, 2023
1 parent 394b3c5 commit 5aaffb6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 47 deletions.
1 change: 1 addition & 0 deletions include/re_uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ int uri_param_unescape(struct re_printf *pf, const struct pl *pl);
int uri_header_escape(struct re_printf *pf, const struct pl *pl);
int uri_header_unescape(struct re_printf *pf, const struct pl *pl);
int uri_display_name_escape(struct re_printf *pf, const struct pl *pl);
int uri_escape_user(struct re_printf *pf, const char *user);
int uri_escape(struct re_printf *pf, const char *uri);
int uri_escape_pl(struct re_printf *pf, const struct pl *pl);
int uri_unescape_pl(struct re_printf *pf, const struct pl *pl);
12 changes: 8 additions & 4 deletions src/sip/contact.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ int sip_contact_print(struct re_printf *pf, const struct sip_contact *contact)
if (!contact)
return 0;

if (contact->uri && strchr(contact->uri, ':'))
return re_hprintf(pf, "Contact: <%s>\r\n", contact->uri);
else
return re_hprintf(pf, "Contact: <sip:%s@%J%s>\r\n",
if (contact->uri && strchr(contact->uri, ':')) {
return re_hprintf(pf, "Contact: <%H>\r\n", uri_escape,
contact->uri);
}
else {
return re_hprintf(pf, "Contact: <sip:%H@%J%s>\r\n",
uri_escape_user,
contact->uri,
contact->addr,
sip_transp_param(contact->tp));
}
}
4 changes: 1 addition & 3 deletions src/sip/dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ int sip_dialog_alloc(struct sip_dialog **dlgp,
dlg->cpos = dlg->mb->pos;
err |= mbuf_printf(dlg->mb, "From: ");
if (from_name) {
pl_set_str(&pl, from_name);
err |= mbuf_printf(dlg->mb, "\"%H\" ", uri_display_name_escape,
&pl);
err |= mbuf_printf(dlg->mb, "\"%s\" ", from_name);
}
err |= mbuf_printf(dlg->mb, "<%H>", uri_escape, from_uri);
err |= mbuf_printf(dlg->mb, ";tag=%016llx\r\n", ltag);
Expand Down
16 changes: 16 additions & 0 deletions src/uri/uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ int uri_headers_apply(const struct pl *pl, uri_apply_h *ah, void *arg)
}


/**
* Escape user part of SIP URI
*
* @param pf Print function to encode into
* @param user User part of SIP URI
*
* @return 0 if success, otherwise errorcode
*/
int uri_escape_user(struct re_printf *pf, const char *user)
{
struct pl pl;
pl_set_str(&pl, user);
return uri_user_escape(pf, &pl);
}


static int uri_escape_helper(struct re_printf *pf, const struct pl *pl,
bool unescape)
{
Expand Down
40 changes: 0 additions & 40 deletions src/uri/uric.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,33 +120,6 @@ static bool is_param_unreserved(char c)
}


static bool is_token_non_alphanum(int c)
{
switch (c) {

case '-':
case '.':
case '!':
case '%':
case '*':
case '_':
case '+':
case '`':
case '\'':
case '~':
return true;
default:
return false;
}
}


static bool is_token(char c)
{
return isalnum((unsigned char)c) || is_token_non_alphanum(c);
}


static bool is_paramchar(char c)
{
return is_param_unreserved(c) || is_unreserved(c);
Expand Down Expand Up @@ -332,16 +305,3 @@ int uri_header_unescape(struct re_printf *pf, const struct pl *pl)
return comp_unescape(pf, pl, is_hvalue);
}


/**
* Escape display name
*
* @param pf Print function
* @param pl String to escape
*
* @return 0 if success, otherwise errorcode
*/
int uri_display_name_escape(struct re_printf *pf, const struct pl *pl)
{
return comp_escape(pf, pl, is_token);
}

0 comments on commit 5aaffb6

Please sign in to comment.