Skip to content

ServerHttpBasicAuthenticationConverter uses platform's default charset #10903

@frozenice

Description

@frozenice

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 via org.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 (in org.springframework.http.HttpHeaders#encodeBasicAuth), but there is org.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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webAn issue in web modules (web, webmvc)type: breaks-passivityA change that breaks passivity with the previous releasetype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions