proa_framework_axum 0.3.0

Axum and Tokio adapter for Proa SSR applications.
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
567
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Reverse proxy implementation for SSR external apps
use std::net::SocketAddr;
use std::sync::{Arc, OnceLock};
use std::time::Duration;

use axum::{
    body::Body,
    extract::{ConnectInfo, Request, State},
    http::{header, HeaderMap, HeaderName, HeaderValue, StatusCode},
    response::{IntoResponse, Response},
};
use thiserror::Error;

/// Default connect timeout for upstream requests.
pub const DEFAULT_PROXY_CONNECT_TIMEOUT: Duration = Duration::from_secs(5);

/// Default end-to-end timeout for upstream requests (connect + send +
/// response headers + body).
pub const DEFAULT_PROXY_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);

/// Default cap on the buffered request body forwarded upstream (10 MiB).
///
/// `FrameworkBuilder::build` aligns this with the framework-wide body
/// limit (`DEFAULT_BODY_LIMIT_BYTES` / `with_body_limit`) when it mounts
/// SSR external apps; this constant is the fallback for callers that
/// construct a [`ProxyConfig`] directly.
pub const DEFAULT_PROXY_REQUEST_BODY_LIMIT: usize = 10 * 1024 * 1024;

/// Default cap on the buffered upstream response body (16 MiB).
///
/// SECURITY: the proxy buffers the full upstream response in memory
/// before relaying it, so an unbounded read would let a malicious or
/// misbehaving upstream exhaust server memory.
pub const DEFAULT_PROXY_RESPONSE_BODY_LIMIT: usize = 16 * 1024 * 1024;

/// Configuration for the reverse proxy
#[derive(Debug, Clone)]
pub struct ProxyConfig {
    /// Upstream server URL (e.g., "http://localhost:4000")
    pub upstream: String,

    /// Whether to strip the mount path prefix when proxying
    pub strip_prefix: bool,

    /// Mount path to strip if strip_prefix is true
    pub mount_path: String,

    /// TCP connect timeout for upstream requests.
    pub connect_timeout: Duration,

    /// End-to-end timeout for upstream requests.
    pub request_timeout: Duration,

    /// Maximum request body size buffered and forwarded upstream.
    pub max_request_body_bytes: usize,

    /// Maximum upstream response body size buffered and relayed back.
    pub max_response_body_bytes: usize,

    /// Lazily built shared HTTP client. One client per proxy mount, so
    /// connection pools are reused across requests instead of paying a
    /// fresh client (and connection) per proxied request.
    client: OnceLock<reqwest::Client>,
}

impl ProxyConfig {
    /// Create a proxy config with the default timeouts and body limits.
    pub fn new(
        upstream: impl Into<String>,
        strip_prefix: bool,
        mount_path: impl Into<String>,
    ) -> Self {
        Self {
            upstream: upstream.into(),
            strip_prefix,
            mount_path: mount_path.into(),
            connect_timeout: DEFAULT_PROXY_CONNECT_TIMEOUT,
            request_timeout: DEFAULT_PROXY_REQUEST_TIMEOUT,
            max_request_body_bytes: DEFAULT_PROXY_REQUEST_BODY_LIMIT,
            max_response_body_bytes: DEFAULT_PROXY_RESPONSE_BODY_LIMIT,
            client: OnceLock::new(),
        }
    }

    /// Override the upstream TCP connect timeout.
    pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = timeout;
        self
    }

    /// Override the end-to-end upstream request timeout.
    pub fn with_request_timeout(mut self, timeout: Duration) -> Self {
        self.request_timeout = timeout;
        self
    }

    /// Override the maximum request body size forwarded upstream.
    pub fn with_max_request_body_bytes(mut self, bytes: usize) -> Self {
        self.max_request_body_bytes = bytes;
        self
    }

    /// Override the maximum upstream response body size relayed back.
    pub fn with_max_response_body_bytes(mut self, bytes: usize) -> Self {
        self.max_response_body_bytes = bytes;
        self
    }

    /// Shared HTTP client for this proxy mount, built once on first use
    /// with the configured timeouts. Cloning `reqwest::Client` is cheap
    /// (it is an `Arc` internally).
    fn client(&self) -> Result<reqwest::Client, ProxyError> {
        if let Some(client) = self.client.get() {
            return Ok(client.clone());
        }
        let built = reqwest::Client::builder()
            .connect_timeout(self.connect_timeout)
            .timeout(self.request_timeout)
            .build()
            .map_err(ProxyError::ClientBuild)?;
        // Benign race: two concurrent first requests may both build a
        // client; `get_or_init` keeps one and the loser is dropped.
        Ok(self.client.get_or_init(|| built).clone())
    }
}

