Skip to content
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

Improve url validation #26

Merged
merged 1 commit into from
Dec 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion crates/light-client-cli/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ impl<
pub fn build(network: Network, opts: Opts) -> Result<Self, Error> {
let home_dir = opts.home_dir();
if !home_dir.exists() {
info!("directory {:?} is created", home_dir);
std::fs::create_dir(&home_dir)?;
info!("directory {:?} is created", home_dir);
}
Ok(Self {
config: network.config(),
Expand Down
24 changes: 20 additions & 4 deletions crates/lodestar-rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ethereum_consensus::beacon::Slot;
use ethereum_consensus::sync_protocol::SyncCommitteePeriod;
use ethereum_consensus::types::H256;
use log::debug;
use reqwest::{Client, StatusCode};
use reqwest::{Client, StatusCode, Url};
use serde::de::DeserializeOwned;

type Result<T> = core::result::Result<T, Error>;
Expand All @@ -20,9 +20,25 @@ pub struct RPCClient {

impl RPCClient {
pub fn new(endpoint: impl Into<String>) -> Self {
let url = Url::parse(&endpoint.into()).expect("Invalid URL");
if url.scheme() != "http" && url.scheme() != "https" {
panic!("Invalid URL scheme: {}", url.scheme());
}
if url.path() != "/" {
panic!("Invalid URL path: {}", url.path());
}
if url.host().is_none() {
panic!("Invalid URL host: {}", url.host().unwrap());
}
if url.query().is_some() {
panic!("Invalid URL query: {}", url.query().unwrap());
}
if url.fragment().is_some() {
panic!("Invalid URL fragment: {}", url.fragment().unwrap());
}
Self {
http_client: reqwest::Client::new(),
endpoint: endpoint.into(),
endpoint: url.as_str().strip_suffix("/").unwrap().to_string(),
}
}

Expand Down Expand Up @@ -168,8 +184,8 @@ impl RPCClient {

#[derive(serde::Serialize, serde::Deserialize)]
struct InternalServerError {
#[serde(rename = "statusCode")]
#[serde(alias = "statusCode", alias = "code")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status_code: u64,
error: String,
error: Option<String>,
message: String,
}
Loading