keri-http-auth 0.2.0

KERI HTTP message signatures and request authentication
Documentation
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Validated HTTP header names, values, and a bounded collection of them.
//!
//! Canonicalizing a signature needs to read the headers a message carries, so this module models
//! just enough of an HTTP field collection to do that safely: names are RFC 9110 tokens, values
//! exclude the control bytes that would forge a line in a signature base, and lookup is
//! ASCII-case-insensitive as HTTP requires.
//!
//! # Why not a plain map
//!
//! Joining repeated fields with a comma would change the bytes a signature covers without either
//! peer noticing, so [`Headers`] refuses a duplicate name instead.

use core::fmt;
use std::error::Error;
use std::str::FromStr;

/// Largest header name this module accepts, in bytes.
pub const MAX_HEADER_NAME_BYTES: usize = 128;

/// Largest header value this module accepts, in bytes.
///
/// Common server limits for a single header sit at 8 KiB.
pub const MAX_HEADER_VALUE_BYTES: usize = 8 * 1024;

/// Largest number of headers one collection holds.
pub const MAX_HEADERS: usize = 128;

/// One validated HTTP field name.
///
/// The accepted bytes are the RFC 9110 `token` set, which excludes `@` — so a header name can never
/// collide with a derived component such as `@method`. The original spelling is preserved because
/// the signature base carries the name exactly as the signer wrote it, while comparison is
/// ASCII-case-insensitive as HTTP requires.
///
/// ```
/// use keri_http_auth::header::HeaderName;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let name = HeaderName::new("Signify-Resource")?;
/// assert_eq!(name.as_str(), "Signify-Resource");
/// assert!(name.matches("signify-resource"));
/// assert!(HeaderName::new("@method").is_err());
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct HeaderName {
    text: String,
}

impl HeaderName {
    /// Validates one field name.
    ///
    /// # Errors
    ///
    /// Returns [`HeaderError::EmptyName`] for empty input, [`HeaderError::NameTooLong`] above
    /// [`MAX_HEADER_NAME_BYTES`], and [`HeaderError::InvalidName`] for a byte outside the token
    /// set.
    pub fn new(text: impl Into<String>) -> Result<Self, HeaderError> {
        let text = text.into();
        if text.is_empty() {
            return Err(HeaderError::EmptyName);
        }
        if text.len() > MAX_HEADER_NAME_BYTES {
            return Err(HeaderError::NameTooLong { length: text.len() });
        }
        if let Some(offset) = text.bytes().position(|byte| !is_token_byte(byte)) {
            return Err(HeaderError::InvalidName {
                name: bounded(&text),
                offset,
            });
        }
        Ok(Self { text })
    }

    /// Returns the name as written.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.text
    }

    /// Returns whether this name is the same field as `other`, ignoring ASCII case.
    #[must_use]
    pub fn matches(&self, other: &str) -> bool {
        self.text.eq_ignore_ascii_case(other)
    }
}

impl PartialEq for HeaderName {
    /// Compares field names the way HTTP does: ignoring ASCII case.
    fn eq(&self, other: &Self) -> bool {
        self.text.eq_ignore_ascii_case(&other.text)
    }
}

impl Eq for HeaderName {}

impl FromStr for HeaderName {
    type Err = HeaderError;

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

impl TryFrom<&str> for HeaderName {
    type Error = HeaderError;

    fn try_from(input: &str) -> Result<Self, Self::Error> {
        Self::new(input)
    }
}

impl fmt::Display for HeaderName {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.text)
    }
}

/// Returns whether a byte is an RFC 9110 `tchar`.
const fn is_token_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric()
        || matches!(
            byte,
            b'!' | b'#' | b'$' | b'%' | b'&' | b'\'' | b'*' | b'+' | b'-' | b'.' | b'^' | b'_' | b'`' | b'|' | b'~'
        )
}

/// One validated HTTP field value, with surrounding whitespace already removed.
///
/// Leading and trailing spaces and tabs are stripped at construction, as RFC 9110 requires of a
/// recipient, so the stored value is the one a signature commits to.
///
/// The accepted scalar values are horizontal tab, `U+0020`–`U+007E`, and `U+00A0`–`U+00FF`: what an
/// HTTP field value can carry, minus the control characters that could forge a line in a signature
/// base.
///
/// ```
/// use keri_http_auth::header::HeaderValue;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// assert_eq!(HeaderValue::new("  application/json  ")?.as_str(), "application/json");
/// assert!(HeaderValue::new("a\nb").is_err());
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HeaderValue {
    text: String,
}

