فهرست منبع

protocol_address: add upper bound to transport requests from other peers

draoi 2 سال پیش
والد
کامیت
f46e6abe94
2فایلهای تغییر یافته به همراه15 افزوده شده و 13 حذف شده
  1. 3 0
      src/error.rs
  2. 12 13
      src/net/protocol/protocol_address.rs

+ 3 - 0
src/error.rs

@@ -116,6 +116,9 @@ pub enum Error {
     #[error("Unsupported network transport upgrade: {0}")]
     UnsupportedTransportUpgrade(String),
 
+    #[error("Transport request exceeds number of accepted transports")]
+    InvalidTransportRequest,
+
     #[error("Connection failed")]
     ConnectFailed,
 

+ 12 - 13
src/net/protocol/protocol_address.rs

@@ -19,7 +19,7 @@
 use std::sync::Arc;
 
 use async_trait::async_trait;
-use log::{debug, warn};
+use log::debug;
 use smol::Executor;
 
 use super::{
@@ -35,7 +35,7 @@ use super::{
     protocol_base::{ProtocolBase, ProtocolBasePtr},
     protocol_jobs_manager::{ProtocolJobsManager, ProtocolJobsManagerPtr},
 };
-use crate::Result;
+use crate::{Error, Result};
 
 /// Defines address and get-address messages. On receiving GetAddr, nodes
 /// reply an AddrMessage containing nodes from their hostlist.  On receiving
@@ -69,6 +69,12 @@ pub struct ProtocolAddress {
 
 const PROTO_NAME: &str = "ProtocolAddress";
 
+/// A vector of all currently accepted transports and valid transport
+/// combinations.  Should be updated if and when new transports are
+/// added. Creates a upper bound on the number of transports a given peer
+/// can request.
+const TRANSPORT_COMBOS: [&str; 7] = ["tor", "tls", "tcp", "nym", "tor+tls", "nym+tls", "tcp+tls"];
+
 impl ProtocolAddress {
     /// Creates a new address protocol. Makes an address, an external address
     /// and a get-address subscription and adds them to the address protocol
@@ -138,17 +144,10 @@ impl ProtocolAddress {
                 "Received GetAddrs({}) message from {}", get_addrs_msg.max, self.channel.address(),
             );
 
-            // Validate transports length
-            // TODO: Verify this limit. It should be the max number of all our allowed transports,
-            //       plus their mixing.
-            if get_addrs_msg.transports.len() > 20 {
-                warn!(target: "net::protocol_address::handle_receive_get_addrs()",
-                "Sending empty Addrs message");
-
-                // TODO: Should this error out, effectively ending the connection?
-                let addrs_msg = AddrsMessage { addrs: vec![] };
-                self.channel.send(&addrs_msg).await?;
-                continue
+            // Check that this peer isn't requesting more transports than we support
+            // (the max number of all transports, plus mixing).
+            if get_addrs_msg.transports.len() > TRANSPORT_COMBOS.len() {
+                return Err(Error::InvalidTransportRequest);
             }
 
             // First we grab address with the requested transports from the gold list