Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ axum = { version = "0.8", default-features = false, features = ["http1", "json",
axum-server = { version = "0.8", default-features = false, features = ["tls-rustls-no-provider"] }
# Compresses the server's larger JSON reads and UI assets for browsers.
tower-http = { version = "0.6", default-features = false, features = ["compression-gzip"] }
# Tests drive the API router in process, with a chosen socket peer.
tower = { version = "0.5", default-features = false, features = ["util"] }
fensu-policy = "=0.22.0"
# Fensu's Python facts and core rules without their Python bindings: the default `python`
# feature adds PyO3, rusqlite, flate2 and sha2.
Expand Down
6 changes: 6 additions & 0 deletions clients/typescript/src/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,13 @@ export interface components {
/** @description What the UI needs first: how people sign in, and what this person may see and do. */
ControlBootstrap: {
access: components["schemas"]["PersonAccess"];
/** @description The display name a trusted proxy reported for this request, if it reports one. */
display_name?: string | null;
/** @description The email address a trusted proxy reported for this request, if it reports one. */
email?: string | null;
login_mode: components["schemas"]["LoginMode"];
/** @description Where signing out goes when a trusted proxy owns the sign-in; absent means no sign-out link. */
logout_url?: string | null;
};
/** @description Estimated query cost of one group, such as a job, a warehouse size or a node. */
CostGroup: {
Expand Down
4 changes: 2 additions & 2 deletions crates/sqlbuild-cli/src/native_commands/main/_run_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::adhoc::main::adhoc_planner::adhoc_planner;
use serde_json::{Value, json};
use sqlbuild_orchestration::authorization::main::bootstrap_user::bootstrap_user;
use sqlbuild_orchestration::authorization::main::grant_admin::grant_admin;
use sqlbuild_orchestration::authorization::main::people::bootstrap_user::bootstrap_user;
use sqlbuild_orchestration::authorization::main::roles::grant_admin::grant_admin;
use sqlbuild_orchestration::credentials::main::identities::create_principal::create_principal;
use sqlbuild_orchestration::credentials::main::locations::create_location::create_location;
use sqlbuild_orchestration::credentials::main::sessions::set_password::set_password;
Expand Down
235 changes: 230 additions & 5 deletions crates/sqlbuild-cli/tests/native_commands/test_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use reqwest::{Client, StatusCode, header};
use serde_json::{Value, json};

use crate::helpers::{api_ready, start_control_in, wait_for_exit};
use crate::test_types::{ProxyCliTestCase, ServerConfigurationTestCase};
use crate::test_types::{
ProxyCliTestCase, ProxyRemotePeerTestCase, ProxyStartupTestCase, ServerConfigurationTestCase,
};

#[test]
fn given_an_owner_configured_proxy_when_requests_carry_a_subject_then_only_explicit_people_resolve_without_sessions_or_machine_authority()
Expand Down Expand Up @@ -94,16 +96,58 @@ fn given_invalid_or_conflicting_server_configuration_when_a_standalone_server_st
expected_error: "requires explicit trusted peer IP addresses",
},
ServerConfigurationTestCase {
description: "non-HTTPS external origin",
configuration: "[trusted_proxy]\norigin = \"http://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\nidentities = {}\n",
description: "an allowed host written as a URL",
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\nallowed_hosts = [\"https://control-short\"]\n",
arguments: &[],
expected_error: "trusted-proxy authentication requires an HTTPS origin",
expected_error: "[trusted_proxy] allowed_hosts lists host names without a scheme, port or path; 'https://control-short' is not one",
},
ServerConfigurationTestCase {
description: "neither an HTTP nor an HTTPS external origin",
configuration: "[trusted_proxy]\norigin = \"ftp://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\n",
arguments: &[],
expected_error: "trusted-proxy authentication requires an HTTP or HTTPS origin",
},
ServerConfigurationTestCase {
description: "a username header a browser sets",
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\nusername_header = \"Cookie\"\n",
arguments: &[],
expected_error: "[trusted_proxy] username_header cannot be cookie",
},
ServerConfigurationTestCase {
description: "a fetch-metadata header a browser sets",
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\nusername_header = \"Sec-Fetch-User\"\n",
arguments: &[],
expected_error: "[trusted_proxy] username_header cannot be sec-fetch-user",
},
ServerConfigurationTestCase {
description: "the display name in the username header",
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",
arguments: &[],
expected_error: "username_header, display_name_header and email_header must differ",
},
ServerConfigurationTestCase {
description: "a script as the sign-out URL",
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\nproxy_logout_url = \"javascript:alert(1)\"\n",
arguments: &[],
expected_error: "proxy_logout_url must be an http:// or https:// URL",
},
ServerConfigurationTestCase {
description: "an unknown policy for unknown users",
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\ntrusted_peers = [\"127.0.0.1\"]\nunknown_users = \"provision_admin\"\n",
arguments: &[],
expected_error: "unknown variant `provision_admin`",
},
ServerConfigurationTestCase {
description: "two bindings for one username",
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",
arguments: &[],
expected_error: "binds 'orders-reader' more than once",
},
ServerConfigurationTestCase {
description: "subject cannot be represented by the header transport",
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",
arguments: &[],
expected_error: "subjects must be nonempty, unpadded ASCII header values",
expected_error: "trusted-proxy usernames must be 1-128 characters",
},
ServerConfigurationTestCase {
description: "a password-origin flag conflicts with the file's trusted proxy",
Expand Down Expand Up @@ -206,3 +250,184 @@ fn given_invalid_or_conflicting_server_configuration_when_a_standalone_server_st
});
}
}

#[test]
fn given_a_misconfigured_proxy_when_requests_arrive_then_startup_warns_and_each_refusal_is_logged_once()
{
let test_cases = [
ProxyStartupTestCase {
description: "the proxy's real address differs from trusted_peers",
configuration: "[trusted_proxy]\norigin = \"http://control.example.com\"\ntrusted_peers = [\"192.0.2.1\"]\nusername_header = \"X-Example-User\"\n",
username: "orders-confidential-reader",
host: "127.0.0.1",
expected_status: StatusCode::UNAUTHORIZED,
expected_warnings: &[
(
"warning: the trusted-proxy origin http://control.example.com is plain HTTP",
1,
),
(
"warning: ignored the trusted-proxy username header x-example-user from 127.0.0.1, which is not in [trusted_proxy] trusted_peers",
1,
),
],
},
ProxyStartupTestCase {
description: "a trusted peer's Host names neither this server nor allowed_hosts",
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\nusername_header = \"X-Example-User\"\n",
username: "orders-rebound-reader",
host: "evil.example:8000",
expected_status: StatusCode::FORBIDDEN,
expected_warnings: &[(
"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",
1,
)],
},
];
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("runtime");
for test_case in test_cases {
runtime.block_on(async {
let directory = tempfile::tempdir().expect("proxy installation");
std::fs::write(
directory.path().join("server.toml"),
test_case.configuration,
)
.expect("server configuration");
let mut control = start_control_in(
directory,
&[
"server",
"--host",
"127.0.0.1",
"--port",
"0",
"--no-scheduler",
"--config",
"server.toml",
],
);
let url = api_ready(&mut control).await;
let client = Client::builder()
.timeout(std::time::Duration::from_secs(5))
.build()
.expect("HTTP client");
let mut statuses = Vec::new();
for _ in 0..2 {
let response = client
.get(format!("{url}/api/control/bootstrap"))
.header(header::HOST, test_case.host)
.header("x-forwarded-host", "control.example.com")
.header("x-example-user", test_case.username)
.send()
.await
.expect("refused proxy request");
statuses.push(response.status());
}
let log = std::fs::read_to_string(&control.stderr).expect("server log");
let counts: Vec<(&str, usize)> = test_case
.expected_warnings
.iter()
.map(|(warning, _)| (*warning, log.matches(warning).count()))
.collect();
assert_eq!(
(
statuses,
counts.as_slice(),
log.contains(test_case.username)
),
(
vec![test_case.expected_status; 2],
test_case.expected_warnings,
false
),
"{}: {log}",
test_case.description
);
#[cfg(unix)]
crate::helpers::terminate(&mut control).await;
});
}
}

#[test]
fn given_the_default_trusted_peers_when_a_non_loopback_peer_sends_the_username_header_then_it_is_ignored_and_logged_once()
{
let test_cases = [ProxyRemotePeerTestCase {
description: "a request from another address of this machine is not from loopback",
configuration: "[trusted_proxy]\norigin = \"https://control.example.com\"\n",
username: "orders-remote-reader",
expected_status: StatusCode::UNAUTHORIZED,
expected_warnings: 1,
}];
// The address this machine routes from; connecting a UDP socket sends nothing.
let route = std::net::UdpSocket::bind("0.0.0.0:0").expect("UDP socket");
route.connect("192.0.2.1:9").expect("a non-loopback route");
let host = route.local_addr().expect("route address").ip().to_string();
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("runtime");
for test_case in test_cases {
runtime.block_on(async {
let directory = tempfile::tempdir().expect("proxy installation");
std::fs::write(
directory.path().join("server.toml"),
test_case.configuration,
)
.expect("server configuration");
let mut control = start_control_in(
directory,
&[
"server",
"--host",
&host,
"--port",
"0",
"--no-scheduler",
"--config",
"server.toml",
],
);
let url = api_ready(&mut control).await;
let client = Client::builder()
.timeout(std::time::Duration::from_secs(5))
.build()
.expect("HTTP client");
let mut statuses = Vec::new();
for _ in 0..2 {
let response = client
.get(format!("{url}/api/control/bootstrap"))
.header("x-forwarded-host", "control.example.com")
.header("x-forwarded-for", "127.0.0.1")
.header("x-sqb-user", test_case.username)
.send()
.await
.expect("remote request");
statuses.push(response.status());
}
let log = std::fs::read_to_string(&control.stderr).expect("server log");
let warning = format!(
"warning: ignored the trusted-proxy username header x-sqb-user from {host}, which is not in [trusted_proxy] trusted_peers"
);
assert_eq!(
(
statuses,
log.matches(&warning).count(),
log.contains(test_case.username)
),
(
vec![test_case.expected_status; 2],
test_case.expected_warnings,
false
),
"{}: {log}",
test_case.description
);
#[cfg(unix)]
crate::helpers::terminate(&mut control).await;
});
}
}
20 changes: 20 additions & 0 deletions crates/sqlbuild-cli/tests/native_commands/test_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ pub(crate) struct ProxyCliTestCase {
pub(crate) expected_statuses: [reqwest::StatusCode; 3],
}

pub(crate) struct ProxyStartupTestCase {
pub(crate) description: &'static str,
pub(crate) configuration: &'static str,
/// The username the refused requests carry, which the log must never show.
pub(crate) username: &'static str,
pub(crate) host: &'static str,
pub(crate) expected_status: reqwest::StatusCode,
pub(crate) expected_warnings: &'static [(&'static str, usize)],
}

