forwarded_header_value/
lib.rs

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
//! This module implements parsing for the Forwarded header as defined by
//! RFC 7239 and RFC 7230. You should generally interact with the [`ForwardedHeaderValue`] struct.
#![warn(missing_docs)]
#![forbid(unsafe_code)]

use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;

use nonempty::NonEmpty;
use thiserror::Error;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
/// HTTP protocol from the remote host
pub enum Protocol {
    /// HTTP
    Http,
    /// HTTPS
    Https,
}

impl FromStr for Protocol {
    type Err = ForwardedHeaderValueParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "http" => Ok(Protocol::Http),
            "https" => Ok(Protocol::Https),
            _ => Err(ForwardedHeaderValueParseError::InvalidProtocol),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[allow(missing_docs)]
/// Remote identifier. This can be an IP:port pair, a bare IP, an underscore-prefixed
/// "obfuscated string", or unknown.
pub enum Identifier {
    SocketAddr(SocketAddr),
    IpAddr(IpAddr),
    String(String),
    Unknown,
}

impl Identifier {
    #[cfg(test)]
    fn for_string<T: ToString>(t: T) -> Self {
        Identifier::String(t.to_string())
    }

    /// Return the IP address for this identifier, if there is one. This will extract it from the
    /// SocketAddr if necessary
    pub fn ip(&self) -> Option<IpAddr> {
        match self {
            Identifier::SocketAddr(sa) => Some(sa.ip()),
            Identifier::IpAddr(ip) => Some(*ip),
            _ => None,
        }
    }
}

impl FromStr for Identifier {
    type Err = ForwardedHeaderValueParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.trim().trim_matches('"').trim_matches('\'');
        if s == "unknown" {
            return Ok(Identifier::Unknown);
        }
        if let Ok(socket_addr) = s.parse::<SocketAddr>() {
            Ok(Identifier::SocketAddr(socket_addr))
        } else if let Ok(ip_addr) = s.parse::<IpAddr>() {
            Ok(Identifier::IpAddr(ip_addr))
        } else if s.starts_with('[') && s.ends_with(']') {
            if let Ok(ip_addr) = s[1..(s.len() - 1)].parse::<IpAddr>() {
                Ok(Identifier::IpAddr(ip_addr))
            } else {
                Err(ForwardedHeaderValueParseError::InvalidAddress)
            }
        } else if s.starts_with('_') {
            Ok(Identifier::String(s.to_string()))
        } else {
            Err(ForwardedHeaderValueParseError::InvalidObfuscatedNode(
                s.to_string(),
            ))
        }
    }
}

#[derive(Debug, Default)]
/// A single forwarded-for line; there may be a sequence of these in a Forwarded header.
///
/// Any parts not specified will be None
#[allow(missing_docs)]
pub struct ForwardedStanza {
    pub forwarded_by: Option<Identifier>,
    pub forwarded_for: Option<Identifier>,
    pub forwarded_host: Option<String>,
    pub forwarded_proto: Option<Protocol>,
}

impl ForwardedStanza {
    /// Get the forwarded-for IP, if one is present
    pub fn forwarded_for_ip(&self) -> Option<IpAddr> {
        self.forwarded_for.as_ref().and_then(|fa| fa.ip())
    }

    /// Get the forwarded-by IP, if one is present
    pub fn forwarded_by_ip(&self) -> Option<IpAddr> {
        self.forwarded_by.as_ref().and_then(|fa| fa.ip())
    }
}

impl FromStr for ForwardedStanza {
    type Err = ForwardedHeaderValueParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut rv = ForwardedStanza::default();
        let s = s.trim();
        for part in s.split(';') {
            let part = part.trim();
            if part.is_empty() {
                continue;
            }
            if let Some((key, value)) = part.split_once('=') {
                match key.to_ascii_lowercase().as_str() {
                    "by" => rv.forwarded_by = Some(value.parse()?),
                    "for" => rv.forwarded_for = Some(value.parse()?),
                    "host" => {
                        rv.forwarded_host = {
                            if value.starts_with('"') && value.ends_with('"') {
                                Some(
                                    value[1..(value.len() - 1)]
                                        .replace("\\\"", "\"")
                                        .replace("\\\\", "\\"),
                                )
                            } else {
                                Some(value.to_string())
                            }
                        }
                    }
                    "proto" => rv.forwarded_proto = Some(value.parse()?),
                    _other => continue,
                }
            } else {
                return Err(ForwardedHeaderValueParseError::InvalidPart(part.to_owned()));
            }
        }
        Ok(rv)
    }
}

