Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix(ui): clickable links that dont start with https:// or www.

Zack Bartel26f1af506adf
diff --git a/src/core/include/tether/psl.hpp b/src/core/include/tether/psl.hpp
new file mode 100644
index 0000000..d0fbd61
--- /dev/null
+++ b/src/core/include/tether/psl.hpp
@@ -0,0 +1,10 @@
+#pragma once
+
+#include <string>
+
+namespace tether {
+
+ // True when label is a top-level domain on the Public Suffix List, case-insensitively.
+ bool is_known_tld(const std::string& label);
+
+} // namespace tether
diff --git a/src/core/src/otp.cpp b/src/core/src/otp.cpp
index 5460b58..6c6532f 100644
--- a/src/core/src/otp.cpp
+++ b/src/core/src/otp.cpp
@@ -1,4 +1,5 @@
#include "tether/otp.hpp"
+#include "tether/psl.hpp"
#include <algorithm>
#include <cctype>
@@ -179,6 +180,12 @@ namespace tether {
}
} // namespace
+ bool is_known_tld(const std::string& label) {
+ const std::string tld = normalize_domain(label);
+ const PslSets& sets = psl_sets();
+ return !tld.empty() && (sets.normal.count(tld) != 0 || sets.wildcard.count(tld) != 0);
+ }
+
uint64_t otp_store(const std::string& code, const std::string& sender_domain, OtpClock::time_point now) {
std::lock_guard<std::mutex> lock(g_mutex);
if (code.empty()) {
diff --git a/src/ui_common/message_format.cpp b/src/ui_common/message_format.cpp
index 77b3724..873959f 100644
--- a/src/ui_common/message_format.cpp
+++ b/src/ui_common/message_format.cpp
@@ -4,6 +4,7 @@
#include <glib.h>
#include <regex>
#include <tether/i18n.hpp>
+#include <tether/psl.hpp>
namespace tether::ui {
@@ -54,7 +55,8 @@ namespace tether::ui {
} // namespace
std::string linkify_markup(const std::string& body) {
- static const std::regex pattern(R"((https?://|www\.)[^\s<>"']+)", std::regex::icase);
+ static const std::regex pattern(R"(((https?://|www\.)[^\s<>"']+)|[a-z0-9-]+(\.[a-z0-9-]+)+(/[^\s<>"']*)?)",
+ std::regex::icase);
std::string out;
auto begin = std::sregex_iterator(body.begin(), body.end(), pattern);
@@ -70,9 +72,21 @@ namespace tether::ui {
url.resize(keep);
const size_t at = static_cast<size_t>(match.position());
+ std::string href;
+ if (match[1].matched) {
+ // bare www. host is not a URI
+ href = (url.compare(0, 4, "www.") == 0) ? "http://" + url : url;
+ } else {
+ // bare host linkify only a real tld and not an email address
+ const std::string host = url.substr(0, url.find('/'));
+ const char before = at > 0 ? body[at - 1] : ' ';
+ const size_t after = at + match.length();
+ if (before == '@' || before == '.' || before == '/' || before == '_' ||
+ (after < body.size() && body[after] == '@') || !is_known_tld(host.substr(host.rfind('.') + 1)))
+ continue;
+ href = "https://" + url;
+ }
out += escape(body.substr(cursor, at - cursor));
- // bare www. host is not a URI
- const std::string href = (url.compare(0, 4, "www.") == 0) ? "http://" + url : url;
out += "<a href=\"" + escape(href) + "\">" + escape(url) + "</a>";
cursor = at + keep;
}
diff --git a/test/test_message_format.cpp b/test/test_message_format.cpp
index f3da83a..bfbdfe5 100644
--- a/test/test_message_format.cpp
+++ b/test/test_message_format.cpp
@@ -58,6 +58,23 @@ TEST(LinkifyMarkup, MultipleUrlsInOneBody) {
"<a href=\"http://a.test\">http://a.test</a> and <a href=\"http://b.test\">http://b.test</a>");
}
+TEST(LinkifyMarkup, BareDomainGetsHttps) {
+ EXPECT_EQ(linkify_markup("try github.com/zackb/tether."),
+ "try <a href=\"https://github.com/zackb/tether\">github.com/zackb/tether</a>.");
+ EXPECT_EQ(linkify_markup("news.ycombinator.com"),
+ "<a href=\"https://news.ycombinator.com\">news.ycombinator.com</a>");
+}
+
+TEST(LinkifyMarkup, NonTldDottedTextStaysText) {
+ EXPECT_EQ(linkify_markup("open file.txt"), "open file.txt");
+ EXPECT_EQ(linkify_markup("v1.2.3"), "v1.2.3");
+ EXPECT_EQ(linkify_markup("e.g. this"), "e.g. this");
+}
+
+TEST(LinkifyMarkup, EmailAddressIsNotLinked) {
+ EXPECT_EQ(linkify_markup("mail [email protected]"), "mail [email protected]");
+}
+
TEST_F(FixedZone, TodayIsAClockTime) { EXPECT_EQ(format_thread_time(NOW - 3600, NOW), "11:00"); }
TEST_F(FixedZone, EarlierTodayStaysAClockTime) {