From 398d34239ec3d1573b7619c79006735250f2d289 Mon Sep 17 00:00:00 2001 From: Mario Toffia Date: Tue, 24 Nov 2026 19:42:38 +0100 Subject: [PATCH] fix(dns): answer an own-suffix name with the emulator's address when its stored record has no resolvable target (#9360) Since #8926 a stored public zone under an emulator suffix answers A and AAAA, and a public alias to a target Floci does not serve, such as a Cognito custom domain aliased to CloudFront, got REFUSED, so a container asked public DNS and got its own loopback address. An A or AAAA standard query for a name under an emulator suffix that the stored zone refuses is now answered as the emulator's own name, after the VPC forwarding-rule check. A query with more than one question, a class other than IN, or a non-standard opcode or flag stays REFUSED. Closes #9359. --- docs/services/route53.md | 4 +- .../core/common/dns/EmbeddedDnsServer.java | 21 ++++++-- .../common/dns/EmbeddedDnsServerTest.java | 30 +++++++++++ .../dns/Route53DnsPacketIntegrationTest.java | 52 +++++++++++++++---- 4 files changed, 90 insertions(+), 17 deletions(-) diff --git a/docs/services/route53.md b/docs/services/route53.md index 51aea4bed0..4695b47227 100644 --- a/docs/services/route53.md +++ b/docs/services/route53.md @@ -79,11 +79,11 @@ For a Docker host client, publish UDP separately from HTTP on loopback, for exam The longest matching public zone answers. Missing names return NXDOMAIN; existing names without the requested supported type return NOERROR with no answers. Both include the zone SOA with the smaller of its TTL and minimum field for negative caching. Changes and deletes affect DNS through the same stored record sets as the Route 53 API. -Simple public `A` aliases to stored Application Load Balancers resolve to the receiving DNS listener's IPv4 address with a 60-second TTL, including the loopback address when only the native host listener is enabled. The target DNS name and canonical hosted-zone ID must match exactly one stored load balancer, including when the zone and balancer have different owners. Names are case-insensitive and accept the load balancer's `dualstack.` prefix. An `AAAA` alias to a valid target returns NOERROR without addresses because this listener has no IPv6 address. Deleted, ambiguous, unsupported, and mismatched targets return REFUSED without recursive DNS or emulator-suffix fallback. +Simple public `A` aliases to stored Application Load Balancers resolve to the receiving DNS listener's IPv4 address with a 60-second TTL, including the loopback address when only the native host listener is enabled. The target DNS name and canonical hosted-zone ID must match exactly one stored load balancer, including when the zone and balancer have different owners. Names are case-insensitive and accept the load balancer's `dualstack.` prefix. An `AAAA` alias to a valid target returns NOERROR without addresses because this listener has no IPv6 address. Deleted, ambiguous, unsupported, and mismatched targets return REFUSED without recursive DNS, except under an emulator suffix as described below. A single simple ALB alias resolves with either `EvaluateTargetHealth` setting. Route 53 still answers when all records are unhealthy, so the setting does not change the answer when there is only one record. -This provides DNS answers; routing an alias hostname through an ALB's HTTP or TLS listener is a separate data-plane capability. Other public alias target types, CNAME chasing, health-based or weighted record selection, and delegation referrals are not implemented. Queries for supported types that require these features, ambiguous duplicate public zones, or malformed stored records return REFUSED without claiming authority. Other query types retain the existing emulator-name and upstream resolution paths. Private-zone and VPC forwarding-rule precedence remains unchanged. UDP responses honor legacy/EDNS size limits and set truncation when necessary; TCP fallback is not implemented. +This provides DNS answers; routing an alias hostname through an ALB's HTTP or TLS listener is a separate data-plane capability. Other public alias target types, CNAME chasing, health-based or weighted record selection, and delegation referrals are not implemented. Queries for supported types that require these features, ambiguous duplicate public zones, or malformed stored records return REFUSED without claiming authority. An `A` or `AAAA` query that would be refused for a name under an emulator suffix such as `localhost.floci.io` gets the emulator-name answer instead: Floci's address for `A`, and NOERROR without addresses for `AAAA`. A query with more than one question, a class other than IN, or a non-standard opcode or flag stays REFUSED. A Cognito custom domain aliased to its CloudFront distribution in such a zone therefore resolves to Floci. Other query types retain the existing emulator-name and upstream resolution paths. Private-zone and VPC forwarding-rule precedence remains unchanged. UDP responses honor legacy/EDNS size limits and set truncation when necessary; TCP fallback is not implemented. ## Default Nameservers diff --git a/src/main/java/io/floci/aws/core/common/dns/EmbeddedDnsServer.java b/src/main/java/io/floci/aws/core/common/dns/EmbeddedDnsServer.java index 499ebe0222..a9aafada0c 100644 --- a/src/main/java/io/floci/aws/core/common/dns/EmbeddedDnsServer.java +++ b/src/main/java/io/floci/aws/core/common/dns/EmbeddedDnsServer.java @@ -317,8 +317,9 @@ public class EmbeddedDnsServer { short qtype = buf.getShort(); short qclass = buf.getShort(); int questionEnd = buf.position(); + boolean standardQuery = qdCount == 1 && qclass == 1 && (flags & 0x7840) == 0; - vertx.executeBlocking(() -> planQuery(qname, senderHost, myIp, qtype), false) + vertx.executeBlocking(() -> planQuery(qname, senderHost, myIp, qtype, standardQuery), false) .onSuccess(plan -> { if (plan.answer().isEmpty()) { if (!plan.ruleTargets().isEmpty()) { @@ -334,8 +335,7 @@ public class EmbeddedDnsServer { return; } DnsAnswer records = plan.answer().orElseThrow(); - if (records.isPublicAuthorityAnswer() - && (qdCount != 1 || qclass != 1 || (flags & 0x7840) != 0)) { + if (records.isPublicAuthorityAnswer() && !standardQuery) { records = DnsAnswer.refused(); } byte[] response = records.isPublicAuthorityAnswer() @@ -427,6 +427,10 @@ public class EmbeddedDnsServer { } } + QueryPlan planQuery(String name, String clientAddress, String myIp, int type) { + return planQuery(name, clientAddress, myIp, type, true); + } + /** * Stored public authority answers take precedence over broad emulator suffix spoofing. * Other names the emulator itself owns come first, whatever the rules say. Route 53 Resolver does @@ -436,9 +440,12 @@ public class EmbeddedDnsServer { * *