/// Iterator over stanzas in a ForwardedHeaderValue
pub struct ForwardedHeaderValueIterator<'a> {
    head: Option<&'a ForwardedStanza>,
    tail: &'a [ForwardedStanza],
}

impl<'a> Iterator for ForwardedHeaderValueIterator<'a> {
    type Item = &'a ForwardedStanza;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(head) = self.head.take() {
            Some(head)
        } else if let Some((first, rest)) = self.tail.split_first() {
            self.tail = rest;
            Some(first)
        } else {
            None
        }
    }
}

impl<'a> DoubleEndedIterator for ForwardedHeaderValueIterator<'a> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if let Some((last, rest)) = self.tail.split_last() {
            self.tail = rest;
            Some(last)
        } else if let Some(head) = self.head.take() {
            Some(head)
        } else {
            None
        }
    }
}

impl<'a> ExactSizeIterator for ForwardedHeaderValueIterator<'a> {
    fn len(&self) -> usize {
        self.tail.len() + if self.head.is_some() { 1 } else { 0 }
    }
}

impl<'a> core::iter::FusedIterator for ForwardedHeaderValueIterator<'a> {}

fn values_from_header(header_value: &str) -> impl Iterator<Item = &str> {
    header_value.trim().split(',').filter_map(|i| {
        let trimmed = i.trim();
        if trimmed.is_empty() {
            None
        } else {
            Some(trimmed)
        }
    })
}

/// This structure represents the contents of the Forwarded header
///
/// It should contain one or more ForwardedStanzas
#[derive(Debug)]
pub struct ForwardedHeaderValue {
    values: NonEmpty<ForwardedStanza>,
}

impl ForwardedHeaderValue {
    /// The number of valid stanzas in this value
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// This can never be empty
    pub fn is_empty(&self) -> bool {
        false
    }

    /// Get the value farthest from this system (the left-most value)
    ///
    /// This may represent the remote client
    pub fn remotest(&self) -> &ForwardedStanza {
        self.values.first()
    }

    /// Get the value farthest from this system (the left-most value), consuming this object
    ///
    /// This may represent the remote client
    pub fn into_remotest(mut self) -> ForwardedStanza {
        if !self.values.tail.is_empty() {
            self.values.tail.pop().unwrap()
        } else {
            self.values.head
        }
    }

    /// Get the value closest to this system (the right-most value).
    ///
    /// This is typically the only trusted value in a well-architected system.
    pub fn proximate(&self) -> &ForwardedStanza {
        self.values.last()
    }

    /// Get the value closest to this system (the right-most value), consuming this object
    ///
    /// This is typically the only trusted value in a well-architected system.
    pub fn into_proximate(mut self) -> ForwardedStanza {
        if !self.values.tail.is_empty() {
            self.values.tail.pop().unwrap()
        } else {
            self.values.head
        }
    }

    /// Iterate through all ForwardedStanzas
    pub fn iter(&self) -> ForwardedHeaderValueIterator {
        ForwardedHeaderValueIterator {
            head: Some(&self.values.head),
            tail: &self.values.tail,
        }
    }

    /// Return the rightmost non-empty forwarded-for IP, if one is present
    pub fn proximate_forwarded_for_ip(&self) -> Option<IpAddr> {
        self.iter().rev().find_map(|i| i.forwarded_for_ip())
    }

    /// Return the leftmost forwarded-for IP, if one is present
    pub fn remotest_forwarded_for_ip(&self) -> Option<IpAddr> {
        self.iter().find_map(|i| i.forwarded_for_ip())
    }

