1
- use super :: rejection:: { FailedToResolveHost , HostRejection } ;
1
+ use super :: {
2
+ rejection:: { FailedToResolveHost , HostRejection } ,
3
+ SpoofableValue ,
4
+ } ;
2
5
use axum:: extract:: FromRequestParts ;
3
6
use http:: {
4
7
header:: { HeaderMap , FORWARDED } ,
@@ -18,7 +21,7 @@ const X_FORWARDED_HOST_HEADER_KEY: &str = "X-Forwarded-Host";
18
21
/// Note that user agents can set `X-Forwarded-Host` and `Host` headers to arbitrary values so make
19
22
/// sure to validate them to avoid security issues.
20
23
#[ derive( Debug , Clone ) ]
21
- pub struct Host ( pub String ) ;
24
+ pub struct Host ( pub SpoofableValue ) ;
22
25
23
26
impl < S > FromRequestParts < S > for Host
24
27
where
@@ -28,27 +31,27 @@ where
28
31
29
32
async fn from_request_parts ( parts : & mut Parts , _state : & S ) -> Result < Self , Self :: Rejection > {
30
33
if let Some ( host) = parse_forwarded ( & parts. headers ) {
31
- return Ok ( Host ( host. to_owned ( ) ) ) ;
34
+ return Ok ( Host ( SpoofableValue :: new ( host. to_owned ( ) ) ) ) ;
32
35
}
33
36
34
37
if let Some ( host) = parts
35
38
. headers
36
39
. get ( X_FORWARDED_HOST_HEADER_KEY )
37
40
. and_then ( |host| host. to_str ( ) . ok ( ) )
38
41
{
39
- return Ok ( Host ( host. to_owned ( ) ) ) ;
42
+ return Ok ( Host ( SpoofableValue :: new ( host. to_owned ( ) ) ) ) ;
40
43
}
41
44
42
45
if let Some ( host) = parts
43
46
. headers
44
47
. get ( http:: header:: HOST )
45
48
. and_then ( |host| host. to_str ( ) . ok ( ) )
46
49
{
47
- return Ok ( Host ( host. to_owned ( ) ) ) ;
50
+ return Ok ( Host ( SpoofableValue :: new ( host. to_owned ( ) ) ) ) ;
48
51
}
49
52
50
53
if let Some ( host) = parts. uri . host ( ) {
51
- return Ok ( Host ( host. to_owned ( ) ) ) ;
54
+ return Ok ( Host ( SpoofableValue :: new ( host. to_owned ( ) ) ) ) ;
52
55
}
53
56
54
57
Err ( HostRejection :: FailedToResolveHost ( FailedToResolveHost ) )
@@ -81,7 +84,7 @@ mod tests {
81
84
82
85
fn test_client ( ) -> TestClient {
83
86
async fn host_as_body ( Host ( host) : Host ) -> String {
84
- host
87
+ host. spoofable_value ( )
85
88
}
86
89
87
90
TestClient :: new ( Router :: new ( ) . route ( "/" , get ( host_as_body) ) )
0 commit comments