From 3f64a4514adcf53ef6ddb81386ca59642a1943c4 Mon Sep 17 00:00:00 2001 From: Adarsh Sahu Date: Sun, 22 Nov 2026 03:44:39 +0530 Subject: [PATCH] fix(route53): escape wildcard record names as \052 in record responses (#9154) * fix(route53): escape wildcard record names as \052 in record responses * fix(route53): sort listed records by decoded name and use unique test order * refactor(route53): share decoding loop between spell and sortKey --- .../services/route53/Route53Controller.java | 4 +- .../services/route53/Route53RecordName.java | 35 +++++--- .../aws/services/route53/Route53Service.java | 21 +++-- .../route53/Route53IntegrationTest.java | 81 +++++++++++++++++++ .../route53/Route53RecordNameTest.java | 5 +- .../services/route53/Route53ServiceTest.java | 52 ++++++++++++ 6 files changed, 178 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/floci/aws/services/route53/Route53Controller.java b/src/main/java/io/floci/aws/services/route53/Route53Controller.java index 344b18fed8..5e85aafec9 100644 --- a/src/main/java/io/floci/aws/services/route53/Route53Controller.java +++ b/src/main/java/io/floci/aws/services/route53/Route53Controller.java @@ -513,7 +513,7 @@ public class Route53Controller { .elem("IsTruncated", String.valueOf(truncated)); if (truncated) { ResourceRecordSet next = fetched.get(maxItems); - xml.elem("NextRecordName", next.getName()) + xml.elem("NextRecordName", Route53Service.normalizeRecordName(next.getName())) .elem("NextRecordType", next.getType()); } xml.elem("MaxItems", String.valueOf(maxItems)) @@ -866,7 +866,7 @@ public class Route53Controller { private String xmlResourceRecordSet(ResourceRecordSet rrs) { XmlBuilder xml = new XmlBuilder() .start("ResourceRecordSet") - .elem("Name", rrs.getName()) + .elem("Name", Route53Service.normalizeRecordName(rrs.getName())) .elem("Type", rrs.getType()); if (rrs.getSetIdentifier() != null) xml.elem("SetIdentifier", rrs.getSetIdentifier()); if (rrs.getWeight() != null) xml.elem("Weight", rrs.getWeight()); diff --git a/src/main/java/io/floci/aws/services/route53/Route53RecordName.java b/src/main/java/io/floci/aws/services/route53/Route53RecordName.java index b3fa3c2873..db767fd0d9 100644 --- a/src/main/java/io/floci/aws/services/route53/Route53RecordName.java +++ b/src/main/java/io/floci/aws/services/route53/Route53RecordName.java @@ -18,24 +18,37 @@ public final class Route53RecordName { * "Normalizing the Route 53 condition key values"): without the final dot, and with {@code *} as {@code \052}. */ public static String conditionName(String name) { - return spell(name, false); + return format(name, true); } /** - * The name a record is stored and matched under: with the final dot, and with {@code *} kept as given, the - * spelling Floci's DNS server matches wildcard records by. + * The name a record is stored, matched and listed under: with the final dot and every byte outside + * {@code a-z}, {@code 0-9}, {@code -}, {@code _} and unescaped {@code .} written as a three-digit octal escape + * (including {@code *} as {@code \052}), as Route 53's API returns it. */ public static String recordName(String name) { if (name == null || name.isEmpty()) { return name; } - return spell(name, true) + "."; + return format(name, true) + "."; } - private static String spell(String name, boolean literalAsterisk) { + /** + * Decoded DNS name with trailing dot and lowercased ASCII letters, used for ordering and + * {@code startRecordName} comparisons so octal-escaped bytes (such as {@code \052} for {@code *}) + * sort by their underlying byte value rather than the leading backslash. + */ + public static String sortKey(String name) { + if (name == null || name.isEmpty()) { + return name; + } + return format(name, false) + "."; + } + + private static String format(String name, boolean escape) { String source = name.endsWith(".") ? name.substring(0, name.length() - 1) : name; byte[] bytes = source.getBytes(StandardCharsets.UTF_8); - StringBuilder spelled = new StringBuilder(bytes.length); + StringBuilder out = new StringBuilder(bytes.length); for (int index = 0; index < bytes.length; index++) { int value = bytes[index] & 0xFF; boolean escaped = value == '\\' && isEscapeCode(bytes, index + 1); @@ -46,14 +59,14 @@ public final class Route53RecordName { if (value >= 'A' && value <= 'Z') { value += 'a' - 'A'; } - if (value >= 'a' && value <= 'z' || value >= '0' && value <= '9' || value == '-' || value == '_' - || value == '*' && literalAsterisk || value == '.' && !escaped) { - spelled.append((char) value); + if (!escape || value >= 'a' && value <= 'z' || value >= '0' && value <= '9' + || value == '-' || value == '_' || value == '.' && !escaped) { + out.append((char) value); } else { - spelled.append(String.format(Locale.ROOT, "\\%03o", value)); + out.append(String.format(Locale.ROOT, "\\%03o", value)); } } - return spelled.toString(); + return out.toString(); } /** Whether the three bytes from {@code start} are the octal digits of a byte, {@code 000} to {@code 377}. */ diff --git a/src/main/java/io/floci/aws/services/route53/Route53Service.java b/src/main/java/io/floci/aws/services/route53/Route53Service.java index 5856e78b2d..e46222d323 100644 --- a/src/main/java/io/floci/aws/services/route53/Route53Service.java +++ b/src/main/java/io/floci/aws/services/route53/Route53Service.java @@ -645,17 +645,17 @@ public class Route53Service implements Resettable, RequestConditionSpelling { recordStore.get(zoneId).orElse(List.of())); records.sort((a, b) -> { - int cmp = normalizeRecordName(a.getName()).compareTo(normalizeRecordName(b.getName())); + int cmp = Route53RecordName.sortKey(a.getName()).compareTo(Route53RecordName.sortKey(b.getName())); if (cmp != 0) return cmp; return a.getType().compareTo(b.getType()); }); if (startName != null && !startName.isEmpty()) { - String normalizedStart = normalizeRecordName(startName); + String startKey = Route53RecordName.sortKey(startName); final String finalStartType = startType; records = records.stream() .filter(r -> { - int cmp = normalizeRecordName(r.getName()).compareTo(normalizedStart); + int cmp = Route53RecordName.sortKey(r.getName()).compareTo(startKey); if (cmp > 0) return true; if (cmp == 0 && finalStartType != null && !finalStartType.isEmpty()) { return r.getType().compareTo(finalStartType) >= 0; @@ -849,14 +849,14 @@ public class Route53Service implements Resettable, RequestConditionSpelling { String rName = normalizeName(rrs.getName()).toLowerCase(); if (rName.equals(normalizedQname)) { nameMatches.add(rrs); - } else if (rName.startsWith("*.")) { + } else if (wildcardSuffix(rName) != null) { zoneWildcards.add(rrs); } } if (nameMatches.isEmpty()) { for (ResourceRecordSet rrs : zoneWildcards) { String rName = normalizeName(rrs.getName()).toLowerCase(); - String wildcardSuffix = rName.substring(2); // e.g. "corp.internal." + String wildcardSuffix = wildcardSuffix(rName); // e.g. "corp.internal." if (normalizedQname.endsWith("." + wildcardSuffix)) { // A wildcard cannot match when the name or an ancestor closer than the wildcard's // parent exists, and a name with records only beneath it exists too (RFC 4592 2.2) @@ -873,6 +873,17 @@ public class Route53Service implements Resettable, RequestConditionSpelling { return !nameMatches.isEmpty() ? nameMatches : wildcardMatches; } + /** Parent domain of a wildcard record name, accepting both Route 53's {@code \052.} and legacy {@code *.} forms. */ + private static String wildcardSuffix(String rName) { + if (rName.startsWith("\\052.")) { + return rName.substring(5); + } + if (rName.startsWith("*.")) { + return rName.substring(2); + } + return null; + } + /** Length of the longest whole-label suffix two normalized names share, trailing dot included. */ private static int sharedSuffixLength(String a, String b) { int i = a.length(); diff --git a/src/test/java/io/floci/aws/services/route53/Route53IntegrationTest.java b/src/test/java/io/floci/aws/services/route53/Route53IntegrationTest.java index e45689f131..93e3758aaa 100644 --- a/src/test/java/io/floci/aws/services/route53/Route53IntegrationTest.java +++ b/src/test/java/io/floci/aws/services/route53/Route53IntegrationTest.java @@ -714,6 +714,87 @@ class Route53IntegrationTest { given().when().delete("/2013-04-01/hostedzone/" + zoneId).then().statusCode(200); } + @Test + @Order(27) + void listResourceRecordSets_escapesWildcardRecordNameAsOctal() { + String location = given() + .contentType(XML) + .body(""" + + + sandbox.example.com + wildcard-list-ref + + """) + .when().post("/2013-04-01/hostedzone") + .then().statusCode(201).extract().header("Location"); + String wildcardZoneId = location.substring(location.lastIndexOf('/') + 1); + + try { + given() + .contentType(XML) + .body(""" + + + + + + CREATE + + *.sandbox.example.com + A + 60 + + 10.0.0.1 + + + + + + + """) + .when().post("/2013-04-01/hostedzone/" + wildcardZoneId + "/rrset") + .then().statusCode(200); + + given() + .queryParam("name", "\\052.sandbox.example.com.") + .queryParam("type", "A") + .queryParam("maxitems", 1) + .when().get("/2013-04-01/hostedzone/" + wildcardZoneId + "/rrset") + .then() + .statusCode(200) + .body("ListResourceRecordSetsResponse.ResourceRecordSets.ResourceRecordSet[0].Name", + equalTo("\\052.sandbox.example.com.")) + .body("ListResourceRecordSetsResponse.ResourceRecordSets.ResourceRecordSet[0].Type", + equalTo("A")); + } finally { + given() + .contentType(XML) + .body(""" + + + + + + DELETE + + \\052.sandbox.example.com. + A + 60 + + 10.0.0.1 + + + + + + + """) + .when().post("/2013-04-01/hostedzone/" + wildcardZoneId + "/rrset"); + given().when().delete("/2013-04-01/hostedzone/" + wildcardZoneId); + } + } + private String createZoneForCommentTest(String callerReference, String comment) { String location = given() .contentType(XML) diff --git a/src/test/java/io/floci/aws/services/route53/Route53RecordNameTest.java b/src/test/java/io/floci/aws/services/route53/Route53RecordNameTest.java index 18700b3469..76ed5bf79e 100644 --- a/src/test/java/io/floci/aws/services/route53/Route53RecordNameTest.java +++ b/src/test/java/io/floci/aws/services/route53/Route53RecordNameTest.java @@ -19,9 +19,10 @@ class Route53RecordNameTest { } @Test - void recordIdentityDecodesLettersAndKeepsWildcardsAndEscapedDots() { + void recordIdentityDecodesLettersAndEscapesWildcardsAndEscapedDots() { assertEquals("a.example.test.", Route53RecordName.recordName("\\101.Example.test")); - assertEquals("*.example.test.", Route53RecordName.recordName("\\052.Example.test")); + assertEquals("\\052.example.test.", Route53RecordName.recordName("*.Example.test")); + assertEquals("\\052.example.test.", Route53RecordName.recordName("\\052.Example.test")); assertEquals(Route53RecordName.recordName("*a.example.test"), Route53RecordName.recordName("\\052a.example.test")); assertNotEquals(Route53RecordName.recordName("a\\056b.example.test"), diff --git a/src/test/java/io/floci/aws/services/route53/Route53ServiceTest.java b/src/test/java/io/floci/aws/services/route53/Route53ServiceTest.java index 1ea37211cb..a04d3749b5 100644 --- a/src/test/java/io/floci/aws/services/route53/Route53ServiceTest.java +++ b/src/test/java/io/floci/aws/services/route53/Route53ServiceTest.java @@ -721,6 +721,58 @@ class Route53ServiceTest { assertThat(service.listHealthChecks(null, 0), hasSize(1)); } + @Test + void wildcardRecordIsListedWithOctalEscapedAsteriskAndMatchesStartRecordName() { + Route53Service service = newService(); + String zone = service.createHostedZone("sandbox.example.com", "wildcard-escape", null, null).zone().getId(); + service.changeResourceRecordSets(zone, List.of(change("CREATE", + aRecord("*.sandbox.example.com", 60, "10.0.0.1"))), null); + + List listed = service.listResourceRecordSets(zone, "\\052.sandbox.example.com.", "A", 1); + assertEquals(1, listed.size()); + assertEquals("\\052.sandbox.example.com.", listed.getFirst().getName()); + assertEquals("A", listed.getFirst().getType()); + + // Creating the same record with the escaped spelling must be rejected as a duplicate. + AwsException dup = assertThrows(AwsException.class, () -> service.changeResourceRecordSets(zone, + List.of(change("CREATE", aRecord("\\052.sandbox.example.com.", 60, "10.0.0.2"))), null)); + assertEquals("InvalidChangeBatch", dup.getErrorCode()); + + // Deleting by either literal or escaped spelling succeeds. + service.changeResourceRecordSets(zone, List.of(change("DELETE", + aRecord("\\052.sandbox.example.com.", 60, "10.0.0.1"))), null); + assertTrue(service.listResourceRecordSets(zone, null, null, 100).stream() + .noneMatch(r -> "A".equals(r.getType()))); + } + + @Test + void wildcardRecordSortsBeforeNumericAndAlphaSiblingsAndHonorsStartName() { + Route53Service service = newService(); + String zone = service.createHostedZone("sandbox.example.com", "wildcard-sort", null, null).zone().getId(); + service.changeResourceRecordSets(zone, List.of( + change("CREATE", aRecord("a.sandbox.example.com", 60, "10.0.0.3")), + change("CREATE", aRecord("0.sandbox.example.com", 60, "10.0.0.2")), + change("CREATE", aRecord("*.sandbox.example.com", 60, "10.0.0.1"))), null); + + List aNames = service.listResourceRecordSets(zone, null, null, 100).stream() + .filter(r -> "A".equals(r.getType())) + .map(ResourceRecordSet::getName) + .toList(); + assertEquals(List.of("\\052.sandbox.example.com.", "0.sandbox.example.com.", "a.sandbox.example.com."), aNames); + + List fromZero = service.listResourceRecordSets(zone, "0.sandbox.example.com.", "A", 100).stream() + .filter(r -> "A".equals(r.getType())) + .map(ResourceRecordSet::getName) + .toList(); + assertEquals(List.of("0.sandbox.example.com.", "a.sandbox.example.com."), fromZero); + + List fromWildcard = service.listResourceRecordSets(zone, "*.sandbox.example.com.", "A", 100).stream() + .filter(r -> "A".equals(r.getType())) + .map(ResourceRecordSet::getName) + .toList(); + assertEquals(List.of("\\052.sandbox.example.com.", "0.sandbox.example.com.", "a.sandbox.example.com."), fromWildcard); + } + @Test void createHealthCheck_sameReferenceWithDifferentSettings_isHealthCheckAlreadyExists() { Route53Service service = newService();