/// Fixed incoming request headers that must not be forwarded to the upstream.
fn is_fixed_blocked_request_header(name: &str) -> bool {
    matches!(
        name,
        "host"
            | "connection"
            | "keep-alive"
            | "transfer-encoding"
            | "upgrade"
            | "te"
            | "trailer"
            | "proxy-connection"
            | "proxy-authenticate"
            | "proxy-authorization"
    )
}

/// Fixed upstream response headers that must not be relayed to the client.
fn is_fixed_blocked_response_header(name: &str) -> bool {
    matches!(
        name,
        "connection"
            | "keep-alive"
            | "transfer-encoding"
            | "upgrade"
            | "te"
            | "trailer"
            | "proxy-connection"
            | "proxy-authenticate"
            | "proxy-authorization"
    )
}

/// A `Connection` header can nominate additional hop-by-hop header names.
/// Those nominated headers are also scoped to one connection and must be
/// stripped before proxying in either direction.
fn is_connection_nominated_header(headers: &HeaderMap, candidate: &str) -> bool {
    headers
        .get_all(header::CONNECTION)
        .iter()
        .filter_map(|value| value.to_str().ok())
        .flat_map(|value| value.split(','))
        .any(|token| token.trim().eq_ignore_ascii_case(candidate))
}

/// Forwarding metadata headers that clients must never be able to supply
/// themselves. SECURITY: a client-supplied `X-Forwarded-For` or
/// `X-Forwarded-Host` would let an attacker spoof their address or the
/// original host to the upstream app (IP allowlists, audit logs, absolute
/// URL generation). The proxy strips them and re-derives trustworthy
/// values itself.
fn is_client_forwarding_header(name: &str) -> bool {
    matches!(
        name,
        "x-forwarded-for" | "x-forwarded-host" | "x-forwarded-proto" | "forwarded"
    )
}

/// Decide whether a single incoming request header may be forwarded
/// verbatim to the upstream.
fn should_forward_request_header(headers: &HeaderMap, name: &str) -> bool {
    !is_fixed_blocked_request_header(name)
        && !is_connection_nominated_header(headers, name)
        && !is_client_forwarding_header(name)
}

/// Decide whether a single upstream response header may be relayed back
/// to the client.
fn should_forward_response_header(headers: &HeaderMap, name: &str) -> bool {
    !is_fixed_blocked_response_header(name) && !is_connection_nominated_header(headers, name)
}

const X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");
const X_FORWARDED_HOST: HeaderName = HeaderName::from_static("x-forwarded-host");
const X_FORWARDED_PROTO: HeaderName = HeaderName::from_static("x-forwarded-proto");

