minco-http 0.5.0

Axum and Tower HTTP conventions, principals, request IDs, limits, and RFC 9457 errors for Minco
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
use axum::Router;
use http::{HeaderName, HeaderValue, Method, StatusCode, header};
use std::{collections::BTreeMap, str::FromStr, time::Duration};
use thiserror::Error;
use tower_http::{
    compression::CompressionLayer,
    cors::{AllowOrigin, CorsLayer},
    limit::RequestBodyLimitLayer,
    request_id::{MakeRequestUuid, PropagateRequestIdLayer, SetRequestIdLayer},
    sensitive_headers::SetSensitiveRequestHeadersLayer,
    timeout::TimeoutLayer,
    trace::TraceLayer,
};

pub static REQUEST_ID_HEADER: HeaderName = HeaderName::from_static("x-request-id");
pub static CSRF_HEADER: HeaderName = HeaderName::from_static("x-minco-csrf");

/// Exact browser-request, response-exposure, and diagnostic-redaction policy.
///
/// Header names are normalized by `http::HeaderName`, de-duplicated
/// deterministically, and never accept the wildcard token. Applications own the
/// baseline policy; installed HTTP plugins add only their exact requirements.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HttpHeaderPolicy {
    allowed_request: BTreeMap<String, HeaderName>,
    exposed_response: BTreeMap<String, HeaderName>,
    sensitive_request: BTreeMap<String, HeaderName>,
}

impl Default for HttpHeaderPolicy {
    fn default() -> Self {
        let mut policy = Self::empty();
        for name in [
            header::AUTHORIZATION,
            header::CONTENT_TYPE,
            HeaderName::from_static("idempotency-key"),
            REQUEST_ID_HEADER.clone(),
        ] {
            policy
                .allow_request_header(name)
                .expect("built-in header is valid");
        }
        policy
            .expose_response_header(REQUEST_ID_HEADER.clone())
            .expect("built-in header is valid");
        for name in [
            header::AUTHORIZATION,
            header::COOKIE,
            HeaderName::from_static("idempotency-key"),
        ] {
            policy
                .mark_request_header_sensitive(name)
                .expect("built-in header is valid");
        }
        policy
    }
}

impl HttpHeaderPolicy {
    #[must_use]
    pub const fn empty() -> Self {
        Self {
            allowed_request: BTreeMap::new(),
            exposed_response: BTreeMap::new(),
            sensitive_request: BTreeMap::new(),
        }
    }

    pub fn allow_request_header(&mut self, name: HeaderName) -> Result<(), HttpConfigurationError> {
        insert_header(&mut self.allowed_request, name)
    }

    pub fn expose_response_header(
        &mut self,
        name: HeaderName,
    ) -> Result<(), HttpConfigurationError> {
        insert_header(&mut self.exposed_response, name)
    }

    pub fn mark_request_header_sensitive(
        &mut self,
        name: HeaderName,
    ) -> Result<(), HttpConfigurationError> {
        insert_header(&mut self.sensitive_request, name)
    }

    pub fn allow_request_header_name(&mut self, name: &str) -> Result<(), HttpConfigurationError> {
        self.allow_request_header(parse_header(name)?)
    }

    pub fn expose_response_header_name(
        &mut self,
        name: &str,
    ) -> Result<(), HttpConfigurationError> {
        self.expose_response_header(parse_header(name)?)
    }

    pub fn mark_request_header_name_sensitive(
        &mut self,
        name: &str,
    ) -> Result<(), HttpConfigurationError> {
        self.mark_request_header_sensitive(parse_header(name)?)
    }

    /// Enables the application-selected cookie/CSRF request boundary.
    ///
    /// `Cookie` is already marked sensitive and is browser-managed, so only
    /// the exact CSRF header is added to the CORS request set.
    pub fn enable_cookie_csrf(&mut self) -> Result<(), HttpConfigurationError> {
        self.allow_request_header(CSRF_HEADER.clone())?;
        self.mark_request_header_sensitive(CSRF_HEADER.clone())
    }

    pub(crate) fn merge(&mut self, additions: &Self) -> Result<(), HttpConfigurationError> {
        for name in additions.allowed_request.values().cloned() {
            self.allow_request_header(name)?;
        }
        for name in additions.exposed_response.values().cloned() {
            self.expose_response_header(name)?;
        }
        for name in additions.sensitive_request.values().cloned() {
            self.mark_request_header_sensitive(name)?;
        }
        Ok(())
    }

