fix(route53): require exact match for ChangeResourceRecordSets DELETE (#3565)

* fix(route53): require exact match for ChangeResourceRecordSets DELETE

DELETE validation only compared record name and type, so a DELETE
naming the wrong TTL, values, alias target, routing fields or set
identifier could remove an existing record set as long as the name
and type happened to match. AWS requires a DELETE to specify every
value used to create the record; a mismatch on any of them fails
with InvalidChangeBatch, using one of two different messages, with
no trailing period: "not found" when no record shares the same
name, type and set identifier, or "the values provided do not match
the current values" when one does but another field differs. Both
messages include the set identifier when the deleted record
specifies one.

Add an exact-match comparison covering TTL, resource record values
(order-independent), alias target, weight, region, failover, health
check ID and set identifier, and use it for both the DELETE
existence check and the removal itself.

Batch changes are now validated and applied sequentially against a
running copy of the zone's records, rather than validated as a whole
against the pre-batch snapshot and then applied. This matches two
documented AWS behaviors: deleting the same record set twice in one
batch now fails with InvalidChangeBatch, and a DELETE followed by a
CREATE of the same name and type in one batch (the standard replace
pattern) now succeeds. The batch is still applied atomically: the
running copy is a local list and the stores are only written once
every change in the batch has succeeded.

Also stop silently swallowing ChangeBatch parsing errors: malformed
XML, a Change missing its Action or ResourceRecordSet, an action
outside CREATE/DELETE/UPSERT, and a non-numeric TTL or Weight now
fail with InvalidInput instead of being ignored or dropped. A
ResourceRecordSet must also specify exactly one of AliasTarget or
TTL and ResourceRecords, matching AWS's structural requirement: real
AWS only rejects a ResourceRecordSet as having more than one shape
when AliasTarget and ResourceRecords are both present, so AliasTarget
combined with TTL alone remains valid. A ChangeBatch with no Changes
is rejected with InvalidInput instead of returning success and
applying nothing, using AWS's own schema-validation wire message for
an empty Changes element, built from the same Route53 namespace
constant used elsewhere in these responses.

* fix(route53): reject AliasTarget combined with TTL
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-14 15:18:53 -04:00
1 parent 2f76640aa8
commit 9da589568d
4 files changed
+814 -26

No files matched your search

@@ -61,6 +61,8 @@ public class Route53Controller {
"af-south-1", "eu-south-1", "eu-south-2",
"il-central-1", "mx-central-1");
private static final Set<String> CHANGE_ACTIONS = Set.of("CREATE", "DELETE", "UPSERT");
@Inject
Route53Service service;
@@ -893,14 +895,42 @@ public class Route53Controller {
}
}
private static void requireExactlyOneRecordShape(String action, ResourceRecordSet rrs, boolean hasRecords) {
boolean hasAlias = rrs.getAliasTarget() != null;
boolean hasTtl = rrs.getTtl() != null;
String found;
if (hasAlias && (hasTtl || hasRecords)) {
found = "more than one";
} else if (!hasAlias && !(hasTtl && hasRecords)) {
found = "none";
} else {
return;
}
throw new AwsException("InvalidInput",
"Invalid request: Expected exactly one of [AliasTarget, all of [TTL, and ResourceRecords], "
+ "or TrafficPolicyInstanceId], but found " + found + " in Change with [Action=" + action
+ ", Name=" + rrs.getName() + ", Type=" + rrs.getType()
+ ", SetIdentifier=" + rrs.getSetIdentifier() + "]", 400);
}
/**
* Parses the ChangeBatch XML using StAX to correctly handle multiple Change elements,
* each containing a ResourceRecordSet with its own set of ResourceRecord/Value children.
*/
private List<Map<String, Object>> parseChangeBatch(String body) {
List<Map<String, Object>> result = new ArrayList<>();
if (body == null || body.isEmpty()) return result;
if (body != null && !body.isEmpty()) {
parseChangeBatchInto(body, result);
}
if (result.isEmpty()) {
throw new AwsException("InvalidInput",
"Invalid XML ; cvc-complex-type.2.4.b: The content of element 'Changes' is not complete. "
+ "One of '{\"" + NS + "\":Change}' is expected.", 400);
}
return result;
}
private void parseChangeBatchInto(String body, List<Map<String, Object>> result) {
try {
XMLStreamReader r = XmlParser.newStreamReader(body);
String currentAction = null;
@@ -930,7 +960,14 @@ public class Route53Controller {
}
}
case "Action" -> {
if (inChange && !inRrs) currentAction = r.getElementText();
if (inChange && !inRrs) {
currentAction = r.getElementText();
if (!CHANGE_ACTIONS.contains(currentAction)) {
throw new AwsException("InvalidInput",
"Invalid value '" + currentAction + "' at 'Action' failed to satisfy "
+ "constraint: Member must satisfy enum value set.", 400);
}
}
}
case "ResourceRecordSet" -> {
if (inChange) {
@@ -960,8 +997,14 @@ public class Route53Controller {
}
case "TTL" -> {
if (inRrs && currentRrs != null) {
try { currentRrs.setTtl(Long.parseLong(r.getElementText())); }
catch (NumberFormatException ignored) {}
String ttl = r.getElementText();
try {
currentRrs.setTtl(Long.parseLong(ttl));
} catch (NumberFormatException e) {
throw new AwsException("InvalidInput",
"Invalid value '" + ttl + "' at 'TTL' failed to satisfy constraint: "
+ "Member must be a valid long.", 400);
}
}
}
case "Value" -> {
@@ -974,8 +1017,14 @@ public class Route53Controller {
}
case "Weight" -> {
if (inRrs && currentRrs != null) {
try { currentRrs.setWeight(Long.parseLong(r.getElementText())); }
catch (NumberFormatException ignored) {}
String weight = r.getElementText();
try {
currentRrs.setWeight(Long.parseLong(weight));
} catch (NumberFormatException e) {
throw new AwsException("InvalidInput",
"Invalid value '" + weight + "' at 'Weight' failed to satisfy "
+ "constraint: Member must be a valid long.", 400);
}
}
}
case "Region" -> {
@@ -1013,13 +1062,26 @@ public class Route53Controller {
currentAlias = null;
}
case "ResourceRecordSet" -> {
if (inRrs && currentRrs != null && currentRecords != null) {
if (!currentRecords.isEmpty()) currentRrs.setRecords(currentRecords);
if (inRrs && currentRrs != null) {
if (currentRrs.getName() == null || currentRrs.getType() == null) {
throw new AwsException("InvalidInput",
"ResourceRecordSet is missing a required Name or Type element.", 400);
}
boolean hasRecords = currentRecords != null && !currentRecords.isEmpty();
if (hasRecords) {
currentRrs.setRecords(currentRecords);
}
requireExactlyOneRecordShape(currentAction, currentRrs, hasRecords);
}
inRrs = false;
}
case "Change" -> {
if (inChange && currentAction != null && currentRrs != null) {
if (inChange) {
if (currentAction == null || currentRrs == null) {
throw new AwsException("InvalidInput",
"Change is missing a required Action or ResourceRecordSet element.",
400);
}
Map<String, Object> change = new HashMap<>();
change.put("action", currentAction);
change.put("rrs", currentRrs);
@@ -1034,8 +1096,11 @@ public class Route53Controller {
}
}
r.close();
} catch (Exception ignored) {}
return result;
} catch (AwsException e) {
throw e;
} catch (Exception e) {
throw new AwsException("InvalidInput", "The XML you provided was not well-formed.", 400);
}
}
private HealthCheckConfig parseHealthCheckConfig(String body) {
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.route53.model.AliasTarget;
import io.github.hectorvent.floci.services.route53.model.ChangeInfo;
import io.github.hectorvent.floci.services.route53.model.HealthCheck;
import io.github.hectorvent.floci.services.route53.model.HealthCheckConfig;
@@ -25,6 +26,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@@ -385,17 +387,10 @@ public class Route53Service {
List<ResourceRecordSet> current = new ArrayList<>(
recordStore.get(zoneId).orElse(new ArrayList<>()));
// Validate all changes before applying any
for (Map<String, Object> change : changes) {
String action = (String) change.get("action");
ResourceRecordSet rrs = (ResourceRecordSet) change.get("rrs");
validateChange(action, rrs, current, zone.getName());
}
// Apply all changes
for (Map<String, Object> change : changes) {
String action = (String) change.get("action");
ResourceRecordSet rrs = (ResourceRecordSet) change.get("rrs");
applyChange(action, rrs, current);
}
@@ -774,12 +769,16 @@ public class Route53Service {
}
}
if ("DELETE".equals(action)) {
boolean found = current.stream().anyMatch(r ->
r.getName().equals(rrs.getName()) && r.getType().equals(rrs.getType()));
if (!found) {
ResourceRecordSet existing = findByNameTypeAndSetIdentifier(current, rrs);
if (existing == null) {
throw new AwsException("InvalidChangeBatch",
"Tried to delete resource record set [name='" + rrs.getName() +
"', type='" + rrs.getType() + "'] but it was not found.", 400);
"Tried to delete resource record set " + deleteTargetDescription(rrs)
+ " but it was not found", 400);
}
if (!recordSetsMatch(existing, rrs)) {
throw new AwsException("InvalidChangeBatch",
"Tried to delete resource record set " + deleteTargetDescription(rrs)
+ " but the values provided do not match the current values", 400);
}
}
}
@@ -787,9 +786,7 @@ public class Route53Service {
private void applyChange(String action, ResourceRecordSet rrs, List<ResourceRecordSet> current) {
switch (action) {
case "CREATE" -> current.add(rrs);
case "DELETE" -> current.removeIf(r ->
r.getName().equals(rrs.getName()) && r.getType().equals(rrs.getType()) &&
equalOrNull(r.getSetIdentifier(), rrs.getSetIdentifier()));
case "DELETE" -> current.removeIf(r -> recordSetsMatch(r, rrs));
case "UPSERT" -> {
current.removeIf(r ->
r.getName().equals(rrs.getName()) && r.getType().equals(rrs.getType()) &&
@@ -804,4 +801,50 @@ public class Route53Service {
if (a == null || b == null) return false;
return a.equals(b);
}
private static ResourceRecordSet findByNameTypeAndSetIdentifier(List<ResourceRecordSet> current,
ResourceRecordSet rrs) {
return current.stream()
.filter(r -> r.getName().equals(rrs.getName())
&& r.getType().equals(rrs.getType())
&& Objects.equals(r.getSetIdentifier(), rrs.getSetIdentifier()))
.findFirst()
.orElse(null);
}
private static String deleteTargetDescription(ResourceRecordSet rrs) {
String description = "[name='" + rrs.getName() + "', type='" + rrs.getType() + "'";
if (rrs.getSetIdentifier() != null) {
description += ", set-identifier='" + rrs.getSetIdentifier() + "'";
}
return description + "]";
}
private static boolean recordSetsMatch(ResourceRecordSet a, ResourceRecordSet b) {
return a.getName().equals(b.getName())
&& a.getType().equals(b.getType())
&& Objects.equals(a.getSetIdentifier(), b.getSetIdentifier())
&& Objects.equals(a.getTtl(), b.getTtl())
&& Objects.equals(a.getWeight(), b.getWeight())
&& Objects.equals(a.getRegion(), b.getRegion())
&& Objects.equals(a.getFailover(), b.getFailover())
&& Objects.equals(a.getHealthCheckId(), b.getHealthCheckId())
&& aliasTargetsMatch(a.getAliasTarget(), b.getAliasTarget())
&& recordValuesMatch(a.getRecords(), b.getRecords());
}
private static boolean aliasTargetsMatch(AliasTarget a, AliasTarget b) {
if (a == null || b == null) {
return a == b;
}
return Objects.equals(a.getHostedZoneId(), b.getHostedZoneId())
&& Objects.equals(a.getDnsName(), b.getDnsName())
&& a.isEvaluateTargetHealth() == b.isEvaluateTargetHealth();
}
private static boolean recordValuesMatch(List<ResourceRecord> a, List<ResourceRecord> b) {
List<String> av = a == null ? List.of() : a.stream().map(ResourceRecord::getValue).sorted().toList();
List<String> bv = b == null ? List.of() : b.stream().map(ResourceRecord::getValue).sorted().toList();
return av.equals(bv);
}
}