Skip to content

Commit 7e09416

Browse files
authored
feat(server)!: sign in through a trusted reverse proxy (#1574)
* feat(server): trusted-proxy sign-in behind a stock reverse proxy [trusted_proxy] gains username_header (default X-SQB-User), optional display_name_header and email_header, unknown_users (provision_viewer, the default, or reject) and proxy_logout_url; identities become optional pre-bindings. The origin may be plain HTTP, with a start-up warning. A newcomer gets a principal named by the canonical username, with an id derived from it, and the installation-wide viewer role; a username that already names another principal is refused rather than taken over. Requests from trusted peers may report the browser host in X-Forwarded-Host, as Apache's mod_proxy does without ProxyPreserveHost. Each untrusted peer that sends the username header is logged once, without header values. Change feeds send Cache-Control: no-cache, no-transform and X-Accel-Buffering: no. * feat(ui): show the proxy's display name and sign-out link * docs(server): sign in through a reverse proxy * fix(server): trusted-proxy writes under any host name, loopback peers by default, case-insensitive name check - A request from a trusted peer may use any host name the proxy serves; a write needs the X-SQB-CSRF header, which the UI now always sends, or the configured Origin. The server still allows no cross-origin requests, so a cross-site page cannot send the header. Password, OIDC and untrusted-peer requests keep the exact Host and Origin checks. - trusted_peers defaults to 127.0.0.1 and ::1 when unset. - A newcomer whose username matches another principal's name in any case is refused rather than provisioned, without a schema change. - Proxy-Authorization, Via, X-Forwarded-Proto, X-Real-IP and any Sec- header are reserved identity-header names. * docs(server): reverse-proxy usernames, identity headers, loopback binding and access removal * test(server): prove only a loopback TCP peer asserts a proxy identity by default Drive the sign-in routes in process with a chosen socket peer, so the default `trusted_peers` is checked against real non-loopback IPv4, IPv6 and IPv4-mapped peers, against forwarding headers that claim loopback, and against a server without `[trusted_proxy]`. State in the server docs that the default lets any process on the host assert an identity, and that listing the proxy's own address narrows it. * refactor(orchestration): group authorization entry points by concern The trusted-proxy resolver made `authorization/main` hold 21 modules, one over the container limit. Group them one level deep, as `credentials/main` already is: `roles` for role definitions, assignments and administrator grants; `decisions` for per-person, scope and row decisions and the security audit; `people` for the first administrator, proxy usernames and each person's access. Only module paths change. * test(server): prove a non-loopback peer's username header is logged and ignored Start the server on this machine's routed address with the default trusted peers and send the username header to it, with a forwarding header claiming loopback. The server refuses both requests, logs the peer once and never logs the username. Drop history wording from the server page. * fix(server): serve trusted proxy peers only under their own host names A DNS-rebinding page in a browser on a trusted-peer host reaches the server under its own host name, and could then send the username and CSRF headers. Refuse every request from a trusted peer, reads included, unless its real Host is 127.0.0.1, localhost or [::1] with any port, the host of `origin`, or a name in the new `allowed_hosts` list, which only a proxy that keeps the browser's host name needs. X-Forwarded-Host never counts. A refused Host gets 403 and is logged once with the setting that allows it. The CSRF rule and the loopback trusted_peers default are unchanged. * docs(server): a proxy on another host lists its backend address in allowed_hosts A stock proxy on another host forwards with its backend address as Host, which is not a loopback name, so it needs allowed_hosts too. Say so in the docs and in the refused-Host warning.
1 parent 22ecb47 commit 7e09416

146 files changed

Lines changed: 2296 additions & 252 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ axum = { version = "0.8", default-features = false, features = ["http1", "json",
8888
axum-server = { version = "0.8", default-features = false, features = ["tls-rustls-no-provider"] }
8989
# Compresses the server's larger JSON reads and UI assets for browsers.
9090
tower-http = { version = "0.6", default-features = false, features = ["compression-gzip"] }
91+
# Tests drive the API router in process, with a chosen socket peer.
92+
tower = { version = "0.5", default-features = false, features = ["util"] }
9193
fensu-policy = "=0.22.0"
9294
# Fensu's Python facts and core rules without their Python bindings: the default `python`
9395
# feature adds PyO3, rusqlite, flate2 and sha2.

‎clients/typescript/src/schema.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,13 @@ export interface components {
18971897
/** @description What the UI needs first: how people sign in, and what this person may see and do. */
18981898
ControlBootstrap: {
18991899
access: components["schemas"]["PersonAccess"];
1900+
/** @description The display name a trusted proxy reported for this request, if it reports one. */
1901+
display_name?: string | null;
1902+
/** @description The email address a trusted proxy reported for this request, if it reports one. */
1903+
email?: string | null;
19001904
login_mode: components["schemas"]["LoginMode"];
1905+
/** @description Where signing out goes when a trusted proxy owns the sign-in; absent means no sign-out link. */
1906+
logout_url?: string | null;
19011907
};
19021908
/** @description Estimated query cost of one group, such as a job, a warehouse size or a node. */
19031909
CostGroup: {

‎crates/sqlbuild-cli/src/native_commands/main/_run_admin.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::adhoc::main::adhoc_planner::adhoc_planner;
44
use serde_json::{Value, json};
5-
use sqlbuild_orchestration::authorization::main::bootstrap_user::bootstrap_user;
6-
use sqlbuild_orchestration::authorization::main::grant_admin::grant_admin;
5+
use sqlbuild_orchestration::authorization::main::people::bootstrap_user::bootstrap_user;
6+
use sqlbuild_orchestration::authorization::main::roles::grant_admin::grant_admin;
77
use sqlbuild_orchestration::credentials::main::identities::create_principal::create_principal;
88
use sqlbuild_orchestration::credentials::main::locations::create_location::create_location;
99
use sqlbuild_orchestration::credentials::main::sessions::set_password::set_password;

‎crates/sqlbuild-cli/tests/native_commands/test_proxy.rs‎

Lines changed: 230 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use reqwest::{Client, StatusCode, header};
44
use serde_json::{Value, json};
55

66
use crate::helpers::{api_ready, start_control_in, wait_for_exit};
7-
use crate::test_types::{ProxyCliTestCase, ServerConfigurationTestCase};
7+
use crate::test_types::{
8+
ProxyCliTestCase, ProxyRemotePeerTestCase, ProxyStartupTestCase, ServerConfigurationTestCase,
9+
};
810

911
#[test]
1012
fn given_an_owner_configured_proxy_when_requests_carry_a_subject_then_only_explicit_people_resolve_without_sessions_or_machine_authority()
@@ -94,16 +96,58 @@ fn given_invalid_or_conflicting_server_configuration_when_a_standalone_server_st
9496
expected_error: "requires explicit trusted peer IP addresses",
9597
},
9698
ServerConfigurationTestCase {
97-
description: "non-HTTPS external origin",
98-
configuration: "[trusted_proxy]\norigin = \"http://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\nidentities = {}\n",
99+
description: "an allowed host written as a URL",
100+
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\nallowed_hosts = [\"https://control-short\"]\n",
99101
arguments: &[],
100-
expected_error: "trusted-proxy authentication requires an HTTPS origin",
102+
expected_error: "[trusted_proxy] allowed_hosts lists host names without a scheme, port or path; 'https://control-short' is not one",
103+
},
104+
ServerConfigurationTestCase {
105+
description: "neither an HTTP nor an HTTPS external origin",
106+
configuration: "[trusted_proxy]\norigin = \"ftp://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\n",
107+
arguments: &[],
108+
expected_error: "trusted-proxy authentication requires an HTTP or HTTPS origin",
109+
},
110+
ServerConfigurationTestCase {
111+
description: "a username header a browser sets",
112+
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\nusername_header = \"Cookie\"\n",
113+
arguments: &[],
114+
expected_error: "[trusted_proxy] username_header cannot be cookie",
115+
},
116+
ServerConfigurationTestCase {
117+
description: "a fetch-metadata header a browser sets",
118+
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\nusername_header = \"Sec-Fetch-User\"\n",
119+
arguments: &[],
120+
expected_error: "[trusted_proxy] username_header cannot be sec-fetch-user",
121+
},
122+
ServerConfigurationTestCase {
123+
description: "the display name in the username header",
124+
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\nusername_header = \"X-Example-User\"\ndisplay_name_header = \"x-example-user\"\n",
125+
arguments: &[],
126+
expected_error: "username_header, display_name_header and email_header must differ",
127+
},
128+
ServerConfigurationTestCase {
129+
description: "a script as the sign-out URL",
130+
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\nproxy_logout_url = \"javascript:alert(1)\"\n",
131+
arguments: &[],
132+
expected_error: "proxy_logout_url must be an http:// or https:// URL",
133+
},
134+
ServerConfigurationTestCase {
135+
description: "an unknown policy for unknown users",
136+
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\nunknown_users = \"provision_admin\"\n",
137+
arguments: &[],
138+
expected_error: "unknown variant `provision_admin`",
139+
},
140+
ServerConfigurationTestCase {
141+
description: "two bindings for one username",
142+
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\n\n[trusted_proxy.identities]\n\"Orders-Reader\" = \"00000000-0000-0000-0000-000000000001\"\n\"orders-reader\" = \"00000000-0000-0000-0000-000000000002\"\n",
143+
arguments: &[],
144+
expected_error: "binds 'orders-reader' more than once",
101145
},
102146
ServerConfigurationTestCase {
103147
description: "subject cannot be represented by the header transport",
104148
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\n\n[trusted_proxy.identities]\n\"external-café\" = \"00000000-0000-0000-0000-000000000001\"\n",
105149
arguments: &[],
106-
expected_error: "subjects must be nonempty, unpadded ASCII header values",
150+
expected_error: "trusted-proxy usernames must be 1-128 characters",
107151
},
108152
ServerConfigurationTestCase {
109153
description: "a password-origin flag conflicts with the file's trusted proxy",
@@ -206,3 +250,184 @@ fn given_invalid_or_conflicting_server_configuration_when_a_standalone_server_st
206250
});
207251
}
208252
}
253+
254+
#[test]
255+
fn given_a_misconfigured_proxy_when_requests_arrive_then_startup_warns_and_each_refusal_is_logged_once()
256+
{
257+
let test_cases = [
258+
ProxyStartupTestCase {
259+
description: "the proxy's real address differs from trusted_peers",
260+
configuration: "[trusted_proxy]\norigin = \"http://control.example.com\"\ntrusted_peers = [\"192.0.2.1\"]\nusername_header = \"X-Example-User\"\n",
261+
username: "orders-confidential-reader",
262+
host: "127.0.0.1",
263+
expected_status: StatusCode::UNAUTHORIZED,
264+
expected_warnings: &[
265+
(
266+
"warning: the trusted-proxy origin http://control.example.com is plain HTTP",
267+
1,
268+
),
269+
(
270+
"warning: ignored the trusted-proxy username header x-example-user from 127.0.0.1, which is not in [trusted_proxy] trusted_peers",
271+
1,
272+
),
273+
],
274+
},
275+
ProxyStartupTestCase {
276+
description: "a trusted peer's Host names neither this server nor allowed_hosts",
277+
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\nusername_header = \"X-Example-User\"\n",
278+
username: "orders-rebound-reader",
279+
host: "evil.example:8000",
280+
expected_status: StatusCode::FORBIDDEN,
281+
expected_warnings: &[(
282+
"warning: refused a request from trusted proxy peer 127.0.0.1 with Host 'evil.example:8000', which is not a loopback name, the [trusted_proxy] origin's host or in [trusted_proxy] allowed_hosts; if your proxy keeps the browser's host name (ProxyPreserveHost On) or forwards to this server by another name or address, add it to allowed_hosts",
283+
1,
284+
)],
285+
},
286+
];
287+
let runtime = tokio::runtime::Builder::new_current_thread()
288+
.enable_all()
289+
.build()
290+
.expect("runtime");
291+
for test_case in test_cases {
292+
runtime.block_on(async {
293+
let directory = tempfile::tempdir().expect("proxy installation");
294+
std::fs::write(
295+
directory.path().join("server.toml"),
296+
test_case.configuration,
297+
)
298+
.expect("server configuration");
299+
let mut control = start_control_in(
300+
directory,
301+
&[
302+
"server",
303+
"--host",
304+
"127.0.0.1",
305+
"--port",
306+
"0",
307+
"--no-scheduler",
308+
"--config",
309+
"server.toml",
310+
],
311+
);
312+
let url = api_ready(&mut control).await;
313+
let client = Client::builder()
314+
.timeout(std::time::Duration::from_secs(5))
315+
.build()
316+
.expect("HTTP client");
317+
let mut statuses = Vec::new();
318+
for _ in 0..2 {
319+
let response = client
320+
.get(format!("{url}/api/control/bootstrap"))
321+
.header(header::HOST, test_case.host)
322+
.header("x-forwarded-host", "control.example.com")
323+
.header("x-example-user", test_case.username)
324+
.send()
325+
.await
326+
.expect("refused proxy request");
327+
statuses.push(response.status());
328+
}
329+
let log = std::fs::read_to_string(&control.stderr).expect("server log");
330+
let counts: Vec<(&str, usize)> = test_case
331+
.expected_warnings
332+
.iter()
333+
.map(|(warning, _)| (*warning, log.matches(warning).count()))
334+
.collect();
335+
assert_eq!(
336+
(
337+
statuses,
338+
counts.as_slice(),
339+
log.contains(test_case.username)
340+
),
341+
(
342+
vec![test_case.expected_status; 2],
343+
test_case.expected_warnings,
344+
false
345+
),
346+
"{}: {log}",
347+
test_case.description
348+
);
349+
#[cfg(unix)]
350+
crate::helpers::terminate(&mut control).await;
351+
});
352+
}
353+
}
354+
355+
#[test]
356+
fn given_the_default_trusted_peers_when_a_non_loopback_peer_sends_the_username_header_then_it_is_ignored_and_logged_once()
357+
{
358+
let test_cases = [ProxyRemotePeerTestCase {
359+
description: "a request from another address of this machine is not from loopback",
360+
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\n",
361+
username: "orders-remote-reader",
362+
expected_status: StatusCode::UNAUTHORIZED,
363+
expected_warnings: 1,
364+
}];
365+
// The address this machine routes from; connecting a UDP socket sends nothing.
366+
let route = std::net::UdpSocket::bind("0.0.0.0:0").expect("UDP socket");
367+
route.connect("192.0.2.1:9").expect("a non-loopback route");
368+
let host = route.local_addr().expect("route address").ip().to_string();
369+
let runtime = tokio::runtime::Builder::new_current_thread()
370+
.enable_all()
371+
.build()
372+
.expect("runtime");
373+
for test_case in test_cases {
374+
runtime.block_on(async {
375+
let directory = tempfile::tempdir().expect("proxy installation");
376+
std::fs::write(
377+
directory.path().join("server.toml"),
378+
test_case.configuration,
379+
)
380+
.expect("server configuration");
381+
let mut control = start_control_in(
382+
directory,
383+
&[
384+
"server",
385+
"--host",
386+
&host,
387+
"--port",
388+
"0",
389+
"--no-scheduler",
390+
"--config",
391+
"server.toml",
392+
],
393+
);
394+
let url = api_ready(&mut control).await;
395+
let client = Client::builder()
396+
.timeout(std::time::Duration::from_secs(5))
397+
.build()
398+
.expect("HTTP client");
399+
let mut statuses = Vec::new();
400+
for _ in 0..2 {
401+
let response = client
402+
.get(format!("{url}/api/control/bootstrap"))
403+
.header("x-forwarded-host", "control.example.com")
404+
.header("x-forwarded-for", "127.0.0.1")
405+
.header("x-sqb-user", test_case.username)
406+
.send()
407+
.await
408+
.expect("remote request");
409+
statuses.push(response.status());
410+
}
411+
let log = std::fs::read_to_string(&control.stderr).expect("server log");
412+
let warning = format!(
413+
"warning: ignored the trusted-proxy username header x-sqb-user from {host}, which is not in [trusted_proxy] trusted_peers"
414+
);
415+
assert_eq!(
416+
(
417+
statuses,
418+
log.matches(&warning).count(),
419+
log.contains(test_case.username)
420+
),
421+
(
422+
vec![test_case.expected_status; 2],
423+
test_case.expected_warnings,
424+
false
425+
),
426+
"{}: {log}",
427+
test_case.description
428+
);
429+
#[cfg(unix)]
430+
crate::helpers::terminate(&mut control).await;
431+
});
432+
}
433+
}

‎crates/sqlbuild-cli/tests/native_commands/test_types.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ pub(crate) struct ProxyCliTestCase {
5252
pub(crate) expected_statuses: [reqwest::StatusCode; 3],
5353
}
5454

55+
pub(crate) struct ProxyStartupTestCase {
56+
pub(crate) description: &'static str,
57+
pub(crate) configuration: &'static str,
58+
/// The username the refused requests carry, which the log must never show.
59+
pub(crate) username: &'static str,
60+
pub(crate) host: &'static str,
61+
pub(crate) expected_status: reqwest::StatusCode,
62+
pub(crate) expected_warnings: &'static [(&'static str, usize)],
63+
}
64+
65+
/// A server with the default `trusted_peers`, reached over a non-loopback address of this machine.
66+
pub(crate) struct ProxyRemotePeerTestCase {
67+
pub(crate) description: &'static str,
68+
pub(crate) configuration: &'static str,
69+
/// The username the remote requests carry, which the log must never show.
70+
pub(crate) username: &'static str,
71+
pub(crate) expected_status: reqwest::StatusCode,
72+
pub(crate) expected_warnings: usize,
73+
}
74+
5575
pub(crate) struct ServerConfigurationTestCase {
5676
pub(crate) description: &'static str,
5777
pub(crate) configuration: &'static str,

‎crates/sqlbuild-orchestration/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ proptest.workspace = true
4646
rcgen.workspace = true
4747
rusqlite = { workspace = true, features = ["functions"] }
4848
tempfile.workspace = true
49+
tower.workspace = true
4950

5051
[lints]
5152
workspace = true

‎crates/sqlbuild-orchestration/src/acknowledgements/main/acknowledge_person_run.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde_json::json;
44
use uuid::Uuid;
55

66
use crate::acknowledgements::models::AcknowledgementRequest;
7-
use crate::authorization::main::authorize_listing::authorize_listing;
7+
use crate::authorization::main::decisions::authorize_listing::authorize_listing;
88
use crate::credentials::main::audit::append_audit::append_audit;
99
use crate::credentials::models::{AuditContext, DefaultRole};
1010
use crate::store::errors::ControlError;

‎crates/sqlbuild-orchestration/src/acknowledgements/main/list_person_acknowledgements.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use chrono::{DateTime, Duration, Utc};
66
use uuid::Uuid;
77

88
use crate::acknowledgements::models::RunAcknowledgement;
9-
use crate::authorization::main::authorize_listing::authorize_listing;
9+
use crate::authorization::main::decisions::authorize_listing::authorize_listing;
1010
use crate::credentials::models::{AuditContext, DefaultRole};
1111
use crate::store::errors::ControlError;
1212
use crate::store::main::{begin::begin, commit::commit, query::query};

‎crates/sqlbuild-orchestration/src/api/_helpers/authentication/bearer.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
use std::net::SocketAddr;
44

55
use axum::extract::{Request, State};
6-
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
6+
use axum::http::{HeaderMap, StatusCode, header};
77
use axum::middleware::Next;
88
use axum::response::Response;
99
use uuid::Uuid;
1010

11+
use crate::api::_helpers::authentication::password_transport::private_cache_control;
1112
use crate::api::errors::ApiError;
1213
use crate::api::models::{DevelopmentScope, ReadResource};
1314
use crate::credentials::main::tokens::authenticate_token::authenticate_token;
@@ -39,9 +40,7 @@ pub(crate) async fn authenticate(
3940
.ok_or_else(unauthorized)?;
4041
request.extensions_mut().insert(identity);
4142
let mut response = next.run(request).await;
42-
response
43-
.headers_mut()
44-
.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
43+
private_cache_control(&mut response);
4544
Ok(response)
4645
}
4746

0 commit comments

Comments
 (0)