Commit 5a239e4119 for bind

commit 5a239e411985a8d91010d1c3e26890294c4fb16e
Author: Štěpán Balážik <stepan@isc.org>
Date:   Mon Oct 26 10:43:10 2026 +0100

    Share the helpers of the nsec_synthesis tests

    The four test modules each carried their own copy of the A record and
    NSEC lookups, three of them the same key generator and pytest marks,
    and all four the same NSEC check under four names, the forged-NSEC ones
    with a key tag on top.  Keep them once in common.py, as other tests do.

    Assisted-by: Claude:claude-fable-5-1

diff --git a/bin/tests/system/nsec_synthesis/common.py b/bin/tests/system/nsec_synthesis/common.py
new file mode 100644
index 0000000000..05beb4e938
--- /dev/null
+++ b/bin/tests/system/nsec_synthesis/common.py
@@ -0,0 +1,83 @@
+# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+#
+# SPDX-License-Identifier: MPL-2.0
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, you can obtain one at https://mozilla.org/MPL/2.0/.
+#
+# See the COPYRIGHT file distributed with this work for additional
+# information regarding copyright ownership.
+
+from cryptography.hazmat.primitives import serialization
+from cryptography.hazmat.primitives.asymmetric import ec
+
+import dns.dnssec
+import dns.name
+import dns.rdataclass
+import dns.rdatatype
+import pytest
+
+import isctest
+import isctest.mark
+
+SIGNED_ZONE_MARKS = [
+    isctest.mark.with_ecdsa_deterministic,
+    pytest.mark.extra_artifacts(
+        [
+            "ans*/keys.json",
+        ]
+    ),
+]
+
+
+def make_key(zone: str):
+    private_key = ec.generate_private_key(ec.SECP256R1())
+    dnskey = dns.dnssec.make_dnskey(
+        private_key.public_key(),
+        algorithm="ECDSAP256SHA256",
+        flags=257,
+    )
+    ds = dns.dnssec.make_ds(dns.name.from_text(zone), dnskey, "SHA256")
+    private_pem = private_key.private_bytes(
+        encoding=serialization.Encoding.PEM,
+        format=serialization.PrivateFormat.PKCS8,
+        encryption_algorithm=serialization.NoEncryption(),
+    ).decode("ascii")
+    return {
+        "private_pem": private_pem,
+        "dnskey": dnskey.to_text(),
+        "ds": ds.to_text(),
+    }
+
+
+def has_a(response, owner, address):
+    rrset = response.get_rrset(
+        response.answer, owner, dns.rdataclass.IN, dns.rdatatype.A
+    )
+    return rrset is not None and any(rdata.address == address for rdata in rrset)
+
+
+def get_nsec(response, section, owner):
+    return response.get_rrset(section, owner, dns.rdataclass.IN, dns.rdatatype.NSEC)
+
+
+def check_nsec(response, section, owner, next_name, signer):
+    text = response.to_text()
+    nsec = get_nsec(response, section, owner)
+    rrsig = response.get_rrset(
+        section,
+        owner,
+        dns.rdataclass.IN,
+        dns.rdatatype.RRSIG,
+        covers=dns.rdatatype.NSEC,
+    )
+    assert nsec and rrsig, text
+    assert nsec[0].next == dns.name.from_text(next_name), text
+    assert rrsig[0].signer == dns.name.from_text(signer), text
+    return rrsig
+
+
+def check_forged_nsec(response, section, owner, next_name, zone, key_tag):
+    rrsig = check_nsec(response, section, owner, next_name, zone)
+    assert rrsig[0].key_tag == key_tag, response.to_text()
diff --git a/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py b/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py
index 5da86ffb3e..99436cdaf9 100644
--- a/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py
+++ b/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py
@@ -13,18 +13,17 @@ from pathlib import Path

 import json

-from cryptography.hazmat.primitives import serialization
-from cryptography.hazmat.primitives.asymmetric import ec
-
-import dns.dnssec
 import dns.flags
-import dns.name
-import dns.rdataclass
-import dns.rdatatype
-import pytest
+
+from nsec_synthesis.common import (
+    SIGNED_ZONE_MARKS,
+    check_forged_nsec,
+    get_nsec,
+    has_a,
+    make_key,
+)

 import isctest
-import isctest.mark

 F023_ZONE = "f023.test."
 EVIL = f"evil.{F023_ZONE}"
@@ -35,67 +34,16 @@ TARGET_A = "192.0.2.77"
 AUTH = "10.53.0.1"
 RESOLVER = "10.53.0.2"

