Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 9cdc4df

Browse files
committed
Fix validation failures of URIs with multiple hash signs.
1 parent 8c8b8f8 commit 9cdc4df

4 files changed

Lines changed: 29 additions & 6 deletions

File tree

app/src/common/shared/org/mozilla/vrbrowser/telemetry/GleanMetricsService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.mozilla.vrbrowser.utils.UrlUtils;
2727

2828
import java.net.URI;
29+
import java.net.URISyntaxException;
2930
import java.util.HashMap;
3031
import java.util.HashSet;
3132
import java.util.Hashtable;
@@ -108,7 +109,7 @@ public static void stopPageLoadTimeWithURI(String uri) {
108109
}
109110

110111
try {
111-
URI uriLink = URI.create(uri);
112+
URI uriLink = UrlUtils.parseUri(uri);
112113
if (uriLink.getHost() == null) {
113114
return;
114115
}
@@ -117,7 +118,7 @@ public static void stopPageLoadTimeWithURI(String uri) {
117118
Url.INSTANCE.domains().add();
118119
}
119120
Url.INSTANCE.visits().add();
120-
} catch (IllegalArgumentException e) {
121+
} catch (URISyntaxException e) {
121122
Log.e(LOGTAG, "Invalid URL", e);
122123
}
123124
}

app/src/common/shared/org/mozilla/vrbrowser/telemetry/TelemetryWrapper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.mozilla.telemetry.schedule.jobscheduler.JobSchedulerTelemetryScheduler;
2222
import org.mozilla.telemetry.serialize.JSONPingSerializer;
2323
import org.mozilla.telemetry.storage.FileTelemetryStorage;
24+
import org.mozilla.thirdparty.com.google.android.exoplayer2.util.UriUtil;
2425
import org.mozilla.vrbrowser.BuildConfig;
2526
import org.mozilla.vrbrowser.R;
2627
import org.mozilla.vrbrowser.browser.SettingsStore;
@@ -31,6 +32,7 @@
3132
import org.mozilla.vrbrowser.utils.UrlUtils;
3233

3334
import java.net.URI;
35+
import java.net.URISyntaxException;
3436
import java.util.HashMap;
3537
import java.util.HashSet;
3638
import java.util.Map;
@@ -307,7 +309,7 @@ public static void uploadPageLoadToHistogram(String uri) {
307309
}
308310

309311
try {
310-
URI uriLink = URI.create(uri);
312+
URI uriLink = UrlUtils.parseUri(uri);
311313
if (uriLink.getHost() == null) {
312314
return;
313315
}
@@ -332,7 +334,7 @@ public static void uploadPageLoadToHistogram(String uri) {
332334

333335
loadingTimeHistogram[histogramLoadIndex]++;
334336

335-
} catch (IllegalArgumentException e) {
337+
} catch (URISyntaxException e) {
336338
Log.e(LOGTAG, "Invalid URL", e);
337339
}
338340
}

app/src/common/shared/org/mozilla/vrbrowser/utils/StringUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public static boolean contains(String[] aTarget, String aText) {
7676
return false;
7777
}
7878

79+
public static long charCount(@NonNull String aString, char target) {
80+
return aString.chars().filter(ch -> ch == target).count();
81+
}
82+
7983
/**
8084
* The version code is composed like: yDDDHHmm
8185
* * y = Double digit year, with 16 substracted: 2017 -> 17 -> 1

app/src/common/shared/org/mozilla/vrbrowser/utils/UrlUtils.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.File;
2323
import java.net.MalformedURLException;
2424
import java.net.URI;
25+
import java.net.URISyntaxException;
2526
import java.net.URL;
2627
import java.util.regex.Pattern;
2728

@@ -139,14 +140,14 @@ public static String titleBarUrl(@Nullable String aUri) {
139140

140141
} else {
141142
try {
142-
URI uri = URI.create(aUri);
143+
URI uri = parseUri(aUri);
143144
URL url = new URL(
144145
uri.getScheme() != null ? uri.getScheme() : "",
145146
uri.getAuthority() != null ? uri.getAuthority() : "",
146147
"");
147148
return url.toString();
148149

149-
} catch (MalformedURLException | IllegalArgumentException e) {
150+
} catch (MalformedURLException | URISyntaxException e) {
150151
return "";
151152
}
152153
}
@@ -202,6 +203,21 @@ public static String getHost(String uri) {
202203
}
203204
}
204205

206+
public static URI parseUri(String aUri) throws URISyntaxException {
207+
try {
208+
return new URI(aUri);
209+
} catch (URISyntaxException e) {
210+
if (!StringUtils.isEmpty(aUri) && StringUtils.charCount(aUri, '#') >= 2) {
211+
// Browsers are able to handle URLs with double # by ignoring everything after the
212+
// second # occurrence. But Java implementation considers it an invalid URL.
213+
// Remove everything after the second #.
214+
int index = aUri.indexOf("#", aUri.indexOf("#") + 1);
215+
return parseUri(aUri.substring(0, index));
216+
}
217+
throw e;
218+
}
219+
}
220+
205221
public static String urlForText(@NonNull Context context, @NonNull String text) {
206222
String url = text.trim();
207223
if ((UrlUtils.isDomain(text) || UrlUtils.isIPUri(text)) && !text.contains(" ")) {

0 commit comments

Comments
 (0)