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

Commit

Permalink
Fix validation failures of URIs with multiple hash signs. (#3609)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored and bluemarvin committed Jul 2, 2020
1 parent 3fb8478 commit e7c8640
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.mozilla.vrbrowser.utils.UrlUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
Expand Down Expand Up @@ -108,7 +109,7 @@ public static void stopPageLoadTimeWithURI(String uri) {
}

try {
URI uriLink = URI.create(uri);
URI uriLink = UrlUtils.parseUri(uri);
if (uriLink.getHost() == null) {
return;
}
Expand All @@ -117,7 +118,7 @@ public static void stopPageLoadTimeWithURI(String uri) {
Url.INSTANCE.domains().add();
}
Url.INSTANCE.visits().add();
} catch (IllegalArgumentException e) {
} catch (URISyntaxException e) {
Log.e(LOGTAG, "Invalid URL", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.mozilla.vrbrowser.utils.UrlUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -307,7 +308,7 @@ public static void uploadPageLoadToHistogram(String uri) {
}

try {
URI uriLink = URI.create(uri);
URI uriLink = UrlUtils.parseUri(uri);
if (uriLink.getHost() == null) {
return;
}
Expand All @@ -332,7 +333,7 @@ public static void uploadPageLoadToHistogram(String uri) {

loadingTimeHistogram[histogramLoadIndex]++;

} catch (IllegalArgumentException e) {
} catch (URISyntaxException e) {
Log.e(LOGTAG, "Invalid URL", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public static boolean contains(String[] aTarget, String aText) {
return false;
}

public static long charCount(@NonNull String aString, char target) {
return aString.chars().filter(ch -> ch == target).count();
}

/**
* The version code is composed like: yDDDHHmm
* * y = Double digit year, with 16 substracted: 2017 -> 17 -> 1
Expand Down
20 changes: 18 additions & 2 deletions app/src/common/shared/org/mozilla/vrbrowser/utils/UrlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.regex.Pattern;

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

} else {
try {
URI uri = URI.create(aUri);
URI uri = parseUri(aUri);
URL url = new URL(
uri.getScheme() != null ? uri.getScheme() : "",
uri.getAuthority() != null ? uri.getAuthority() : "",
"");
return url.toString();

} catch (MalformedURLException | IllegalArgumentException e) {
} catch (MalformedURLException | URISyntaxException e) {
return "";
}
}
Expand Down Expand Up @@ -197,4 +198,19 @@ public static String getHost(String uri) {
return uri;
}
}

public static URI parseUri(String aUri) throws URISyntaxException {
try {
return new URI(aUri);
} catch (URISyntaxException e) {
if (!StringUtils.isEmpty(aUri) && StringUtils.charCount(aUri, '#') >= 2) {
// Browsers are able to handle URLs with double # by ignoring everything after the
// second # occurrence. But Java implementation considers it an invalid URL.
// Remove everything after the second #.
int index = aUri.indexOf("#", aUri.indexOf("#") + 1);
return parseUri(aUri.substring(0, index));
}
throw e;
}
}
}

0 comments on commit e7c8640

Please sign in to comment.