Skip to content

Commit 1399a82

Browse files
kse-musicjzheaux
authored andcommitted
Return Null Request When Cookie Is Malformed
Closes gh-15905
1 parent ec33e40 commit 1399a82

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

web/src/main/java/org/springframework/security/web/savedrequest/CookieRequestCache.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -74,6 +74,9 @@ public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse r
7474
return null;
7575
}
7676
String originalURI = decodeCookie(savedRequestCookie.getValue());
77+
if (originalURI == null) {
78+
return null;
79+
}
7780
UriComponents uriComponents = UriComponentsBuilder.fromUriString(originalURI).build();
7881
DefaultSavedRequest.Builder builder = new DefaultSavedRequest.Builder();
7982
int port = getPort(uriComponents);
@@ -123,8 +126,14 @@ private static String encodeCookie(String cookieValue) {
123126
return Base64.getEncoder().encodeToString(cookieValue.getBytes());
124127
}
125128

126-
private static String decodeCookie(String encodedCookieValue) {
127-
return new String(Base64.getDecoder().decode(encodedCookieValue.getBytes()));
129+
private String decodeCookie(String encodedCookieValue) {
130+
try {
131+
return new String(Base64.getDecoder().decode(encodedCookieValue.getBytes()));
132+
}
133+
catch (IllegalArgumentException ex) {
134+
this.logger.debug("Failed decode cookie value " + encodedCookieValue);
135+
return null;
136+
}
128137
}
129138

130139
private static String getCookiePath(HttpServletRequest request) {

web/src/test/java/org/springframework/security/web/savedrequest/CookieRequestCacheTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -213,4 +213,14 @@ private static String decodeCookie(String encodedCookieValue) {
213213
return new String(Base64.getDecoder().decode(encodedCookieValue.getBytes()));
214214
}
215215

216+
// gh-15905
217+
@Test
218+
public void illegalCookieValueReturnNull() {
219+
CookieRequestCache cookieRequestCache = new CookieRequestCache();
220+
MockHttpServletRequest request = new MockHttpServletRequest();
221+
request.setCookies(new Cookie(DEFAULT_COOKIE_NAME, "123^456"));
222+
SavedRequest savedRequest = cookieRequestCache.getRequest(request, new MockHttpServletResponse());
223+
assertThat(savedRequest).isNull();
224+
}
225+
216226
}

0 commit comments

Comments
 (0)