mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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
This commit is contained in:
1 parent
1bcd7620bb
commit
3f64a4514a
6 files changed
+178
-20
No files matched your search
@@ -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());
|
||||
|
||||
@@ -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}. */
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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("""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
|
||||
<Name>sandbox.example.com</Name>
|
||||
<CallerReference>wildcard-list-ref</CallerReference>
|
||||
</CreateHostedZoneRequest>
|
||||
""")
|
||||
.when().post("/2013-04-01/hostedzone")
|
||||
.then().statusCode(201).extract().header("Location");
|
||||
String wildcardZoneId = location.substring(location.lastIndexOf('/') + 1);
|
||||
|
||||
try {
|
||||
given()
|
||||
.contentType(XML)
|
||||
.body("""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
|
||||
<ChangeBatch>
|
||||
<Changes>
|
||||
<Change>
|
||||
<Action>CREATE</Action>
|
||||
<ResourceRecordSet>
|
||||
<Name>*.sandbox.example.com</Name>
|
||||
<Type>A</Type>
|
||||
<TTL>60</TTL>
|
||||
<ResourceRecords>
|
||||
<ResourceRecord><Value>10.0.0.1</Value></ResourceRecord>
|
||||
</ResourceRecords>
|
||||
</ResourceRecordSet>
|
||||
</Change>
|
||||
</Changes>
|
||||
</ChangeBatch>
|
||||
</ChangeResourceRecordSetsRequest>
|
||||
""")
|
||||
.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("""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
|
||||
<ChangeBatch>
|
||||
<Changes>
|
||||
<Change>
|
||||
<Action>DELETE</Action>
|
||||
<ResourceRecordSet>
|
||||
<Name>\\052.sandbox.example.com.</Name>
|
||||
<Type>A</Type>
|
||||
<TTL>60</TTL>
|
||||
<ResourceRecords>
|
||||
<ResourceRecord><Value>10.0.0.1</Value></ResourceRecord>
|
||||
</ResourceRecords>
|
||||
</ResourceRecordSet>
|
||||
</Change>
|
||||
</Changes>
|
||||
</ChangeBatch>
|
||||
</ChangeResourceRecordSetsRequest>
|
||||
""")
|
||||
.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)
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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<ResourceRecordSet> 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<String> 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<String> 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<String> 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();
|
||||
|
||||
Reference in new issue
Block a user