    fn validate(&self) -> Result<(), HttpConfigurationError> {
        for name in self
            .allowed_request
            .values()
            .chain(self.exposed_response.values())
            .chain(self.sensitive_request.values())
        {
            reject_wildcard(name)?;
        }
        Ok(())
    }

    #[must_use]
    pub fn allowed_request_headers(&self) -> Vec<HeaderName> {
        self.allowed_request.values().cloned().collect()
    }

    #[must_use]
    pub fn exposed_response_headers(&self) -> Vec<HeaderName> {
        self.exposed_response.values().cloned().collect()
    }

    #[must_use]
    pub fn sensitive_request_headers(&self) -> Vec<HeaderName> {
        self.sensitive_request.values().cloned().collect()
    }
}

#[derive(Debug, Clone)]
pub struct HttpRuntimeConfig {
    /// Exact origins accepted by the browser API. Wildcards are intentionally unsupported.
    pub allowed_origins: Vec<String>,
    /// Allows cookies and browser authorization credentials for exact configured origins.
    pub allow_credentials: bool,
    pub timeout: Duration,
    pub max_request_body_bytes: usize,
    pub compression: bool,
    /// Application baseline extended by exact installed-plugin requirements.
    pub header_policy: HttpHeaderPolicy,
}

impl Default for HttpRuntimeConfig {
    fn default() -> Self {
        Self {
            allowed_origins: vec!["http://127.0.0.1:3000".into()],
            allow_credentials: false,
            timeout: Duration::from_secs(15),
            max_request_body_bytes: 1024 * 1024,
            compression: true,
            header_policy: HttpHeaderPolicy::default(),
        }
    }
}

pub fn apply_standard_middleware(
    router: Router,
    config: &HttpRuntimeConfig,
) -> Result<Router, HttpConfigurationError> {
    validate_runtime_config(config)?;
    let origins = config
        .allowed_origins
        .iter()
        .map(|origin| {
            HeaderValue::from_str(origin).map_err(|source| HttpConfigurationError::InvalidOrigin {
                origin: origin.clone(),
                source,
            })
        })
        .collect::<Result<Vec<_>, _>>()?;

    let cors = CorsLayer::new()
        .allow_origin(AllowOrigin::list(origins))
        .allow_methods([
            Method::GET,
            Method::POST,
            Method::PUT,
            Method::PATCH,
            Method::DELETE,
            Method::OPTIONS,
        ])
        .allow_headers(config.header_policy.allowed_request_headers())
        .expose_headers(config.header_policy.exposed_response_headers());
    let cors = if config.allow_credentials {
        cors.allow_credentials(true)
    } else {
        cors
    };

    let router = router
        .layer(RequestBodyLimitLayer::new(config.max_request_body_bytes))
        .layer(TimeoutLayer::with_status_code(
            StatusCode::REQUEST_TIMEOUT,
            config.timeout,
        ))
        .layer(PropagateRequestIdLayer::new(REQUEST_ID_HEADER.clone()))
        .layer(SetRequestIdLayer::new(
            REQUEST_ID_HEADER.clone(),
            MakeRequestUuid,
        ))
        .layer(SetSensitiveRequestHeadersLayer::new(
            config.header_policy.sensitive_request_headers(),
        ))
        .layer(cors)
        .layer(TraceLayer::new_for_http());

    Ok(if config.compression {
        router.layer(CompressionLayer::new())
    } else {
        router
    })
}

fn validate_runtime_config(config: &HttpRuntimeConfig) -> Result<(), HttpConfigurationError> {
    if config.allowed_origins.is_empty() {
        return Err(HttpConfigurationError::NoAllowedOrigins);
    }
    if config
        .allowed_origins
        .iter()
        .any(|origin| origin.trim() == "*")
    {
        return Err(HttpConfigurationError::WildcardOrigin);
    }
    if config.timeout.is_zero() {
        return Err(HttpConfigurationError::ZeroTimeout);
    }
    if config.max_request_body_bytes == 0 {
        return Err(HttpConfigurationError::ZeroRequestBodyLimit);
    }
    config.header_policy.validate()
}

fn parse_header(name: &str) -> Result<HeaderName, HttpConfigurationError> {
    HeaderName::from_str(name).map_err(|source| HttpConfigurationError::InvalidHeaderName {
        name: name.to_owned(),
        source,
    })
}

