Skip to content

Add LSPS5 DOS protections. #3993

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions lightning-liquidity/src/lsps1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ where
&self.config
}

pub(crate) fn has_active_requests(&self, counterparty_node_id: &PublicKey) -> bool {
let outer_state_lock = self.per_peer_state.read().unwrap();
outer_state_lock.get(counterparty_node_id).map_or(false, |inner| {
let peer_state = inner.lock().unwrap();
!(peer_state.pending_requests.is_empty()
&& peer_state.outbound_channels_by_order_id.is_empty())
})
}

fn handle_get_info_request(
&self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey,
) -> Result<(), LightningError> {
Expand Down
17 changes: 17 additions & 0 deletions lightning-liquidity/src/lsps2/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,23 @@ where
&self.config
}

/// Returns whether the peer has any opening or open JIT channels.
pub(crate) fn has_opening_or_open_jit_channel(&self, counterparty_node_id: &PublicKey) -> bool {
let outer_state_lock = self.per_peer_state.read().unwrap();
outer_state_lock.get(counterparty_node_id).map_or(false, |inner| {
let peer_state = inner.lock().unwrap();
peer_state.outbound_channels_by_intercept_scid.values().any(|chan| {
matches!(
chan.state,
OutboundJITChannelState::PendingChannelOpen { .. }
| OutboundJITChannelState::PendingPaymentForward { .. }
| OutboundJITChannelState::PendingPayment { .. }
| OutboundJITChannelState::PaymentForwarded { .. }
)
})
})
}

/// Used by LSP to inform a client requesting a JIT Channel the token they used is invalid.
///
/// Should be called in response to receiving a [`LSPS2ServiceEvent::GetInfo`] event.
Expand Down
13 changes: 13 additions & 0 deletions lightning-liquidity/src/lsps5/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ where
}
}

/// Returns whether a request from the given client should be accepted.
///
/// Prior activity includes an existing open channel, an active LSPS1 flow,
/// or an LSPS2 flow that has an opening or open JIT channel.
pub(crate) fn can_accept_request(
&self, client_id: &PublicKey, lsps2_has_opening_or_open_jit_channel: bool,
lsps1_has_activity: bool,
) -> bool {
self.client_has_open_channel(client_id)
|| lsps2_has_opening_or_open_jit_channel
|| lsps1_has_activity
}

fn check_prune_stale_webhooks(&self) {
let now =
LSPSDateTime::new_from_duration_since_epoch(self.time_provider.duration_since_epoch());
Expand Down
26 changes: 26 additions & 0 deletions lightning-liquidity/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,32 @@ where
LSPSMessage::LSPS5(msg @ LSPS5Message::Request(..)) => {
match &self.lsps5_service_handler {
Some(lsps5_service_handler) => {
let lsps2_has_opening_or_open_jit_channel = self
.lsps2_service_handler
.as_ref()
.map_or(false, |h| h.has_opening_or_open_jit_channel(sender_node_id));
#[cfg(lsps1_service)]
let lsps1_has_active_requests = self
.lsps1_service_handler
.as_ref()
.map_or(false, |h| h.has_active_requests(sender_node_id));
#[cfg(not(lsps1_service))]
let lsps1_has_active_requests = false;

if !lsps5_service_handler.can_accept_request(
sender_node_id,
lsps2_has_opening_or_open_jit_channel,
lsps1_has_active_requests,
) {
return Err(LightningError {
err: format!(
"Rejecting LSPS5 request from {:?} without prior activity (requires open channel or active LSPS1 or LSPS2 flow)",
sender_node_id
),
action: ErrorAction::IgnoreAndLog(Level::Debug),
});
}

lsps5_service_handler.handle_message(msg, sender_node_id)?;
},
None => {
Expand Down
Loading
Loading