]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: acme: match DNS challenges against all TXT records
authorWilliam Lallemand <wlallemand@haproxy.com>
Mon, 23 Nov 2026 18:07:54 +0000 (18:07 +0000)
committerWilliam Lallemand <wlallemand@haproxy.com>
Tue, 24 Nov 2026 09:25:04 +0000 (10:25 +0100)
A certificate covering a domain and its wildcard requires two dns-01 TXT
values at the same _acme-challenge name. The DNS parser retains both
records, but the ACME resolver callback only copied the first one. With
challenge-ready dns, one authorization could therefore never match its
expected value and renewal would time out.

Copy all TXT records into an owned singly linked list in one response-tree
traversal, using istdup() for each value. Match each challenge against any
entry and free the complete list on replacement, allocation failure, and
resolver teardown. Keep successful empty-list responses valid as before
and leave CLI readiness unchanged.

This should be backported to 3.4, where the DNS propagation pre-check was
introduced by 631fd5f99b.

doc/configuration.txt
include/haproxy/acme_resolvers-t.h
src/acme.c
src/acme_resolvers.c

index 42f6959489cdc7e9cd4672b5de3392115653c168..b8ca23b9bbc3f4a792f5bd257bc87767af169f72 100644 (file)
@@ -34688,17 +34688,17 @@ challenge-ready <value>[,<value>]*
            It is also possible to signal the "cli" readiness using the
            ACME.challenge_ready() lua function.
 
-    dns  - perform a DNS pre-check by resolving the TXT record for
+    dns  - perform a DNS pre-check by resolving the TXT records for
            "_acme-challenge.<domain>" using the configured "default" resolvers
            section, not the authoritative name servers. The challenge is not
-           submitted until the TXT record matches the expected token. Results
+           submitted until a TXT record matches the expected token. Results
            may therefore be affected by DNS caching at the resolver level. The
            delay between resolution attempts is controlled by "dns-delay". This
            option is independent of the CLI command, so no human intervention
            is required.
 
-           For dns-01, the TXT record at "_acme-challenge.<domain>" is
-           resolved and must match the expected token. For dns-persist-01,
+           For dns-01, the TXT records at "_acme-challenge.<domain>" are
+           resolved and one must match the expected token. For dns-persist-01,
            the TXT record at "_validation-persist.<domain>" is resolved and
            only its presence is checked.
 
index dde06121035e46909a58dfba519aabb7368e1134..491ef03e87846731c22caa1fdb1376ee705c1cbb 100644 (file)
@@ -8,6 +8,11 @@
 struct dns_counters;
 
 /* TXT records for dns-01 */