    /// Parse the value from a Forwarded header into this structure
    /// ### Example
    /// ```rust
    /// # use forwarded_header_value::{ForwardedHeaderValue, ForwardedHeaderValueParseError};
    /// # fn main() -> Result<(), ForwardedHeaderValueParseError> {
    /// let input = "for=1.2.3.4;by=\"[::1]:1234\"";
    /// let value = ForwardedHeaderValue::from_forwarded(input)?;
    /// assert_eq!(value.len(), 1);
    /// assert_eq!(value.remotest_forwarded_for_ip(), Some("1.2.3.4".parse()?));
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_forwarded(header_value: &str) -> Result<Self, ForwardedHeaderValueParseError> {
        values_from_header(header_value)
            .map(|stanza| stanza.parse::<ForwardedStanza>())
            .collect::<Result<Vec<_>, _>>()
            .and_then(|v| {
                NonEmpty::from_vec(v).ok_or(ForwardedHeaderValueParseError::HeaderIsEmpty)
            })
            .map(|v| ForwardedHeaderValue { values: v })
    }

    /// Parse the value from an X-Forwarded-For header into this structure
    /// ### Example
    /// ```rust
    /// # use forwarded_header_value::{ForwardedHeaderValue, ForwardedHeaderValueParseError};
    /// # fn main() -> Result<(), ForwardedHeaderValueParseError> {
    /// let input = "1.2.3.4, 5.6.7.8";
    /// let value = ForwardedHeaderValue::from_x_forwarded_for(input)?;
    /// assert_eq!(value.len(), 2);
    /// assert_eq!(value.remotest_forwarded_for_ip(), Some("1.2.3.4".parse()?));
    /// assert_eq!(value.proximate_forwarded_for_ip(), Some("5.6.7.8".parse()?));
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_x_forwarded_for(
        header_value: &str,
    ) -> Result<Self, ForwardedHeaderValueParseError> {
        values_from_header(header_value)
            .map(|address| {
                let a = address.parse::<IpAddr>()?;
                Ok(ForwardedStanza {
                    forwarded_for: Some(Identifier::IpAddr(a)),
                    ..Default::default()
                })
            })
            .collect::<Result<Vec<_>, _>>()
            .and_then(|v| {
                NonEmpty::from_vec(v).ok_or(ForwardedHeaderValueParseError::HeaderIsEmpty)
            })
            .map(|v| ForwardedHeaderValue { values: v })
    }
}

impl IntoIterator for ForwardedHeaderValue {
    type Item = ForwardedStanza;
    type IntoIter = std::iter::Chain<std::iter::Once<Self::Item>, std::vec::IntoIter<Self::Item>>;

    fn into_iter(self) -> Self::IntoIter {
        self.values.into_iter()
    }
}

#[derive(Error, Debug)]
#[allow(missing_docs)]
/// Errors that can occur while parsing a ForwardedHeaderValue
pub enum ForwardedHeaderValueParseError {
    #[error("Header is empty")]
    HeaderIsEmpty,
    #[error("Stanza contained illegal part {0}")]
    InvalidPart(String),
    #[error("Stanza specified an invalid protocol")]
    InvalidProtocol,
    #[error("Identifier specified an invalid or malformed IP address")]
    InvalidAddress,
    #[error("Identifier specified an invalid or malformed port")]
    InvalidPort,
    #[error("Identifier specified uses an obfuscated node ({0:?}) that is invalid")]
    InvalidObfuscatedNode(String),
    #[error("Identifier specified an invalid or malformed IP address")]
    IpParseErr(#[from] std::net::AddrParseError),
}

impl FromStr for ForwardedHeaderValue {
    type Err = ForwardedHeaderValueParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_forwarded(s)
    }
}

