From 1c56befb939087df7cb942c2de393089f8a557c9 Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Sun, 5 Jan 2014 04:55:30 +0100 Subject: [PATCH] fixed mess with test on localhost (which means local hosts for some cases) --- source/net/yacy/cora/protocol/Domains.java | 22 ++++++++++++++----- .../yacy/http/Jetty8YaCySecurityHandler.java | 2 +- source/net/yacy/peers/Network.java | 4 ++-- source/net/yacy/peers/Seed.java | 4 ++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/source/net/yacy/cora/protocol/Domains.java b/source/net/yacy/cora/protocol/Domains.java index 2f9556e12..74267d668 100644 --- a/source/net/yacy/cora/protocol/Domains.java +++ b/source/net/yacy/cora/protocol/Domains.java @@ -73,7 +73,8 @@ public class Domains { private static Method InetAddressLocatorGetLocaleInetAddressMethod; private static final Set ccSLD_TLD = new HashSet(); 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_MISS_SIZE = 1000; @@ -1029,6 +1030,15 @@ public class Domains { 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 * this method will return true if noLocalCheck is switched on. This means that @@ -1038,11 +1048,11 @@ public class Domains { * @param host * @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 - (host != null && LOCAL_PATTERNS.matcher(host).matches())); + (host != null && INTRANET_PATTERNS.matcher(host).matches())); } - + /** * check if the given host is a local address. * 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; // check local ip addresses - if (isLocalhost(host)) return true; + if (isIntranet(host)) return true; if (hostaddress != null && ( - isLocalhost(hostaddress.getHostAddress()) || + isIntranet(hostaddress.getHostAddress()) || isLocal(hostaddress) )) return true; diff --git a/source/net/yacy/http/Jetty8YaCySecurityHandler.java b/source/net/yacy/http/Jetty8YaCySecurityHandler.java index 0c8a81e16..eceaeffb6 100644 --- a/source/net/yacy/http/Jetty8YaCySecurityHandler.java +++ b/source/net/yacy/http/Jetty8YaCySecurityHandler.java @@ -183,7 +183,7 @@ public class Jetty8YaCySecurityHandler extends SecurityHandler { refererHost = null; } 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; boolean protectedPage = (pathInContext.indexOf("_p.") > 0); // check "/gsa" and "/solr" if not publicSearchpage diff --git a/source/net/yacy/peers/Network.java b/source/net/yacy/peers/Network.java index 502851e51..2879f840a 100644 --- a/source/net/yacy/peers/Network.java +++ b/source/net/yacy/peers/Network.java @@ -765,8 +765,8 @@ public class Network } seedURL = new DigestURL(seedURLStr); final String host = seedURL.getHost(); - if (Domains.isLocalhost(host)) { // check seedlist reacheable - final String errorMsg = "seedURL in localhost rejected (localhost can't be reached from outside)"; + if (Domains.isIntranet(host)) { // check seedlist reacheable + final String errorMsg = "seedURL in local network rejected (local hosts can't be reached from outside)"; log.warn("SaveSeedList: " + errorMsg); return errorMsg; } diff --git a/source/net/yacy/peers/Seed.java b/source/net/yacy/peers/Seed.java index 9a75d2197..352cd55d5 100644 --- a/source/net/yacy/peers/Seed.java +++ b/source/net/yacy/peers/Seed.java @@ -1087,8 +1087,8 @@ public class Seed implements Cloneable, Comparable, Comparator try { final URL url = new URL(seedURL); final String host = url.getHost(); - if (Domains.isLocalhost(host)) { - return "seedURL in localhost rejected"; + if (Domains.isIntranet(host)) { + return "seedURL in local network rejected"; } } catch (final MalformedURLException e ) { return "seedURL malformed";