spring-projects / spring-projects/spring-security

DefaultBearerTokenResolver rejects RFC-legal multi-space `Authorization` header

Open Beginner friendly
#19,500 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: bug
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Component: spring-security-oauth2-resource-server · Affects: 6.5.x, 7.1.0, current main

Problem

DefaultBearerTokenResolver matches the header against
^Bearer (?<token>[a-zA-Z0-9-._~+/]+=*)$ — a single literal space. RFC 6750 §2.1
(credentials = "Bearer" 1*SP b64token) and RFC 9110 §11.6.2 (1*SP) allow one or more
spaces. A conformant Authorization: Bearer␣␣<token> is therefore rejected with
invalid_token / "Bearer token is malformed".

Reproduce

Send an otherwise valid request with two spaces after Bearer:

Authorization: Bearer  eyJhbGciOi...

→ HTTP 401, WWW-Authenticate: Bearer error="invalid_token", error_description="Bearer token is malformed".

The token never reaches the decoder; it fails at header resolution.

Expected

Per 1*SP, the token is resolved and validated normally (→ 200 for a valid token).

Notes

  • Masked by some servlet containers: Undertow (2.3.24) collapses the double space to one so
    Spring accepts it; Jetty preserves it verbatim so Spring rejects it. Switching containers
    surfaces the latent rejection with no client change.
  • Regex identical in 6.5.11 and 7.1.0.

Suggested fix

Match 1*SP:

Pattern.compile("^Bearer +(?<token>[a-zA-Z0-9-._~+/]+=*)$", Pattern.CASE_INSENSITIVE);

Failing test

@Test
public void resolveWhenHeaderHasMultipleSpacesBeforeTokenThenTokenIsResolved() {
	// RFC 6750 section 2.1: credentials = "Bearer" 1*SP b64token (one or more spaces)
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Authorization", "Bearer  " + TEST_TOKEN);
	assertThat(this.resolver.resolve(request)).isEqualTo(TEST_TOKEN);
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultBearerTokenResolver and the resolver test containing resolveWhenHeaderHasMultipleSpacesBeforeTokenThenTokenIsResolved. Run that test to reproduce rejection of the double-space Authorization header, then verify that a valid token is resolved and reaches normal validation when one or more spaces follow Bearer.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authentication, security
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.