+struct acme_rslv_txt {
+       struct acme_rslv_txt *next;
+       struct ist value;                        /* owned TXT value */
+};
+
 
 struct acme_rslv {
        enum obj_type obj_type;                   /* OBJ_TYPE_ACME_RSLV */
@@ -19,7 +24,7 @@ struct acme_rslv {
        int result;                               /* RSLV_STATUS_* — NONE until done */
        int error_code;                           /* RSLV_RESP_* from the error callback */
        struct task *acme_task;                    /* ACME task to wake on completion, or NULL */
-       struct ist txt;                           /* first TXT record found */
+       struct acme_rslv_txt *txt;                /* owned list of TXT records */
        int (*success_cb)(struct resolv_requester *, struct dns_counters *);
        int (*error_cb)(struct resolv_requester *, int);
 };
index a1da89bd0c9d78feea6c91470142bd19cb206867..377af2a7dfcdc3c46579443eb54555e96654e1cd 100644 (file)
@@ -3013,20 +3013,24 @@ re:
                        for (auth = ctx->auths; auth != NULL; auth = auth->next) {
                                if (auth->ready == ctx->cfg->cond_ready)
                                        continue;
-                               /* for dns-01, verify the TXT record content matches the
-                                * expected token. for dns-persist-01, only check that
-                                * the record exists since the resolver cannot read
-                                * multiple strings within a single TXT entry */
+                               /* For dns-01, at least one TXT record must match the expected
+                                * token. For dns-persist-01, only check that a record exists.
+                                */
                                if (auth->rslv->result == RSLV_STATUS_VALID) {
                                        if (strcasecmp(ctx->cfg->challenge, "dns-01") == 0) {
-                                               if (isteq(auth->rslv->txt, auth->token)) {
+                                               struct acme_rslv_txt *txt;
+
+                                               for (txt = auth->rslv->txt; txt != NULL; txt = txt->next) {
+                                                       if (isteq(txt->value, auth->token))
+                                                               break;
+                                               }
+                                               if (txt) {
                                                        auth->ready |= ACME_RDY_DNS;
                                                } else {
                                                        send_log(NULL, LOG_NOTICE,
-                                                       "acme: %s: dns-01: TXT record mismatch for \"_acme-challenge.%.*s\": expected \"%.*s\", got \"%.*s\"\n",
+                                                       "acme: %s: dns-01: no matching TXT record for \"_acme-challenge.%.*s\": expected \"%.*s\"\n",
                                                                 ctx->store->path, (int)auth->dns.len, auth->dns.ptr,
-                                                                (int)auth->token.len, auth->token.ptr,
-                                                                (int)auth->rslv->txt.len, auth->rslv->txt.ptr);
+                                                                (int)auth->token.len, auth->token.ptr);
                                                        all_ready = 0;
                                                }
                                        } else if (strcasecmp(ctx->cfg->challenge, "dns-persist-01") == 0) {
index 0659b757b3b162eb446d7494964dfaca2993f513..68ab49b90f6db321f1220c003ab94b7077daa774 100644 (file)
 #include <haproxy/obj_type.h>
 #include <haproxy/resolvers.h>
 
-/* success callback, copy the TXT string to rslv->txt */
+static void acme_rslv_clear_txt(struct acme_rslv *rslv)
+{
+       struct acme_rslv_txt *txt;
+
+       while (rslv->txt) {
+               txt = rslv->txt;
+               rslv->txt = txt->next;
+               istfree(&txt->value);
+               free(txt);
+       }
+}
+
+/* Success callback, copy all TXT records to rslv->txt. */
 static int acme_rslv_success_cb(struct resolv_requester *req, struct dns_counters *counters)
 {
        struct acme_rslv *rslv = objt_acme_rslv(req->owner);
        struct resolv_resolution *res;
        struct eb32_node *eb32;
        struct resolv_answer_item *item;
+       struct acme_rslv_txt *txt;
 
        if (!rslv)
                return 1;
 
        rslv->result = RSLV_STATUS_INVALID;
+       acme_rslv_clear_txt(rslv);
 
        res = req->resolution;
        if (!res)
                goto done;
 
-       /* XXX: must fail on multiple TXT entries for the same dn */
-
-       /* copy the data from the response tree */
+       /* The response tree is shared and its records may be purged after this
+        * callback. Copy each TXT record into an owned list in a single pass.
+        */
        for (eb32 = eb32_first(&res->response.answer_tree); eb32 != NULL; eb32 = eb32_next(eb32)) {
                item = eb32_entry(eb32, typeof(*item), link);
-               /* only handle 1 entry */
-               if (item->type == DNS_RTYPE_TXT) {
-                       int len = item->data_len;
-
-                       if (len > DNS_MAX_NAME_SIZE)
-                               len = DNS_MAX_NAME_SIZE;
-                       rslv->txt = istdup(ist2(item->data.target, len));
-                       break;
+               if (item->type != DNS_RTYPE_TXT)
+                       continue;
+               txt = malloc(sizeof(*txt));
+               if (!txt)
+                       goto error;
+               txt->value = istdup(ist2(item->data.target, item->data_len));
+               if (!isttest(txt->value)) {
+                       free(txt);
+                       goto error;
                }
+               txt->next = rslv->txt;
+               rslv->txt = txt;
        }
-
        rslv->result = RSLV_STATUS_VALID;
+       goto done;
+
+error:
+       acme_rslv_clear_txt(rslv);
 done:
        /* if there's no other DNS task for this acme task, wake up acme_task */
        if (HA_ATOMIC_SUB_FETCH(rslv->dnstasks, 1) == 0) {
@@ -88,7 +108,7 @@ void acme_rslv_free(struct acme_rslv *rslv)
                HA_SPIN_UNLOCK(DNS_LOCK, &rslv->resolvers->lock);
        }
        free(rslv->hostname_dn);
-       istfree(&rslv->txt);
+       acme_rslv_clear_txt(rslv);
        free(rslv);
 }