Replace singleton with static methods.

Apologies for messing with code after the fact, but some OOPisms are just too
much for me to stomach. This should just be a function of two arguments. No
objects required.

Yes, we're now reparsing the whitelist on every call, but this function is
called only a handful of times.
This commit is contained in:
Michael Bayne
2018-09-14 14:26:07 -07:00
parent 64ce5c2e1c
commit 0548befe2b
4 changed files with 19 additions and 32 deletions
@@ -484,7 +484,7 @@ public class Application
String suffix = _trackingURLSuffix == null ? "" : _trackingURLSuffix; String suffix = _trackingURLSuffix == null ? "" : _trackingURLSuffix;
String ga = getGATrackingCode(); String ga = getGATrackingCode();
return _trackingURL == null ? null : return _trackingURL == null ? null :
HostWhitelist.INSTANCE.verify(new URL(_trackingURL + encodePath(event + suffix + ga))); HostWhitelist.verify(new URL(_trackingURL + encodePath(event + suffix + ga)));
} catch (MalformedURLException mue) { } catch (MalformedURLException mue) {
log.warning("Invalid tracking URL", "path", _trackingURL, "event", event, "error", mue); log.warning("Invalid tracking URL", "path", _trackingURL, "event", event, "error", mue);
return null; return null;
@@ -601,7 +601,7 @@ public class Application
latest = SysProps.replaceDomain(latest); latest = SysProps.replaceDomain(latest);
} }
try { try {
_latest = HostWhitelist.INSTANCE.verify(new URL(latest)); _latest = HostWhitelist.verify(new URL(latest));
} catch (MalformedURLException mue) { } catch (MalformedURLException mue) {
log.warning("Invalid URL for latest attribute.", mue); log.warning("Invalid URL for latest attribute.", mue);
} }
@@ -1409,7 +1409,7 @@ public class Application
throws MalformedURLException throws MalformedURLException
{ {
String url = version < 0 ? _appbase : _appbase.replace("%VERSION%", "" + version); String url = version < 0 ? _appbase : _appbase.replace("%VERSION%", "" + version);
return HostWhitelist.INSTANCE.verify(new URL(url)); return HostWhitelist.verify(new URL(url));
} }
/** /**
@@ -328,7 +328,7 @@ public class Config
} }
if (!StringUtil.isBlank(value)) { if (!StringUtil.isBlank(value)) {
try { try {
HostWhitelist.INSTANCE.verify(new URL(value)); HostWhitelist.verify(new URL(value));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
log.warning("Invalid URL.", "url", value, e); log.warning("Invalid URL.", "url", value, e);
value = null; value = null;
@@ -7,7 +7,6 @@ package com.threerings.getdown.util;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Collections;
import java.util.List; import java.util.List;
import com.threerings.getdown.data.Build; import com.threerings.getdown.data.Build;
@@ -20,49 +19,36 @@ import com.threerings.getdown.data.Build;
*/ */
public final class HostWhitelist public final class HostWhitelist
{ {
public static final HostWhitelist INSTANCE = new HostWhitelist(); /**
* Verifies that the specified URL should be accessible, per the built-in host whitelist.
private final List<String> _hosts; * See {@link Build#hostWhitelist()} and {@link #verify(List,URL)}.
*/
HostWhitelist () public static URL verify (URL url) throws MalformedURLException
{ {
this(Build.hostWhitelist()); return verify(Build.hostWhitelist(), url);
}
HostWhitelist (List<String> hosts)
{
_hosts = Collections.unmodifiableList(hosts);
} }
/** /**
* Verifies that the specified URL should be accessible, per this host whitelist. * Verifies that the specified URL should be accessible, per the supplied host whitelist.
* If the URL should not be accessible, this method throws a {@link MalformedURLException}. * If the URL should not be accessible, this method throws a {@link MalformedURLException}.
* If the URL should be accessible, this method simply returns the {@link URL} passed in. * If the URL should be accessible, this method simply returns the {@link URL} passed in.
*/ */
public final URL verify (URL url) throws MalformedURLException public static URL verify (List<String> hosts, URL url) throws MalformedURLException
{ {
if (url == null || _hosts.isEmpty()) { if (url == null || hosts.isEmpty()) {
// either there is no URL to validate or no whitelist was configured // either there is no URL to validate or no whitelist was configured
return url; return url;
} }
String urlHost = url.getHost(); String urlHost = url.getHost();
for (String host : hosts) {
for (String host : _hosts) {
String regex = host.replace(".", "\\.").replace("*", ".*"); String regex = host.replace(".", "\\.").replace("*", ".*");
if (urlHost.matches(regex)) { if (urlHost.matches(regex)) {
return url; return url;
} }
} }
throw new MalformedURLException("The host for the specified URL (" + url throw new MalformedURLException(
+ ") is not in the host whitelist: " + _hosts); "The host for the specified URL (" + url + ") is not in the host whitelist: " + hosts);
}
/** {@inheritDoc} */
@Override
public String toString()
{
return _hosts.toString();
} }
} }
@@ -10,6 +10,7 @@ import static org.junit.Assert.assertEquals;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.junit.Test; import org.junit.Test;
@@ -141,12 +142,12 @@ public class HostWhitelistTest
private static void checkCanVerify (String whitelist, String url, boolean expectedToPass) private static void checkCanVerify (String whitelist, String url, boolean expectedToPass)
throws MalformedURLException throws MalformedURLException
{ {
HostWhitelist w = new HostWhitelist(Arrays.asList(StringUtil.parseStringArray(whitelist))); List<String> w = Arrays.asList(StringUtil.parseStringArray(whitelist));
URL u = new URL(url); URL u = new URL(url);
boolean passed; boolean passed;
try { try {
w.verify(u); HostWhitelist.verify(w, u);
passed = true; passed = true;
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
passed = false; passed = false;