#[cfg(test)]
mod tests {
    use super::{ForwardedHeaderValue, Identifier, Protocol};
    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};

    #[test]
    fn test_basic() {
        let s: ForwardedHeaderValue =
            "for=192.0.2.43;proto=https, for=198.51.100.17;by=\"[::1]:1234\";host=\"example.com\""
                .parse()
                .unwrap();
        assert_eq!(s.len(), 2);
        assert_eq!(
            s.proximate().forwarded_for_ip(),
            Some("198.51.100.17".parse().unwrap())
        );
        assert_eq!(
            s.proximate().forwarded_by_ip(),
            Some("::1".parse().unwrap())
        );
        assert_eq!(
            s.proximate().forwarded_host,
            Some(String::from("example.com")),
        );
        assert_eq!(
            s.remotest().forwarded_for_ip(),
            Some("192.0.2.43".parse().unwrap())
        );
        assert_eq!(s.remotest().forwarded_proto, Some(Protocol::Https));
    }

    #[test]
    fn test_rfc_examples() {
        let s = "for=\"_gazonk\"".parse::<ForwardedHeaderValue>().unwrap();
        assert_eq!(
            s.into_proximate().forwarded_for.unwrap(),
            Identifier::for_string("_gazonk")
        );
        let s = "For=\"[2001:db8:cafe::17]:4711\""
            .parse::<ForwardedHeaderValue>()
            .unwrap();
        assert_eq!(s.len(), 1);
        assert_eq!(
            s.into_proximate().forwarded_for.unwrap(),
            Identifier::SocketAddr(SocketAddr::new(
                IpAddr::V6(Ipv6Addr::new(
                    0x2001, 0xdb8, 0xcafe, 0x0, 0x0, 0x0, 0x0, 0x17
                )),
                4711
            ))
        );
        let s = "for=192.0.2.60;proto=http;by=203.0.113.43"
            .parse::<ForwardedHeaderValue>()
            .unwrap();
        assert_eq!(s.len(), 1);
        let proximate = s.into_proximate();
        assert_eq!(
            proximate.forwarded_for.unwrap(),
            Identifier::IpAddr(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 60)))
        );
        assert_eq!(proximate.forwarded_proto.unwrap(), Protocol::Http);
        assert_eq!(
            proximate.forwarded_by.unwrap(),
            Identifier::IpAddr(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 43)))
        );
        assert_eq!(proximate.forwarded_host, None);

        let s = ForwardedHeaderValue::from_forwarded(
            "for=192.0.2.43,for=\"[2001:db8:cafe::17]\",for=unknown",
        )
        .unwrap();
        assert_eq!(
            s.proximate_forwarded_for_ip().unwrap(),
            IpAddr::V6(Ipv6Addr::new(
                0x2001, 0xdb8, 0xcafe, 0x0, 0x0, 0x0, 0x0, 0x17
            ))
        );
        assert_eq!(
            s.remotest_forwarded_for_ip().unwrap(),
            IpAddr::V4(Ipv4Addr::new(192, 0, 2, 43))
        );
    }

    #[test]
    fn test_garbage() {
        let s =
            ForwardedHeaderValue::from_forwarded("for=unknown, for=unknown, for=_poop").unwrap();
        assert_eq!(s.remotest_forwarded_for_ip(), None);
        assert_eq!(s.proximate_forwarded_for_ip(), None);
    }

    #[test]
    fn test_weird_identifiers() {
        let s: ForwardedHeaderValue = "for=unknown, for=_private, for=_secret, ".parse().unwrap();
        assert_eq!(s.len(), 3);
        assert_eq!(
            vec![
                Identifier::Unknown,
                Identifier::for_string("_private"),
                Identifier::for_string("_secret")
            ],
            s.into_iter()
                .map(|s| s.forwarded_for.unwrap())
                .collect::<Vec<Identifier>>()
        );
    }

    #[test]
    fn test_iter_both_directions() {
        let s = ForwardedHeaderValue::from_x_forwarded_for("0.0.0.1, 0.0.0.2, 0.0.0.3").unwrap();
        let forward = s
            .iter()
            .map(|s| {
                if let Some(IpAddr::V4(i)) = s.forwarded_for_ip() {
                    i.octets()[3]
                } else {
                    panic!("bad forward")
                }
            })
            .collect::<Vec<_>>();
        assert_eq!(forward, vec![1u8, 2u8, 3u8]);
        let reverse = s
            .iter()
            .rev()
            .map(|s| {
                if let Some(IpAddr::V4(i)) = s.forwarded_for_ip() {
                    i.octets()[3]
                } else {
                    panic!("bad forward")
                }
            })
            .collect::<Vec<_>>();
        assert_eq!(reverse, vec![3u8, 2u8, 1u8]);
    }
}