Skip to content

Commit b5f707a

Browse files
authored
Merge pull request #14 from pixee/host-pattern-easier
Add a new API for validating hostnames within domains
2 parents 806f5d7 + d26ec30 commit b5f707a

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

.github/badges/jacoco.svg

Lines changed: 1 addition & 1 deletion
Loading

src/main/java/io/github/pixee/security/HostValidator.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ public boolean isAllowed(final String host) {
4747
static HostValidator fromAllowedHostPattern(final Pattern allowPattern) {
4848
return new PatternBasedHostValidator(allowPattern);
4949
}
50+
51+
/**
52+
* Return a {@link HostValidator} that will assure a given domain is within the allowed domain. For example, given
53+
* a domain of "good.com", this validator will allow "good.com", "www.good.com", "internal.good.com", etc.
54+
*
55+
* @param domainName the domain to allow, e.g., "good.com", or "internal-host"
56+
* @return a validator that will only allow hosts within the given domain space
57+
*/
58+
static HostValidator fromAllowedHostDomain(final String domainName) {
59+
Pattern p = Pattern.compile("(.*\\." + Pattern.quote(domainName) + "|" + Pattern.quote(domainName) +")");
60+
return new PatternBasedHostValidator(p);
61+
}
5062
}

src/test/java/io/github/pixee/security/UrlsTest.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package io.github.pixee.security;
22

3-
import static io.github.pixee.security.J8ApiBridge.setOf;
4-
import static org.hamcrest.MatcherAssert.assertThat;
5-
import static org.hamcrest.Matchers.*;
6-
import static org.junit.jupiter.api.Assertions.assertThrows;
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
import org.junit.jupiter.params.provider.MethodSource;
77

88
import java.net.MalformedURLException;
99
import java.net.URL;
1010
import java.util.regex.Pattern;
1111
import java.util.stream.Stream;
12-
import org.junit.jupiter.api.Test;
13-
import org.junit.jupiter.params.ParameterizedTest;
14-
import org.junit.jupiter.params.provider.Arguments;
15-
import org.junit.jupiter.params.provider.MethodSource;
12+
13+
import static io.github.pixee.security.J8ApiBridge.setOf;
14+
import static org.hamcrest.MatcherAssert.assertThat;
15+
import static org.hamcrest.Matchers.*;
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
1617

1718
final class UrlsTest {
1819

@@ -138,6 +139,21 @@ void it_disallows_bad_domains() throws MalformedURLException {
138139
() -> {
139140
Urls.create("https://evil.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotCom);
140141
});
142+
143+
HostValidator allowsOnlyGoodDotComByDomainString = HostValidator.fromAllowedHostDomain("good.com");
144+
Urls.create("https://good.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);
145+
Urls.create("https://sub.good.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);
146+
Urls.create("https://different-sub-123.good.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);
147+
Urls.create("https://.good.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);
148+
149+
Stream.of("https://goodAcom/", "https://evil.com", "https://good.com.evil", "https://good.com.").forEach(badDomain -> {
150+
assertThrows(
151+
SecurityException.class,
152+
() -> {
153+
Urls.create(badDomain, setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);
154+
});
155+
});
156+
141157
}
142158

143159
@Test

0 commit comments

Comments
 (0)