From e866bfaa25c318bda88aea7f7dc8bbb3eea4c065 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 17 Aug 2021 08:44:44 +0200 Subject: [PATCH] Check if link title has valid parent domain --- .../faircode/email/FragmentDialogOpenLink.java | 8 +++++--- .../main/java/eu/faircode/email/UriHelper.java | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java index d420576e67..447f9630ac 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java @@ -171,9 +171,11 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { // Process title final Uri uriTitle; - if (title != null && PatternsCompat.WEB_URL.matcher(title).matches()) - uriTitle = Uri.parse(title.contains("://") ? title : "http://" + title); - else + if (title != null && PatternsCompat.WEB_URL.matcher(title).matches()) { + Uri u = Uri.parse(title.contains("://") ? title : "http://" + title); + String host = u.getHost(); // Capture1.PNG + uriTitle = (UriHelper.hasParentDomain(context, host) ? u : null); + } else uriTitle = null; // Get views diff --git a/app/src/main/java/eu/faircode/email/UriHelper.java b/app/src/main/java/eu/faircode/email/UriHelper.java index 45764ba112..530f62d257 100644 --- a/app/src/main/java/eu/faircode/email/UriHelper.java +++ b/app/src/main/java/eu/faircode/email/UriHelper.java @@ -26,6 +26,7 @@ import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashSet; +import java.util.Locale; public class UriHelper { // https://publicsuffix.org/ @@ -37,14 +38,23 @@ public class UriHelper { static String getParentDomain(Context context, String host) { if (host == null) return null; + String parent = _getSuffix(context, host); + return (parent == null ? host : parent); + } + static boolean hasParentDomain(Context context, String host) { + return (host != null && _getSuffix(context, host) != null); + } + + private static String _getSuffix(Context context, String host) { ensureSuffixList(context); - String h = host; + String h = host.toLowerCase(Locale.ROOT); while (true) { int dot = h.indexOf('.'); if (dot < 0) - return host; + return null; + String prefix = h.substring(0, dot); h = h.substring(dot + 1); @@ -52,8 +62,8 @@ public class UriHelper { String w = (d < 0 ? null : '*' + h.substring(d)); synchronized (suffixList) { - if ((suffixList.contains(h) || suffixList.contains(w)) && - !suffixList.contains('!' + h)) { + if (!suffixList.contains('!' + h) && + (suffixList.contains(h) || suffixList.contains(w))) { String parent = prefix + "." + h; Log.d("Host=" + host + " parent=" + parent); return parent;