impl HeaderValue {
    /// Validates one field value, trimming surrounding whitespace.
    ///
    /// # Errors
    ///
    /// Returns [`HeaderError::ValueTooLong`] above [`MAX_HEADER_VALUE_BYTES`] and
    /// [`HeaderError::InvalidValue`] for a control character or a scalar an HTTP field cannot
    /// carry. An empty value is accepted, as HTTP allows one.
    pub fn new(text: impl AsRef<str>) -> Result<Self, HeaderError> {
        let text = text.as_ref().trim_matches(is_optional_whitespace);
        if text.len() > MAX_HEADER_VALUE_BYTES {
            return Err(HeaderError::ValueTooLong { length: text.len() });
        }
        if let Some((offset, _)) = text.char_indices().find(|(_, scalar)| !is_field_scalar(*scalar)) {
            return Err(HeaderError::InvalidValue {
                value: bounded(text),
                offset,
            });
        }
        Ok(Self { text: text.to_owned() })
    }

    /// Returns the trimmed value.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.text
    }
}

impl FromStr for HeaderValue {
    type Err = HeaderError;

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

impl TryFrom<&str> for HeaderValue {
    type Error = HeaderError;

    fn try_from(input: &str) -> Result<Self, Self::Error> {
        Self::new(input)
    }
}

impl fmt::Display for HeaderValue {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.text)
    }
}

/// Returns whether a character is the optional whitespace HTTP allows around a field value.
const fn is_optional_whitespace(scalar: char) -> bool {
    matches!(scalar, ' ' | '\t')
}

/// Returns whether a scalar value may appear inside a field value.
const fn is_field_scalar(scalar: char) -> bool {
    matches!(scalar, '\t' | '\u{20}'..='\u{7e}' | '\u{a0}'..='\u{ff}')
}

/// The headers of one request or response, in the order they were added.
///
/// Names are unique, ignoring case. Lookup is case-insensitive.
///
/// ```
/// use keri_http_auth::header::{HeaderName, HeaderValue, Headers};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let headers = Headers::from_pairs([
///     (
///         HeaderName::new("Signify-Resource")?,
///         HeaderValue::new("EWJkQCFvKuyxZi582yJPb0wcwuW3VXmFNuvbQuBpgmIs")?,
///     ),
///     (HeaderName::new("Content-Type")?, HeaderValue::new("application/json")?),
/// ])?;
///
/// assert_eq!(
///     headers.get("signify-resource"),
///     Some("EWJkQCFvKuyxZi582yJPb0wcwuW3VXmFNuvbQuBpgmIs"),
/// );
/// assert_eq!(headers.get("absent"), None);
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Headers {
    entries: Vec<(HeaderName, HeaderValue)>,
}

impl Headers {
    /// Creates an empty collection.
    #[must_use]
    pub const fn new() -> Self {
        Self { entries: Vec::new() }
    }

    /// Collects headers in the given order.
    ///
    /// # Errors
    ///
    /// Returns [`HeaderError::DuplicateHeader`] for a repeated name and
    /// [`HeaderError::TooManyHeaders`] above [`MAX_HEADERS`].
    pub fn from_pairs(pairs: impl IntoIterator<Item = (HeaderName, HeaderValue)>) -> Result<Self, HeaderError> {
        let mut headers = Self::new();
        for (name, value) in pairs {
            headers.insert(name, value)?;
        }
        Ok(headers)
    }

    /// Collects received headers without failing on ones the caller never reads.
    ///
    /// A pair this type cannot hold is dropped, every value of a repeated name is dropped, and
    /// nothing past [`MAX_HEADERS`] is kept. A dropped header reads as absent.
    #[must_use]
    pub fn received<'pair>(pairs: impl IntoIterator<Item = (&'pair str, &'pair str)>) -> Self {
        let mut headers = Self::new();
        let mut repeated: Vec<HeaderName> = Vec::new();
        for (name, value) in pairs {
            let Ok(name) = HeaderName::new(name) else { continue };
            if repeated.contains(&name) {
                continue;
            }
            if let Some(position) = headers.entries.iter().position(|(existing, _)| *existing == name) {
                headers.entries.remove(position);
                repeated.push(name);
                continue;
            }
            if let Ok(value) = HeaderValue::new(value) {
                drop(headers.insert(name, value));
            }
        }
        headers
    }

    /// Adds one header.
    ///
    /// # Errors
    ///
    /// Returns [`HeaderError::DuplicateHeader`] when the name is already present, ignoring case,
    /// and [`HeaderError::TooManyHeaders`] above [`MAX_HEADERS`].
    pub fn insert(&mut self, name: HeaderName, value: HeaderValue) -> Result<(), HeaderError> {
        if self.entries.len() >= MAX_HEADERS {
            return Err(HeaderError::TooManyHeaders { maximum: MAX_HEADERS });
        }
        if self.entries.iter().any(|(existing, _)| *existing == name) {
            return Err(HeaderError::DuplicateHeader {
                name: bounded(name.as_str()),
            });
        }
        self.entries.push((name, value));
        Ok(())
    }

    /// Returns the value of one header, ignoring ASCII case.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&str> {
        self.entries
            .iter()
            .find(|(existing, _)| existing.matches(name))
            .map(|(_, value)| value.as_str())
    }

    /// Returns whether a header is present, ignoring ASCII case.
    #[must_use]
    pub fn contains(&self, name: &str) -> bool {
        self.get(name).is_some()
    }

    /// Returns every header in insertion order.
    pub fn iter(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)> {
        self.entries.iter().map(|(name, value)| (name, value))
    }

    /// Returns how many headers there are.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns whether there are no headers.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

