|
34 | 34 | import org.apache.hc.client5.http.cookie.CommonCookieAttributeHandler; |
35 | 35 | import org.apache.hc.client5.http.cookie.Cookie; |
36 | 36 | import org.apache.hc.client5.http.cookie.CookieOrigin; |
| 37 | +import org.apache.hc.client5.http.cookie.CookieSpec; |
37 | 38 | import org.apache.hc.client5.http.cookie.MalformedCookieException; |
| 39 | +import org.apache.hc.client5.http.psl.PublicSuffixMatcher; |
| 40 | +import org.apache.hc.client5.http.psl.PublicSuffixMatcherLoader; |
38 | 41 | import org.apache.hc.core5.http.Header; |
39 | 42 | import org.apache.hc.core5.http.message.BasicHeader; |
40 | 43 | import org.junit.jupiter.api.Assertions; |
|
44 | 47 |
|
45 | 48 | class TestRFC6265CookieSpec { |
46 | 49 |
|
| 50 | + /** |
| 51 | + * xn--9tfky.id (ᬩᬮᬶ.id) is a real Mozilla Public Suffix List entry whose |
| 52 | + * Unicode label is unassigned under the Unicode 3.2 repertoire java.net.IDN enforces by |
| 53 | + * default. Without IDN.ALLOW_UNASSIGNED the matcher never decodes it, so it falls |
| 54 | + * through to treating it as an ordinary registrable domain instead of a public suffix. |
| 55 | + * Uses the PSL-aware CookieSpec wiring exactly as HttpClientBuilder sets it up by |
| 56 | + * default (RFC6265CookieSpecFactory with PublicSuffixMatcherLoader.getDefault()). |
| 57 | + */ |
| 58 | + @Test |
| 59 | + void testRejectUnassignedCodePointSuffix() throws Exception { |
| 60 | + final CookieOrigin evilTenant = new CookieOrigin("evil-tenant.xn--9tfky.id", 80, "/", false); |
| 61 | + final CookieOrigin otherTenant = new CookieOrigin("other-tenant.xn--9tfky.id", 80, "/", false); |
| 62 | + final CookieOrigin legitOrigin = new CookieOrigin("foo.legit.xn--9tfky.id", 80, "/", false); |
| 63 | + final CookieOrigin legitSibling = new CookieOrigin("bar.legit.xn--9tfky.id", 80, "/", false); |
| 64 | + final PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault(); |
| 65 | + final List<CookieSpec> cookieSpecs = Arrays.asList( |
| 66 | + new RFC6265CookieSpecFactory(RFC6265CookieSpecFactory.CompatibilityLevel.RELAXED, publicSuffixMatcher) |
| 67 | + .create(null), |
| 68 | + new RFC6265CookieSpecFactory(RFC6265CookieSpecFactory.CompatibilityLevel.STRICT, publicSuffixMatcher) |
| 69 | + .create(null)); |
| 70 | + |
| 71 | + for (final CookieSpec cookieSpec : cookieSpecs) { |
| 72 | + // A cookie for the bare public suffix itself must not reach another tenant under it. |
| 73 | + final Cookie suffixCookie = cookieSpec.parse( |
| 74 | + new BasicHeader("Set-Cookie", "name=value; Domain=xn--9tfky.id"), evilTenant).get(0); |
| 75 | + Assertions.assertFalse(cookieSpec.match(suffixCookie, otherTenant)); |
| 76 | + |
| 77 | + // A cookie for a genuine registrable domain under the suffix is still a valid |
| 78 | + // cookie domain and reaches siblings under it, like any ordinary domain. |
| 79 | + final Cookie legitCookie = cookieSpec.parse( |
| 80 | + new BasicHeader("Set-Cookie", "name=value; Domain=legit.xn--9tfky.id"), legitOrigin).get(0); |
| 81 | + Assertions.assertTrue(cookieSpec.match(legitCookie, legitSibling)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
47 | 85 | @Test |
48 | 86 | void testParseCookieBasics() throws Exception { |
49 | 87 | final CommonCookieAttributeHandler h1 = Mockito.mock(CommonCookieAttributeHandler.class); |
|
0 commit comments