/// Build the full header map sent to the upstream from the incoming
/// request headers.
///
/// Pure function so the forwarding policy is unit-testable without a
/// network: hop-by-hop and client-supplied `X-Forwarded-*` / `Forwarded`
/// headers are dropped, everything else is forwarded (duplicates
/// preserved), and fresh `X-Forwarded-Host` / `X-Forwarded-Proto` values
/// are derived from the request itself. `X-Forwarded-For` is only set
/// when a verified peer address is available (axum `ConnectInfo`);
/// otherwise it is omitted entirely rather than trusting client input.
fn build_upstream_headers(
    incoming: &HeaderMap,
    scheme: &str,
    peer_addr: Option<SocketAddr>,
) -> HeaderMap {
    let mut out = HeaderMap::with_capacity(incoming.len() + 3);
    for (name, value) in incoming.iter() {
        if should_forward_request_header(incoming, name.as_str()) {
            // `append`, not `insert`: repeated headers (e.g. multiple
            // `cookie` lines) must all reach the upstream.
            out.append(name.clone(), value.clone());
        }
    }

    if let Some(host) = incoming.get(header::HOST) {
        out.insert(X_FORWARDED_HOST, host.clone());
    }
    if let Ok(proto) = HeaderValue::from_str(scheme) {
        out.insert(X_FORWARDED_PROTO, proto);
    }
    if let Some(peer) = peer_addr {
        if let Ok(value) = HeaderValue::from_str(&peer.ip().to_string()) {
            out.insert(X_FORWARDED_FOR, value);
        }
    }

    out
}

/// Reverse proxy handler that forwards requests to an upstream server
pub async fn proxy_handler(
    State(config): State<Arc<ProxyConfig>>,
    req: Request,
) -> Result<Response, ProxyError> {
    // Get the request URI path
    let path = req.uri().path();
    let query = req.uri().query();

    // Construct the upstream URL
    let upstream_path = if config.strip_prefix && path.starts_with(&config.mount_path) {
        // Strip the mount path prefix
        path.strip_prefix(&config.mount_path).unwrap_or(path)
    } else {
        path
    };

    let upstream_url = if let Some(q) = query {
        format!("{}{}?{}", config.upstream, upstream_path, q)
    } else {
        format!("{}{}", config.upstream, upstream_path)
    };

    tracing::debug!("Proxying {} → {}", path, upstream_url);

    let client = config.client()?;
    let method = req.method().clone();
    let scheme = req.uri().scheme_str().unwrap_or("http");
    // Real peer address, if the app was served with
    // `into_make_service_with_connect_info::<SocketAddr>()`. When absent
    // we omit X-Forwarded-For instead of trusting client headers.
    let peer_addr = req
        .extensions()
        .get::<ConnectInfo<SocketAddr>>()
        .map(|info| info.0);
    let upstream_headers = build_upstream_headers(req.headers(), scheme, peer_addr);

    let mut upstream_req = client
        .request(method, &upstream_url)
        .headers(upstream_headers);

    // Forward the request body (bounded to prevent memory exhaustion).
    let body_bytes = axum::body::to_bytes(req.into_body(), config.max_request_body_bytes)
        .await
        .map_err(|e| ProxyError::BodyRead(e.to_string()))?;

    if !body_bytes.is_empty() {
        upstream_req = upstream_req.body(body_bytes);
    }

    // Send the request to upstream
    let mut upstream_resp = upstream_req.send().await.map_err(|e| {
        if e.is_timeout() {
            ProxyError::UpstreamTimeout(e)
        } else {
            ProxyError::Upstream(e)
        }
    })?;

    // Convert the upstream response to an axum response
    let status = upstream_resp.status();
    let mut response_builder = Response::builder().status(status);

    // Forward response headers (excluding hop-by-hop headers)
    let upstream_headers = upstream_resp.headers();
    for (name, value) in upstream_headers.iter() {
        if should_forward_response_header(upstream_headers, name.as_str()) {
            response_builder = response_builder.header(name, value);
        }
    }

    // Read the response body with a hard cap so a malicious or broken
    // upstream cannot exhaust memory with an unbounded body.
    let limit = config.max_response_body_bytes;
    if let Some(len) = upstream_resp.content_length() {
        if len > limit as u64 {
            return Err(ProxyError::ResponseTooLarge { limit });
        }
    }
    let mut body = Vec::with_capacity(
        upstream_resp
            .content_length()
            .map(|len| len.min(limit as u64) as usize)
            .unwrap_or(8 * 1024),
    );
    while let Some(chunk) = upstream_resp.chunk().await.map_err(|e| {
        if e.is_timeout() {
            ProxyError::UpstreamTimeout(e)
        } else {
            ProxyError::ResponseRead(e)
        }
    })? {
        if body.len() + chunk.len() > limit {
            return Err(ProxyError::ResponseTooLarge { limit });
        }
        body.extend_from_slice(&chunk);
    }

    let response = response_builder
        .body(Body::from(body))
        .map_err(ProxyError::ResponseBuild)?;

    Ok(response)
}

