Skip to content

Commit 2d0bdb2

Browse files
committed
Merge bitcoin/bitcoin#22362: Drop only invalid entries when reading banlist.json
faa6c3d net: Drop only invalid entries when reading banlist.json (MarcoFalke) Pull request description: All entries will be dropped when there is at least one invalid one in `banlist.json`. Fix this by only dropping invalid ones. Also suggested in bitcoin/bitcoin#20966 (comment) ACKs for top commit: laanwj: Re-ACK faa6c3d Tree-SHA512: 5a58e7f1dcabf78d0c65d8c6d5d997063af1efeaa50ca7730fc00056fda7e0061b6f7a38907ea045fe667c9f61d392e01e556b425a95e6b126e3c41cd33deb83
2 parents 965ffe2 + faa6c3d commit 2d0bdb2

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ BITCOIN_TESTS =\
6969
test/allocator_tests.cpp \
7070
test/amount_tests.cpp \
7171
test/arith_uint256_tests.cpp \
72+
test/banman_tests.cpp \
7273
test/base32_tests.cpp \
7374
test/base58_tests.cpp \
7475
test/base64_tests.cpp \

src/net_types.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44

55
#include <net_types.h>
66

7+
#include <logging.h>
78
#include <netaddress.h>
89
#include <netbase.h>
910
#include <univalue.h>
1011

12+
static const char* BANMAN_JSON_VERSION_KEY{"version"};
13+
1114
CBanEntry::CBanEntry(const UniValue& json)
12-
: nVersion(json["version"].get_int()), nCreateTime(json["ban_created"].get_int64()),
15+
: nVersion(json[BANMAN_JSON_VERSION_KEY].get_int()),
16+
nCreateTime(json["ban_created"].get_int64()),
1317
nBanUntil(json["banned_until"].get_int64())
1418
{
1519
}
1620

1721
UniValue CBanEntry::ToJson() const
1822
{
1923
UniValue json(UniValue::VOBJ);
20-
json.pushKV("version", nVersion);
24+
json.pushKV(BANMAN_JSON_VERSION_KEY, nVersion);
2125
json.pushKV("ban_created", nCreateTime);
2226
json.pushKV("banned_until", nBanUntil);
2327
return json;
@@ -54,11 +58,16 @@ UniValue BanMapToJson(const banmap_t& bans)
5458
void BanMapFromJson(const UniValue& bans_json, banmap_t& bans)
5559
{
5660
for (const auto& ban_entry_json : bans_json.getValues()) {
61+
const int version{ban_entry_json[BANMAN_JSON_VERSION_KEY].get_int()};
62+
if (version != CBanEntry::CURRENT_VERSION) {
63+
LogPrintf("Dropping entry with unknown version (%s) from ban list\n", version);
64+
continue;
65+
}
5766
CSubNet subnet;
5867
const auto& subnet_str = ban_entry_json[BANMAN_JSON_ADDR_KEY].get_str();
5968
if (!LookupSubNet(subnet_str, subnet)) {
60-
throw std::runtime_error(
61-
strprintf("Cannot parse banned address or subnet: %s", subnet_str));
69+
LogPrintf("Dropping entry with unparseable address or subnet (%s) from ban list\n", subnet_str);
70+
continue;
6271
}
6372
bans.insert_or_assign(subnet, CBanEntry{ban_entry_json});
6473
}

src/test/banman_tests.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <banman.h>
6+
#include <chainparams.h>
7+
#include <netbase.h>
8+
#include <streams.h>
9+
#include <test/util/logging.h>
10+
#include <test/util/setup_common.h>
11+
#include <util/readwritefile.h>
12+
13+
14+
#include <boost/test/unit_test.hpp>
15+
16+
BOOST_FIXTURE_TEST_SUITE(banman_tests, BasicTestingSetup)
17+
18+
BOOST_AUTO_TEST_CASE(file)
19+
{
20+
SetMockTime(777s);
21+
const fs::path banlist_path{m_args.GetDataDirBase() / "banlist_test"};
22+
{
23+
const std::string entries_write{
24+
"{ \"banned_nets\": ["
25+
" { \"version\": 1, \"ban_created\": 0, \"banned_until\": 778, \"address\": \"aaaaaaaaa\" },"
26+
" { \"version\": 2, \"ban_created\": 0, \"banned_until\": 778, \"address\": \"bbbbbbbbb\" },"
27+
" { \"version\": 1, \"ban_created\": 0, \"banned_until\": 778, \"address\": \"1.0.0.0/8\" }"
28+
"] }",
29+
};
30+
assert(WriteBinaryFile(banlist_path + ".json", entries_write));
31+
{
32+
// The invalid entries will be dropped, but the valid one remains
33+
ASSERT_DEBUG_LOG("Dropping entry with unparseable address or subnet (aaaaaaaaa) from ban list");
34+
ASSERT_DEBUG_LOG("Dropping entry with unknown version (2) from ban list");
35+
BanMan banman{banlist_path, /*client_interface=*/nullptr, /*default_ban_time=*/0};
36+
banmap_t entries_read;
37+
banman.GetBanned(entries_read);
38+
assert(entries_read.size() == 1);
39+
}
40+
}
41+
}
42+
43+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)