-pytestmark = [
-    isctest.mark.with_ecdsa_deterministic,
-    pytest.mark.extra_artifacts(
-        [
-            "ans*/keys.json",
-        ]
-    ),
-]
-
-
-def _make_key(zone: str):
-    private_key = ec.generate_private_key(ec.SECP256R1())
-    dnskey = dns.dnssec.make_dnskey(
-        private_key.public_key(),
-        algorithm="ECDSAP256SHA256",
-        flags=257,
-    )
-    ds = dns.dnssec.make_ds(dns.name.from_text(zone), dnskey, "SHA256")
-    private_pem = private_key.private_bytes(
-        encoding=serialization.Encoding.PEM,
-        format=serialization.PrivateFormat.PKCS8,
-        encryption_algorithm=serialization.NoEncryption(),
-    ).decode("ascii")
-    return {
-        "private_pem": private_pem,
-        "dnskey": dnskey.to_text(),
-        "ds": ds.to_text(),
-    }
+pytestmark = SIGNED_ZONE_MARKS


 def bootstrap():
-    keys = {F023_ZONE: _make_key(F023_ZONE)}
+    keys = {F023_ZONE: make_key(F023_ZONE)}
     Path("ans1/keys.json").write_text(json.dumps(keys, indent=2), encoding="ascii")
     dnskey = "".join(keys[F023_ZONE]["dnskey"].split()[3:])
     return {"DNSKEY": dnskey}


-def _has_a(response, owner, address):
-    rrset = response.get_rrset(
-        response.answer, owner, dns.rdataclass.IN, dns.rdatatype.A
-    )
-    return rrset is not None and any(rdata.address == address for rdata in rrset)
-
-
-def _check_malicious_nsec(response, section):
-    nsec = response.get_rrset(section, POISON, dns.rdataclass.IN, dns.rdatatype.NSEC)
-    assert nsec is not None, response.to_text()
-    assert nsec[0].next == dns.name.from_text(POISON_NEXT), response.to_text()
-
-    rrsig = response.get_rrset(
-        section,
-        POISON,
-        dns.rdataclass.IN,
-        dns.rdatatype.RRSIG,
-        covers=dns.rdatatype.NSEC,
-    )
-    assert rrsig is not None, response.to_text()
-    assert rrsig[0].signer == dns.name.from_text(F023_ZONE), response.to_text()
-    assert rrsig[0].key_tag == 12345, response.to_text()
-
-
 def _prime_parent_for_aggressive_nsec():
     soa = isctest.query.tcp(isctest.query.create(F023_ZONE, "SOA"), RESOLVER)
     isctest.check.noerror(soa)
@@ -110,7 +58,7 @@ def test_direct_insecure_child_nsec_next_fixture():
     poison = isctest.query.tcp(isctest.query.create(POISON, "NSEC"), AUTH)
     isctest.check.noerror(poison)
     assert poison.flags & dns.flags.AA
-    _check_malicious_nsec(poison, poison.answer)
+    check_forged_nsec(poison, poison.answer, POISON, POISON_NEXT, F023_ZONE, 12345)


 def test_resolver_does_not_synth_from_insecure_child_nsec():
@@ -119,15 +67,10 @@ def test_resolver_does_not_synth_from_insecure_child_nsec():
     poison = isctest.query.tcp(isctest.query.create(POISON, "NSEC"), RESOLVER)
     isctest.check.noerror(poison)
     isctest.check.noadflag(poison)
-    _check_malicious_nsec(poison, poison.answer)
+    check_forged_nsec(poison, poison.answer, POISON, POISON_NEXT, F023_ZONE, 12345)

     response = isctest.query.tcp(isctest.query.create(TARGET, "A"), RESOLVER)
     isctest.check.noerror(response)
-    assert _has_a(response, TARGET, TARGET_A), response.to_text()
+    assert has_a(response, TARGET, TARGET_A), response.to_text()
     isctest.check.adflag(response)
-    assert (
-        response.get_rrset(
-            response.authority, POISON, dns.rdataclass.IN, dns.rdatatype.NSEC
-        )
-        is None
-    )
+    assert get_nsec(response, response.authority, POISON) is None
diff --git a/bin/tests/system/nsec_synthesis/tests_nsec_exact_nodata.py b/bin/tests/system/nsec_synthesis/tests_nsec_exact_nodata.py
index 0d07e480d1..01f099a376 100644
--- a/bin/tests/system/nsec_synthesis/tests_nsec_exact_nodata.py
+++ b/bin/tests/system/nsec_synthesis/tests_nsec_exact_nodata.py
@@ -13,17 +13,15 @@ from pathlib import Path

 import json

