Skip to content

Commit b633cb2

Browse files
committed
Use utf-8 in ServerHttpBasicAuthenticationConverter
1 parent 6dbd88a commit b633cb2

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.web.server;
1818

19+
import java.nio.charset.StandardCharsets;
1920
import java.util.Base64;
2021
import java.util.function.Function;
2122

@@ -51,9 +52,8 @@ public Mono<Authentication> apply(ServerWebExchange exchange) {
5152
if (!StringUtils.startsWithIgnoreCase(authorization, "basic ")) {
5253
return Mono.empty();
5354
}
54-
String credentials = (authorization.length() <= BASIC.length()) ? ""
55-
: authorization.substring(BASIC.length(), authorization.length());
56-
String decoded = new String(base64Decode(credentials));
55+
String credentials = (authorization.length() <= BASIC.length()) ? "" : authorization.substring(BASIC.length());
56+
String decoded = new String(base64Decode(credentials), StandardCharsets.UTF_8);
5757
String[] parts = decoded.split(":", 2);
5858
if (parts.length != 2) {
5959
return Mono.empty();

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void applyWhenNotBase64ThenEmpty() {
6262
}
6363

6464
@Test
65-
public void applyWhenNoSemicolonThenEmpty() {
65+
public void applyWhenNoColonThenEmpty() {
6666
Mono<Authentication> result = apply(this.request.header(HttpHeaders.AUTHORIZATION, "Basic dXNlcg=="));
6767
assertThat(result.block()).isNull();
6868
}
@@ -104,6 +104,16 @@ public void applyWhenWrongSchemeThenEmpty() {
104104
assertThat(result.block()).isNull();
105105
}
106106

107+
@Test
108+
public void applyWhenNonAsciiThenAuthentication() {
109+
Mono<Authentication> result = apply(
110+
this.request.header(HttpHeaders.AUTHORIZATION, "Basic w7xzZXI6cGFzc3fDtnJk"));
111+
UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class)
112+
.block();
113+
assertThat(authentication.getPrincipal()).isEqualTo("üser");
114+
assertThat(authentication.getCredentials()).isEqualTo("passwörd");
115+
}
116+
107117
private Mono<Authentication> apply(MockServerHttpRequest.BaseBuilder<?> request) {
108118
return this.converter.convert(MockServerWebExchange.from(this.request.build()));
109119
}

0 commit comments

Comments
 (0)