Skip to content

Commit f438d7b

Browse files
committed
Catch base64 decode exception
Closes gh-15905
1 parent fe79766 commit f438d7b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

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

Lines changed: 9 additions & 2 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,7 +73,14 @@ public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse r
7373
if (savedRequestCookie == null) {
7474
return null;
7575
}
76-
String originalURI = decodeCookie(savedRequestCookie.getValue());
76+
String originalURI;
77+
try {
78+
originalURI = decodeCookie(savedRequestCookie.getValue());
79+
}
80+
catch (IllegalArgumentException ex) {
81+
this.logger.debug("Failed decode cookie value " + savedRequestCookie.getValue());
82+
return null;
83+
}
7784
UriComponents uriComponents = UriComponentsBuilder.fromUriString(originalURI).build();
7885
DefaultSavedRequest.Builder builder = new DefaultSavedRequest.Builder();
7986
int port = getPort(uriComponents);

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)