impl<'headers> IntoIterator for &'headers Headers {
    type Item = (&'headers HeaderName, &'headers HeaderValue);
    type IntoIter = std::iter::Map<
        std::slice::Iter<'headers, (HeaderName, HeaderValue)>,
        fn(&'headers (HeaderName, HeaderValue)) -> (&'headers HeaderName, &'headers HeaderValue),
    >;

    fn into_iter(self) -> Self::IntoIter {
        self.entries.iter().map(|(name, value)| (name, value))
    }
}

/// Copies at most thirty-two characters of rejected text into an error.
fn bounded(text: &str) -> String {
    text.chars().take(32).collect()
}

/// Stable HTTP header failures.
#[derive(Debug)]
#[non_exhaustive]
pub enum HeaderError {
    /// A field name is empty.
    EmptyName,
    /// A field name exceeds [`MAX_HEADER_NAME_BYTES`].
    NameTooLong {
        /// Actual length in bytes.
        length: usize,
    },
    /// A field name holds a byte outside the RFC 9110 token set.
    InvalidName {
        /// Bounded copy of the rejected name.
        name: String,
        /// Offset of the first offending byte.
        offset: usize,
    },
    /// A field value exceeds [`MAX_HEADER_VALUE_BYTES`].
    ValueTooLong {
        /// Actual length in bytes.
        length: usize,
    },
    /// A field value holds a control character or a scalar HTTP cannot carry.
    InvalidValue {
        /// Bounded copy of the rejected value.
        value: String,
        /// Byte offset of the first offending character.
        offset: usize,
    },
    /// The same field name appears twice, which would make canonicalization ambiguous.
    DuplicateHeader {
        /// Bounded copy of the repeated name.
        name: String,
    },
    /// A collection exceeds [`MAX_HEADERS`].
    TooManyHeaders {
        /// Accepted maximum.
        maximum: usize,
    },
}

impl fmt::Display for HeaderError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyName => formatter.write_str("a header name is empty"),
            Self::NameTooLong { length } => write!(
                formatter,
                "header name of {length} bytes exceeds maximum {MAX_HEADER_NAME_BYTES}"
            ),
            Self::InvalidName { name, offset } => write!(
                formatter,
                "header name {name:?} holds a byte outside the token set at offset {offset}"
            ),
            Self::ValueTooLong { length } => write!(
                formatter,
                "header value of {length} bytes exceeds maximum {MAX_HEADER_VALUE_BYTES}"
            ),
            Self::InvalidValue { value, offset } => write!(
                formatter,
                "header value {value:?} holds an unusable character at offset {offset}"
            ),
            Self::DuplicateHeader { name } => {
                write!(formatter, "header {name:?} appears more than once")
            }
            Self::TooManyHeaders { maximum } => {
                write!(formatter, "a message carries more than {maximum} headers")
            }
        }
    }
}

impl Error for HeaderError {}

#[cfg(test)]
mod tests {
    use super::*;
    use std::error::Error;

    fn headers() -> Result<Headers, Box<dyn Error>> {
        Ok(Headers::from_pairs([
            (
                HeaderName::new("Signify-Resource")?,
                HeaderValue::new("EWJkQCFvKuyxZi582yJPb0wcwuW3VXmFNuvbQuBpgmIs")?,
            ),
            (
                HeaderName::new("Signify-Timestamp")?,
                HeaderValue::new("2022-09-24T00:05:48.196795+00:00")?,
            ),
        ])?)
    }

