-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmiddleware.rs
More file actions
236 lines (202 loc) · 8.01 KB
/
Copy pathmiddleware.rs
File metadata and controls
236 lines (202 loc) · 8.01 KB
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
use std::sync::Arc;
use async_trait::async_trait;
use edgezero_core::context::RequestContext;
use edgezero_core::error::EdgeError;
use edgezero_core::http::{HeaderValue, Response};
use edgezero_core::middleware::{Middleware, Next};
use trusted_server_core::auth::enforce_basic_auth;
use trusted_server_core::constants::HEADER_X_GEO_INFO_AVAILABLE;
use trusted_server_core::settings::Settings;
// ---------------------------------------------------------------------------
// FinalizeResponseMiddleware
// ---------------------------------------------------------------------------
/// Outermost middleware: injects all standard TS response headers.
///
/// Geo availability is determined by the presence of the `cf-ipcountry` header
/// (injected by the Cloudflare Workers runtime). On the native host target the
/// header is absent, so `X-Geo-Info-Available: false` is emitted.
///
/// Registered first in the middleware chain so that every outgoing response —
/// including auth-rejected ones — carries a consistent set of headers.
pub struct FinalizeResponseMiddleware {
settings: Arc<Settings>,
}
impl FinalizeResponseMiddleware {
/// Creates a new [`FinalizeResponseMiddleware`] with the given settings.
#[must_use]
pub fn new(settings: Arc<Settings>) -> Self {
Self { settings }
}
}
#[async_trait(?Send)]
impl Middleware for FinalizeResponseMiddleware {
async fn handle(&self, ctx: RequestContext, next: Next<'_>) -> Result<Response, EdgeError> {
let geo_available = ctx
.request()
.headers()
.get("cf-ipcountry")
.and_then(|v| v.to_str().ok())
.filter(|s| !s.is_empty() && *s != "XX")
.is_some();
let mut response = next.run(ctx).await?;
apply_finalize_headers(&self.settings, geo_available, &mut response);
Ok(response)
}
}
// ---------------------------------------------------------------------------
// AuthMiddleware
// ---------------------------------------------------------------------------
/// Inner middleware: enforces basic-auth before the handler runs.
///
/// - `Ok(Some(response))` from [`enforce_basic_auth`] → auth failed; return the
/// challenge response (bubbles through [`FinalizeResponseMiddleware`] for header injection).
/// - `Ok(None)` → no auth required or credentials accepted; continue the chain.
/// - `Err(report)` → internal error; log and convert to a 500 HTTP response.
pub struct AuthMiddleware {
settings: Arc<Settings>,
}
impl AuthMiddleware {
/// Creates a new [`AuthMiddleware`] with the given settings.
#[must_use]
pub fn new(settings: Arc<Settings>) -> Self {
Self { settings }
}
}
#[async_trait(?Send)]
impl Middleware for AuthMiddleware {
async fn handle(&self, ctx: RequestContext, next: Next<'_>) -> Result<Response, EdgeError> {
match enforce_basic_auth(&self.settings, ctx.request()) {
Ok(Some(response)) => return Ok(response),
Ok(None) => {}
Err(report) => {
log::error!("auth check failed: {:?}", report);
return Ok(crate::app::http_error(&report));
}
}
next.run(ctx).await
}
}
// ---------------------------------------------------------------------------
// apply_finalize_headers — extracted for unit testing
// ---------------------------------------------------------------------------
/// Applies standard Trusted Server response headers to the given response.
///
/// `geo_available` controls `X-Geo-Info-Available`; pass `true` when
/// `cf-ipcountry` was present and non-`XX` in the incoming request.
/// Operator-configured `settings.response_headers` are applied last (with the
/// shared cookie cache-privacy hardening) and can override any managed header.
pub(crate) fn apply_finalize_headers(
settings: &Settings,
geo_available: bool,
response: &mut Response,
) {
response.headers_mut().insert(
HEADER_X_GEO_INFO_AVAILABLE,
HeaderValue::from_static(if geo_available { "true" } else { "false" }),
);
// Cloudflare is a real shared cache: cookie-bearing responses must stay
// private and operator headers must not re-enable caching for uncacheable
// per-user payloads.
trusted_server_core::response_privacy::apply_response_headers_with_cache_privacy(
settings, response,
);
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use edgezero_core::body::Body;
use edgezero_core::http::response_builder;
fn empty_response() -> Response {
response_builder()
.body(Body::empty())
.expect("should build empty test response")
}
fn settings_with_response_headers(headers: Vec<(&str, &str)>) -> Settings {
// Build from explicit test settings: the settings baked into the
// binary contain placeholder secrets that `get_settings()` rejects
// by design.
let mut s = Settings::from_toml(
r#"
[[handlers]]
path = "^/_ts/admin"
username = "admin"
password = "admin-pass"
[publisher]
domain = "test-publisher.example.com"
cookie_domain = ".test-publisher.example.com"
origin_url = "https://origin.test-publisher.example.com"
proxy_secret = "unit-test-proxy-secret"
[ec]
provider = "hmac"
[ec.providers.hmac]
passphrase = "test-secret-key-32-bytes-minimum"
"#,
)
.expect("should load test settings");
s.response_headers = headers
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
s
}
#[test]
fn sets_geo_available_false_when_no_country_header() {
let settings = settings_with_response_headers(vec![]);
let mut response = empty_response();
apply_finalize_headers(&settings, false, &mut response);
assert_eq!(
response
.headers()
.get("x-geo-info-available")
.and_then(|v| v.to_str().ok()),
Some("false"),
"should set X-Geo-Info-Available: false when geo is unavailable"
);
}
#[test]
fn sets_geo_available_true_when_country_header_present() {
let settings = settings_with_response_headers(vec![]);
let mut response = empty_response();
apply_finalize_headers(&settings, true, &mut response);
assert_eq!(
response
.headers()
.get("x-geo-info-available")
.and_then(|v| v.to_str().ok()),
Some("true"),
"should set X-Geo-Info-Available: true when cf-ipcountry is present"
);
}
#[test]
fn operator_response_headers_override_geo_header() {
let settings =
settings_with_response_headers(vec![("X-Geo-Info-Available", "operator-override")]);
let mut response = empty_response();
apply_finalize_headers(&settings, false, &mut response);
assert_eq!(
response
.headers()
.get("x-geo-info-available")
.and_then(|v| v.to_str().ok()),
Some("operator-override"),
"should override the managed geo header with the operator-configured value"
);
}
#[test]
fn applies_custom_operator_headers() {
let settings = settings_with_response_headers(vec![("X-Custom-Header", "custom-value")]);
let mut response = empty_response();
apply_finalize_headers(&settings, false, &mut response);
assert_eq!(
response
.headers()
.get("x-custom-header")
.and_then(|v| v.to_str().ok()),
Some("custom-value"),
"should apply operator-configured response headers"
);
}
}