To protect the credentials, the URL must use HTTPS. Plain HTTP is only allowed for - * loopback hosts (e.g. {@code localhost}) for testing purposes. + *
To protect the credentials, only HTTPS URLs for {@code api.cloudflare.com} are accepted. * * @param baseUrl the base URL to be used for API requests, {@code null} resets it to * {@link #DEFAULT_BASEURL} * @return the current instance of {@code CfDnsClientBuilder}, enabling method chaining - * @throws IllegalArgumentException if the URL is invalid or doesn't use HTTPS + * @throws IllegalArgumentException if the URL is invalid, doesn't use HTTPS or doesn't target + * {@code api.cloudflare.com} */ public CfDnsClientBuilder withBaseUrl(@Nullable String baseUrl) { this.baseUrl = baseUrl == null ? null : validateBaseUrl(baseUrl); @@ -124,7 +118,6 @@ public class CfDnsClientBuilder { } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid base URL: " + baseUrl, e); } - String scheme = uri.getScheme() == null ? "" : uri.getScheme().toLowerCase(Locale.ROOT); String host = uri.getHost(); if (host == null || host.isBlank()) { throw new IllegalArgumentException("Base URL must contain a host: " + baseUrl); @@ -132,9 +125,11 @@ public class CfDnsClientBuilder { if (uri.getRawQuery() != null || uri.getRawFragment() != null) { throw new IllegalArgumentException("Base URL must not contain a query or fragment: " + baseUrl); } - boolean https = "https".equals(scheme); - boolean loopbackHttp = "http".equals(scheme) && LOOPBACK_HOSTS.contains(host.toLowerCase(Locale.ROOT)); - if (!https && !loopbackHttp) { + if (uri.getRawUserInfo() != null) { + throw new IllegalArgumentException("Base URL must not contain user information: " + baseUrl); + } + String scheme = uri.getScheme() == null ? "" : uri.getScheme().toLowerCase(Locale.ROOT); + if (!"https".equals(scheme)) { throw new IllegalArgumentException("Base URL must use HTTPS: " + baseUrl); } String normalized = uri.toString(); diff --git a/src/test/java/codes/thischwa/cf/CfBasicHttpClientErrorTest.java b/src/test/java/codes/thischwa/cf/CfBasicHttpClientErrorTest.java index 0193b7f..35a0d9e 100644 --- a/src/test/java/codes/thischwa/cf/CfBasicHttpClientErrorTest.java +++ b/src/test/java/codes/thischwa/cf/CfBasicHttpClientErrorTest.java @@ -35,7 +35,6 @@ class CfBasicHttpClientErrorTest { private final AtomicInteger requestCount = new AtomicInteger(); private volatile int status; private volatile String body; - private volatile String lastPath; private volatile long delayMillis; @BeforeEach @@ -43,7 +42,6 @@ class CfBasicHttpClientErrorTest { server = HttpServer.create(new InetSocketAddress("localhost", 0), 0); server.createContext("/", exchange -> { requestCount.incrementAndGet(); - lastPath = exchange.getRequestURI().getRawPath(); if (delayMillis > 0) { try { Thread.sleep(delayMillis); @@ -58,10 +56,8 @@ class CfBasicHttpClientErrorTest { } }); server.start(); - client = new CfDnsClientBuilder() - .withApiTokenAuth("test-token") - .withBaseUrl("http://localhost:" + server.getAddress().getPort()) - .build(); + client = new CfDnsClient(false, "http://localhost:" + server.getAddress().getPort(), + new CfDnsClientBuilder.ApiTokenAuth("test-token")); } @AfterEach @@ -138,27 +134,14 @@ class CfBasicHttpClientErrorTest { assertTrue(e.getErrors().isEmpty()); } - @Test - void trailingSlashOfBaseUrlIsRemoved() throws Exception { - respond(200, ZONE_JSON); - try (CfDnsClient slashClient = new CfDnsClientBuilder() - .withApiTokenAuth("test-token") - .withBaseUrl("http://localhost:" + server.getAddress().getPort() + "/client/v4/") - .build()) { - slashClient.zoneList(); - } - assertEquals("/client/v4/zones", lastPath); - } - @Test void responseTimeoutIsApplied() throws Exception { respond(200, ZONE_JSON); delayMillis = 2000; - try (CfDnsClient timeoutClient = new CfDnsClientBuilder() - .withApiTokenAuth("test-token") - .withBaseUrl("http://localhost:" + server.getAddress().getPort()) - .withResponseTimeout(Duration.ofMillis(200)) - .build()) { + try (CfDnsClient timeoutClient = new CfDnsClient(false, + "http://localhost:" + server.getAddress().getPort(), + new CfDnsClientBuilder.ApiTokenAuth("test-token"), CfDnsClient.RECORDS_PER_PAGE, + CfDnsClientBuilder.DEFAULT_CONNECT_TIMEOUT, Duration.ofMillis(200))) { long start = System.nanoTime(); CloudflareApiException e = assertThrows(CloudflareApiException.class, timeoutClient::zoneList); long elapsedMillis = (System.nanoTime() - start) / 1_000_000; diff --git a/src/test/java/codes/thischwa/cf/CfDnsClientBuilderTest.java b/src/test/java/codes/thischwa/cf/CfDnsClientBuilderTest.java index cfca4f7..7b71fde 100644 --- a/src/test/java/codes/thischwa/cf/CfDnsClientBuilderTest.java +++ b/src/test/java/codes/thischwa/cf/CfDnsClientBuilderTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.lang.reflect.Method; import java.time.Duration; import org.junit.jupiter.api.Test; @@ -125,18 +126,6 @@ class CfDnsClientBuilderTest { assertNotNull(client); } - @Test - void testBuilderWithMultipleBaseUrls() { - // Test setting base URL multiple times (last one wins) - CfDnsClient client = new CfDnsClientBuilder() - .withApiTokenAuth("test-token") - .withBaseUrl("https://old-api.example.com") - .withBaseUrl("https://new-api.example.com") // This should override - .build(); - - assertNotNull(client); - } - @Test void testEmptyResultThrowsExceptionToggle() { // Test toggling the flag multiple times (last one wins) @@ -162,13 +151,21 @@ class CfDnsClientBuilderTest { } @Test - void testBaseUrlAllowsHttpForLoopbackOnly() { - assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t").withBaseUrl("http://localhost:8080").build()); - assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t").withBaseUrl("http://127.0.0.1:8080/v4").build()); - assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t").withBaseUrl("HTTPS://API.example.com/").build()); + void testBaseUrlAllowsOnlyCloudflareHttps() { + assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t") + .withBaseUrl("HTTPS://API.CLOUDFLARE.COM/client/v4/").build()); assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t").withBaseUrl(null).build()); } + @Test + void testBaseUrlTrailingSlashIsRemoved() throws ReflectiveOperationException { + Method validateBaseUrl = CfDnsClientBuilder.class.getDeclaredMethod("validateBaseUrl", String.class); + validateBaseUrl.setAccessible(true); + + assertEquals("HTTPS://API.CLOUDFLARE.COM/client/v4", + validateBaseUrl.invoke(null, "HTTPS://API.CLOUDFLARE.COM/client/v4/")); + } + @Test void testInvalidTimeoutsAreRejected() { CfDnsClientBuilder builder = new CfDnsClientBuilder();