Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

auth: prevent createReverse6 from generating illegal IDN record #14975

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/lua-records/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ Reverse DNS functions
- ``%3%`` = 0
- ``%4%`` = 1
- ``%33%`` converts the compressed address format into a dashed format, e.g. ``2001:a::1`` to ``2001-a--1``
This format may add '0' to the result, preventing it from being identified as an illegal IDN by ``dig``.
- ``%34%`` to ``%41%`` represent the 8 uncompressed 2-byte chunks
- **Example:** PTR query for ``2001:a:b::123``
- ``%34%`` - returns ``2001`` (chunk 1)
Expand Down
10 changes: 10 additions & 0 deletions pdns/lua-record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,16 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn
string dashed=ip6.toString();
boost::replace_all(dashed, ":", "-");

// https://github.com/PowerDNS/pdns/issues/7524
if (boost::ends_with(dashed, "-")) {
// "a--a-" -> "a--a-0"
dashed.push_back('0');
}
if (boost::starts_with(dashed, "-") || dashed.compare(2, 2, "--") == 0) {
miodvallat marked this conversation as resolved.
Show resolved Hide resolved
// "-a--a" -> "0-a--a" "aa--a" -> "0aa--a"
dashed.insert(0, "0");
}

for(int i=31; i>=0; --i)
fmt % labels[i];
fmt % dashed;
Expand Down
48 changes: 47 additions & 1 deletion regression-tests.auth-py/test_LuaRecords.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,14 @@ class TestLuaRecords(AuthTest):
createforward6.example.org. 3600 IN NS ns1.example.org.
createforward6.example.org. 3600 IN NS ns2.example.org.
* IN LUA AAAA "filterForward(createForward6(), newNMG{{'2000::/3'}}, 'fe80::1')"
"""
""",
# the separate createforward6 zone is because some of the code in lua-record.cc insists on working relatively to the zone apex
'no-filter.createforward6.example.org': """
no-filter.createforward6.example.org. 3600 IN SOA {soa}
no-filter.createforward6.example.org. 3600 IN NS ns1.example.org.
no-filter.createforward6.example.org. 3600 IN NS ns2.example.org.
* IN LUA AAAA "createForward6()"
"""
}
_web_rrsets = []

Expand Down Expand Up @@ -1048,6 +1054,46 @@ def testCreateForwardAndReverse(self):
self.assertRcodeEqual(res, dns.rcode.NOERROR)
self.assertEqual(res.answer, response.answer)

def testCreateForwardAndReverseWithZero(self):
"""
Fix #7524
"""
expected = {
".no-filter.createforward6.example.org." : (dns.rdatatype.AAAA, {
"0--0" : "::",
"0--1" : "::1",
"0aa--0" : "aa::",
"0aa--1" : "aa::1",
"2001--0" : "2001::",
"a-b--c" : "a:b::c",
"a--b-c" : "a::b:c"
}),
".createreverse6.example.org." : (dns.rdatatype.PTR, {
"0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0" : "0--0.example.com.",
"1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0" : "0--1.example.com.",
"0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.a.a.0.0" : "0aa--0.example.com.",
"1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.a.a.0.0" : "0aa--1.example.com.",
"0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.2" : "2001--0.example.com.",
"c.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.b.0.0.0.a.0.0.0" : "a-b--c.example.com.",
"c.0.0.0.b.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.a.0.0.0" : "a--b-c.example.com."
})
}

for suffix, v in expected.items():
qtype, pairs = v
for prefix, target in pairs.items():
name = prefix + suffix

query = dns.message.make_query(name, qtype)
response = dns.message.make_response(query)
response.answer.append(dns.rrset.from_text(
name, 0, dns.rdataclass.IN, qtype, target))

res = self.sendUDPQuery(query)
print(res)
self.assertRcodeEqual(res, dns.rcode.NOERROR)
self.assertEqual(res.answer, response.answer)

def _getCounter(self, tcp=False):
"""
Helper function for shared/non-shared testing
Expand Down