Skip to content

Commit 7e7f85c

Browse files
committed
Polish Bearer Token Padding
Issue gh-8502
1 parent 157498b commit 7e7f85c

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*/
3737
public final class DefaultBearerTokenResolver implements BearerTokenResolver {
3838

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

4141
private boolean allowFormEncodedBodyParameter = false;
4242

@@ -98,7 +98,7 @@ private static String resolveFromAuthorizationHeader(HttpServletRequest request)
9898
throw new OAuth2AuthenticationException(error);
9999
}
100100

101-
return authorization.substring(7);
101+
return matcher.group("token");
102102
}
103103
return null;
104104
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*/
4444
public class ServerBearerTokenAuthenticationConverter
4545
implements ServerAuthenticationConverter {
46-
private static final Pattern authorizationPattern = Pattern.compile("^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$");
46+
private static final Pattern authorizationPattern = Pattern.compile("^Bearer (?<token>[a-zA-Z0-9-._~+/]+=*)$");
4747

4848
private boolean allowUriQueryParameter = false;
4949

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

+3-11
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.
@@ -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

+15-3
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;
@@ -56,6 +57,17 @@ public void resolveWhenValidHeaderIsPresentThenTokenIsResolved() {
5657
assertThat(convertToToken(request).getToken()).isEqualTo(TEST_TOKEN);
5758
}
5859

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

0 commit comments

Comments
 (0)