Skip to content

Commit 3f32906

Browse files
vinhhieu21snicoll
authored andcommitted
Reject unknown JWS algorithms configured on NimbusJwtDecoder
See gh-50118 Signed-off-by: Hieu Bui Vinh <buivinhhieu217@gmail.com>
1 parent eceb603 commit 3f32906

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/reactive/ReactiveOAuth2ResourceServerJwkConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.boot.autoconfigure.security.oauth2.resource.ConditionalOnPublicKeyJwtDecoder;
3535
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
3636
import org.springframework.boot.context.properties.PropertyMapper;
37+
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
3738
import org.springframework.context.annotation.Bean;
3839
import org.springframework.context.annotation.Conditional;
3940
import org.springframework.context.annotation.Configuration;
@@ -103,7 +104,12 @@ ReactiveJwtDecoder jwtDecoder(ObjectProvider<JwkSetUriReactiveJwtDecoderBuilderC
103104

104105
private void jwsAlgorithms(Set<SignatureAlgorithm> signatureAlgorithms) {
105106
for (String algorithm : this.properties.getJwsAlgorithms()) {
106-
signatureAlgorithms.add(SignatureAlgorithm.from(algorithm));
107+
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.from(algorithm);
108+
if (signatureAlgorithm == null) {
109+
throw new InvalidConfigurationPropertyValueException(
110+
"spring.security.oauth2.resourceserver.jwt.jws-algorithms", algorithm, "Unknown algorithm");
111+
}
112+
signatureAlgorithms.add(signatureAlgorithm);
107113
}
108114
}
109115

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/servlet/OAuth2ResourceServerJwtConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.boot.autoconfigure.security.oauth2.resource.ConditionalOnPublicKeyJwtDecoder;
3636
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
3737
import org.springframework.boot.context.properties.PropertyMapper;
38+
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
3839
import org.springframework.context.annotation.Bean;
3940
import org.springframework.context.annotation.Conditional;
4041
import org.springframework.context.annotation.Configuration;
@@ -102,7 +103,12 @@ JwtDecoder jwtDecoderByJwkKeySetUri(ObjectProvider<JwkSetUriJwtDecoderBuilderCus
102103

103104
private void jwsAlgorithms(Set<SignatureAlgorithm> signatureAlgorithms) {
104105
for (String algorithm : this.properties.getJwsAlgorithms()) {
105-
signatureAlgorithms.add(SignatureAlgorithm.from(algorithm));
106+
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.from(algorithm);
107+
if (signatureAlgorithm == null) {
108+
throw new InvalidConfigurationPropertyValueException(
109+
"spring.security.oauth2.resourceserver.jwt.jws-algorithms", algorithm, "Unknown algorithm");
110+
}
111+
signatureAlgorithms.add(signatureAlgorithm);
106112
}
107113
}
108114

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/reactive/ReactiveOAuth2ResourceServerAutoConfigurationTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ void autoConfigurationUsingJwkSetUriShouldConfigureResourceServerUsingMultipleJw
177177
});
178178
}
179179

180+
@Test
181+
void autoConfigurationUsingJwkSetUriShouldFailIfJwsAlgorithmIsUnknown() {
182+
this.contextRunner
183+
.withPropertyValues("spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://jwk-set-uri.com",
184+
"spring.security.oauth2.resourceserver.jwt.jws-algorithms=NOT_VALID")
185+
.run((context) -> {
186+
assertThat(context).hasFailed();
187+
assertThat(context.getStartupFailure())
188+
.hasRootCauseMessage("Property spring.security.oauth2.resourceserver.jwt.jws-algorithms with value "
189+
+ "'NOT_VALID' is invalid: Unknown algorithm");
190+
});
191+
}
192+
180193
@Test
181194
@WithPublicKeyResource
182195
void autoConfigurationUsingPublicKeyValueShouldConfigureResourceServerUsingSingleJwsAlgorithm() {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/servlet/OAuth2ResourceServerAutoConfigurationTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ void autoConfigurationShouldConfigureResourceServerWithMultipleJwsAlgorithms() {
178178
});
179179
}
180180

181+
@Test
182+
void autoConfigurationUsingJwkSetUriShouldFailIfJwsAlgorithmIsUnknown() {
183+
this.contextRunner
184+
.withPropertyValues("spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://jwk-set-uri.com",
185+
"spring.security.oauth2.resourceserver.jwt.jws-algorithms=NOT_VALID")
186+
.run((context) -> {
187+
assertThat(context).hasFailed();
188+
assertThat(context.getStartupFailure())
189+
.hasRootCauseMessage("Property spring.security.oauth2.resourceserver.jwt.jws-algorithms with value "
190+
+ "'NOT_VALID' is invalid: Unknown algorithm");
191+
});
192+
}
193+
181194
@Test
182195
@WithPublicKeyResource
183196
void autoConfigurationUsingPublicKeyValueShouldConfigureResourceServerUsingSingleJwsAlgorithm() {

0 commit comments

Comments
 (0)