-from cryptography.hazmat.primitives import serialization
-from cryptography.hazmat.primitives.asymmetric import ec
-
-import dns.dnssec
-import dns.name
-import dns.rdataclass
-import dns.rdatatype
-import pytest
+from nsec_synthesis.common import (
+    SIGNED_ZONE_MARKS,
+    check_forged_nsec,
+    get_nsec,
+    has_a,
+    make_key,
+)

 import isctest
-import isctest.mark

 F004_ZONE = "f004.test."
 VICTIM = f"victim.{F004_ZONE}"
@@ -32,67 +30,16 @@ VICTIM_A = "203.0.113.1"
 AUTH = "10.53.0.1"
 RESOLVER = "10.53.0.2"

-pytestmark = [
-    isctest.mark.with_ecdsa_deterministic,
-    pytest.mark.extra_artifacts(
-        [
-            "ans*/keys.json",
-        ]
-    ),
-]
-
-
-def _make_key(zone: str):
-    private_key = ec.generate_private_key(ec.SECP256R1())
-    dnskey = dns.dnssec.make_dnskey(
-        private_key.public_key(),
-        algorithm="ECDSAP256SHA256",
-        flags=257,
-    )
-    ds = dns.dnssec.make_ds(dns.name.from_text(zone), dnskey, "SHA256")
-    private_pem = private_key.private_bytes(
-        encoding=serialization.Encoding.PEM,
-        format=serialization.PrivateFormat.PKCS8,
-        encryption_algorithm=serialization.NoEncryption(),
-    ).decode("ascii")
-    return {
-        "private_pem": private_pem,
-        "dnskey": dnskey.to_text(),
-        "ds": ds.to_text(),
-    }
+pytestmark = SIGNED_ZONE_MARKS


 def bootstrap():
-    keys = {F004_ZONE: _make_key(F004_ZONE)}
+    keys = {F004_ZONE: make_key(F004_ZONE)}
     Path("ans1/keys.json").write_text(json.dumps(keys, indent=2), encoding="ascii")
     dnskey = "".join(keys[F004_ZONE]["dnskey"].split()[3:])
     return {"DNSKEY": dnskey}


