Skip to main content

actpub_httpsig/cavage/
header.rs

1//! Parsing and emitting the Cavage `Signature:` header value.
2//!
3//! The header is a comma-separated list of `name=value` pairs where
4//! string-typed parameters (`keyId`, `algorithm`, `headers`, `signature`)
5//! are double-quoted and numeric ones (`created`, `expires`) appear
6//! without quotes. Parameter order is not significant.
7
8use crate::cavage::canonical::CavageHeaderSet;
9use crate::error::Error;
10
11/// Name of the `Signature:` HTTP header.
12pub const SIGNATURE_HEADER: &str = "signature";
13
14/// Parsed `Signature:` header parameters.
15#[derive(Debug, Clone, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct CavageHeaderParams {
18    /// Reference to the public key to verify against.
19    pub key_id: String,
20    /// Algorithm hint; `None` means "detect from the key itself".
21    pub algorithm: Option<String>,
22    /// Which headers participate in the signature base string.
23    pub headers: CavageHeaderSet,
24    /// Base64-encoded signature bytes.
25    pub signature: String,
26    /// Optional `(created)` timestamp in seconds since the UNIX epoch.
27    pub created: Option<i64>,
28    /// Optional `(expires)` timestamp in seconds since the UNIX epoch.
29    pub expires: Option<i64>,
30}
31
32impl CavageHeaderParams {
33    /// Parses the raw `Signature:` header value.
34    ///
35    /// # Errors
36    ///
37    /// Returns [`Error::MalformedSignatureHeader`] if the parameter list
38    /// cannot be decoded, and [`Error::MissingSignatureParameter`] if
39    /// `keyId` or `signature` is absent.
40    pub fn parse(raw: &str) -> Result<Self, Error> {
41        let mut key_id = None;
42        let mut algorithm = None;
43        let mut headers_field: Option<String> = None;
44        let mut signature = None;
45        let mut created = None;
46        let mut expires = None;
47
48        for pair in split_top_level_commas(raw) {
49            let pair = pair.trim();
50            if pair.is_empty() {
51                continue;
52            }
53            let (name, value) = split_once_trim(pair, '=').ok_or_else(|| {
54                Error::MalformedSignatureHeader(format!("missing `=` in `{pair}`"))
55            })?;
56            let value = unquote(value)?;
57            match name {
58                "keyId" | "keyid" => key_id = Some(value.into_owned()),
59                "algorithm" => algorithm = Some(value.into_owned()),
60                "headers" => headers_field = Some(value.into_owned()),
61                "signature" => signature = Some(value.into_owned()),
62                "created" => created = Some(parse_i64_param("created", &value)?),
63                "expires" => expires = Some(parse_i64_param("expires", &value)?),
64                _ => {
65                    // Unknown parameters are ignored per draft §2.1.
66                }
67            }
68        }
69
70        let key_id = key_id.ok_or(Error::MissingSignatureParameter("keyId"))?;
71        let signature = signature.ok_or(Error::MissingSignatureParameter("signature"))?;
72
73        // Per draft §2.1.3: "If not specified, [headers] defaults to
74        // the single value `(created)`. If the `created` signature
75        // parameter is not provided, this parameter defaults to
76        // the single value `date`."
77        //
78        // Fediverse actors always set `headers` explicitly, so the
79        // defaulting branch is a pure fallback for spec-correctness.
80        let headers = headers_field.map_or_else(
81            || {
82                if created.is_some() {
83                    CavageHeaderSet::new(["(created)"])
84                } else {
85                    CavageHeaderSet::new(["date"])
86                }
87            },
88            |v| CavageHeaderSet::new(v.split_ascii_whitespace().map(str::to_owned)),
89        );
90
91        Ok(Self {
92            key_id,
93            algorithm,
94            headers,
95            signature,
96            created,
97            expires,
98        })
99    }
100
101    /// Serialises back into a `Signature:` header value.
102    ///
103    /// String-valued parameters (`keyId`, `algorithm`, `headers`,
104    /// `signature`) are quoted and any `\` or `"` character inside
105    /// them is backslash-escaped per the Cavage quoted-string grammar.
106    #[must_use]
107    #[allow(
108        clippy::expect_used,
109        reason = "writing to an owned `String` via `core::fmt::Write` is infallible; the `Result` only exists to satisfy the trait"
110    )]
111    pub fn to_header_value(&self) -> String {
112        use core::fmt::Write as _;
113        let mut out = String::new();
114        let infallible = "writing to an owned String is infallible";
115        write!(out, r#"keyId="{}""#, escape_quoted(&self.key_id)).expect(infallible);
116        if let Some(alg) = &self.algorithm {
117            write!(out, r#",algorithm="{}""#, escape_quoted(alg)).expect(infallible);
118        }
119        write!(
120            out,
121            r#",headers="{}""#,
122            escape_quoted(&self.headers.join_spaces()),
123        )
124        .expect(infallible);
125        if let Some(c) = self.created {
126            write!(out, ",created={c}").expect(infallible);
127        }
128        if let Some(e) = self.expires {
129            write!(out, ",expires={e}").expect(infallible);
130        }
131        write!(out, r#",signature="{}""#, escape_quoted(&self.signature)).expect(infallible);
132        out
133    }
134}
135
136/// Applies the Cavage quoted-string escape rules: `\` → `\\` and
137/// `"` → `\"`. All other bytes pass through unchanged.
138fn escape_quoted(raw: &str) -> String {
139    let mut out = String::with_capacity(raw.len());
140    for c in raw.chars() {
141        if c == '\\' || c == '"' {
142            out.push('\\');
143        }
144        out.push(c);
145    }
146    out
147}
148
149/// Splits the raw header on top-level commas, skipping commas inside
150/// double-quoted regions so that `signature="a,b,c"` does not break.
151///
152/// The scanner also honours the quoted-string escape sequence `\X`,
153/// treating the next character as literal content. Without this a
154/// payload containing `\"` would prematurely terminate the quoted
155/// region and let an attacker inject additional parameter pairs.
156fn split_top_level_commas(raw: &str) -> impl Iterator<Item = &str> {
157    let mut parts = Vec::new();
158    let mut start = 0;
159    let mut in_quotes = false;
160    let mut escaped = false;
161    for (i, c) in raw.char_indices() {
162        if escaped {
163            escaped = false;
164            continue;
165        }
166        match c {
167            '\\' if in_quotes => escaped = true,
168            '"' => in_quotes = !in_quotes,
169            ',' if !in_quotes => {
170                parts.push(&raw[start..i]);
171                start = i + 1;
172            }
173            _ => {}
174        }
175    }
176    parts.push(&raw[start..]);
177    parts.into_iter()
178}
179
180fn split_once_trim(s: &str, c: char) -> Option<(&str, &str)> {
181    s.split_once(c).map(|(a, b)| (a.trim(), b.trim()))
182}
183
184fn parse_i64_param(name: &'static str, value: &str) -> Result<i64, Error> {
185    value.parse::<i64>().map_err(|_| {
186        Error::MalformedSignatureHeader(format!("`{name}` is not an integer: `{value}`"))
187    })
188}
189
190fn unquote(raw: &str) -> Result<std::borrow::Cow<'_, str>, Error> {
191    if raw.len() < 2 || !raw.starts_with('"') || !raw.ends_with('"') {
192        return Ok(std::borrow::Cow::Borrowed(raw));
193    }
194    let inner = &raw[1..raw.len() - 1];
195    if !inner.contains('\\') {
196        return Ok(std::borrow::Cow::Borrowed(inner));
197    }
198    Ok(std::borrow::Cow::Owned(unescape(inner)?))
199}
200
201/// Unescapes a quoted-string body: `\<X>` becomes `<X>` for any
202/// `<X>`. A trailing lone backslash is an encoding error and is
203/// signalled by [`Error::MalformedSignatureHeader`] via the caller
204/// re-wrapping; `unquote` upgrades the result to `Cow::Owned` only
205/// after confirming the escape sequence is well-formed.
206fn unescape(inner: &str) -> Result<String, Error> {
207    let mut out = String::with_capacity(inner.len());
208    let mut chars = inner.chars();
209    while let Some(c) = chars.next() {
210        if c == '\\' {
211            let next = chars.next().ok_or_else(|| {
212                Error::MalformedSignatureHeader(
213                    "quoted-string ends with a lone backslash".to_owned(),
214                )
215            })?;
216            out.push(next);
217        } else {
218            out.push(c);
219        }
220    }
221    Ok(out)
222}
223
224#[cfg(test)]
225mod tests {
226    use pretty_assertions::assert_eq;
227
228    use super::*;
229
230    const MASTODON_SAMPLE: &str = r#"keyId="https://mastodon.social/users/alice#main-key",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="Zm9v""#;
231
232    #[test]
233    fn parses_mastodon_style_header() {
234        let params = CavageHeaderParams::parse(MASTODON_SAMPLE).expect("parse");
235        assert_eq!(
236            params.key_id,
237            "https://mastodon.social/users/alice#main-key"
238        );
239        assert_eq!(params.algorithm.as_deref(), Some("rsa-sha256"));
240        assert_eq!(params.headers.len(), 4);
241        assert_eq!(params.signature, "Zm9v");
242        assert_eq!(params.created, None);
243        assert_eq!(params.expires, None);
244    }
245
246    #[test]
247    fn header_roundtrips_through_serialisation() {
248        let params = CavageHeaderParams::parse(MASTODON_SAMPLE).expect("parse");
249        let emitted = params.to_header_value();
250        let reparsed = CavageHeaderParams::parse(&emitted).expect("reparse");
251        assert_eq!(reparsed, params);
252    }
253
254    #[test]
255    fn missing_key_id_produces_specific_error() {
256        let err =
257            CavageHeaderParams::parse(r#"algorithm="rsa-sha256",headers="host",signature="Zm9v""#)
258                .expect_err("missing keyId");
259        assert!(matches!(err, Error::MissingSignatureParameter("keyId")));
260    }
261
262    #[test]
263    fn missing_signature_produces_specific_error() {
264        let err = CavageHeaderParams::parse(r#"keyId="foo",algorithm="rsa-sha256",headers="host""#)
265            .expect_err("missing signature");
266        assert!(matches!(err, Error::MissingSignatureParameter("signature")));
267    }
268
269    #[test]
270    fn unquoted_parameters_are_tolerated() {
271        // Some server implementations emit bare tokens for created/expires.
272        let raw =
273            r#"keyId="foo",headers="host",created=1700000000,expires=1700001000,signature="Zm9v""#;
274        let params = CavageHeaderParams::parse(raw).expect("parse");
275        assert_eq!(params.created, Some(1_700_000_000));
276        assert_eq!(params.expires, Some(1_700_001_000));
277    }
278
279    #[test]
280    fn unknown_parameters_are_silently_skipped() {
281        let raw = r#"keyId="foo",headers="host",signature="Zm9v",future_thing="ignored""#;
282        let params = CavageHeaderParams::parse(raw).expect("parse");
283        assert_eq!(params.key_id, "foo");
284    }
285
286    #[test]
287    fn commas_inside_quoted_signature_do_not_split_parameters() {
288        let raw = r#"keyId="has,comma",headers="host",signature="ZmF,vo""#;
289        let params = CavageHeaderParams::parse(raw).expect("parse");
290        assert_eq!(params.key_id, "has,comma");
291        assert_eq!(params.signature, "ZmF,vo");
292    }
293
294    #[test]
295    fn missing_headers_parameter_with_created_defaults_to_created_pseudo() {
296        // Per §2.1.3 the default `headers` value is `(created)` when
297        // the `created` parameter is present.
298        let raw = r#"keyId="k",created=1700000000,signature="Zm9v""#;
299        let params = CavageHeaderParams::parse(raw).expect("parse");
300        assert_eq!(params.headers.len(), 1);
301        assert!(params.headers.iter().any(|h| h == "(created)"));
302    }
303
304    #[test]
305    fn missing_headers_parameter_without_created_defaults_to_date() {
306        // §2.1.3 falls back to `date` when both `headers` and
307        // `created` are absent -- the Mastodon-compatible corner case.
308        let raw = r#"keyId="k",signature="Zm9v""#;
309        let params = CavageHeaderParams::parse(raw).expect("parse");
310        assert_eq!(params.headers.len(), 1);
311        assert!(params.headers.iter().any(|h| h == "date"));
312    }
313
314    #[test]
315    fn escaped_double_quote_inside_quoted_string_survives_splitting() {
316        // Without the escape-aware splitter this payload would split
317        // early at the `"` inside `evil"`, dropping the `,attacker=`
318        // trailer into a separate parameter.
319        let raw = r#"keyId="legit\"evil",headers="host",signature="Zm9v""#;
320        let params = CavageHeaderParams::parse(raw).expect("parse");
321        assert_eq!(params.key_id, r#"legit"evil"#);
322    }
323
324    #[test]
325    fn to_header_value_escapes_embedded_quote_and_backslash() {
326        let raw = r#"keyId="legit\"evil\\trail",headers="host",signature="Zm9v""#;
327        let params = CavageHeaderParams::parse(raw).expect("parse");
328        let emitted = params.to_header_value();
329        // Re-parsing must recover the same logical value.
330        let reparsed = CavageHeaderParams::parse(&emitted).expect("reparse escaped");
331        assert_eq!(reparsed.key_id, r#"legit"evil\trail"#);
332        // And the escapes must actually be present on the wire.
333        assert!(emitted.contains(r#"\""#));
334        assert!(emitted.contains(r"\\"));
335    }
336
337    #[test]
338    fn parameter_value_with_lone_trailing_backslash_is_rejected() {
339        // A quoted value whose contents end in an unpaired backslash
340        // is an encoding error: the parser has no way to know which
341        // character the `\` was meant to escape.
342        let raw = r#"keyId="abc\""#;
343        let err = CavageHeaderParams::parse(raw).expect_err("malformed escape must fail");
344        assert!(matches!(err, Error::MalformedSignatureHeader(_)));
345    }
346}