Skip to content

Commit e0faaa4

Browse files
committed
Relax domain name checks in ResponseCookie
Closes gh-23924
1 parent 2e49441 commit e0faaa4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

spring-web/src/main/java/org/springframework/http/ResponseCookie.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public static void validateDomain(@Nullable String domain) {
374374
}
375375
int char1 = domain.charAt(0);
376376
int charN = domain.charAt(domain.length() - 1);
377-
if (char1 == '.' || char1 == '-' || charN == '.' || charN == '-') {
377+
if (char1 == '-' || charN == '.' || charN == '-') {
378378
throw new IllegalArgumentException("Invalid first/last char in cookie domain: " + domain);
379379
}
380380
for (int i = 0, c = -1; i < domain.length(); i++) {

spring-web/src/test/java/org/springframework/http/ResponseCookieTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,31 @@ public void valueChecks() {
8585
});
8686
}
8787

88+
@Test
89+
public void domainChecks() {
90+
91+
Arrays.asList("abc", "abc.org", "abc-def.org", "abc3.org", ".abc.org")
92+
.forEach(domain -> ResponseCookie.from("n", "v").domain(domain).build());
93+
94+
Arrays.asList("-abc.org", "abc.org.", "abc.org-", "-abc.org", "abc.org-")
95+
.forEach(domain -> {
96+
try {
97+
ResponseCookie.from("n", "v").domain(domain).build();
98+
}
99+
catch (IllegalArgumentException ex) {
100+
assertThat(ex.getMessage(), Matchers.containsString("Invalid first/last char"));
101+
}
102+
});
88103

104+
Arrays.asList("abc..org", "abc.-org", "abc-.org")
105+
.forEach(domain -> {
106+
try {
107+
ResponseCookie.from("n", "v").domain(domain).build();
108+
}
109+
catch (IllegalArgumentException ex) {
110+
assertThat(ex.getMessage(), Matchers.containsString("invalid cookie domain char"));
111+
}
112+
});
113+
}
89114

90115
}

0 commit comments

Comments
 (0)