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

security: Rate limit GetAddr responses #7955

Merged
merged 26 commits into from
Nov 21, 2023
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
969819f
Updates ADDR_RESPONSE_LIMIT_DENOMINATOR to 4
arya2 Nov 16, 2023
ba0cfc6
Moves logic getting a fraction of Zebra's peers to a method in the ad…
arya2 Nov 16, 2023
70e0cc1
Adds and uses CachedPeerAddrs struct in inbound service
arya2 Nov 16, 2023
81cf5a5
moves and documents constant
arya2 Nov 16, 2023
f28c529
fixes test
arya2 Nov 16, 2023
a82a404
Apply suggestions from code review
arya2 Nov 17, 2023
78234ba
updates docs
arya2 Nov 17, 2023
a5d6b25
renames sanitized_window method
arya2 Nov 17, 2023
a431e1b
renames CachedPeerAddrs to CachedPeerAddrResponse
arya2 Nov 17, 2023
66e1cfc
updates test
arya2 Nov 17, 2023
788c620
moves try_refresh to per request
arya2 Nov 17, 2023
29f3aed
Make unused sanitization method pub(crate)
teor2345 Nov 19, 2023
2032361
Apply suggestions from code review
arya2 Nov 20, 2023
f2c2968
moves CachedPeerAddrResponse to a module
arya2 Nov 20, 2023
2ee9a20
updates unit test
arya2 Nov 20, 2023
4d9bc0f
fixes unit test
arya2 Nov 20, 2023
7c35da6
removes unnecessary condition
arya2 Nov 20, 2023
cca2857
clears cached getaddr response if it can't refresh for over a minute …
arya2 Nov 20, 2023
7c84398
tests that inbound service gives out the same addresses for every Pee…
arya2 Nov 20, 2023
f72a52b
Applies suggestion from code review
arya2 Nov 20, 2023
5327d17
fixes doc link
arya2 Nov 20, 2023
1e3fb0e
renames constant
arya2 Nov 20, 2023
a4a8963
Fix docs on new constant
arya2 Nov 20, 2023
f902adb
applies suggestion from code review
arya2 Nov 20, 2023
120b6a5
uses longer cache expiry time
arya2 Nov 20, 2023
48f419e
Adds code comments
arya2 Nov 21, 2023
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
Prev Previous commit
Next Next commit
tests that inbound service gives out the same addresses for every Pee…
…rs request before the refresh interval
  • Loading branch information
arya2 committed Nov 20, 2023
commit 7c843983cd0932a6f50ffa6c0dd92de8919e8a67
114 changes: 112 additions & 2 deletions zebrad/src/components/inbound/tests/fake_peer_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ use zebra_chain::{
block::{Block, Height},
fmt::humantime_seconds,
parameters::Network::{self, *},
serialization::ZcashDeserializeInto,
serialization::{DateTime32, ZcashDeserializeInto},
transaction::{UnminedTx, UnminedTxId, VerifiedUnminedTx},
};
use zebra_consensus::{error::TransactionError, transaction, Config as ConsensusConfig};
use zebra_network::{
constants::DEFAULT_MAX_CONNS_PER_IP, AddressBook, InventoryResponse, Request, Response,
constants::{
ADDR_RESPONSE_LIMIT_DENOMINATOR, DEFAULT_MAX_CONNS_PER_IP, MAX_ADDRS_IN_ADDRESS_BOOK,
},
types::{MetaAddr, PeerServices},
AddressBook, InventoryResponse, Request, Response,
};
use zebra_node_services::mempool;
use zebra_state::{ChainTipChange, Config as StateConfig, CHAIN_TIP_UPDATE_WAIT_LIMIT};
Expand Down Expand Up @@ -742,6 +746,112 @@ async fn inbound_block_height_lookahead_limit() -> Result<(), crate::BoxError> {
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
/// Checks that Zebra won't give out its entire address book over a short duration.
async fn caches_getaddr_response() {
const NUM_ADDRESSES: usize = 20;
const NUM_REQUESTS: usize = 10;
const EXPECTED_NUM_RESULTS: usize = NUM_ADDRESSES / ADDR_RESPONSE_LIMIT_DENOMINATOR;

let _init_guard = zebra_test::init();

let addrs = (0..NUM_ADDRESSES)
.map(|idx| format!("127.0.0.{idx}:{idx}"))
.map(|addr| {
MetaAddr::new_gossiped_meta_addr(
addr.parse().unwrap(),
PeerServices::NODE_NETWORK,
DateTime32::now(),
)
});

let inbound = {
let network = Mainnet;
let consensus_config = ConsensusConfig::default();
let state_config = StateConfig::ephemeral();
let address_book = AddressBook::new_with_addrs(
SocketAddr::from_str("0.0.0.0:0").unwrap(),
Mainnet,
DEFAULT_MAX_CONNS_PER_IP,
MAX_ADDRS_IN_ADDRESS_BOOK,
Span::none(),
addrs,
);

let address_book = Arc::new(std::sync::Mutex::new(address_book));

// UTXO verification doesn't matter for these tests.
let (state, _read_only_state_service, latest_chain_tip, _chain_tip_change) =
zebra_state::init(state_config.clone(), network, Height::MAX, 0);

let state_service = ServiceBuilder::new().buffer(1).service(state);

// Download task panics and timeouts are propagated to the tests that use Groth16 verifiers.
let (
block_verifier,
_transaction_verifier,
_groth16_download_handle,
_max_checkpoint_height,
) = zebra_consensus::router::init(consensus_config.clone(), network, state_service.clone())
.await;

let peer_set = MockService::build()
.with_max_request_delay(MAX_PEER_SET_REQUEST_DELAY)
.for_unit_tests();
let buffered_peer_set = Buffer::new(BoxService::new(peer_set.clone()), 10);

let buffered_mempool_service =
Buffer::new(BoxService::new(MockService::build().for_unit_tests()), 10);
let (setup_tx, setup_rx) = oneshot::channel();

let inbound_service = ServiceBuilder::new()
.load_shed()
.service(Inbound::new(MAX_INBOUND_CONCURRENCY, setup_rx));
let inbound_service = BoxService::new(inbound_service);
let inbound_service = ServiceBuilder::new().buffer(1).service(inbound_service);

let setup_data = InboundSetupData {
address_book: address_book.clone(),
block_download_peer_set: buffered_peer_set,
block_verifier,
mempool: buffered_mempool_service.clone(),
state: state_service.clone(),
latest_chain_tip,
};
let r = setup_tx.send(setup_data);
// We can't expect or unwrap because the returned Result does not implement Debug
assert!(r.is_ok(), "unexpected setup channel send failure");

inbound_service
};

let Ok(zebra_network::Response::Peers(first_result)) =
inbound.clone().oneshot(zebra_network::Request::Peers).await
else {
panic!("result should match Ok(Peers(_))")
};

assert_eq!(
first_result.len(),
EXPECTED_NUM_RESULTS,
"inbound service should respond with expected number of peer addresses",
);

for _ in 0..NUM_REQUESTS {
let Ok(zebra_network::Response::Peers(peers)) =
inbound.clone().oneshot(zebra_network::Request::Peers).await
else {
panic!("result should match Ok(Peers(_))")
};

assert_eq!(
peers,
first_result,
"inbound service should return the same result for every Peers request until the refresh time",
);
}
}

/// Setup a fake Zebra network stack, with fake peer set.
///
/// Adds some initial state blocks, and mempool transactions if `add_transactions` is true.
Expand Down
Loading