Skip to content

Commit 0c26d1b

Browse files
committed
ServerHttpBasicAuthenticationConverter Validates Scheme Name
Fixes: gh-5414
1 parent e3d4d66 commit 0c26d1b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

web/src/main/java/org/springframework/security/web/server/ServerHttpBasicAuthenticationConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Mono<Authentication> apply(ServerWebExchange exchange) {
4141
ServerHttpRequest request = exchange.getRequest();
4242

4343
String authorization = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
44-
if(authorization == null) {
44+
if(authorization == null || !authorization.toLowerCase().startsWith("basic ")) {
4545
return Mono.empty();
4646
}
4747

web/src/test/java/org/springframework/security/web/server/ServerHttpBasicAuthenticationConverterTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ public void applyWhenUserPasswordThenAuthentication() {
7979
assertThat(authentication.getCredentials()).isEqualTo("password");
8080
}
8181

82+
@Test
83+
public void applyWhenLowercaseSchemeThenAuthentication() {
84+
Mono<Authentication> result = apply(this.request.header(HttpHeaders.AUTHORIZATION, "basic dXNlcjpwYXNzd29yZA=="));
85+
86+
UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class).block();
87+
assertThat(authentication.getPrincipal()).isEqualTo("user");
88+
assertThat(authentication.getCredentials()).isEqualTo("password");
89+
}
90+
91+
@Test
92+
public void applyWhenWrongSchemeThenAuthentication() {
93+
Mono<Authentication> result = apply(this.request.header(HttpHeaders.AUTHORIZATION, "token dXNlcjpwYXNzd29yZA=="));
94+
95+
assertThat(result.block()).isNull();
96+
}
97+
8298
private Mono<Authentication> apply(MockServerHttpRequest.BaseBuilder<?> request) {
8399
return this.converter.apply(MockServerWebExchange.from(this.request.build()));
84100
}

0 commit comments

Comments
 (0)