|
1 | 1 | package io.github.pixee.security; |
2 | 2 |
|
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; |
7 | 7 |
|
8 | 8 | import java.net.MalformedURLException; |
9 | 9 | import java.net.URL; |
10 | 10 | import java.util.regex.Pattern; |
11 | 11 | 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; |
16 | 17 |
|
17 | 18 | final class UrlsTest { |
18 | 19 |
|
@@ -138,6 +139,21 @@ void it_disallows_bad_domains() throws MalformedURLException { |
138 | 139 | () -> { |
139 | 140 | Urls.create("https://evil.com/", setOf(UrlProtocol.HTTPS), allowsOnlyGoodDotCom); |
140 | 141 | }); |
| 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 | + |
141 | 157 | } |
142 | 158 |
|
143 | 159 | @Test |
|
0 commit comments