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:
Adarsh Sahu authored and GitHub committed 2026-11-21 18:14:39 -04:00
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();