Skip to content

Commit 0fff04a

Browse files
fix: use url crate to parse hostnames in RedirectHttps (#160)
* Adjusted http upgrade to use url crate to parse hostnames * test: add RedirectHttps to_ipv6 test --------- Co-authored-by: Rob Ede <[email protected]>
1 parent afd6c47 commit 0fff04a

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

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.

actix-web-lab/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix `RedirectHttps` middleware when used with IPv6 addresses.
6+
57
## 0.24.0
68

79
- Re-work `Json` extractor error handling.

actix-web-lab/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ serde_path_to_error = "0.1"
7676
tokio = { version = "1.43.0", features = ["sync", "macros"] }
7777
tokio-stream = "0.1.17"
7878
tracing = { version = "0.1.41", features = ["log"] }
79+
url = "2.1"
7980

8081
# cbor
8182
serde_cbor_2 = { version = "0.12.0-dev", optional = true }

actix-web-lab/src/redirect_to_https.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ where
119119
let host = conn_info.host();
120120

121121
// construct equivalent https path
122-
let (hostname, _port) = host.split_once(':').unwrap_or((host, ""));
122+
let parsed_url = url::Url::parse(&format!("http://{host}"));
123+
let hostname = match &parsed_url {
124+
Ok(url) => url.host_str().unwrap_or(""),
125+
Err(_) => host.split_once(':').map_or("", |(host, _port)| host),
126+
};
123127

124128
let path = req.uri().path();
125129
let uri = match port {
@@ -267,6 +271,18 @@ mod tests {
267271
assert_response_matches!(res, TEMPORARY_REDIRECT; "location" => "https://localhost:8443/");
268272
}
269273

274+
#[actix_web::test]
275+
async fn to_ipv6() {
276+
let app = RedirectHttps::default()
277+
.new_transform(test::ok_service())
278+
.await
279+
.unwrap();
280+
281+
let req = test_request!(GET "http://[fe80::1234:1234:1234:1234]/").to_srv_request();
282+
let res = test::call_service(&app, req).await;
283+
assert_response_matches!(res, TEMPORARY_REDIRECT; "location" => "https://[fe80::1234:1234:1234:1234]/");
284+
}
285+
270286
#[actix_web::test]
271287
async fn to_custom_port_when_port_in_host() {
272288
let app = RedirectHttps::default()

0 commit comments

Comments
 (0)