1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
use crate::rejection::InfallibleRejection;
pub use crate::rejection::StringRejection;
use axum::{
    async_trait,
    extract::FromRequestParts,
    http::{request::Parts, HeaderMap},
};
use std::net::IpAddr;

/// Extracts a list of valid IP addresses from `X-Forwarded-For` header
#[derive(Debug)]
pub struct XForwardedFor(pub Vec<IpAddr>);

/// Extracts the leftmost IP from `X-Forwarded-For` header
///
/// Rejects with a 500 error if the header is absent or there's no valid IP
#[derive(Debug)]
pub struct LeftmostXForwardedFor(pub IpAddr);

/// Extracts the rightmost IP from `X-Forwarded-For` header
///
/// Rejects with a 500 error if the header is absent or there's no valid IP
#[derive(Debug)]
pub struct RightmostXForwardedFor(pub IpAddr);

/// Extracts a list of valid IP addresses from `Forwarded` header
#[derive(Debug)]
pub struct Forwarded(pub Vec<IpAddr>);

/// Extracts the leftmost IP from `Forwarded` header
///
/// Rejects with a 500 error if the header is absent or there's no valid IP
#[derive(Debug)]
pub struct LeftmostForwarded(pub IpAddr);

/// Extracts the rightmost IP from `Forwarded` header
///
/// Rejects with a 500 error if the header is absent or there's no valid IP
#[derive(Debug)]
pub struct RightmostForwarded(pub IpAddr);

/// Extracts a valid IP from `X-Real-Ip` (Nginx) header
///
/// Rejects with a 500 error if the header is absent or the IP isn't valid
#[derive(Debug)]
pub struct XRealIp(pub IpAddr);

/// Extracts a valid IP from `Fly-Client-IP` (Fly.io) header
///
/// When [FlyClientIp] extractor is run for health check path,
/// provide required `Fly-Client-IP` header through
/// [`services.http_checks.headers`](https://fly.io/docs/reference/configuration/#services-http_checks)
/// or [`http_service.checks.headers`](https://fly.io/docs/reference/configuration/#services-http_checks)
///
/// Rejects with a 500 error if the header is absent or the IP isn't valid
#[derive(Debug)]
pub struct FlyClientIp(pub IpAddr);

/// Extracts a valid IP from `True-Client-IP` (Akamai, Cloudflare) header
///
/// Rejects with a 500 error if the header is absent or the IP isn't valid
#[derive(Debug)]
pub struct TrueClientIp(pub IpAddr);

/// Extracts a valid IP from `CF-Connecting-IP` (Cloudflare) header
///
/// Rejects with a 500 error if the header is absent or the IP isn't valid
#[derive(Debug)]
pub struct CfConnectingIp(pub IpAddr);

pub(crate) trait SingleIpHeader {
    const HEADER: &'static str;

    fn maybe_ip_from_headers(headers: &HeaderMap) -> Option<IpAddr> {
        headers
            .get(Self::HEADER)
            .and_then(|hv| hv.to_str().ok())
            .and_then(|s| s.parse::<IpAddr>().ok())
    }

    fn ip_from_headers(headers: &HeaderMap) -> Result<IpAddr, StringRejection> {
        Self::maybe_ip_from_headers(headers).ok_or_else(|| Self::rejection())
    }

    fn rejection() -> StringRejection {
        format!("No `{}` header, or the IP is invalid", Self::HEADER).into()
    }
}

pub(crate) trait MultiIpHeader {
    const HEADER: &'static str;

    fn ips_from_header_value(header_value: &str) -> Vec<IpAddr>;

    fn ips_from_headers(headers: &HeaderMap) -> Vec<IpAddr> {
        headers
            .get_all(Self::HEADER)
            .iter()
            .filter_map(|hv| hv.to_str().ok())
            .flat_map(Self::ips_from_header_value)
            .collect()
    }

    fn maybe_leftmost_ip(headers: &HeaderMap) -> Option<IpAddr> {
        headers
            .get_all(Self::HEADER)
            .iter()
            .filter_map(|hv| hv.to_str().ok())
            .flat_map(Self::ips_from_header_value)
            .next()
    }

    fn leftmost_ip(headers: &HeaderMap) -> Result<IpAddr, StringRejection> {
        Self::maybe_leftmost_ip(headers).ok_or_else(|| Self::rejection())
    }

    fn maybe_rightmost_ip(headers: &HeaderMap) -> Option<IpAddr> {
        headers
            .get_all(Self::HEADER)
            .iter()
            .filter_map(|hv| hv.to_str().ok())
            .flat_map(Self::ips_from_header_value)
            .next_back()
    }

    fn rightmost_ip(headers: &HeaderMap) -> Result<IpAddr, StringRejection> {
        Self::maybe_rightmost_ip(headers).ok_or_else(|| Self::rejection())
    }

