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

Commit e7c8640

Browse files
MortimerGorobluemarvin
authored andcommitted
Fix validation failures of URIs with multiple hash signs. (#3609)
1 parent 3fb8478 commit e7c8640

4 files changed

Lines changed: 28 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.mozilla.vrbrowser.utils.UrlUtils;
3232

3333
import java.net.URI;
34+
import java.net.URISyntaxException;
3435
import java.util.HashMap;
3536
import java.util.HashSet;
3637
import java.util.Map;
@@ -307,7 +308,7 @@ public static void uploadPageLoadToHistogram(String uri) {
307308
}
308309

309310
try {
310-
URI uriLink = URI.create(uri);
311+
URI uriLink = UrlUtils.parseUri(uri);
311312
if (uriLink.getHost() == null) {
312313
return;
313314
}
@@ -332,7 +333,7 @@ public static void uploadPageLoadToHistogram(String uri) {
332333

333334
loadingTimeHistogram[histogramLoadIndex]++;
334335

335-
} catch (IllegalArgumentException e) {
336+
} catch (URISyntaxException e) {
336337
Log.e(LOGTAG, "Invalid URL", e);
337338
}
338339
}

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
@@ -18,6 +18,7 @@
1818
import java.io.File;
1919
import java.net.MalformedURLException;
2020
import java.net.URI;
21+
import java.net.URISyntaxException;
2122
import java.net.URL;
2223
import java.util.regex.Pattern;
2324

@@ -135,14 +136,14 @@ public static String titleBarUrl(@Nullable String aUri) {
135136

136137
} else {
137138
try {
138-
URI uri = URI.create(aUri);
139+
URI uri = parseUri(aUri);
139140
URL url = new URL(
140141
uri.getScheme() != null ? uri.getScheme() : "",
141142
uri.getAuthority() != null ? uri.getAuthority() : "",
142143
"");
143144
return url.toString();
144145

145-
} catch (MalformedURLException | IllegalArgumentException e) {
146+
} catch (MalformedURLException | URISyntaxException e) {
146147
return "";
147148
}
148149
}
@@ -197,4 +198,19 @@ public static String getHost(String uri) {
197198
return uri;
198199
}
199200
}
201+
202+
public static URI parseUri(String aUri) throws URISyntaxException {
203+
try {
204+
return new URI(aUri);
205+
} catch (URISyntaxException e) {
206+
if (!StringUtils.isEmpty(aUri) && StringUtils.charCount(aUri, '#') >= 2) {
207+
// Browsers are able to handle URLs with double # by ignoring everything after the
208+
// second # occurrence. But Java implementation considers it an invalid URL.
209+
// Remove everything after the second #.
210+
int index = aUri.indexOf("#", aUri.indexOf("#") + 1);
211+
return parseUri(aUri.substring(0, index));
212+
}
213+
throw e;
214+
}
215+
}
200216
}

0 commit comments

Comments
 (0)