A forwarding rule associated with the querying VPC is then applied before Floci resolves * the name from a record source, because a Resolver rule takes precedence over a private - * hosted zone that matches the same name. + * hosted zone that matches the same name. An address query under an emulator suffix that the + * stored zone refuses, such as an alias to a target Floci does not serve, is then answered as + * the emulator's own name, unless the packet has a shape the zone refuses on its own: more + * than one question, a class other than IN, or a non-standard opcode or flag. */ - QueryPlan planQuery(String name, String clientAddress, String myIp, int type) { + QueryPlan planQuery(String name, String clientAddress, String myIp, int type, boolean standardQuery) { Optional stored = resolveFromRecordSources(name, type, true, myIp); Optional emulatorName = resolveEmulatorName(name, myIp, type); if (emulatorName.isPresent() && stored.isEmpty()) { @@ -450,6 +457,10 @@ public class EmbeddedDnsServer { if (rule.isPresent() && rule.orElseThrow().forwards()) { return QueryPlan.forwardingTo(rule.orElseThrow().targets()); } + if (standardQuery && matchesSuffix(name) && (type == 1 || type == 28) + && stored.filter(answer -> answer.responseCode() == 5).isPresent()) { + return QueryPlan.answering(emulatorName.orElseThrow()); + } return stored.or(() -> resolveFromRecordSources(name, type)) .map(QueryPlan::answering).orElse(QueryPlan.UPSTREAM); } diff --git a/src/test/java/io/floci/aws/core/common/dns/EmbeddedDnsServerTest.java b/src/test/java/io/floci/aws/core/common/dns/EmbeddedDnsServerTest.java index 5eb910904e..e5c69455bc 100644 --- a/src/test/java/io/floci/aws/core/common/dns/EmbeddedDnsServerTest.java +++ b/src/test/java/io/floci/aws/core/common/dns/EmbeddedDnsServerTest.java @@ -408,6 +408,36 @@ class EmbeddedDnsServerTest { dns.planQuery("example.com", CLIENT, MY_IP, 1).ruleTargets()); } + @Test + void planQuery_answersAnOwnNameWhoseStoredAddressIsRefusedLikeAnyOtherOwnName() { + DnsRecordSource refusing = new DnsRecordSource() { + @Override + public Optional resolveAuthoritative(String name, int type) { + return Optional.of(DnsAnswer.refused()); + } + + @Override + public Optional resolveIpv4(String name) { + return Optional.empty(); + } + }; + EmbeddedDnsServer dns = withRules(List.of(refusing), + rules("vpc-a", forward("rule.localhost.floci.io", "10.1.0.53")), clientVpc("vpc-a")); + + assertEquals(List.of(MY_IP), + dns.planQuery("api.auth.localhost.floci.io", CLIENT, MY_IP, 1).answer().orElseThrow().addresses()); + assertEquals(DnsAnswer.noData(), + dns.planQuery("api.auth.localhost.floci.io", CLIENT, MY_IP, 28).answer().orElseThrow()); + assertEquals(DnsAnswer.refused(), + dns.planQuery("api.auth.localhost.floci.io", CLIENT, MY_IP, 16).answer().orElseThrow()); + assertEquals(DnsAnswer.refused(), + dns.planQuery("api.auth.example.test", CLIENT, MY_IP, 1).answer().orElseThrow()); + assertEquals(DnsAnswer.refused(), + dns.planQuery("ip-172-16-128-9.ec2.internal", CLIENT, MY_IP, 1).answer().orElseThrow()); + assertEquals(List.of(new DnsForwardingRule.Target("10.1.0.53", 53)), + dns.planQuery("api.rule.localhost.floci.io", CLIENT, MY_IP, 1).ruleTargets()); + } + @Test void planQuery_keepsResolvingWhenARuleSourceThrows() { DnsForwardingRuleSource failing = (accountId, region, vpcId) -> { diff --git a/src/test/java/io/floci/aws/core/common/dns/Route53DnsPacketIntegrationTest.java b/src/test/java/io/floci/aws/core/common/dns/Route53DnsPacketIntegrationTest.java index eb5110de56..d6edf70a8d 100644 --- a/src/test/java/io/floci/aws/core/common/dns/Route53DnsPacketIntegrationTest.java +++ b/src/test/java/io/floci/aws/core/common/dns/Route53DnsPacketIntegrationTest.java @@ -2,6 +2,7 @@ package io.floci.aws.core.common.dns; import io.floci.aws.services.route53.Route53DnsRecordSource; import io.floci.aws.services.route53.Route53Service; +import io.floci.aws.services.route53.model.AliasTarget; import io.floci.aws.services.route53.model.ResourceRecord; import io.floci.aws.services.route53.model.ResourceRecordSet; import io.floci.aws.services.route53.model.VpcAssociation; @@ -296,22 +297,40 @@ class Route53DnsPacketIntegrationTest { } } + @ParameterizedTest + @CsvSource({"A,1,1", "AAAA,28,0"}) + void anOwnNameAliasToATargetFlociDoesNotServeAnswersAsTheEmulator(String recordType, int type, int answers) + throws Exception { + String zone = uniqueName() + ".localhost.floci.io"; + String id = createZone(zone); + String name = "api.auth." + zone; + AliasTarget cloudFront = new AliasTarget(); + cloudFront.setHostedZoneId("Z2FDTNDATAQYW2"); + cloudFront.setDnsName("d111111abcdef8.cloudfront.net."); + ResourceRecordSet alias = new ResourceRecordSet(); + alias.setName(name); + alias.setType(recordType); + alias.setAliasTarget(cloudFront); + change(id, "CREATE", alias); + + byte[] response = query(DnsTestQuery.query(name, type, 0x1234)); + assertPrivateHeader(response, 0, answers); + if (type == 1) { + assertArrayEquals(new DnsRecord.Address(1, "127.0.0.1").data(), + Arrays.copyOfRange(response, response.length - 4, response.length)); + } + for (int field : List.of(2, 4, -1, -2)) { + assertHeader(query(malformed(DnsTestQuery.query(name, type, 0x1234), field)), 5, false, 0, 0); + } + } + @Test void privateZonePrecedenceAndPublicQueryShapeRemainExplicit() throws Exception { String name = uniqueName() + ".example"; String id = createZone(name); change(id, "CREATE", record("marker." + name, "TXT", 60, "\"public\"")); for (int field : List.of(2, 4, -1, -2)) { - byte[] request = DnsTestQuery.query("marker." + name, 16, 0x1234); - ByteBuffer packet = ByteBuffer.wrap(request); - if (field == -2) { - packet.putShort(2, (short) 0x0140); - } else if (field == -1) { - packet.putShort(request.length - 2, (short) 3); - } else { - packet.putShort(field, (short) (field == 2 ? 0x0900 : 2)); - } - assertHeader(query(request), 5, false, 0, 0); + assertHeader(query(malformed(DnsTestQuery.query("marker." + name, 16, 0x1234), field)), 5, false, 0, 0); } String privateId = route53.createHostedZone(name, UUID.randomUUID().toString(), null, new VpcAssociation("vpc-dns-packet", "us-east-1")).zone().getId(); @@ -390,6 +409,19 @@ class Route53DnsPacketIntegrationTest { return "dns-" + UUID.randomUUID().toString().substring(0, 8); } + /** A shape a public zone refuses: opcode 1, two questions, class CH, or the reserved Z bit. */ + private static byte[] malformed(byte[] request, int field) { + ByteBuffer packet = ByteBuffer.wrap(request); + if (field == -2) { + packet.putShort(2, (short) 0x0140); + } else if (field == -1) { + packet.putShort(request.length - 2, (short) 3); + } else { + packet.putShort(field, (short) (field == 2 ? 0x0900 : 2)); + } + return request; + } + private static void assertPrivateHeader(byte[] response, int rcode, int answers) { ByteBuffer packet = ByteBuffer.wrap(response);