    fn rejection() -> StringRejection {
        format!("Couldn't find a valid IP in the `{}` header", Self::HEADER).into()
    }
}

macro_rules! impl_single_header {
    ($type:ty, $header:literal) => {
        impl SingleIpHeader for $type {
            const HEADER: &'static str = $header;
        }

        #[async_trait]
        impl<S> FromRequestParts<S> for $type
        where
            S: Sync,
        {
            type Rejection = StringRejection;

            async fn from_request_parts(
                parts: &mut Parts,
                _state: &S,
            ) -> Result<Self, Self::Rejection> {
                Ok(Self(
                    Self::maybe_ip_from_headers(&parts.headers).ok_or_else(Self::rejection)?,
                ))
            }
        }
    };
}

impl_single_header!(XRealIp, "X-Real-Ip");
impl_single_header!(FlyClientIp, "Fly-Client-IP");
impl_single_header!(TrueClientIp, "True-Client-IP");
impl_single_header!(CfConnectingIp, "CF-Connecting-IP");

impl MultiIpHeader for XForwardedFor {
    const HEADER: &'static str = "X-Forwarded-For";

    fn ips_from_header_value(header_value: &str) -> Vec<IpAddr> {
        header_value
            .split(',')
            .filter_map(|s| s.trim().parse::<IpAddr>().ok())
            .collect()
    }
}

impl MultiIpHeader for Forwarded {
    const HEADER: &'static str = "Forwarded";

    fn ips_from_header_value(header_value: &str) -> Vec<IpAddr> {
        use forwarded_header_value::{ForwardedHeaderValue, Identifier};

        let Ok(fv) = ForwardedHeaderValue::from_forwarded(header_value) else {
            return Vec::new();
        };
        fv.iter()
            .filter_map(|fs| fs.forwarded_for.as_ref())
            .filter_map(|ff| match ff {
                Identifier::SocketAddr(a) => Some(a.ip()),
                Identifier::IpAddr(ip) => Some(*ip),
                _ => None,
            })
            .collect()
    }
}

#[async_trait]
impl<S> FromRequestParts<S> for XForwardedFor
where
    S: Sync,
{
    type Rejection = InfallibleRejection;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        Ok(Self(Self::ips_from_headers(&parts.headers)))
    }
}

#[async_trait]
impl<S> FromRequestParts<S> for LeftmostXForwardedFor
where
    S: Sync,
{
    type Rejection = StringRejection;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        Ok(Self(
            XForwardedFor::maybe_leftmost_ip(&parts.headers)
                .ok_or_else(XForwardedFor::rejection)?,
        ))
    }
}

#[async_trait]
impl<S> FromRequestParts<S> for RightmostXForwardedFor
where
    S: Sync,
{
    type Rejection = StringRejection;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        Ok(Self(
            XForwardedFor::maybe_rightmost_ip(&parts.headers)
                .ok_or_else(XForwardedFor::rejection)?,
        ))
    }
}

#[async_trait]
impl<S> FromRequestParts<S> for Forwarded
where
    S: Sync,
{
    type Rejection = InfallibleRejection;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        Ok(Self(Self::ips_from_headers(&parts.headers)))
    }
}

#[async_trait]
impl<S> FromRequestParts<S> for LeftmostForwarded
where
    S: Sync,
{
    type Rejection = StringRejection;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        Ok(Self(
            Forwarded::maybe_leftmost_ip(&parts.headers).ok_or_else(Forwarded::rejection)?,
        ))
    }
}

