- bug fixes: API errors are no longer wrapped into "Unexpected error!", zoneGet no longer crashes on unknown zones, getSubdomain handles apex and multi-level names, batch operations send the full record name, list methods fetch all pages, recordDelete handles empty results - rename "sld" to "subdomain": getSubdomain(), recordCreateSubdomain(), old methods deprecated; "@" (CfDnsClient.APEX) addresses the apex - one shared HTTP client with connection pooling, CfDnsClient implements AutoCloseable - CloudflareApiException provides HTTP status code and Cloudflare errors, HTTP 404 throws CloudflareNotFoundException - RecordEntity supports priority, data, comment and tags - recordCreate/recordUpdate send only writable attributes and don't modify the given record - breaking: recordList overloads replaced by recordListBySubdomain and recordListByType, timestamps are java.time.Instant - single record type is filtered server-side (?type=) - tests, README and changelog updated refs #2 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
4.6 KiB
Java
114 lines
4.6 KiB
Java
package codes.thischwa.cf;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
|
|
|
import codes.thischwa.cf.model.RecordType;
|
|
import codes.thischwa.cf.model.ZoneEntity;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
import org.junit.jupiter.api.DisplayName;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
/**
|
|
* Penetration-style tests with malicious and invalid inputs to ensure CfDnsClient behaves safely
|
|
* (throws appropriate exceptions, does not crash, and does not create unintended resources).
|
|
* <p>
|
|
* These tests will be skipped if API_EMAIL or API_KEY are not provided via environment variables.
|
|
*/
|
|
public class CfClientPenTest {
|
|
|
|
private static final String ZONE_STR = "mein-d-ns.de"; // existing baseline zone
|
|
|
|
private static final String API_TOKEN = System.getenv("API_TOKEN");
|
|
|
|
@BeforeAll
|
|
static void checkEnv() {
|
|
assumeTrue(API_TOKEN != null && !API_TOKEN.isBlank(), "API_TOKEN not set; skipping pen tests");
|
|
}
|
|
|
|
private CfDnsClient newClient() {
|
|
return new CfDnsClientBuilder().withEmptyResultThrowsException(true).withApiTokenAuth(API_TOKEN).build();
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Invalid credentials should not authenticate and must throw CloudflareApiException")
|
|
void testInvalidCredentialsShouldFail() {
|
|
// Use syntactically valid but wrong credentials
|
|
CfDnsClient badClient = new CfDnsClientBuilder().withEmailKeyAuth("invalid@example.com", UUID.randomUUID().toString()).build();
|
|
assertThrows(CloudflareApiException.class, badClient::zoneList);
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Malicious SLD inputs must not crash and should throw proper exception type")
|
|
void testMaliciousSldPatternsDoNotSucceed() throws Exception {
|
|
CfDnsClient client = newClient();
|
|
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
|
|
|
List<String> syntacticallyInvalidSlds =
|
|
List.of("; rm -rf /", "| cat /etc/passwd", "`shutdown -h now`",
|
|
"<script>alert('x')</script>", "\"quoted\"");
|
|
|
|
List<String> syntacticallyValidOrNotAllowedFromCloudflare =
|
|
List.of(".", "..", "../..", "..%2F..%2F", "a".repeat(300), "$(reboot)", "emoji-🥷-忍者",
|
|
"doesnotexist", "abcdef12345", "unwahrscheinlich-" + System.currentTimeMillis());
|
|
|
|
for (String sld : syntacticallyInvalidSlds) {
|
|
assertThrows(IllegalArgumentException.class, () -> client.recordListBySubdomain(zone, sld),
|
|
"Should throw IllegalArgumentException for invalid SLD '" + sld + "'");
|
|
}
|
|
for (String sld : syntacticallyValidOrNotAllowedFromCloudflare) {
|
|
assertThrows(CloudflareNotFoundException.class, () -> client.recordListBySubdomain(zone, sld),
|
|
"Should throw CloudflareNotFoundException for valid but non-existing SLD '" + sld + "'");
|
|
}
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Invalid record content and TTL boundaries must be rejected by API")
|
|
void testInvalidRecordCreateInputsRejected() throws Exception {
|
|
CfDnsClient client = newClient();
|
|
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
|
|
|
String sld = "pentest-" + System.currentTimeMillis();
|
|
String fqdn = sld + "." + ZONE_STR;
|
|
|
|
// Ensure clean state and guarantee cleanup later
|
|
try {
|
|
client.recordDeleteTypeIfExists(zone, sld, RecordType.A, RecordType.AAAA, RecordType.CNAME);
|
|
} catch (Exception ignored) {
|
|
}
|
|
|
|
try {
|
|
// A record with invalid IPv4
|
|
assertThrows(CloudflareApiException.class,
|
|
() -> client.recordCreate(zone, fqdn, 60, RecordType.A, "999.999.999.999"));
|
|
|
|
// AAAA record with non-IP content
|
|
assertThrows(CloudflareApiException.class,
|
|
() -> client.recordCreate(zone, fqdn, 60, RecordType.AAAA, "not-an-ipv6"));
|
|
|
|
// TTL boundary checks
|
|
assertThrows(CloudflareApiException.class,
|
|
() -> client.recordCreate(zone, fqdn, -1, RecordType.A, "130.0.0.3"));
|
|
} finally {
|
|
// Best-effort cleanup in case anything slipped through
|
|
assertDoesNotThrow(
|
|
() -> client.recordDeleteTypeIfExists(zone, sld, RecordType.A, RecordType.AAAA,
|
|
RecordType.CNAME));
|
|
}
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("recordDeleteTypeIfExists must be safe on non-existing SLD and types")
|
|
void testDeleteTypeIfExistsOnNonExistingIsSafe() throws Exception {
|
|
CfDnsClient client = newClient();
|
|
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
|
String randomSld = "nonexist-" + System.currentTimeMillis();
|
|
// Should not throw even if nothing exists
|
|
assertDoesNotThrow(
|
|
() -> client.recordDeleteTypeIfExists(zone, randomSld, RecordType.A, RecordType.AAAA));
|
|
}
|
|
}
|