Skip to content

Commit cf03f2f

Browse files
committed
Merge branch '6.3.x'
2 parents b2e0539 + 5048a68 commit cf03f2f

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.
@@ -78,6 +78,9 @@ public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse r
7878
return null;
7979
}
8080
String originalURI = decodeCookie(savedRequestCookie.getValue());
81+
if (originalURI == null) {
82+
return null;
83+
}
8184
UriComponents uriComponents = UriComponentsBuilder.fromUriString(originalURI).build();
8285
DefaultSavedRequest.Builder builder = new DefaultSavedRequest.Builder();
8386
int port = getPort(uriComponents);
@@ -127,8 +130,14 @@ private static String encodeCookie(String cookieValue) {
127130
return Base64.getEncoder().encodeToString(cookieValue.getBytes());
128131
}
129132

130-
private static String decodeCookie(String encodedCookieValue) {
131-
return new String(Base64.getDecoder().decode(encodedCookieValue.getBytes()));
133+
private String decodeCookie(String encodedCookieValue) {
134+
try {
135+
return new String(Base64.getDecoder().decode(encodedCookieValue.getBytes()));
136+
}
137+
catch (IllegalArgumentException ex) {
138+
this.logger.debug("Failed decode cookie value " + encodedCookieValue);
139+
return null;
140+
}
132141
}
133142

134143
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.
@@ -229,4 +229,14 @@ private static String decodeCookie(String encodedCookieValue) {
229229
return new String(Base64.getDecoder().decode(encodedCookieValue.getBytes()));
230230
}
231231

232+
// gh-15905
233+
@Test
234+
public void illegalCookieValueReturnNull() {
235+
CookieRequestCache cookieRequestCache = new CookieRequestCache();
236+
MockHttpServletRequest request = new MockHttpServletRequest();
237+
request.setCookies(new Cookie(DEFAULT_COOKIE_NAME, "123^456"));
238+
SavedRequest savedRequest = cookieRequestCache.getRequest(request, new MockHttpServletResponse());
239+
assertThat(savedRequest).isNull();
240+
}
241+
232242
}

0 commit comments

Comments
 (0)