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

fix: storage url encoding for http endpoint #361

Merged
merged 3 commits into from
Dec 18, 2023
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
63 changes: 32 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/satellite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ic-certification = "1.3.0"
sha2 = "0.10.7"
base64 = "0.13.1"
url = "2.4.0"
urlencoding = "2.1.3"
globset = "0.4.13"
hex = "0.4.3"
ic-stable-structures = "0.6.0"
Expand Down
55 changes: 36 additions & 19 deletions src/satellite/src/storage/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,21 @@ use crate::storage::types::http_request::MapUrl;
use crate::storage::types::state::FullPath;
use globset::Glob;
use std::collections::HashMap;
use url::{ParseError, Url};
use url::Url;
use urlencoding::decode;

pub fn map_url(url: &String) -> Result<MapUrl, &'static str> {
let parsed_url = build_url(url);
let parsed_url = build_url(url)?;

match parsed_url {
Err(_) => {
let error = format!("Url {} cannot be parsed.", url.clone()).into_boxed_str();
Err(Box::leak(error))
}
Ok(parsed_url) => {
// Clean path without query params
let requested_path = parsed_url.path();
// Clean path without query params
let requested_path = decode_path(&parsed_url)?;

let token = map_token(parsed_url.clone());
let token = map_token(parsed_url.clone());

Ok(MapUrl {
path: requested_path.to_string(),
token,
})
}
}
Ok(MapUrl {
path: requested_path,
token,
})
}

pub fn map_alternative_paths(path: &String) -> Vec<String> {
Expand Down Expand Up @@ -84,10 +77,34 @@ fn aliased_by(key: &String) -> Option<Vec<String>> {

/// END

pub fn build_url(url: &String) -> Result<Url, ParseError> {
fn build_url(url: &String) -> Result<Url, &'static str> {
let separator = separator(url);

Url::parse(&["http://localhost", separator, url].join(""))
let parsed_url = Url::parse(&["http://localhost", separator, url].join(""));

match parsed_url {
Err(_) => {
let error = format!("Url {} cannot be parsed.", url.clone()).into_boxed_str();
Err(Box::leak(error))
}
Ok(url) => Ok(url),
}
}

/// Currently encoded URL are not supported, only non encoded path because the certification except a tree containing only non encoded paths.

fn decode_path(parsed_url: &Url) -> Result<String, &'static str> {
let path = parsed_url.path();

let decoded_path = decode(path);

match decoded_path {
Err(_) => {
let error = format!("Path {} cannot be decoded.", path.clone()).into_boxed_str();
Err(Box::leak(error))
}
Ok(decoded_path) => Ok(decoded_path.to_string()),
}
}

/// Ensure path always will begin with a /
Expand Down