Skip to content

Commit

Permalink
safeguard link conversion from URL to Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
uerceg committed May 12, 2021
1 parent f8f5ef9 commit 93d6997
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public static void resolveLink(final String url,
}

if (! urlMatchesSuffix(originalURL.getHost(), resolveUrlSuffixArray)) {
adjustLinkResolutionCallback.resolvedLinkCallback(Uri.parse(originalURL.toString()));
adjustLinkResolutionCallback.resolvedLinkCallback(
AdjustLinkResolution.convertToUri(originalURL));
return;
}

Expand Down Expand Up @@ -78,19 +79,22 @@ private static void resolveLink(
{
// return (possible null) previous url when the current one does not exist
if (responseUrl == null) {
adjustLinkResolutionCallback.resolvedLinkCallback(Uri.parse(previousUrl.toString()));
adjustLinkResolutionCallback.resolvedLinkCallback(
AdjustLinkResolution.convertToUri(previousUrl));
return;
}

// return found url with expected host
if (isTerminalUrl(responseUrl.getHost())) {
adjustLinkResolutionCallback.resolvedLinkCallback(Uri.parse(responseUrl.toString()));
adjustLinkResolutionCallback.resolvedLinkCallback(
AdjustLinkResolution.convertToUri(responseUrl));
return;
}

// return previous (non-null) url when it reached the max number of recursive tries
if (recursionNumber > maxRecursions) {
adjustLinkResolutionCallback.resolvedLinkCallback(Uri.parse(responseUrl.toString()));
adjustLinkResolutionCallback.resolvedLinkCallback(
AdjustLinkResolution.convertToUri(responseUrl));
return;
}

Expand Down Expand Up @@ -172,4 +176,12 @@ private static URL convertToHttps(final URL urlToConvert) {

return convertedUrl;
}

private static Uri convertToUri(URL url) {
if (url == null) {
return null;
}

return Uri.parse(url.toString());
}
}

0 comments on commit 93d6997

Please sign in to comment.