Skip to content

Commit 281ccff

Browse files
rs017991jzheaux
authored andcommitted
Fixed NPE in HttpsRedirectWebFilter
A more descriptive IllegalStateException is now thrown instead in the case that no such port mapping exists. Fixes: gh-6639
1 parent 8dd2864 commit 281ccff

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

web/src/main/java/org/springframework/security/web/server/transport/HttpsRedirectWebFilter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.security.web.server.transport;
1818

1919
import java.net.URI;
20+
import java.util.Optional;
2021

2122
import reactor.core.publisher.Mono;
2223

@@ -101,8 +102,9 @@ private URI createRedirectUri(ServerWebExchange exchange) {
101102
UriComponentsBuilder.fromUri(exchange.getRequest().getURI());
102103

103104
if (port > 0) {
104-
port = this.portMapper.lookupHttpsPort(port);
105-
builder.port(port);
105+
builder.port(Optional.ofNullable(this.portMapper.lookupHttpsPort(port))
106+
.orElseThrow(() -> new IllegalStateException(
107+
"HTTP Port '" + port + "' does not have a corresponding HTTPS Port")));
106108
}
107109

108110
return builder.scheme("https").build().toUri();

web/src/test/java/org/springframework/security/web/server/transport/HttpsRedirectWebFilterTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ public void filterWhenRequestIsInsecureThenPortMapperRemapsPort() {
112112
verify(portMapper).lookupHttpsPort(314);
113113
}
114114

115+
@Test
116+
public void filterWhenRequestIsInsecureAndNoPortMappingThenThrowsIllegalState() {
117+
ServerWebExchange exchange = get("http://localhost:1234");
118+
assertThatCode(() -> this.filter.filter(exchange, this.chain).block())
119+
.isInstanceOf(IllegalStateException.class);
120+
}
115121

116122
@Test
117123
public void filterWhenInsecureRequestHasAPathThenRedirects() {

0 commit comments

Comments
 (0)