mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-11 18:04:18 +00:00
Penalize peers that send an invalid rpc request (#6986)
Since https://github.com/sigp/lighthouse/pull/6847, invalid `BlocksByRange`/`BlobsByRange` requests, which do not comply with the spec, are [handled in the Handler](3d16d1080f/beacon_node/lighthouse_network/src/rpc/handler.rs (L880-L911)). Any peer that sends an invalid request is penalized and disconnected.
However, other kinds of invalid rpc request, which result in decoding errors, are just dropped. No penalty is applied and the connection with the peer remains.
I have added handling for the `ListenUpgradeError` event to notify the application of an `RPCError:InvalidData` error and disconnect to the peer that sent the invalid rpc request.
I also added tests for handling invalid rpc requests.
Co-Authored-By: ackintosh <sora.akatsuki@gmail.com>
This commit is contained in:
@@ -13,7 +13,8 @@ use futures::prelude::*;
|
||||
use libp2p::PeerId;
|
||||
use libp2p::swarm::handler::{
|
||||
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
|
||||
FullyNegotiatedInbound, FullyNegotiatedOutbound, StreamUpgradeError, SubstreamProtocol,
|
||||
FullyNegotiatedInbound, FullyNegotiatedOutbound, ListenUpgradeError, StreamUpgradeError,
|
||||
SubstreamProtocol,
|
||||
};
|
||||
use libp2p::swarm::{ConnectionId, Stream};
|
||||
use logging::crit;
|
||||
@@ -888,6 +889,16 @@ where
|
||||
ConnectionEvent::DialUpgradeError(DialUpgradeError { info, error }) => {
|
||||
self.on_dial_upgrade_error(info, error)
|
||||
}
|
||||
ConnectionEvent::ListenUpgradeError(ListenUpgradeError {
|
||||
error: (proto, error),
|
||||
..
|
||||
}) => {
|
||||
self.events_out.push(HandlerEvent::Err(HandlerErr::Inbound {
|
||||
id: self.current_inbound_substream_id,
|
||||
proto,
|
||||
error,
|
||||
}));
|
||||
}
|
||||
_ => {
|
||||
// NOTE: ConnectionEvent is a non exhaustive enum so updates should be based on
|
||||
// release notes more than compiler feedback
|
||||
@@ -924,7 +935,7 @@ where
|
||||
request.count()
|
||||
)),
|
||||
}));
|
||||
return self.shutdown(None);
|
||||
return;
|
||||
}
|
||||
}
|
||||
RequestType::BlobsByRange(request) => {
|
||||
@@ -940,7 +951,7 @@ where
|
||||
max_allowed, max_requested_blobs
|
||||
)),
|
||||
}));
|
||||
return self.shutdown(None);
|
||||
return;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -675,7 +675,7 @@ where
|
||||
E: EthSpec,
|
||||
{
|
||||
type Output = InboundOutput<TSocket, E>;
|
||||
type Error = RPCError;
|
||||
type Error = (Protocol, RPCError);
|
||||
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
|
||||
|
||||
fn upgrade_inbound(self, socket: TSocket, protocol: ProtocolId) -> Self::Future {
|
||||
@@ -717,10 +717,12 @@ where
|
||||
)
|
||||
.await
|
||||
{
|
||||
Err(e) => Err(RPCError::from(e)),
|
||||
Err(e) => Err((versioned_protocol.protocol(), RPCError::from(e))),
|
||||
Ok((Some(Ok(request)), stream)) => Ok((request, stream)),
|
||||
Ok((Some(Err(e)), _)) => Err(e),
|
||||
Ok((None, _)) => Err(RPCError::IncompleteStream),
|
||||
Ok((Some(Err(e)), _)) => Err((versioned_protocol.protocol(), e)),
|
||||
Ok((None, _)) => {
|
||||
Err((versioned_protocol.protocol(), RPCError::IncompleteStream))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user