Avoid direct URL construction.

Fixes gh-2783
This commit is contained in:
spencergibb
2022-11-16 10:37:00 -05:00
parent 50922f7dd8
commit be8e3fb2c5
2 changed files with 4 additions and 7 deletions

View File

@@ -18,8 +18,6 @@ package org.springframework.cloud.gateway.filter.factory;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
@@ -71,9 +69,11 @@ public class RequestHeaderToRequestUriGatewayFilterFactory
String requestUrl = exchange.getRequest().getHeaders().getFirst(config.getName());
return Optional.ofNullable(requestUrl).map(url -> {
try {
return new URL(url).toURI();
URI uri = URI.create(url);
uri.toURL(); // validate url
return uri;
}
catch (MalformedURLException | URISyntaxException e) {
catch (IllegalArgumentException | MalformedURLException e) {
log.info("Request url is invalid : url={}, error={}", requestUrl, e.getMessage());
return null;
}

View File

@@ -20,7 +20,6 @@ import java.net.URI;
import java.util.Optional;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringBootConfiguration;
@@ -48,7 +47,6 @@ public class RequestHeaderToRequestUriGatewayFilterFactoryIntegrationTests exten
int port;
@Test
@Disabled
public void changeUriWorkWithProperties() {
testClient.get().uri("/").header("Host", "www.changeuri.org")
.header("X-CF-Forwarded-Url", "http://localhost:" + port + "/actuator/health").exchange()
@@ -56,7 +54,6 @@ public class RequestHeaderToRequestUriGatewayFilterFactoryIntegrationTests exten
}
@Test
@Disabled
public void changeUriWorkWithDsl() {
testClient.get().uri("/").header("Host", "www.changeuri.org")
.header("X-Next-Url", "http://localhost:" + port + "/actuator/health").exchange()