Skip to content

Commit 52675c8

Browse files
committed
Check For Null Exception Message
Closes gh-13768
1 parent b919ece commit 52675c8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

web/src/main/java/org/springframework/security/web/authentication/ui/DefaultLoginPageGeneratingFilter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -36,6 +36,7 @@
3636
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
3737
import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
3838
import org.springframework.util.Assert;
39+
import org.springframework.util.StringUtils;
3940
import org.springframework.web.filter.GenericFilterBean;
4041
import org.springframework.web.util.HtmlUtils;
4142

@@ -244,7 +245,8 @@ private String generateLoginPageHtml(HttpServletRequest request, boolean loginEr
244245
if (session != null) {
245246
AuthenticationException ex = (AuthenticationException) session
246247
.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
247-
errorMsg = (ex != null) ? ex.getMessage() : "Invalid credentials";
248+
errorMsg = (ex != null && StringUtils.hasLength(ex.getMessage())) ? ex.getMessage()
249+
: "Invalid credentials";
248250
}
249251
}
250252
String contextPath = request.getContextPath();

web/src/test/java/org/springframework/security/web/authentication/DefaultLoginPageGeneratingFilterTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ public void generatesForSaml2LoginAndEscapesClientName() throws Exception {
182182
.contains("<a href=\"/saml/sso/google\">Google &lt; &gt; &quot; &#39; &amp;</a>");
183183
} // Fake OpenID filter (since it's not in this module
184184

185+
// gh-13768
186+
@Test
187+
public void generatesWhenExceptionWithEmptyMessageThenInvalidCredentials() throws Exception {
188+
DefaultLoginPageGeneratingFilter filter = new DefaultLoginPageGeneratingFilter(
189+
new UsernamePasswordAuthenticationFilter());
190+
filter.setLoginPageUrl(DefaultLoginPageGeneratingFilter.DEFAULT_LOGIN_PAGE_URL);
191+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/login");
192+
request.setQueryString("error");
193+
request.getSession().setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, new BadCredentialsException(null));
194+
MockHttpServletResponse response = new MockHttpServletResponse();
195+
filter.doFilter(request, response, this.chain);
196+
assertThat(response.getContentAsString()).contains("Invalid credentials");
197+
}
198+
185199
@SuppressWarnings("unused")
186200
private static class MockProcessingFilter extends AbstractAuthenticationProcessingFilter {
187201

0 commit comments

Comments
 (0)