-def _has_a(response, owner, address):
-    rrset = response.get_rrset(
-        response.answer, owner, dns.rdataclass.IN, dns.rdatatype.A
-    )
-    return rrset is not None and any(rdata.address == address for rdata in rrset)
-
-
-def _check_forged_nsec(response, section):
-    nsec = response.get_rrset(section, VICTIM, dns.rdataclass.IN, dns.rdatatype.NSEC)
-    assert nsec is not None, response.to_text()
-    assert nsec[0].next == dns.name.from_text(POISON_NEXT), response.to_text()
-
-    rrsig = response.get_rrset(
-        section,
-        VICTIM,
-        dns.rdataclass.IN,
-        dns.rdatatype.RRSIG,
-        covers=dns.rdatatype.NSEC,
-    )
-    assert rrsig is not None, response.to_text()
-    assert rrsig[0].signer == dns.name.from_text(F004_ZONE), response.to_text()
-    assert rrsig[0].key_tag == 9999, response.to_text()
-
-
 def test_pending_exact_nodata_nsec_cache_poisoning():
     """
     Companion to #5872 that covers the query_coveringnsec() trust check on
@@ -114,23 +61,13 @@ def test_pending_exact_nodata_nsec_cache_poisoning():
     poison = isctest.query.tcp(isctest.query.create(VICTIM, "NSEC", cd=True), RESOLVER)
     isctest.check.noerror(poison)
     isctest.check.noadflag(poison)
-    _check_forged_nsec(poison, poison.answer)
+    check_forged_nsec(poison, poison.answer, VICTIM, POISON_NEXT, F004_ZONE, 9999)

     # Query the victim's A record. The pending NSEC must not be used to
     # synthesize a NODATA; the resolver fetches and validates the real A.
     response = isctest.query.tcp(isctest.query.create(VICTIM, "A"), RESOLVER)
     isctest.check.noerror(response)
-    assert _has_a(response, VICTIM, VICTIM_A), response.to_text()
+    assert has_a(response, VICTIM, VICTIM_A), response.to_text()
     isctest.check.adflag(response)
-    assert (
-        response.get_rrset(
-            response.answer, VICTIM, dns.rdataclass.IN, dns.rdatatype.NSEC
-        )
-        is None
-    )
-    assert (
-        response.get_rrset(
-            response.authority, VICTIM, dns.rdataclass.IN, dns.rdatatype.NSEC
-        )
-        is None
-    )
+    assert get_nsec(response, response.answer, VICTIM) is None
+    assert get_nsec(response, response.authority, VICTIM) is None
diff --git a/bin/tests/system/nsec_synthesis/tests_nsec_pending_cd.py b/bin/tests/system/nsec_synthesis/tests_nsec_pending_cd.py
index 07ee253a65..4c3cde1f84 100644
--- a/bin/tests/system/nsec_synthesis/tests_nsec_pending_cd.py
+++ b/bin/tests/system/nsec_synthesis/tests_nsec_pending_cd.py
@@ -13,17 +13,15 @@ from pathlib import Path

 import json

-from cryptography.hazmat.primitives import serialization
-from cryptography.hazmat.primitives.asymmetric import ec
-
-import dns.dnssec
-import dns.name
-import dns.rdataclass
-import dns.rdatatype
-import pytest
+from nsec_synthesis.common import (
+    SIGNED_ZONE_MARKS,
+    check_forged_nsec,
+    get_nsec,
+    has_a,
+    make_key,
+)

 import isctest
-import isctest.mark

 F004_ZONE = "f004.test."
 ATTACKER = f"attacker.{F004_ZONE}"
@@ -33,69 +31,16 @@ VICTIM_A = "203.0.113.1"
 AUTH = "10.53.0.1"
 RESOLVER = "10.53.0.2"

-pytestmark = [
-    isctest.mark.with_ecdsa_deterministic,
-    pytest.mark.extra_artifacts(
-        [
-            "ans*/keys.json",
-        ]
-    ),
-]
-
-
-def _make_key(zone: str):
-    private_key = ec.generate_private_key(ec.SECP256R1())
-    dnskey = dns.dnssec.make_dnskey(
-        private_key.public_key(),
-        algorithm="ECDSAP256SHA256",
-        flags=257,
-    )
-    ds = dns.dnssec.make_ds(dns.name.from_text(zone), dnskey, "SHA256")
-    private_pem = private_key.private_bytes(
-        encoding=serialization.Encoding.PEM,
-        format=serialization.PrivateFormat.PKCS8,
-        encryption_algorithm=serialization.NoEncryption(),
-    ).decode("ascii")
-    return {
-        "private_pem": private_pem,
-        "dnskey": dnskey.to_text(),
-        "ds": ds.to_text(),
-    }
+pytestmark = SIGNED_ZONE_MARKS


 def bootstrap():
-    keys = {F004_ZONE: _make_key(F004_ZONE)}
+    keys = {F004_ZONE: make_key(F004_ZONE)}
     Path("ans1/keys.json").write_text(json.dumps(keys, indent=2), encoding="ascii")
     dnskey = "".join(keys[f"{F004_ZONE}"]["dnskey"].split()[3:])
     return {"DNSKEY": dnskey}


-def _has_a(response, owner, address):
-    rrset = response.get_rrset(
-        response.answer, owner, dns.rdataclass.IN, dns.rdatatype.A
-    )
-    return rrset is not None and any(rdata.address == address for rdata in rrset)
-
-
-def _check_attacker_nsec(response, section):
-    nsec = response.get_rrset(
-        section, f"{ATTACKER}", dns.rdataclass.IN, dns.rdatatype.NSEC
-    )
-    assert nsec is not None, response.to_text()
-    assert nsec[0].next == dns.name.from_text(POISON_NEXT), response.to_text()
-
-    rrsig = response.get_rrset(
-        section,
-        f"{ATTACKER}",
-        dns.rdataclass.IN,
-        dns.rdatatype.RRSIG,
-        covers=dns.rdatatype.NSEC,
-    )
-    assert rrsig is not None, response.to_text()
-    assert rrsig[0].signer == dns.name.from_text(f"{F004_ZONE}"), response.to_text()
-    assert rrsig[0].key_tag == 9999, response.to_text()
-
-
 def test_pending_trust_nsec_cd_cache_poisoning():
     """
     Reproducer for #5872:
@@ -112,33 +57,23 @@ def test_pending_trust_nsec_cd_cache_poisoning():
     )
     isctest.check.noerror(poison)
     isctest.check.noadflag(poison)
