]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Unknown flag fixes.
authorBob Halley <halley@dnspython.org>
Sun, 28 Jun 2026 00:39:57 +0000 (17:39 -0700)
committerBob Halley <halley@dnspython.org>
Sun, 28 Jun 2026 00:39:57 +0000 (17:39 -0700)
We now mask out opcode and rcode for (regular) flags,
and the extended rcode and version for EDNS flags.

We now test with unknown flag 0x0040 for flags, as
it is the only actually undefined flag bit.

We now test with unknown flags 0x0003 for EDNS, as
flag 0x2000 is expected to be used in DELEG.

dns/flags.py
tests/test_flags.py

index 2757e62e4605420f761a06cfd6b932afea380956..9fd1d0e6bdf3baf7ba5af03981d3831b4a013818 100644 (file)
@@ -50,6 +50,13 @@ class EDNSFlag(enum.IntFlag):
     CO = 0x4000
 
 
+# Flags Mask (excludes opcode and rcode)
+FLAGS_MASK = 0x87F0
+
+# EDNS Flags Mask (excludes extended rcode and version)
+EDNS_FLAGS_MASK = 0x0000FFFF
+
+
 def _from_text(text: str, enum_class: Any) -> int:
     flags = 0
     tokens = text.split()
@@ -100,7 +107,8 @@ def to_text(flags: int) -> str:
     :rtype: str
     """
 
-    return _to_text(flags, Flag)
+    # We & with 0xff0 to mask out rcode.
+    return _to_text(flags & FLAGS_MASK, Flag)
 
 
 def edns_from_text(text: str) -> int:
@@ -120,7 +128,7 @@ def edns_to_text(flags: int) -> str:
     :rtype: str
     """
 
-    return _to_text(flags, EDNSFlag)
+    return _to_text(flags & EDNS_FLAGS_MASK, EDNSFlag)
 
 
 ### BEGIN generated Flag constants
index be5342bd421cfbfe467ebd21bb1ebca465034731..bbe6b85ed587b00134663e2ff527a9f393983061 100644 (file)
@@ -18,8 +18,8 @@
 import unittest
 
 import dns.flags
-import dns.rcode
 import dns.opcode
+import dns.rcode
 
 
 class FlagsTestCase(unittest.TestCase):
@@ -59,17 +59,17 @@ class FlagsTestCase(unittest.TestCase):
 
     def test_to_text_unknown_flag(self):
         # An unknown flag bit must not be silently dropped (issue #1264).
-        self.assertEqual(dns.flags.to_text(0x8000 | 0x0001), "QR FLAG0")
+        self.assertEqual(dns.flags.to_text(0x8000 | 0x0040), "QR FLAG6")
 
     def test_edns_to_text_unknown_flag(self):
         # EDNS flags behave the same: the unnamed 0x2000 bit is rendered.
-        self.assertEqual(dns.flags.edns_to_text(0x2000), "FLAG13")
-        self.assertEqual(dns.flags.edns_to_text(0x8000 | 0x2000), "DO FLAG13")
+        self.assertEqual(dns.flags.edns_to_text(0x0003), "FLAG0 FLAG1")
+        self.assertEqual(dns.flags.edns_to_text(0x8000 | 0x0001), "DO FLAG0")
 
     def test_unknown_flag_roundtrip(self):
-        for value in [0x0001, 0x8000 | 0x0001, 0x0400 | 0x0008]:
+        for value in [0x0040, 0x8000 | 0x0040]:
             self.assertEqual(dns.flags.from_text(dns.flags.to_text(value)), value)
-        for value in [0x2000, 0x8000 | 0x2000, 0x4000 | 0x2000]:
+        for value in [0x0003, 0x8000 | 0x0003]:
             self.assertEqual(
                 dns.flags.edns_from_text(dns.flags.edns_to_text(value)), value
             )