    #[test]
    fn names_compare_and_look_up_without_regard_to_case() -> Result<(), Box<dyn Error>> {
        let headers = headers()?;
        assert_eq!(
            headers.get("SIGNIFY-RESOURCE"),
            Some("EWJkQCFvKuyxZi582yJPb0wcwuW3VXmFNuvbQuBpgmIs")
        );
        assert!(headers.contains("signify-timestamp"));
        assert!(!headers.contains("absent"));
        assert_eq!(HeaderName::new("Content-Type")?, HeaderName::new("content-type")?);
        // The spelling is still preserved, because the signature base carries it verbatim.
        assert_eq!(HeaderName::new("Content-Type")?.as_str(), "Content-Type");
        assert_eq!(HeaderName::new("Content-Type")?.to_string(), "Content-Type");
        Ok(())
    }

    #[test]
    fn a_derived_component_is_not_a_header_name() {
        // `@` is not a token byte, so `@method` can never be built as a field name.
        for name in ["@method", "@path", "@signature-params", "a b", "a:b", "a\"b", ""] {
            assert!(HeaderName::new(name).is_err(), "{name:?} was accepted");
        }
    }

    #[test]
    fn header_names_are_bounded() {
        assert!(matches!(
            HeaderName::new("a".repeat(MAX_HEADER_NAME_BYTES + 1)),
            Err(HeaderError::NameTooLong { length }) if length == MAX_HEADER_NAME_BYTES + 1
        ));
        assert!(matches!(HeaderName::new(""), Err(HeaderError::EmptyName)));
        assert!(matches!(
            HeaderName::new("a@b"),
            Err(HeaderError::InvalidName { offset: 1, .. })
        ));
    }

    #[test]
    fn values_are_trimmed_and_control_free() -> Result<(), Box<dyn Error>> {
        assert_eq!(HeaderValue::new("\t padded \t")?.as_str(), "padded");
        assert_eq!(HeaderValue::new("")?.as_str(), "");
        assert_eq!(HeaderValue::new("a b")?.to_string(), "a b");
        // A newline in a value would forge a whole line in the signature base.
        for value in ["a\nb", "a\rb", "a\u{0}b", "a\u{7f}b", "a\u{85}b"] {
            assert!(
                matches!(HeaderValue::new(value), Err(HeaderError::InvalidValue { .. })),
                "{value:?} was accepted",
            );
        }
        // Latin-1 is what an HTTP field value can actually carry.
        assert_eq!(HeaderValue::new("héllo")?.as_str(), "héllo");
        // Nothing above Latin-1 can.
        assert!(matches!(
            HeaderValue::new("世界"),
            Err(HeaderError::InvalidValue { offset: 0, .. })
        ));
        assert!(matches!(
            HeaderValue::new("a".repeat(MAX_HEADER_VALUE_BYTES + 1)),
            Err(HeaderError::ValueTooLong { length }) if length == MAX_HEADER_VALUE_BYTES + 1
        ));
        assert_eq!(HeaderValue::from_str(" x ")?, HeaderValue::try_from("x")?);
        Ok(())
    }

    #[test]
    fn duplicate_names_are_refused() -> Result<(), Box<dyn Error>> {
        // Joining them would change the signature base.
        let mut headers = headers()?;
        assert!(matches!(
            headers.insert(HeaderName::new("signify-resource")?, HeaderValue::new("other")?),
            Err(HeaderError::DuplicateHeader { .. })
        ));
        assert_eq!(headers.len(), 2);
        assert!(!headers.is_empty());
        assert!(Headers::new().is_empty());
        Ok(())
    }

    #[test]
    fn collections_are_bounded_and_iterable() -> Result<(), Box<dyn Error>> {
        let mut headers = Headers::new();
        for position in 0..MAX_HEADERS {
            headers.insert(HeaderName::new(format!("x-{position}"))?, HeaderValue::new("1")?)?;
        }
        assert!(matches!(
            headers.insert(HeaderName::new("x-extra")?, HeaderValue::new("1")?),
            Err(HeaderError::TooManyHeaders { maximum }) if maximum == MAX_HEADERS
        ));

        let headers = self::headers()?;
        let names: Vec<&str> = headers.iter().map(|(name, _)| name.as_str()).collect();
        assert_eq!(names, ["Signify-Resource", "Signify-Timestamp"]);
        assert_eq!((&headers).into_iter().count(), 2);
        Ok(())
    }

    #[test]
    fn errors_carry_readable_text() {
        for error in [
            HeaderError::EmptyName,
            HeaderError::NameTooLong { length: 9 },
            HeaderError::InvalidName {
                name: "a@b".to_owned(),
                offset: 1,
            },
            HeaderError::ValueTooLong { length: 9 },
            HeaderError::InvalidValue {
                value: "a\nb".to_owned(),
                offset: 1,
            },
            HeaderError::DuplicateHeader { name: "x".to_owned() },
            HeaderError::TooManyHeaders { maximum: MAX_HEADERS },
        ] {
            assert_ne!(error.to_string(), "");
            assert!(error.source().is_none());
        }
    }
}