Skip to content

Commit f943659

Browse files
authored
fix: Fix having extra slashes in the endpoint URL (#206)
The url crate will intelligently join URL paths together so we don't end up with something like `http://localhost:8000//wpush/...`.
1 parent 1b0b18b commit f943659

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

autopush-common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ slog-stdlog = "4.0.0"
5151
tokio-core = "0.1.17"
5252
# XXX: pin to < 0.10 until hyper 0.13
5353
tungstenite = { version = "0.9.2", default-features = false }
54+
url = "2.1"
5455
uuid = { version = "0.8.1", features = ["serde", "v4"] }

autopush-common/src/endpoint.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::errors::{Result, ResultExt};
22
use fernet::MultiFernet;
33
use openssl::hash;
4+
use url::Url;
45
use uuid::Uuid;
56

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

@@ -26,9 +30,15 @@ pub fn make_endpoint(
2630
.chain_err(|| "Error creating message digest for key")?;
2731
base.extend(key_digest.iter());
2832
let encrypted = fernet.encrypt(&base).trim_matches('=').to_string();
29-
Ok(format!("{}v2/{}", root, encrypted))
33+
let final_url = root
34+
.join(&format!("v2/{}", encrypted))
35+
.chain_err(|| "Encrypted data is not URL-safe")?;
36+
Ok(final_url.to_string())
3037
} else {
3138
let encrypted = fernet.encrypt(&base).trim_matches('=').to_string();
32-
Ok(format!("{}v1/{}", root, encrypted))
39+
let final_url = root
40+
.join(&format!("v1/{}", encrypted))
41+
.chain_err(|| "Encrypted data is not URL-safe")?;
42+
Ok(final_url.to_string())
3343
}
3444
}

0 commit comments

Comments
 (0)