Skip to content

Commit 121235c

Browse files
rksmctron
authored andcommitted
add proxy option no_redirect
When serving responses with the Trunk proxy, redirects are automatically followed by default. This can be undesirable, e.g. when serving a login page that response with a redirect and a set-cookie header like ``` HTTP/1.1 303 See Other location: / set-cookie: session=...; ...; ``` This PR adds a proxy option `no_redirect` that when set will instruct the proxy to not automatically follow redirects. The default behavior is unchanged.
1 parent bea4eeb commit 121235c

File tree

8 files changed

+30
-1
lines changed

8 files changed

+30
-1
lines changed

site/content/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ ws = false # Use WebSocket for this proxy
119119
insecure = false # Disable certificate validation
120120
no_system_proxy = false # Disable system proxy
121121
rewrite = "" # Strip the given prefix off paths
122+
no_redirect = false # Disable following redirects of proxy responses
122123
```
123124

124125
## Hooks section

src/cmd/serve.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ pub struct ProxyArgs {
9090
requires = "proxy_backend"
9191
)]
9292
pub proxy_no_system_proxy: bool,
93+
/// Configure the proxy to not automatically follow redirects if a backend responds with a redirect
94+
#[arg(
95+
long,
96+
env = "TRUNK_SERVE_PROXY_NO_REDIRECT",
97+
requires = "proxy_backend"
98+
)]
99+
pub proxy_no_redirect: bool,
93100
}
94101

95102
impl Serve {
@@ -107,6 +114,7 @@ impl Serve {
107114
proxy_ws,
108115
proxy_insecure,
109116
proxy_no_system_proxy,
117+
proxy_no_redirect,
110118
},
111119
no_autoreload,
112120
no_error_reporting,
@@ -148,6 +156,7 @@ impl Serve {
148156
ws: proxy_ws,
149157
insecure: proxy_insecure,
150158
no_system_proxy: proxy_no_system_proxy,
159+
no_redirect: proxy_no_redirect,
151160
});
152161
}
153162

src/config/models/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl ConfigModel for Configuration {
106106
ws: self.serve.proxy_ws.unwrap_or_default(),
107107
insecure: self.serve.proxy_insecure.unwrap_or_default(),
108108
no_system_proxy: self.serve.proxy_no_system_proxy.unwrap_or_default(),
109+
no_redirect: self.serve.proxy_no_redirect.unwrap_or_default(),
109110
})
110111
}
111112

src/config/models/proxy.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ pub struct Proxy {
2828
#[serde(alias = "no-system-proxy")]
2929
#[serde(default)]
3030
pub no_system_proxy: bool,
31+
/// Automatically redirect proxy requests? `no_redirect` Defaults to
32+
/// `false`, i.e. yes, follow redirects automatically.
33+
#[serde(default)]
34+
pub no_redirect: bool,
3135
}
3236

3337
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, JsonSchema)]

src/config/models/serve.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ pub struct Serve {
5252
/// A base path to serve the application from
5353
#[serde(default)]
5454
pub serve_base: Option<String>,
55+
/// Configure the proxy to not follow redirects
56+
#[serde(default)]
57+
pub proxy_no_redirect: Option<bool>,
5558

5659
/// A URL to which requests will be proxied [default: None]
5760
#[deprecated]
@@ -98,6 +101,7 @@ impl Default for Serve {
98101
proxy_ws: None,
99102
proxy_insecure: None,
100103
proxy_no_system_proxy: None,
104+
proxy_no_redirect: None,
101105
}
102106
}
103107
}

src/config/rt/serve.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl RtcServe {
9393
proxy_ws: _,
9494
proxy_insecure: _,
9595
proxy_no_system_proxy: _,
96+
proxy_no_redirect: _,
9697
} = config.serve;
9798

9899
let tls = tls_config(

src/serve/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ fn router(state: Arc<State>, cfg: Arc<RtcServe>) -> Result<Router> {
384384
ProxyClientOptions {
385385
insecure: proxy.insecure,
386386
no_system_proxy: proxy.no_system_proxy,
387+
redirect: !proxy.no_redirect,
387388
},
388389
)?;
389390
}

src/serve/proxy.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use axum::http::Uri;
55
use axum::Router;
66
use console::Emoji;
77
use http::HeaderMap;
8+
use reqwest::redirect::Policy;
89
use reqwest::Client;
910
use std::collections::hash_map::Entry;
1011
use std::collections::HashMap;
@@ -86,6 +87,7 @@ impl ProxyBuilder {
8687
pub(crate) struct ProxyClientOptions {
8788
pub insecure: bool,
8889
pub no_system_proxy: bool,
90+
pub redirect: bool,
8991
}
9092

9193
#[derive(Default)]
@@ -107,7 +109,13 @@ impl ProxyClients {
107109

108110
/// Create a new client for proxying
109111
fn create_client(opts: ProxyClientOptions) -> anyhow::Result<Client> {
110-
let mut builder = reqwest::ClientBuilder::new().http1_only();
112+
let mut builder = reqwest::ClientBuilder::new()
113+
.http1_only()
114+
.redirect(if opts.redirect {
115+
Policy::default()
116+
} else {
117+
Policy::none()
118+
});
111119

112120
#[cfg(any(feature = "native-tls", feature = "rustls"))]
113121
if opts.insecure {

0 commit comments

Comments
 (0)