Skip to content

Commit c936539

Browse files
committed
Implement matchWildcard (Issue #72)
1 parent c9df52f commit c936539

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

dnslib/label.py

+32
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ class DNSLabel(object):
5858
True
5959
>>> l3.matchGlob("*.[abc]xx.bbb.ccc")
6060
False
61+
>>> l1.matchWildcard("*.bbb.ccc")
62+
True
63+
>>> l1.matchWildcard("*.CCC")
64+
True
65+
>>> l1.matchWildcard("*.xxx.bbb.ccc")
66+
False
67+
>>> l1.matchWildcard("aaa.bbb.ccc")
68+
True
69+
>>> l1.matchWildcard("bbb.ccc")
70+
False
71+
>>> l1.matchWildcard("xxx.aaa.bbb.ccc")
72+
False
6173
6274
# Too hard to get unicode doctests to work on Python 3.2
6375
# (works on 3.3)
@@ -110,6 +122,26 @@ def matchGlob(self,pattern):
110122
pattern = DNSLabel(pattern)
111123
return fnmatch.fnmatch(str(self).lower(),str(pattern).lower())
112124

125+
def matchWildcard(self,pattern):
126+
"""
127+
Wildcard match according to https://datatracker.ietf.org/doc/html/rfc1034#section-4.3.3
128+
(This only allows a single '*' prefix wildcard which matches one or more labels)
129+
130+
Note that the spec is defined at the zone level (if there is a more
131+
specific overlapping match within the zone) some soem additional
132+
logic is required in the resolved to implement this.
133+
134+
"""
135+
if type(pattern) != DNSLabel:
136+
pattern = DNSLabel(pattern)
137+
if pattern.label[0] != b'*' and len(self.label) != len(pattern.label):
138+
# No wildcard - number of labels must match
139+
return False
140+
for (a,b) in zip(reversed(self.label),reversed(pattern.label)):
141+
if b != b'*' and a.lower() != b.lower():
142+
return False
143+
return True
144+
113145
def matchSuffix(self,suffix):
114146
"""
115147
Return True if label suffix matches

0 commit comments

Comments
 (0)