Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

dnsdist: Document that RE2 does full matching, add regression tests #15111

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/actions/spell-check/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,11 @@ postinst
postresolve
powerdnsrecursor
powerdnssec
powerdnssuffix
powerldap
pqpp
Predota
prefixpowerdns
preoutquery
Preproc
prepublishkeyroll
Expand Down
3 changes: 3 additions & 0 deletions .github/actions/spell-check/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,6 @@ DoH

# Twitter status
\btwitter\.com/[^/\s"')]*(?:/status/\d+(?:\?[-_0-9a-zA-Z&=]*|)|)

# LaTex instructions in our RST documentation
\\\w{2,}\{
2 changes: 1 addition & 1 deletion pdns/dnsdistdist/docs/reference/selectors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ Selectors can be combined via :func:`AndRule`, :func:`OrRule` and :func:`NotRule

.. function:: RE2Rule(regex)

Matches the query name against the supplied regex using the RE2 engine.
Matches the query name against the supplied regex using the RE2 engine. Note that this rule requires a full match of the query name, meaning that for example the ``powerdns`` expression will match a query name of ``powerdns`` but neither``prefixpowerdns``, ``sub.powerdns``, ``powerdnssuffix`` nor ``powerdns.tld``. In short, the expression is processed as if it started with a ``^`` and ended with a ``$``.

For an example of usage, see :func:`RegexRule`.

Expand Down
50 changes: 50 additions & 0 deletions regression-tests.dnsdist/test_RE2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python
import dns
from dnsdisttests import DNSDistTest

class TestRE2(DNSDistTest):
_config_template = """
newServer{address="127.0.0.1:%d"}

-- keep in mind this is a FULL match, as if the expression started with
-- a '^' and ended with a '$'
addAction(RE2Rule("re2\\\\.tests\\\\.powerdns\\\\.com"), RCodeAction(DNSRCode.REFUSED))
"""

def testMatch(self):
"""
RE2: Match
"""
name = 're2.tests.powerdns.com.'
query = dns.message.make_query(name, 'A', 'IN')
query.flags &= ~dns.flags.RD
expectedResponse = dns.message.make_response(query)
expectedResponse.set_rcode(dns.rcode.REFUSED)

for method in ("sendUDPQuery", "sendTCPQuery"):
sender = getattr(self, method)
(_, receivedResponse) = sender(query, response=None, useQueue=False)
self.assertEqual(receivedResponse, expectedResponse)

def testNoMatch(self):
"""
RE2: No match
"""
name = 'sub.re2.tests.powerdns.com.'
query = dns.message.make_query(name, 'A', 'IN')
query.flags &= ~dns.flags.RD
response = dns.message.make_response(query)
rrset = dns.rrset.from_text(name,
3600,
dns.rdataclass.IN,
dns.rdatatype.A,
'127.0.0.1')

response.answer.append(rrset)

for method in ("sendUDPQuery", "sendTCPQuery"):
sender = getattr(self, method)
(receivedQuery, receivedResponse) = sender(query, response=response)
receivedQuery.id = query.id
self.assertEqual(receivedQuery, query)
self.assertEqual(receivedResponse, response)