-    _check_attacker_nsec(poison, poison.answer)
+    check_forged_nsec(poison, poison.answer, ATTACKER, POISON_NEXT, F004_ZONE, 9999)

     # Step 3: Exploit Against Victim Domain
     response = isctest.query.tcp(isctest.query.create(VICTIM, "A"), RESOLVER)
     isctest.check.noerror(response)
-    assert _has_a(response, VICTIM, VICTIM_A), response.to_text()
+    assert has_a(response, VICTIM, VICTIM_A), response.to_text()
     isctest.check.adflag(response)
-    assert (
-        response.get_rrset(
-            response.authority, ATTACKER, dns.rdataclass.IN, dns.rdatatype.NSEC
-        )
-        is None
-    )
+    assert get_nsec(response, response.authority, ATTACKER) is None

     # Step 4: Exploit Against Stub Client
     response = isctest.query.tcp(
         isctest.query.create(VICTIM, "A", dnssec=False), RESOLVER
     )
     isctest.check.noerror(response)
-    assert _has_a(response, VICTIM, VICTIM_A), response.to_text()
+    assert has_a(response, VICTIM, VICTIM_A), response.to_text()
     isctest.check.adflag(response)
-    assert (
-        response.get_rrset(
-            response.authority, ATTACKER, dns.rdataclass.IN, dns.rdatatype.NSEC
-        )
-        is None
-    )
+    assert get_nsec(response, response.authority, ATTACKER) is None

     # Step 5: Verify Upstream Queries
     with open("ans1/ans.run", "r", encoding="utf-8") as file:
diff --git a/bin/tests/system/nsec_synthesis/tests_nsec_selfloop.py b/bin/tests/system/nsec_synthesis/tests_nsec_selfloop.py
index 3378fab356..5651a599c6 100644
--- a/bin/tests/system/nsec_synthesis/tests_nsec_selfloop.py
+++ b/bin/tests/system/nsec_synthesis/tests_nsec_selfloop.py
@@ -12,12 +12,10 @@
 from pathlib import Path

 import dns.flags
-import dns.name
-import dns.rdataclass
-import dns.rdatatype
 import pytest

 from isctest.run import EnvCmd
+from nsec_synthesis.common import check_nsec, get_nsec, has_a

 import isctest

@@ -88,51 +86,21 @@ victim A {VICTIM_A}
     }


-def _has_a(response, owner, address):
-    rrset = response.get_rrset(
-        response.answer, owner, dns.rdataclass.IN, dns.rdatatype.A
-    )
-    return rrset is not None and any(rdata.address == address for rdata in rrset)
-
-
-def _check_child_self_loop_nsec(response):
-    nsec = response.get_rrset(
-        response.authority, CHILD, dns.rdataclass.IN, dns.rdatatype.NSEC
-    )
-    assert nsec is not None, response.to_text()
-    assert nsec[0].next == dns.name.from_text(CHILD), response.to_text()
-
-    rrsig = response.get_rrset(
-        response.authority,
-        CHILD,
-        dns.rdataclass.IN,
-        dns.rdatatype.RRSIG,
-        covers=dns.rdatatype.NSEC,
-    )
-    assert rrsig is not None, response.to_text()
-    assert rrsig[0].signer == dns.name.from_text(CHILD), response.to_text()
-
-
 def test_direct_child_self_loop_nsec_fixture():
     response = isctest.query.tcp(isctest.query.create(PROBE, "A"), AUTH)
     isctest.check.nxdomain(response)
     assert response.flags & dns.flags.AA
-    _check_child_self_loop_nsec(response)
+    check_nsec(response, response.authority, CHILD, CHILD, CHILD)


 def test_resolver_does_not_synth_parent_nxdomain_from_child_nsec():
     poison = isctest.query.tcp(isctest.query.create(PROBE, "A"), RESOLVER)
     isctest.check.nxdomain(poison)
     isctest.check.adflag(poison)
-    _check_child_self_loop_nsec(poison)
+    check_nsec(poison, poison.authority, CHILD, CHILD, CHILD)

     response = isctest.query.tcp(isctest.query.create(VICTIM, "A"), RESOLVER)
     isctest.check.noerror(response)
-    assert _has_a(response, VICTIM, VICTIM_A), response.to_text()
+    assert has_a(response, VICTIM, VICTIM_A), response.to_text()
     isctest.check.adflag(response)
-    assert (
-        response.get_rrset(
-            response.authority, CHILD, dns.rdataclass.IN, dns.rdatatype.NSEC
-        )
-        is None
-    )
+    assert get_nsec(response, response.authority, CHILD) is None