Skip to content

Validate token passed in query parameters same as headers #7012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public final class DefaultBearerTokenResolver implements BearerTokenResolver {
private static final Pattern authorizationPattern = Pattern.compile(
"^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$",
Pattern.CASE_INSENSITIVE);
private static final Pattern queryParametersPattern = Pattern.compile(
"^(?<token>[a-zA-Z0-9-._~+/]+)=*$",
Pattern.CASE_INSENSITIVE);

private boolean allowFormEncodedBodyParameter = false;

Expand Down Expand Up @@ -90,17 +93,7 @@ public void setAllowUriQueryParameter(boolean allowUriQueryParameter) {
private static String resolveFromAuthorizationHeader(HttpServletRequest request) {
String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
if (StringUtils.startsWithIgnoreCase(authorization, "bearer")) {
Matcher matcher = authorizationPattern.matcher(authorization);

if (!matcher.matches()) {
BearerTokenError error = new BearerTokenError(BearerTokenErrorCodes.INVALID_TOKEN,
HttpStatus.UNAUTHORIZED,
"Bearer token is malformed",
"https://tools.ietf.org/html/rfc6750#section-3.1");
throw new OAuth2AuthenticationException(error);
}

return matcher.group("token");
return validateToken(authorization, authorizationPattern);
}
return null;
}
Expand All @@ -112,7 +105,7 @@ private static String resolveFromRequestParameters(HttpServletRequest request) {
}

if (values.length == 1) {
return values[0];
return validateToken(values[0], queryParametersPattern);
}

BearerTokenError error = new BearerTokenError(BearerTokenErrorCodes.INVALID_REQUEST,
Expand All @@ -126,4 +119,18 @@ private boolean isParameterTokenSupportedForRequest(HttpServletRequest request)
return ((this.allowFormEncodedBodyParameter && "POST".equals(request.getMethod()))
|| (this.allowUriQueryParameter && "GET".equals(request.getMethod())));
}

private static String validateToken(String token, Pattern pattern) {
Matcher matcher = pattern.matcher(token);

if (!matcher.matches()) {
BearerTokenError error = new BearerTokenError(BearerTokenErrorCodes.INVALID_TOKEN,
HttpStatus.UNAUTHORIZED,
"Bearer token is malformed",
"https://tools.ietf.org/html/rfc6750#section-3.1");
throw new OAuth2AuthenticationException(error);
}

return matcher.group("token");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,28 @@ public void resolveWhenQueryParameterIsPresentAndNotSupportedThenTokenIsNotResol

assertThat(this.resolver.resolve(request)).isNull();
}

@Test
public void resolveWhenQueryParameterIsPresentWithMissingTokenThenAuthenticationExceptionIsThrown() {
this.resolver.setAllowUriQueryParameter(true);

MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.addParameter("access_token", "");

assertThatCode(() -> this.resolver.resolve(request)).isInstanceOf(OAuth2AuthenticationException.class)
.hasMessageContaining(("Bearer token is malformed"));
}

@Test
public void resolveWhenQueryParameterWithInvalidCharactersIsPresentThenAuthenticationExceptionIsThrown() {
this.resolver.setAllowUriQueryParameter(true);

MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.addParameter("access_token", "an\"invalid\"token");

assertThatCode(() -> this.resolver.resolve(request)).isInstanceOf(OAuth2AuthenticationException.class)
.hasMessageContaining(("Bearer token is malformed"));
}
}