Skip to content

Commit b55322b

Browse files
committed
Make basic authentication scheme case-insensitive
Fixes: gh-7163
1 parent 8e6e975 commit b55322b

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

web/src/main/java/org/springframework/security/web/authentication/www/BasicAuthenticationConverter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.security.web.authentication.AuthenticationConverter;
3131
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
3232
import org.springframework.util.Assert;
33+
import org.springframework.util.StringUtils;
3334

3435
/**
3536
* Converts from a HttpServletRequest to
@@ -83,7 +84,7 @@ public UsernamePasswordAuthenticationToken convert(HttpServletRequest request) {
8384
}
8485

8586
header = header.trim();
86-
if (!header.startsWith(AUTHENTICATION_SCHEME_BASIC) && !header.startsWith(AUTHENTICATION_SCHEME_BASIC.toLowerCase())) {
87+
if (!StringUtils.startsWithIgnoreCase(header, AUTHENTICATION_SCHEME_BASIC)) {
8788
return null;
8889
}
8990

web/src/test/java/org/springframework/security/web/authentication/www/BasicAuthenticationConverterTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ public void testNormalOperation() throws Exception {
6161
assertThat(authentication.getName()).isEqualTo("rod");
6262
}
6363

64+
@Test
65+
public void requestWhenAuthorizationSchemeInMixedCaseThenAuthenticates() {
66+
String token = "rod:koala";
67+
MockHttpServletRequest request = new MockHttpServletRequest();
68+
request.addHeader("Authorization", "BaSiC " + new String(Base64.encodeBase64(token.getBytes())));
69+
UsernamePasswordAuthenticationToken authentication = converter.convert(request);
70+
71+
verify(authenticationDetailsSource).buildDetails(any());
72+
assertThat(authentication).isNotNull();
73+
assertThat(authentication.getName()).isEqualTo("rod");
74+
}
75+
6476
@Test
6577
public void testWhenUnsupportedAuthorizationHeaderThenIgnored() throws Exception {
6678
MockHttpServletRequest request = new MockHttpServletRequest();

web/src/test/java/org/springframework/security/web/authentication/www/BasicAuthenticationFilterTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,24 @@ public void doFilterWhenSchemeLowercaseThenCaseInsensitveMatchWorks() throws Exc
176176
.isEqualTo("rod");
177177
}
178178

179+
@Test
180+
public void doFilterWhenSchemeMixedCaseThenCaseInsensitiveMatchWorks() throws Exception {
181+
String token = "rod:koala";
182+
MockHttpServletRequest request = new MockHttpServletRequest();
183+
request.addHeader("Authorization",
184+
"BaSiC " + new String(Base64.encodeBase64(token.getBytes())));
185+
request.setServletPath("/some_file.html");
186+
187+
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
188+
FilterChain chain = mock(FilterChain.class);
189+
filter.doFilter(request, new MockHttpServletResponse(), chain);
190+
191+
verify(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
192+
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
193+
assertThat(SecurityContextHolder.getContext().getAuthentication().getName())
194+
.isEqualTo("rod");
195+
}
196+
179197
@Test
180198
public void testOtherAuthorizationSchemeIsIgnored() throws Exception {
181199

0 commit comments

Comments
 (0)