fixed mess with test on localhost (which means local hosts for some

cases)
pull/1/head
Michael Peter Christen 11 years ago
parent 7d6fc79eb8
commit 1c56befb93

@ -73,7 +73,8 @@ public class Domains {
private static Method InetAddressLocatorGetLocaleInetAddressMethod; private static Method InetAddressLocatorGetLocaleInetAddressMethod;
private static final Set<String> ccSLD_TLD = new HashSet<String>(); private static final Set<String> ccSLD_TLD = new HashSet<String>();
private static final String PRESENT = ""; private static final String PRESENT = "";
private static final Pattern LOCAL_PATTERNS = Pattern.compile("(10\\..*)|(127\\..*)|(172\\.(1[6-9]|2[0-9]|3[0-1])\\..*)|(169\\.254\\..*)|(192\\.168\\..*)|(localhost)|(\\[?\\:\\:1/.*)|(\\[?fc.*)|(\\[?fd.*)|(\\[?(fe80|0)\\:0\\:0\\:0\\:0\\:0\\:0\\:1.*)"); private static final Pattern LOCALHOST_PATTERNS = Pattern.compile("(127\\..*)|(localhost)|(\\[?(fe80|0)\\:0\\:0\\:0\\:0\\:0\\:0\\:1.*)");
private static final Pattern INTRANET_PATTERNS = Pattern.compile("(10\\..*)|(127\\..*)|(172\\.(1[6-9]|2[0-9]|3[0-1])\\..*)|(169\\.254\\..*)|(192\\.168\\..*)|(localhost)|(\\[?\\:\\:1/.*)|(\\[?fc.*)|(\\[?fd.*)|(\\[?(fe80|0)\\:0\\:0\\:0\\:0\\:0\\:0\\:1.*)");
private static final int MAX_NAME_CACHE_HIT_SIZE = 10000; private static final int MAX_NAME_CACHE_HIT_SIZE = 10000;
private static final int MAX_NAME_CACHE_MISS_SIZE = 1000; private static final int MAX_NAME_CACHE_MISS_SIZE = 1000;
@ -1029,6 +1030,15 @@ public class Domains {
return (isLocal(host, hostaddress)) ? TLD_Local_ID : TLD_Generic_ID; return (isLocal(host, hostaddress)) ? TLD_Local_ID : TLD_Generic_ID;
} }
/**
* check the host ip string against localhost names
* @param host
* @return true if the host from the string is the localhost
*/
public static boolean isLocalhost(final String host) {
return (host != null && LOCALHOST_PATTERNS.matcher(host).matches());
}
/** /**
* check if a given host is the name for a local host address * check if a given host is the name for a local host address
* this method will return true if noLocalCheck is switched on. This means that * this method will return true if noLocalCheck is switched on. This means that
@ -1038,11 +1048,11 @@ public class Domains {
* @param host * @param host
* @return * @return
*/ */
public static boolean isLocalhost(final String host) { public static boolean isIntranet(final String host) {
return (noLocalCheck || // DO NOT REMOVE THIS! it is correct to return true if the check is off return (noLocalCheck || // DO NOT REMOVE THIS! it is correct to return true if the check is off
(host != null && LOCAL_PATTERNS.matcher(host).matches())); (host != null && INTRANET_PATTERNS.matcher(host).matches()));
} }
/** /**
* check if the given host is a local address. * check if the given host is a local address.
* the hostaddress is optional and shall be given if the address is already known * the hostaddress is optional and shall be given if the address is already known
@ -1061,9 +1071,9 @@ public class Domains {
host.isEmpty()) return true; host.isEmpty()) return true;
// check local ip addresses // check local ip addresses
if (isLocalhost(host)) return true; if (isIntranet(host)) return true;
if (hostaddress != null && ( if (hostaddress != null && (
isLocalhost(hostaddress.getHostAddress()) || isIntranet(hostaddress.getHostAddress()) ||
isLocal(hostaddress) isLocal(hostaddress)
)) return true; )) return true;

@ -183,7 +183,7 @@ public class Jetty8YaCySecurityHandler extends SecurityHandler {
refererHost = null; refererHost = null;
} }
final boolean accessFromLocalhost = Domains.isLocalhost(request.getRemoteHost()) && (refererHost == null || refererHost.length() == 0 || Domains.isLocalhost(refererHost)); final boolean accessFromLocalhost = Domains.isLocalhost(request.getRemoteHost()) && (refererHost == null || refererHost.length() == 0 || Domains.isLocalhost(refererHost));
// ! note : accessFromLocalhost compares localhost ip pattern ( ! currently also any intranet host is a local host) // ! note : accessFromLocalhost compares localhost ip pattern
final boolean grantedForLocalhost = adminAccountForLocalhost && accessFromLocalhost; final boolean grantedForLocalhost = adminAccountForLocalhost && accessFromLocalhost;
boolean protectedPage = (pathInContext.indexOf("_p.") > 0); boolean protectedPage = (pathInContext.indexOf("_p.") > 0);
// check "/gsa" and "/solr" if not publicSearchpage // check "/gsa" and "/solr" if not publicSearchpage

@ -765,8 +765,8 @@ public class Network
} }
seedURL = new DigestURL(seedURLStr); seedURL = new DigestURL(seedURLStr);
final String host = seedURL.getHost(); final String host = seedURL.getHost();
if (Domains.isLocalhost(host)) { // check seedlist reacheable if (Domains.isIntranet(host)) { // check seedlist reacheable
final String errorMsg = "seedURL in localhost rejected (localhost can't be reached from outside)"; final String errorMsg = "seedURL in local network rejected (local hosts can't be reached from outside)";
log.warn("SaveSeedList: " + errorMsg); log.warn("SaveSeedList: " + errorMsg);
return errorMsg; return errorMsg;
} }

@ -1087,8 +1087,8 @@ public class Seed implements Cloneable, Comparable<Seed>, Comparator<Seed>
try { try {
final URL url = new URL(seedURL); final URL url = new URL(seedURL);
final String host = url.getHost(); final String host = url.getHost();
if (Domains.isLocalhost(host)) { if (Domains.isIntranet(host)) {
return "seedURL in localhost rejected"; return "seedURL in local network rejected";
} }
} catch (final MalformedURLException e ) { } catch (final MalformedURLException e ) {
return "seedURL malformed"; return "seedURL malformed";

Loading…
Cancel
Save