Skip to content

Commit d3bea02

Browse files
committed
Polish Bearer Token Padding
Issue gh-8502
1 parent d38daba commit d3bea02

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/DefaultBearerTokenResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
public final class DefaultBearerTokenResolver implements BearerTokenResolver {
3939

4040
private static final Pattern authorizationPattern = Pattern.compile(
41-
"^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$",
41+
"^Bearer (?<token>[a-zA-Z0-9-._~+/]+=*)$",
4242
Pattern.CASE_INSENSITIVE);
4343

4444
private boolean allowFormEncodedBodyParameter = false;
@@ -110,7 +110,7 @@ private String resolveFromAuthorizationHeader(HttpServletRequest request) {
110110
throw new OAuth2AuthenticationException(error);
111111
}
112112

113-
return authorization.substring(7);
113+
return matcher.group("token");
114114
}
115115
return null;
116116
}

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/server/ServerBearerTokenAuthenticationConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
public class ServerBearerTokenAuthenticationConverter
4747
implements ServerAuthenticationConverter {
4848
private static final Pattern authorizationPattern = Pattern.compile(
49-
"^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$",
49+
"^Bearer (?<token>[a-zA-Z0-9-._~+/]+=*)$",
5050
Pattern.CASE_INSENSITIVE);
5151

5252
private boolean allowUriQueryParameter = false;

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/DefaultBearerTokenResolverTests.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@
3434
*/
3535
public class DefaultBearerTokenResolverTests {
3636
private static final String CUSTOM_HEADER = "custom-header";
37-
private static final String TEST_TOKEN = "ab5FG/ywfXPwiPc6ErRQM643QqY";
37+
private static final String TEST_TOKEN = "test-token";
3838

3939
private DefaultBearerTokenResolver resolver;
4040

@@ -51,17 +51,9 @@ public void resolveWhenValidHeaderIsPresentThenTokenIsResolved() {
5151
assertThat(this.resolver.resolve(request)).isEqualTo(TEST_TOKEN);
5252
}
5353

54+
// gh-8502
5455
@Test
55-
public void resolveWhenValidHeaderIsPresentWithSingleBytePaddingIndicatorThenTokenIsResolved() {
56-
String token = TEST_TOKEN + "=";
57-
MockHttpServletRequest request = new MockHttpServletRequest();
58-
request.addHeader("Authorization", "Bearer " + token);
59-
60-
assertThat(this.resolver.resolve(request)).isEqualTo(token);
61-
}
62-
63-
@Test
64-
public void resolveWhenValidHeaderIsPresentWithTwoBytesPaddingIndicatorThenTokenIsResolved() {
56+
public void resolveWhenHeaderEndsWithPaddingIndicatorThenTokenIsResolved() {
6557
String token = TEST_TOKEN + "==";
6658
MockHttpServletRequest request = new MockHttpServletRequest();
6759
request.addHeader("Authorization", "Bearer " + token);

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/server/ServerBearerTokenAuthenticationConverterTests.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,11 @@
1616

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

19+
import java.util.Base64;
20+
1921
import org.junit.Before;
2022
import org.junit.Test;
23+
2124
import org.springframework.http.HttpHeaders;
2225
import org.springframework.http.HttpStatus;
2326
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
@@ -27,8 +30,6 @@
2730
import org.springframework.security.oauth2.server.resource.BearerTokenError;
2831
import org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes;
2932

30-
import java.util.Base64;
31-
3233
import static org.assertj.core.api.Assertions.assertThat;
3334
import static org.assertj.core.api.Assertions.assertThatCode;
3435
import static org.assertj.core.api.Assertions.catchThrowableOfType;
@@ -57,6 +58,17 @@ public void resolveWhenValidHeaderIsPresentThenTokenIsResolved() {
5758
assertThat(convertToToken(request).getToken()).isEqualTo(TEST_TOKEN);
5859
}
5960

61+
// gh-8502
62+
@Test
63+
public void resolveWhenHeaderEndsWithPaddingIndicatorThenTokenIsResolved() {
64+
String token = TEST_TOKEN + "==";
65+
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest
66+
.get("/")
67+
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token);
68+
69+
assertThat(convertToToken(request).getToken()).isEqualTo(token);
70+
}
71+
6072
@Test
6173
public void resolveWhenCustomDefinedHeaderIsValidAndPresentThenTokenIsResolved() {
6274
this.converter.setBearerTokenHeaderName(CUSTOM_HEADER);

0 commit comments

Comments
 (0)