Skip to content

Commit addc7c5

Browse files
committed
Merge branch '5.8.x' into 6.2.x
Closes gh-15985
2 parents 3592253 + 1399a82 commit addc7c5

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.
@@ -73,6 +73,9 @@ public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse r
7373
return null;
7474
}
7575
String originalURI = decodeCookie(savedRequestCookie.getValue());
76+
if (originalURI == null) {
77+
return null;
78+
}
7679
UriComponents uriComponents = UriComponentsBuilder.fromUriString(originalURI).build();
7780
DefaultSavedRequest.Builder builder = new DefaultSavedRequest.Builder();
7881
int port = getPort(uriComponents);
@@ -122,8 +125,14 @@ private static String encodeCookie(String cookieValue) {
122125
return Base64.getEncoder().encodeToString(cookieValue.getBytes());
123126
}
124127

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

129138
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.
@@ -212,4 +212,14 @@ private static String decodeCookie(String encodedCookieValue) {
212212
return new String(Base64.getDecoder().decode(encodedCookieValue.getBytes()));
213213
}
214214

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

0 commit comments

Comments
 (0)