From 0d85bc39cfdceb0e61401545ff9bf9e2250f29bb Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 7 May 2020 03:21:05 +0300 Subject: [PATCH] Use case-insensitive scheme comparison in check_url. GitOrigin-RevId: d5c9cb66c49194d5264b63871aaabcc6768aaac4 --- td/telegram/misc.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/td/telegram/misc.cpp b/td/telegram/misc.cpp index f99e222d9..80457bd8d 100644 --- a/td/telegram/misc.cpp +++ b/td/telegram/misc.cpp @@ -305,13 +305,25 @@ string get_emoji_fingerprint(uint64 num) { return emojis[static_cast((num & 0x7FFFFFFFFFFFFFFF) % emojis.size())].str(); } +static bool tolower_begins_with(Slice str, Slice prefix) { + if (prefix.size() > str.size()) { + return false; + } + for (size_t i = 0; i < prefix.size(); i++) { + if (to_lower(str[i]) != prefix[i]) { + return false; + } + } + return true; +} + Result check_url(Slice url) { bool is_tg = false; bool is_ton = false; - if (begins_with(url, "tg:")) { + if (tolower_begins_with(url, "tg:")) { url.remove_prefix(3); is_tg = true; - } else if (begins_with(url, "ton:")) { + } else if (tolower_begins_with(url, "ton:")) { url.remove_prefix(4); is_ton = true; } @@ -320,8 +332,8 @@ Result check_url(Slice url) { } TRY_RESULT(http_url, parse_url(url)); if (is_tg || is_ton) { - if (begins_with(url, "http://") || http_url.protocol_ == HttpUrl::Protocol::HTTPS || !http_url.userinfo_.empty() || - http_url.specified_port_ != 0 || http_url.is_ipv6_) { + if (tolower_begins_with(url, "http://") || http_url.protocol_ == HttpUrl::Protocol::HTTPS || + !http_url.userinfo_.empty() || http_url.specified_port_ != 0 || http_url.is_ipv6_) { return Status::Error(is_tg ? Slice("Wrong tg URL") : Slice("Wrong ton URL")); }