mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 08:24:13 +00:00
validate samesite attribute in ResponseCookie
Signed-off-by: dxbjavid <dxbjavid@gmail.com>
This commit is contained in:
@@ -75,6 +75,7 @@ public final class ResponseCookie extends HttpCookie {
|
||||
Rfc6265Utils.validateCookieValue(value);
|
||||
Rfc6265Utils.validateDomain(domain);
|
||||
Rfc6265Utils.validatePath(path);
|
||||
Rfc6265Utils.validateSameSite(sameSite);
|
||||
}
|
||||
|
||||
|
||||
@@ -433,6 +434,18 @@ public final class ResponseCookie extends HttpCookie {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void validateSameSite(@Nullable String sameSite) {
|
||||
if (sameSite == null) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < sameSite.length(); i++) {
|
||||
char c = sameSite.charAt(i);
|
||||
if (c < 0x20 || c > 0x7E || c == ';') {
|
||||
throw new IllegalArgumentException(sameSite + ": Invalid cookie SameSite char '" + c + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -82,6 +82,17 @@ class ResponseCookieTests {
|
||||
.hasMessageContaining("invalid cookie domain char"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameSiteChecks() {
|
||||
|
||||
Arrays.asList("Strict", "Lax", "None")
|
||||
.forEach(sameSite -> ResponseCookie.from("n", "v").sameSite(sameSite).build());
|
||||
|
||||
Arrays.asList("Lax\r\nSet-Cookie: x=y", "Lax\n", "La;x", "Lax\t", "Lax\u0005")
|
||||
.forEach(sameSite -> assertThatThrownBy(() -> ResponseCookie.from("n", "v").sameSite(sameSite).build())
|
||||
.hasMessageContaining("Invalid cookie SameSite char"));
|
||||
}
|
||||
|
||||
@Test // gh-24663
|
||||
void domainWithEmptyDoubleQuotes() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user