Skip to content

Commit

Permalink
fix: Fix having extra slashes in the endpoint URL (#206)
Browse files Browse the repository at this point in the history
The url crate will intelligently join URL paths together so we don't end
up with something like `http://localhost:8000//wpush/...`.
  • Loading branch information
AzureMarker authored Aug 5, 2020
1 parent 1b0b18b commit f943659
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 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 autopush-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ slog-stdlog = "4.0.0"
tokio-core = "0.1.17"
# XXX: pin to < 0.10 until hyper 0.13
tungstenite = { version = "0.9.2", default-features = false }
url = "2.1"
uuid = { version = "0.8.1", features = ["serde", "v4"] }
16 changes: 13 additions & 3 deletions autopush-common/src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::errors::{Result, ResultExt};
use fernet::MultiFernet;
use openssl::hash;
use url::Url;
use uuid::Uuid;

/// Create an v1 or v2 WebPush endpoint from the identifiers
Expand All @@ -15,7 +16,10 @@ pub fn make_endpoint(
endpoint_url: &str,
fernet: &MultiFernet,
) -> Result<String> {
let root = format!("{}/wpush/", endpoint_url);
let root = Url::parse(endpoint_url)
.chain_err(|| "endpoint_url is not a valid URL")?
.join("wpush/")
.chain_err(|| "Error creating URL")?;
let mut base = uaid.as_bytes().to_vec();
base.extend(chid.as_bytes());

Expand All @@ -26,9 +30,15 @@ pub fn make_endpoint(
.chain_err(|| "Error creating message digest for key")?;
base.extend(key_digest.iter());
let encrypted = fernet.encrypt(&base).trim_matches('=').to_string();
Ok(format!("{}v2/{}", root, encrypted))
let final_url = root
.join(&format!("v2/{}", encrypted))
.chain_err(|| "Encrypted data is not URL-safe")?;
Ok(final_url.to_string())
} else {
let encrypted = fernet.encrypt(&base).trim_matches('=').to_string();
Ok(format!("{}v1/{}", root, encrypted))
let final_url = root
.join(&format!("v1/{}", encrypted))
.chain_err(|| "Encrypted data is not URL-safe")?;
Ok(final_url.to_string())
}
}

0 comments on commit f943659

Please sign in to comment.