Skip to content

Commit 3147aac

Browse files
joelwurtzrobjtede
andauthored
feat: do not use host header on http2 for guard (#3525)
* feat(guard): do not use host header on http2 for guard * docs: update changelog --------- Co-authored-by: Rob Ede <[email protected]>
1 parent 079400a commit 3147aac

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

actix-web/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Improve handling of non-UTF-8 header values in `Logger` middleware.
77
- Add `HttpServer::shutdown_signal()` method.
88
- Mark `HttpServer` as `#[must_use]`.
9+
- Ignore `Host` header in `Host` guard when connection protocol is HTTP/2.
910
- Re-export `mime` dependency.
1011
- Update `brotli` dependency to `8`.
1112

actix-web/src/guard/host.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use actix_http::{header, uri::Uri, RequestHead};
1+
use actix_http::{header, uri::Uri, RequestHead, Version};
22

33
use super::{Guard, GuardContext};
44

@@ -66,6 +66,7 @@ fn get_host_uri(req: &RequestHead) -> Option<Uri> {
6666
req.headers
6767
.get(header::HOST)
6868
.and_then(|host_value| host_value.to_str().ok())
69+
.filter(|_| req.version < Version::HTTP_2)
6970
.or_else(|| req.uri.host())
7071
.and_then(|host| host.parse().ok())
7172
}
@@ -123,6 +124,38 @@ mod tests {
123124
use super::*;
124125
use crate::test::TestRequest;
125126

127+
#[test]
128+
fn host_not_from_header_if_http2() {
129+
let req = TestRequest::default()
130+
.uri("www.rust-lang.org")
131+
.insert_header((
132+
header::HOST,
133+
header::HeaderValue::from_static("www.example.com"),
134+
))
135+
.to_srv_request();
136+
137+
let host = Host("www.example.com");
138+
assert!(host.check(&req.guard_ctx()));
139+
140+
let host = Host("www.rust-lang.org");
141+
assert!(!host.check(&req.guard_ctx()));
142+
143+
let req = TestRequest::default()
144+
.version(actix_http::Version::HTTP_2)
145+
.uri("www.rust-lang.org")
146+
.insert_header((
147+
header::HOST,
148+
header::HeaderValue::from_static("www.example.com"),
149+
))
150+
.to_srv_request();
151+
152+
let host = Host("www.example.com");
153+
assert!(!host.check(&req.guard_ctx()));
154+
155+
let host = Host("www.rust-lang.org");
156+
assert!(host.check(&req.guard_ctx()));
157+
}
158+
126159
#[test]
127160
fn host_from_header() {
128161
let req = TestRequest::default()

0 commit comments

Comments
 (0)