Repository: crawljax/crawljax
Issue: Escape domain detection has a leak

When crawljax asserts if it has left the domain it checks if the host of its current url is in the new url. 

This is done here in [Crawljax.java](https://github.com/crawljax/crawljax/blob/fa5a31390f202f6e8d55f4bb6f229c02a867c530/core/src/main/java/com/crawljax/core/Crawler.java#L410-L413)

However, when it visits a social platform like Google+, that new URL has the domain int it as well, as a parameter. 

Crawljax should check that the hostname occurs in the host part of the current URL.


Changed files:
core/src/test/java/com/crawljax/util/UrlUtilsTest.java, core/src/main/java/com/crawljax/util/UrlUtils.java, core/src/main/java/com/crawljax/core/Crawler.java

diff --git a/core/src/main/java/com/crawljax/core/Crawler.java b/core/src/main/java/com/crawljax/core/Crawler.java
index 9ab526f3..1d4d51a0 100644
--- a/core/src/main/java/com/crawljax/core/Crawler.java
+++ b/core/src/main/java/com/crawljax/core/Crawler.java
@@ -408,8 +408,7 @@ public class Crawler {
 	}
 
 	private boolean crawlerLeftDomain() {
-		return !browser.getCurrentUrl().toLowerCase()
-		        .contains(url.getHost().toLowerCase());
+		return !UrlUtils.isSameDomain(browser.getCurrentUrl(), url);
 	}
 
 	private long parseWaitTimeOrReturnDefault(Matcher m) {
diff --git a/core/src/main/java/com/crawljax/util/UrlUtils.java b/core/src/main/java/com/crawljax/util/UrlUtils.java
index 969832ed..048c7ab0 100644
--- a/core/src/main/java/com/crawljax/util/UrlUtils.java
+++ b/core/src/main/java/com/crawljax/util/UrlUtils.java
@@ -1,6 +1,8 @@
 package com.crawljax.util;
 
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.regex.PatternSyntaxException;
 
@@ -93,6 +95,26 @@ public class UrlUtils {
 		return null;
 	}
 
+	/**
+	 * Checks if the given URL is part of the domain, or a subdomain of the given {@link URL}.
+	 * 
+	 * @param currentUrl
+	 *            The url you want to check.
+	 * @param url
+	 *            The URL acting as the base.
+	 * @return If the URL is part of the domain.
+	 */
+	public static boolean isSameDomain(String currentUrl, URL url) {
+		try {
+			String current = URI.create(currentUrl).getHost().toLowerCase();
+			String original = url.toURI().getHost().toLowerCase();
+			return current.endsWith(original);
+		} catch (URISyntaxException e) {
+			LOG.warn("Could not parse URI {}", currentUrl);
+			return false;
+		}
+	}
+
 	private UrlUtils() {
 
 	}
diff --git a/core/src/test/java/com/crawljax/util/UrlUtilsTest.java b/core/src/test/java/com/crawljax/util/UrlUtilsTest.java
index 598c9851..6c2ca3f5 100644
--- a/core/src/test/java/com/crawljax/util/UrlUtilsTest.java
+++ b/core/src/test/java/com/crawljax/util/UrlUtilsTest.java
@@ -65,4 +65,23 @@ public class UrlUtilsTest {
 
 	}
 
+	@Test
+	public void testIsSameDomain() throws MalformedURLException {
+
+		// Same URL
+		assertThat(UrlUtils.isSameDomain("http://example.com",
+		        new URL("http://example.com")), is(true));
+
+		// Different URL
+		assertThat(UrlUtils.isSameDomain("http://test.com",
+		        new URL("http://example.com")), is(false));
+
+		// Same URL with subdomain
+		assertThat(UrlUtils.isSameDomain("http://test.example.com",
+		        new URL("http://example.com")), is(true));
+
+		// Same URL but with HTTPS
+		assertThat(UrlUtils.isSameDomain("https://example.com",
+		        new URL("http://example.com")), is(true));
+	}
 }