From 2b6bc5dd0b3ba14f277062299f1e3692a744344d Mon Sep 17 00:00:00 2001 From: David Kirstein Date: Fri, 25 Feb 2022 20:03:13 +0100 Subject: [PATCH] Use configurable charset in ServerHttpBasicAuthenticationConverter Closes gh-10903 --- ...erverHttpBasicAuthenticationConverter.java | 19 ++++++++-- ...HttpBasicAuthenticationConverterTests.java | 36 ++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/web/src/main/java/org/springframework/security/web/server/ServerHttpBasicAuthenticationConverter.java b/web/src/main/java/org/springframework/security/web/server/ServerHttpBasicAuthenticationConverter.java index db33e5e107..89097721f8 100644 --- a/web/src/main/java/org/springframework/security/web/server/ServerHttpBasicAuthenticationConverter.java +++ b/web/src/main/java/org/springframework/security/web/server/ServerHttpBasicAuthenticationConverter.java @@ -16,6 +16,8 @@ package org.springframework.security.web.server; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.function.Function; @@ -25,6 +27,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; @@ -43,6 +46,8 @@ public class ServerHttpBasicAuthenticationConverter implements Function apply(ServerWebExchange exchange) { @@ -51,9 +56,8 @@ public class ServerHttpBasicAuthenticationConverter implements Function result = apply(this.request.header(HttpHeaders.AUTHORIZATION, "Basic dXNlcg==")); assertThat(result.block()).isNull(); } @@ -104,6 +106,38 @@ public class ServerHttpBasicAuthenticationConverterTests { assertThat(result.block()).isNull(); } + @Test + public void applyWhenNonAsciiThenAuthentication() { + Mono result = apply( + this.request.header(HttpHeaders.AUTHORIZATION, "Basic w7xzZXI6cGFzc3fDtnJk")); + UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class) + .block(); + assertThat(authentication.getPrincipal()).isEqualTo("üser"); + assertThat(authentication.getCredentials()).isEqualTo("passwörd"); + } + + @Test + public void applyWhenIsoOnlyAsciiThenAuthentication() { + this.converter.setCredentialsCharset(StandardCharsets.ISO_8859_1); + Mono result = apply( + this.request.header(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd29yZA==")); + UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class) + .block(); + assertThat(authentication.getPrincipal()).isEqualTo("user"); + assertThat(authentication.getCredentials()).isEqualTo("password"); + } + + @Test + public void applyWhenIsoNonAsciiThenAuthentication() { + this.converter.setCredentialsCharset(StandardCharsets.ISO_8859_1); + Mono result = apply( + this.request.header(HttpHeaders.AUTHORIZATION, "Basic /HNlcjpwYXNzd/ZyZA==")); + UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class) + .block(); + assertThat(authentication.getPrincipal()).isEqualTo("üser"); + assertThat(authentication.getCredentials()).isEqualTo("passwörd"); + } + private Mono apply(MockServerHttpRequest.BaseBuilder request) { return this.converter.convert(MockServerWebExchange.from(this.request.build())); }