#[async_trait]
impl<S> FromRequestParts<S> for RightmostForwarded
where
    S: Sync,
{
    type Rejection = StringRejection;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        Ok(Self(
            Forwarded::maybe_rightmost_ip(&parts.headers).ok_or_else(Forwarded::rejection)?,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::{
        CfConnectingIp, FlyClientIp, Forwarded, LeftmostForwarded, LeftmostXForwardedFor,
        RightmostForwarded, RightmostXForwardedFor, TrueClientIp, XForwardedFor, XRealIp,
    };
    use axum::{
        body::{Body, BoxBody},
        http::{Request, StatusCode},
        routing::get,
        Router,
    };
    use tower::ServiceExt;

    async fn body_string(body: BoxBody) -> String {
        let bytes = hyper::body::to_bytes(body).await.unwrap();
        String::from_utf8_lossy(&bytes).into()
    }

    #[tokio::test]
    async fn x_real_ip() {
        fn app() -> Router {
            Router::new().route("/", get(|ip: XRealIp| async move { ip.0.to_string() }))
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let req = Request::builder()
            .uri("/")
            .header("X-Real-Ip", "1.2.3.4")
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "1.2.3.4");
    }

    #[tokio::test]
    async fn fly_client_ip() {
        fn app() -> Router {
            Router::new().route("/", get(|ip: FlyClientIp| async move { ip.0.to_string() }))
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let req = Request::builder()
            .uri("/")
            .header("Fly-Client-IP", "1.2.3.4")
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "1.2.3.4");
    }

    #[tokio::test]
    async fn true_client_ip() {
        fn app() -> Router {
            Router::new().route("/", get(|ip: TrueClientIp| async move { ip.0.to_string() }))
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let req = Request::builder()
            .uri("/")
            .header("True-Client-IP", "1.2.3.4")
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "1.2.3.4");
    }

    #[tokio::test]
    async fn cf_connecting_ip() {
        fn app() -> Router {
            Router::new().route(
                "/",
                get(|ip: CfConnectingIp| async move { ip.0.to_string() }),
            )
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let req = Request::builder()
            .uri("/")
            .header("CF-Connecting-IP", "1.2.3.4")
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "1.2.3.4");
    }

    #[tokio::test]
    async fn x_forwarded_for() {
        fn app() -> Router {
            Router::new().route(
                "/",
                get(|ips: XForwardedFor| async move { format!("{:?}", ips.0) }),
            )
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "[]");

        let req = Request::builder()
            .uri("/")
            .header(
                "X-Forwarded-For",
                "1.1.1.1, foo, 2001:db8:85a3:8d3:1319:8a2e:370:7348",
            )
            .header("X-Forwarded-For", "bar")
            .header("X-Forwarded-For", "2.2.2.2")
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(
            body_string(res.into_body()).await,
            "[1.1.1.1, 2001:db8:85a3:8d3:1319:8a2e:370:7348, 2.2.2.2]"
        );
    }

    #[tokio::test]
    async fn leftmost_x_forwarded_for() {
        fn app() -> Router {
            Router::new().route(
                "/",
                get(|ip: LeftmostXForwardedFor| async move { format!("{:?}", ip.0) }),
            )
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let req = Request::builder()
            .uri("/")
            .header(
                "X-Forwarded-For",
                "1.1.1.1, foo, 2001:db8:85a3:8d3:1319:8a2e:370:7348",
            )
            .header("X-Forwarded-For", "bar")
            .header("X-Forwarded-For", "2.2.2.2")
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "1.1.1.1");
    }

    #[tokio::test]
    async fn rightmost_x_forwarded_for() {
        fn app() -> Router {
            Router::new().route(
                "/",
                get(|ip: RightmostXForwardedFor| async move { format!("{:?}", ip.0) }),
            )
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let req = Request::builder()
            .uri("/")
            .header(
                "X-Forwarded-For",
                "1.1.1.1, foo, 2001:db8:85a3:8d3:1319:8a2e:370:7348",
            )
            .header("X-Forwarded-For", "bar")
            .header("X-Forwarded-For", "2.2.2.2, 3.3.3.3")
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "3.3.3.3");
    }

    #[tokio::test]
    async fn forwarded() {
        fn app() -> Router {
            Router::new().route(
                "/",
                get(|ips: Forwarded| async move { format!("{:?}", ips.0) }),
            )
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "[]");

        let req = Request::builder()
            .uri("/")
            .header("Forwarded", r#"for="_mdn""#)
            .header("Forwarded", r#"For="[2001:db8:cafe::17]:4711""#)
            .header("Forwarded", r#"for=192.0.2.60;proto=http;by=203.0.113.43"#)
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(
            body_string(res.into_body()).await,
            "[2001:db8:cafe::17, 192.0.2.60]"
        );
    }

    #[tokio::test]
    async fn leftmost_forwarded() {
        fn app() -> Router {
            Router::new().route(
                "/",
                get(|ip: LeftmostForwarded| async move { format!("{:?}", ip.0) }),
            )
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let req = Request::builder()
            .uri("/")
            .header("Forwarded", r#"for="_mdn""#)
            .header("Forwarded", r#"For="[2001:db8:cafe::17]:4711""#)
            .header("Forwarded", r#"for=192.0.2.60;proto=http;by=203.0.113.43"#)
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "2001:db8:cafe::17");
    }

    #[tokio::test]
    async fn rightmost_forwarded() {
        fn app() -> Router {
            Router::new().route(
                "/",
                get(|ip: RightmostForwarded| async move { format!("{:?}", ip.0) }),
            )
        }

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let req = Request::builder()
            .uri("/")
            .header("Forwarded", r#"for="_mdn""#)
            .header("Forwarded", r#"For="[2001:db8:cafe::17]:4711""#)
            .header("Forwarded", r#"for=192.0.2.60;proto=http;by=203.0.113.43"#)
            .body(Body::empty())
            .unwrap();
        let res = app().oneshot(req).await.unwrap();
        assert_eq!(body_string(res.into_body()).await, "192.0.2.60");
    }
}