Skip to content

Commit be8e3fb

Browse files
committed
Avoid direct URL construction.
Fixes gh-2783
1 parent 50922f7 commit be8e3fb

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/RequestHeaderToRequestUriGatewayFilterFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import java.net.MalformedURLException;
2020
import java.net.URI;
21-
import java.net.URISyntaxException;
22-
import java.net.URL;
2321
import java.util.Arrays;
2422
import java.util.List;
2523
import java.util.Optional;
@@ -71,9 +69,11 @@ protected Optional<URI> determineRequestUri(ServerWebExchange exchange, NameConf
7169
String requestUrl = exchange.getRequest().getHeaders().getFirst(config.getName());
7270
return Optional.ofNullable(requestUrl).map(url -> {
7371
try {
74-
return new URL(url).toURI();
72+
URI uri = URI.create(url);
73+
uri.toURL(); // validate url
74+
return uri;
7575
}
76-
catch (MalformedURLException | URISyntaxException e) {
76+
catch (IllegalArgumentException | MalformedURLException e) {
7777
log.info("Request url is invalid : url={}, error={}", requestUrl, e.getMessage());
7878
return null;
7979
}

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/RequestHeaderToRequestUriGatewayFilterFactoryIntegrationTests.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Optional;
2121

2222
import com.fasterxml.jackson.databind.JsonNode;
23-
import org.junit.jupiter.api.Disabled;
2423
import org.junit.jupiter.api.Test;
2524

2625
import org.springframework.boot.SpringBootConfiguration;
@@ -48,15 +47,13 @@ public class RequestHeaderToRequestUriGatewayFilterFactoryIntegrationTests exten
4847
int port;
4948

5049
@Test
51-
@Disabled
5250
public void changeUriWorkWithProperties() {
5351
testClient.get().uri("/").header("Host", "www.changeuri.org")
5452
.header("X-CF-Forwarded-Url", "http://localhost:" + port + "/actuator/health").exchange()
5553
.expectBody(JsonNode.class).consumeWith(r -> assertThat(r.getResponseBody().has("status")).isTrue());
5654
}
5755

5856
@Test
59-
@Disabled
6057
public void changeUriWorkWithDsl() {
6158
testClient.get().uri("/").header("Host", "www.changeuri.org")
6259
.header("X-Next-Url", "http://localhost:" + port + "/actuator/health").exchange()

0 commit comments

Comments
 (0)