/// Errors that can occur during proxying.
///
/// SECURITY: the `IntoResponse` impl deliberately maps every variant to a
/// short generic body. Upstream error details (addresses, TLS or DNS
/// failures, internal topology) are logged via `tracing::warn!` only and
/// never leak to the client.
#[derive(Debug, Error)]
pub enum ProxyError {
    #[error("failed to read proxied request body: {0}")]
    BodyRead(String),
    #[error("failed to build upstream HTTP client: {0}")]
    ClientBuild(#[source] reqwest::Error),
    #[error("upstream request timed out: {0}")]
    UpstreamTimeout(#[source] reqwest::Error),
    #[error("upstream request failed: {0}")]
    Upstream(#[source] reqwest::Error),
    #[error("failed to read upstream response: {0}")]
    ResponseRead(#[source] reqwest::Error),
    #[error("upstream response exceeded the configured limit of {limit} bytes")]
    ResponseTooLarge { limit: usize },
    #[error("failed to build proxied response: {0}")]
    ResponseBuild(#[source] axum::http::Error),
}

impl IntoResponse for ProxyError {
    fn into_response(self) -> Response {
        tracing::warn!(error = %self, "proxy request failed");
        let (status, message) = match self {
            ProxyError::BodyRead(_) => (StatusCode::BAD_REQUEST, "Bad Request"),
            ProxyError::UpstreamTimeout(_) => (StatusCode::GATEWAY_TIMEOUT, "Gateway Timeout"),
            ProxyError::ClientBuild(_)
            | ProxyError::Upstream(_)
            | ProxyError::ResponseRead(_)
            | ProxyError::ResponseTooLarge { .. } => (StatusCode::BAD_GATEWAY, "Bad Gateway"),
            ProxyError::ResponseBuild(_) => {
                (StatusCode::INTERNAL_SERVER_ERROR, "Internal Server Error")
            }
        };

        (status, message).into_response()
    }
}

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

    fn incoming_headers(pairs: &[(&'static str, &'static str)]) -> HeaderMap {
        let mut headers = HeaderMap::new();
        for (name, value) in pairs {
            headers.append(
                HeaderName::from_static(name),
                HeaderValue::from_static(value),
            );
        }
        headers
    }

    #[test]
    fn client_supplied_forwarding_headers_are_stripped() {
        let incoming = incoming_headers(&[
            ("host", "example.com"),
            ("x-forwarded-for", "6.6.6.6"),
            ("x-forwarded-host", "evil.example"),
            ("x-forwarded-proto", "https"),
            ("forwarded", "for=6.6.6.6;host=evil.example"),
        ]);

        let out = build_upstream_headers(&incoming, "http", None);

        assert!(out.get("forwarded").is_none());
        // The forwarded values must be re-derived, never the client's.
        assert_eq!(out.get("x-forwarded-host").unwrap(), "example.com");
        assert_eq!(out.get("x-forwarded-proto").unwrap(), "http");
        assert!(
            out.get("x-forwarded-for").is_none(),
            "without a verified peer address X-Forwarded-For must be omitted"
        );
    }

    #[test]
    fn hop_by_hop_headers_are_stripped() {
        let incoming = incoming_headers(&[
            ("host", "example.com"),
            ("connection", "keep-alive"),
            ("keep-alive", "timeout=5"),
            ("transfer-encoding", "chunked"),
            ("upgrade", "websocket"),
            ("te", "trailers"),
            ("trailer", "expires"),
            ("proxy-connection", "keep-alive"),
            ("proxy-authorization", "Basic abc"),
        ]);

        let out = build_upstream_headers(&incoming, "http", None);

        for name in [
            "host",
            "connection",
            "keep-alive",
            "transfer-encoding",
            "upgrade",
            "te",
            "trailer",
            "proxy-connection",
            "proxy-authorization",
        ] {
            assert!(out.get(name).is_none(), "{name} should not be forwarded");
        }
    }

    #[test]
    fn connection_nominated_request_headers_are_stripped() {
        let incoming = incoming_headers(&[
            ("host", "example.com"),
            ("connection", "x-internal-auth, x-debug-token"),
            ("x-internal-auth", "secret"),
            ("x-debug-token", "debug"),
            ("x-request-id", "abc-123"),
        ]);

        let out = build_upstream_headers(&incoming, "http", None);

        assert!(out.get("x-internal-auth").is_none());
        assert!(out.get("x-debug-token").is_none());
        assert_eq!(out.get("x-request-id").unwrap(), "abc-123");
    }

    #[test]
    fn normal_headers_are_forwarded_with_duplicates() {
        let incoming = incoming_headers(&[
            ("accept", "text/html"),
            ("cookie", "a=1"),
            ("cookie", "b=2"),
            ("authorization", "Bearer t"),
            ("x-request-id", "abc-123"),
        ]);

        let out = build_upstream_headers(&incoming, "http", None);

        assert_eq!(out.get("accept").unwrap(), "text/html");
        assert_eq!(out.get("authorization").unwrap(), "Bearer t");
        assert_eq!(out.get("x-request-id").unwrap(), "abc-123");
        let cookies: Vec<_> = out.get_all("cookie").iter().collect();
        assert_eq!(cookies, vec!["a=1", "b=2"]);
    }

    #[test]
    fn forwarded_host_and_proto_are_derived_from_the_request() {
        let incoming = incoming_headers(&[("host", "shop.example.com:8443")]);

        let out = build_upstream_headers(&incoming, "https", None);

        assert_eq!(
            out.get("x-forwarded-host").unwrap(),
            "shop.example.com:8443"
        );
        assert_eq!(out.get("x-forwarded-proto").unwrap(), "https");
    }

    #[test]
    fn forwarded_for_uses_the_verified_peer_address_only() {
        let incoming = incoming_headers(&[("host", "example.com"), ("x-forwarded-for", "6.6.6.6")]);
        let peer: SocketAddr = "203.0.113.9:51724".parse().unwrap();

        let out = build_upstream_headers(&incoming, "http", Some(peer));

        assert_eq!(out.get("x-forwarded-for").unwrap(), "203.0.113.9");
    }

    #[test]
    fn response_hop_by_hop_headers_are_not_relayed() {
        let headers = HeaderMap::new();
        assert!(!should_forward_response_header(&headers, "connection"));
        assert!(!should_forward_response_header(&headers, "keep-alive"));
        assert!(!should_forward_response_header(
            &headers,
            "transfer-encoding"
        ));
        assert!(!should_forward_response_header(&headers, "upgrade"));
        assert!(!should_forward_response_header(
            &headers,
            "proxy-authenticate"
        ));
        assert!(!should_forward_response_header(
            &headers,
            "proxy-authorization"
        ));
        assert!(should_forward_response_header(&headers, "content-type"));
        assert!(should_forward_response_header(&headers, "set-cookie"));
    }

    #[test]
    fn connection_nominated_response_headers_are_not_relayed() {
        let headers = incoming_headers(&[
            ("connection", "x-upstream-secret, x-cache-hop"),
            ("x-upstream-secret", "secret"),
            ("x-cache-hop", "1"),
            ("content-type", "text/html"),
        ]);

        assert!(!should_forward_response_header(
            &headers,
            "x-upstream-secret"
        ));
        assert!(!should_forward_response_header(&headers, "x-cache-hop"));
        assert!(should_forward_response_header(&headers, "content-type"));
    }

    #[test]
    fn proxy_errors_render_generic_bodies() {
        let response = ProxyError::BodyRead("boom".into()).into_response();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);

        let response = ProxyError::ResponseTooLarge { limit: 16 }.into_response();
        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
    }
}