@@ -40,6 +40,10 @@ use std::collections::btree_map::Entry as BtreeEntry;
40
40
use std:: ops:: Deref ;
41
41
use bitcoin:: hashes:: hex:: ToHex ;
42
42
43
+ /// The maximum number of extra bytes which we do not understand in a gossip message before we will
44
+ /// refuse to relay the message.
45
+ const MAX_EXCESS_BYTES_FOR_RELAY : usize = 1024 ;
46
+
43
47
/// Represents the network as nodes and channels between them
44
48
#[ derive( PartialEq ) ]
45
49
pub struct NetworkGraph {
@@ -139,13 +143,15 @@ macro_rules! secp_verify_sig {
139
143
impl < C : Deref + Sync + Send , L : Deref + Sync + Send > RoutingMessageHandler for NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
140
144
fn handle_node_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < bool , LightningError > {
141
145
self . network_graph . write ( ) . unwrap ( ) . update_node_from_announcement ( msg, & self . secp_ctx ) ?;
142
- Ok ( msg. contents . excess_data . is_empty ( ) && msg. contents . excess_address_data . is_empty ( ) )
146
+ Ok ( msg. contents . excess_data . len ( ) > MAX_EXCESS_BYTES_FOR_RELAY ||
147
+ msg. contents . excess_address_data . len ( ) > MAX_EXCESS_BYTES_FOR_RELAY ||
148
+ msg. contents . excess_data . len ( ) + msg. contents . excess_address_data . len ( ) > MAX_EXCESS_BYTES_FOR_RELAY )
143
149
}
144
150
145
151
fn handle_channel_announcement ( & self , msg : & msgs:: ChannelAnnouncement ) -> Result < bool , LightningError > {
146
152
self . network_graph . write ( ) . unwrap ( ) . update_channel_from_announcement ( msg, & self . chain_access , & self . secp_ctx ) ?;
147
153
log_trace ! ( self . logger, "Added channel_announcement for {}{}" , msg. contents. short_channel_id, if !msg. contents. excess_data. is_empty( ) { " with excess uninterpreted data!" } else { "" } ) ;
148
- Ok ( msg. contents . excess_data . is_empty ( ) )
154
+ Ok ( msg. contents . excess_data . len ( ) > MAX_EXCESS_BYTES_FOR_RELAY )
149
155
}
150
156
151
157
fn handle_htlc_fail_channel_update ( & self , update : & msgs:: HTLCFailChannelUpdate ) {
@@ -164,7 +170,7 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
164
170
165
171
fn handle_channel_update ( & self , msg : & msgs:: ChannelUpdate ) -> Result < bool , LightningError > {
166
172
self . network_graph . write ( ) . unwrap ( ) . update_channel ( msg, & self . secp_ctx ) ?;
167
- Ok ( msg. contents . excess_data . is_empty ( ) )
173
+ Ok ( msg. contents . excess_data . len ( ) > MAX_EXCESS_BYTES_FOR_RELAY )
168
174
}
169
175
170
176
fn get_next_channel_announcements ( & self , starting_point : u64 , batch_amount : u8 ) -> Vec < ( ChannelAnnouncement , Option < ChannelUpdate > , Option < ChannelUpdate > ) > {
@@ -680,7 +686,10 @@ impl NetworkGraph {
680
686
}
681
687
}
682
688
683
- let should_relay = msg. excess_data . is_empty ( ) && msg. excess_address_data . is_empty ( ) ;
689
+ let should_relay =
690
+ msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
691
+ msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
692
+ msg. excess_data . len ( ) + msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY ;
684
693
node. announcement_info = Some ( NodeAnnouncementInfo {
685
694
features : msg. features . clone ( ) ,
686
695
last_update : msg. timestamp ,
@@ -773,7 +782,8 @@ impl NetworkGraph {
773
782
node_two : msg. node_id_2 . clone ( ) ,
774
783
two_to_one : None ,
775
784
capacity_sats : utxo_value,
776
- announcement_message : if msg. excess_data . is_empty ( ) { full_msg. cloned ( ) } else { None } ,
785
+ announcement_message : if msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY
786
+ { full_msg. cloned ( ) } else { None } ,
777
787
} ;
778
788
779
789
match self . channels . entry ( msg. short_channel_id ) {
@@ -902,7 +912,8 @@ impl NetworkGraph {
902
912
chan_was_enabled = false ;
903
913
}
904
914
905
- let last_update_message = if msg. excess_data. is_empty( ) { full_msg. cloned( ) } else { None } ;
915
+ let last_update_message = if msg. excess_data. len( ) <= MAX_EXCESS_BYTES_FOR_RELAY
916
+ { full_msg. cloned( ) } else { None } ;
906
917
907
918
let updated_channel_dir_info = DirectionalChannelInfo {
908
919
enabled: chan_enabled,
0 commit comments