Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/main/java/io/github/pixee/security/HostValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ public boolean isAllowed(final String host) {
static HostValidator fromAllowedHostPattern(final Pattern allowPattern) {
return new PatternBasedHostValidator(allowPattern);
}

/**
* Return a {@link HostValidator} that will assure a given domain is within the allowed domain. For example, given
* a domain of "good.com", this validator will allow "good.com", "www.good.com", "internal.good.com", etc.
*
* @param domainName the domain to allow, e.g., "good.com", or "internal-host"
* @return a validator that will only allow hosts within the given domain space
*/
static HostValidator fromAllowedHostDomain(final String domainName) {
Pattern p = Pattern.compile("(.*\\." + Pattern.quote(domainName) + "|" + Pattern.quote(domainName) +")");
return new PatternBasedHostValidator(p);
}
}
32 changes: 24 additions & 8 deletions src/test/java/io/github/pixee/security/UrlsTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package io.github.pixee.security;

import static io.github.pixee.security.J8ApiBridge.setOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static io.github.pixee.security.J8ApiBridge.setOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;

final class UrlsTest {

Expand Down Expand Up @@ -138,6 +139,21 @@ void it_disallows_bad_domains() throws MalformedURLException {
() -> {
Urls.create("https://evil.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotCom);
});

HostValidator allowsOnlyGoodDotComByDomainString = HostValidator.fromAllowedHostDomain("good.com");
Urls.create("https://good.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);
Urls.create("https://sub.good.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);
Urls.create("https://different-sub-123.good.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);
Urls.create("https://.good.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);

Stream.of("https://goodAcom/", "https://evil.com", "https://good.com.evil", "https://good.com.").forEach(badDomain -> {
assertThrows(
SecurityException.class,
() -> {
Urls.create(badDomain, setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotComByDomainString);
});
});

}

@Test
Expand Down