From b3a69dbcb44ed7c28b67ebfa5fa8e530464e454d Mon Sep 17 00:00:00 2001 From: spencergibb Date: Fri, 28 Aug 2020 11:07:09 -0400 Subject: [PATCH] Avoids possible null value in Principal.getName() Changes to get the Principal first via map, the return the name or empty if null. Fixes gh-1854 --- .../ratelimit/PrincipalNameKeyResolver.java | 5 +-- .../PrincipalNameKeyResolverTests.java | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolverTests.java diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolver.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolver.java index a36c7e56..a8afe105 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolver.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolver.java @@ -16,8 +16,6 @@ package org.springframework.cloud.gateway.filter.ratelimit; -import java.security.Principal; - import reactor.core.publisher.Mono; import org.springframework.web.server.ServerWebExchange; @@ -31,8 +29,7 @@ public class PrincipalNameKeyResolver implements KeyResolver { @Override public Mono resolve(ServerWebExchange exchange) { - return exchange.getPrincipal().map(Principal::getName) - .switchIfEmpty(Mono.empty()); + return exchange.getPrincipal().flatMap(p -> Mono.justOrEmpty(p.getName())); } } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolverTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolverTests.java new file mode 100644 index 00000000..581073fa --- /dev/null +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/PrincipalNameKeyResolverTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.gateway.filter.ratelimit; + +import java.security.Principal; +import java.time.Duration; + +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import org.springframework.web.server.ServerWebExchange; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class PrincipalNameKeyResolverTests { + + @Test + public void nullPrincipalNameWorks() { + PrincipalNameKeyResolver keyResolver = new PrincipalNameKeyResolver(); + ServerWebExchange exchange = mock(ServerWebExchange.class); + when(exchange.getPrincipal()).thenReturn(Mono.just(mock(Principal.class))); + StepVerifier.create(keyResolver.resolve(exchange)).expectComplete() + .verify(Duration.ofSeconds(5)); + } + +}