/// A server with the default `trusted_peers`, reached over a non-loopback address of this machine.
pub(crate) struct ProxyRemotePeerTestCase {
pub(crate) description: &'static str,
pub(crate) configuration: &'static str,
/// The username the remote requests carry, which the log must never show.
pub(crate) username: &'static str,
pub(crate) expected_status: reqwest::StatusCode,
pub(crate) expected_warnings: usize,
}

pub(crate) struct ServerConfigurationTestCase {
pub(crate) description: &'static str,
pub(crate) configuration: &'static str,
Expand Down
1 change: 1 addition & 0 deletions crates/sqlbuild-orchestration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ proptest.workspace = true
rcgen.workspace = true
rusqlite = { workspace = true, features = ["functions"] }
tempfile.workspace = true
tower.workspace = true

[lints]
workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde_json::json;
use uuid::Uuid;

use crate::acknowledgements::models::AcknowledgementRequest;
use crate::authorization::main::authorize_listing::authorize_listing;
use crate::authorization::main::decisions::authorize_listing::authorize_listing;
use crate::credentials::main::audit::append_audit::append_audit;
use crate::credentials::models::{AuditContext, DefaultRole};
use crate::store::errors::ControlError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use chrono::{DateTime, Duration, Utc};
use uuid::Uuid;

use crate::acknowledgements::models::RunAcknowledgement;
use crate::authorization::main::authorize_listing::authorize_listing;
use crate::authorization::main::decisions::authorize_listing::authorize_listing;
use crate::credentials::models::{AuditContext, DefaultRole};
use crate::store::errors::ControlError;
use crate::store::main::{begin::begin, commit::commit, query::query};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
use std::net::SocketAddr;

use axum::extract::{Request, State};
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
use axum::http::{HeaderMap, StatusCode, header};
use axum::middleware::Next;
use axum::response::Response;
use uuid::Uuid;

use crate::api::_helpers::authentication::password_transport::private_cache_control;
use crate::api::errors::ApiError;
use crate::api::models::{DevelopmentScope, ReadResource};
use crate::credentials::main::tokens::authenticate_token::authenticate_token;
Expand Down Expand Up @@ -39,9 +40,7 @@ pub(crate) async fn authenticate(
.ok_or_else(unauthorized)?;
request.extensions_mut().insert(identity);
let mut response = next.run(request).await;
response
.headers_mut()
.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
private_cache_control(&mut response);
Ok(response)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pub(crate) mod configure;
pub(crate) mod development;
pub(crate) mod oidc;
pub(crate) mod oidc_provider;
pub(crate) mod origins;
pub(crate) mod password_sessions;
pub(crate) mod password_transport;
pub(crate) mod proxy_settings;
pub(crate) mod proxy_warnings;
pub(crate) mod trusted_proxy;
Loading
Loading