-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
Expected Behavior
ServerHttpBasicAuthenticationConverter
should use utf-8 (or a configurable charset) for String
construction. My understanding is that the majority of HTTP clients, especially browsers, use utf-8 nowadays for basic auth encoding (citation needed).
Current Behavior
In org.springframework.security.web.server.ServerHttpBasicAuthenticationConverter
the Authorization header is parsed and turned into a UsernamePasswordAuthenticationToken
. The header content after "Basic
" is base64-decoded with Base64.getDecoder().decode(value)
to get the raw bytes. These bytes are then converted to a String
(here):
new String(base64Decode(credentials))
This uses a String
constructor without specifying a charset (which, in my opinion, is always bad). Without an explicit charset it will use the platform's default charset - so, for example, utf-8 on Linux and iso-8859-1 on Windows.
Context
This is not the first time an issue with basic auth encoding is brought up (see e. g. #2969, but that is for BasicAuthenticationFilter
to first use iso-8859-1, then utf-8, instead of utf-8 only, funnily), but I haven't found one for ServerHttpBasicAuthenticationConverter
.
I have a workaround (disabling httpBasic and adding my own AuthenticationWebFilter
), but that's really ugly and missing some features (the matcher / entryPoint stuff from org.springframework.security.config.web.server.ServerHttpSecurity.HttpBasicSpec#configure
).
Notes:
org.springframework.security.web.authentication.www.BasicAuthenticationFilter
uses utf-8 as a default. It is configurable viaorg.springframework.security.web.authentication.www.BasicAuthenticationFilter#setCredentialsCharset
(although I don't know how or even if that is exposed for configuration).org.springframework.security.web.server.ServerHttpBasicAuthenticationConverter
uses the platform's default charset, unconfigurably.org.springframework.http.HttpHeaders#setBasicAuth(java.lang.String, java.lang.String)
uses iso-8859-1 as the default (inorg.springframework.http.HttpHeaders#encodeBasicAuth
), but there isorg.springframework.http.HttpHeaders#setBasicAuth(java.lang.String, java.lang.String, java.nio.charset.Charset)
to specify another charset.
Am I wrong in wanting utf-8 as a default? Is there a consensus on what should be the default in Spring (spanning clients and servers)?
/cc @jgrandja