Skip to content

Validate Scopes in ClientRegistration.Builder #6285

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

Merged
merged 1 commit into from
Dec 14, 2018
Merged
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 @@ -15,13 +15,6 @@
*/
package org.springframework.security.oauth2.client.registration;

import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.oauth2.core.AuthenticationMethod;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -31,6 +24,13 @@
import java.util.Map;
import java.util.Set;

import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.oauth2.core.AuthenticationMethod;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* A representation of a client registration with an OAuth 2.0 or OpenID Connect 1.0 Provider.
*
Expand Down Expand Up @@ -489,6 +489,7 @@ public ClientRegistration build() {
} else {
this.validateAuthorizationCodeGrantType();
}
this.validateScopes();
return this.create();
}

Expand Down Expand Up @@ -545,5 +546,27 @@ private void validateClientCredentialsGrantType() {
Assert.hasText(this.clientId, "clientId cannot be empty");
Assert.hasText(this.tokenUri, "tokenUri cannot be empty");
}

private void validateScopes() {
if (this.scopes == null) {
return;
}

for (String scope : this.scopes) {
Assert.isTrue(validateScope(scope), "scope \"" + scope + "\" contains invalid characters");
}
}

private static boolean validateScope(String scope) {
return scope == null ||
scope.chars().allMatch(c ->
withinTheRangeOf(c, 0x21, 0x21) ||
withinTheRangeOf(c, 0x23, 0x5B) ||
withinTheRangeOf(c, 0x5D, 0x7E));
}

private static boolean withinTheRangeOf(int c, int min, int max) {
return c >= min && c <= max;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,23 @@ public void buildWhenClientCredentialsGrantTokenUriIsNullThenThrowIllegalArgumen
.build()
).isInstanceOf(IllegalArgumentException.class);
}

// gh-6256
@Test
public void buildWhenScopesContainASpaceThenThrowIllegalArgumentException() {
assertThatThrownBy(() ->
TestClientRegistrations.clientCredentials()
.scope("openid profile email")
.build()
).isInstanceOf(IllegalArgumentException.class);
}

@Test
public void buildWhenScopesContainAnInvalidCharacterThenThrowIllegalArgumentException() {
assertThatThrownBy(() ->
TestClientRegistrations.clientCredentials()
.scope("an\"invalid\"scope")
.build()
).isInstanceOf(IllegalArgumentException.class);
}
}