fn insert_header(
    destination: &mut BTreeMap<String, HeaderName>,
    name: HeaderName,
) -> Result<(), HttpConfigurationError> {
    reject_wildcard(&name)?;
    destination.insert(name.as_str().to_owned(), name);
    Ok(())
}

fn reject_wildcard(name: &HeaderName) -> Result<(), HttpConfigurationError> {
    if name.as_str() == "*" {
        Err(HttpConfigurationError::WildcardHeader)
    } else {
        Ok(())
    }
}

#[derive(Debug, Error)]
pub enum HttpConfigurationError {
    #[error("HTTP policy requires at least one exact allowed origin")]
    NoAllowedOrigins,
    #[error("wildcard CORS origins are unsupported")]
    WildcardOrigin,
    #[error("wildcard HTTP headers are unsupported")]
    WildcardHeader,
    #[error("HTTP timeout must be greater than zero")]
    ZeroTimeout,
    #[error("HTTP request-body limit must be greater than zero")]
    ZeroRequestBodyLimit,
    #[error("invalid allowed origin {origin:?}: {source}")]
    InvalidOrigin {
        origin: String,
        #[source]
        source: http::header::InvalidHeaderValue,
    },
    #[error("invalid HTTP header name {name:?}: {source}")]
    InvalidHeaderName {
        name: String,
        #[source]
        source: http::header::InvalidHeaderName,
    },
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{body::Body, routing::get};
    use tower::ServiceExt;

    #[tokio::test]
    async fn standard_stack_sets_and_propagates_request_ids() {
        let app = apply_standard_middleware(
            Router::new().route("/", get(|| async { "ok" })),
            &HttpRuntimeConfig::default(),
        )
        .unwrap();
        let response = app
            .oneshot(http::Request::get("/").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert!(response.headers().contains_key(&REQUEST_ID_HEADER));
    }

    #[tokio::test]
    async fn credentialed_cors_uses_only_the_exact_configured_origin() {
        let config = HttpRuntimeConfig {
            allowed_origins: vec!["https://client.example".into()],
            allow_credentials: true,
            ..HttpRuntimeConfig::default()
        };
        let app =
            apply_standard_middleware(Router::new().route("/", get(|| async { "ok" })), &config)
                .unwrap();
        let response = app
            .oneshot(
                http::Request::builder()
                    .method(Method::OPTIONS)
                    .uri("/")
                    .header(header::ORIGIN, "https://client.example")
                    .header(header::ACCESS_CONTROL_REQUEST_METHOD, "GET")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(
            response.headers().get(header::ACCESS_CONTROL_ALLOW_ORIGIN),
            Some(&HeaderValue::from_static("https://client.example"))
        );
        assert_eq!(
            response
                .headers()
                .get(header::ACCESS_CONTROL_ALLOW_CREDENTIALS),
            Some(&HeaderValue::from_static("true"))
        );
    }

    #[test]
    fn wildcard_origins_and_headers_fail_configuration() {
        let mut config = HttpRuntimeConfig {
            allowed_origins: vec!["*".into()],
            ..HttpRuntimeConfig::default()
        };
        assert!(matches!(
            apply_standard_middleware(Router::new(), &config),
            Err(HttpConfigurationError::WildcardOrigin)
        ));

        config.allowed_origins = vec!["https://client.example".into()];
        assert!(matches!(
            config
                .header_policy
                .allow_request_header(HeaderName::from_static("*")),
            Err(HttpConfigurationError::WildcardHeader)
        ));
    }

    #[tokio::test]
    async fn default_policy_does_not_allow_plugin_specific_headers() {
        let app = apply_standard_middleware(
            Router::new().route("/", get(|| async { "ok" })),
            &HttpRuntimeConfig::default(),
        )
        .unwrap();
        let response = app
            .oneshot(
                http::Request::builder()
                    .method(Method::OPTIONS)
                    .uri("/")
                    .header(header::ORIGIN, "http://127.0.0.1:3000")
                    .header(header::ACCESS_CONTROL_REQUEST_METHOD, "GET")
                    .header(
                        header::ACCESS_CONTROL_REQUEST_HEADERS,
                        "x-minco-feedback-token",
                    )
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        let allowed = response
            .headers()
            .get(header::ACCESS_CONTROL_ALLOW_HEADERS)
            .and_then(|value| value.to_str().ok())
            .unwrap_or_default();
        assert!(!allowed.contains("x-minco-feedback-token"), "{allowed}");
    }
}