Skip to content

Commit

Permalink
validate relay address
Browse files Browse the repository at this point in the history
  • Loading branch information
1440000bytes committed Mar 4, 2025
1 parent 657fcab commit 442acb4
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion rust/joinstr/src/joinstr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use crate::{
utils::{now, rand_delay},
};

use std::net::IpAddr;
use url::Url;

// delay we wait between (non-blocking) polls of a channel
pub const WAIT: u64 = 50;

Expand Down Expand Up @@ -296,8 +299,26 @@ impl<'a> Joinstr<'a> {
/// Add a relay address to [`Joinstr::relays`]
pub fn relay<T: Into<String>>(mut self, url: T) -> Result<Self, Error> {
self.pool_not_exists()?;
// TODO: check the address is valid

let url: String = url.into();

// Validate if it's a valid "ws" or "wss" URL
if let Ok(parsed_url) = Url::parse(&url) {
if parsed_url.scheme() != "wss" && parsed_url.scheme() != "ws" {
return Err(Error::InvalidRelayAddress);
}

if let Some(host) = parsed_url.host_str() {
if host.parse::<IpAddr>().is_err() && !host.contains('.') {
return Err(Error::InvalidRelayAddress);
}
} else {
return Err(Error::InvalidRelayAddress);
}
} else {
return Err(Error::InvalidRelayAddress);
}

self.relays.push(url);
Ok(self)
}
Expand Down

0 comments on commit 442acb4

Please sign in to comment.