Skip to content

Commit fefb916

Browse files
tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters
1 parent 9574de8 commit fefb916

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/test/net_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
9999
BOOST_CHECK(Lookup("250.7.1.1", addr1, 8333, false));
100100
BOOST_CHECK(Lookup("250.7.2.2", addr2, 9999, false));
101101
BOOST_CHECK(Lookup("250.7.3.3", addr3, 9999, false));
102+
BOOST_CHECK(Lookup(std::string("250.7.3.3", 9), addr3, 9999, false));
103+
BOOST_CHECK(!Lookup(std::string("250.7.3.3\0example.com", 21), addr3, 9999, false));
102104

103105
// Add three addresses to new table.
104106
CService source;

src/test/netbase_tests.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
127127

128128
BOOST_AUTO_TEST_CASE(onioncat_test)
129129
{
130-
131130
// values from https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat
132131
CNetAddr addr1(ResolveIP("5wyqrzbvrdsumnok.onion"));
133132
CNetAddr addr2(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca"));
@@ -402,4 +401,22 @@ BOOST_AUTO_TEST_CASE(netpermissions_test)
402401
BOOST_CHECK(std::find(strings.begin(), strings.end(), "mempool") != strings.end());
403402
}
404403

404+
BOOST_AUTO_TEST_CASE(netbase_dont_resolve_strings_with_embedded_nul_characters)
405+
{
406+
CNetAddr addr;
407+
BOOST_CHECK(LookupHost(std::string("127.0.0.1", 9), addr, false));
408+
BOOST_CHECK(!LookupHost(std::string("127.0.0.1\0", 10), addr, false));
409+
BOOST_CHECK(!LookupHost(std::string("127.0.0.1\0example.com", 21), addr, false));
410+
BOOST_CHECK(!LookupHost(std::string("127.0.0.1\0example.com\0", 22), addr, false));
411+
CSubNet ret;
412+
BOOST_CHECK(LookupSubNet(std::string("1.2.3.0/24", 10), ret));
413+
BOOST_CHECK(!LookupSubNet(std::string("1.2.3.0/24\0", 11), ret));
414+
BOOST_CHECK(!LookupSubNet(std::string("1.2.3.0/24\0example.com", 22), ret));
415+
BOOST_CHECK(!LookupSubNet(std::string("1.2.3.0/24\0example.com\0", 23), ret));
416+
BOOST_CHECK(LookupSubNet(std::string("5wyqrzbvrdsumnok.onion", 22), ret));
417+
BOOST_CHECK(!LookupSubNet(std::string("5wyqrzbvrdsumnok.onion\0", 23), ret));
418+
BOOST_CHECK(!LookupSubNet(std::string("5wyqrzbvrdsumnok.onion\0example.com", 34), ret));
419+
BOOST_CHECK(!LookupSubNet(std::string("5wyqrzbvrdsumnok.onion\0example.com\0", 35), ret));
420+
}
421+
405422
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)