| 738 | } |
| 739 | |
| 740 | char *db_url_escape(const str *url) |
| 741 | { |
| 742 | static str buf; |
| 743 | char *at, *slash, *scn; |
| 744 | str upw; |
| 745 | |
| 746 | if (!url || !url->s) |
| 747 | return NULL; |
| 748 | |
| 749 | if (pkg_str_extend(&buf, url->len + 6 + 1) < 0) { |
| 750 | LM_ERR("oom\n"); |
| 751 | return NULL; |
| 752 | } |
| 753 | |
| 754 | /* if there's no '@' sign, the URL has no password */ |
| 755 | at = q_memchr(url->s, '@', url->len); |
| 756 | if (!at) |
| 757 | goto url_is_safe; |
| 758 | |
| 759 | /* locate the end of the scheme (typical start for the user:password) */ |
| 760 | slash = q_memchr(url->s, '/', url->len); |
| 761 | if (!slash || slash >= at) |
| 762 | goto url_is_safe; |
| 763 | |
| 764 | upw.s = slash; |
| 765 | upw.len = at - slash; |
| 766 | |
| 767 | /* if the semicolon is missing, the URL has no password (only username) */ |
| 768 | scn = q_memchr(upw.s, ':', upw.len); |
| 769 | if (!scn) |
| 770 | goto url_is_safe; |
| 771 | |
| 772 | sprintf(buf.s, "%.*s:xxxxxx@%.*s", (int)(scn - url->s), url->s, |
| 773 | (int)(url->len - (at - url->s) - 1), at + 1); |
| 774 | |
| 775 | return buf.s; |
| 776 | |
| 777 | url_is_safe: |
| 778 | sprintf(buf.s, "%.*s", url->len, url->s); |
| 779 | return buf.s; |
| 780 | } |
no test coverage detected