Skip to content

Commit

Permalink
fix: storage url encoding for http endpoint (#361)
Browse files Browse the repository at this point in the history
* fix: url encoding

Signed-off-by: David Dal Busco <[email protected]>

* chore: clean

Signed-off-by: David Dal Busco <[email protected]>

* docs: move comment

Signed-off-by: David Dal Busco <[email protected]>

---------

Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker authored Dec 18, 2023
1 parent 1a98803 commit ad26a6a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 50 deletions.
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

0 comments on commit ad26a6a

Please sign in to comment.