diff --git a/Cargo.lock b/Cargo.lock index bf2629e..d2e847a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,6 +64,28 @@ dependencies = [ "topological-sort", ] +[[package]] +name = "authorization-injector-core" +version = "0.1.0" +dependencies = [ + "http", + "http-utils", + "test-harness", + "tokio", + "wasmtime", + "wit-bindgen", +] + +[[package]] +name = "authorization-injector-tests" +version = "0.1.0" +dependencies = [ + "http", + "test-harness", + "tokio", + "wasmtime", +] + [[package]] name = "base64" version = "0.22.1" @@ -2646,6 +2668,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b85a987ab61d365382d25c7d47e26edd48941b3bf6e3fbf76eff918bc71dab06" dependencies = [ "bitflags", + "futures", "wit-bindgen-rust-macro", ] diff --git a/Cargo.toml b/Cargo.toml index 46eaba2..23a09ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,8 @@ [workspace] resolver = "2" members = [ + "components/authorization-injector-core", + "components/authorization-injector", "components/client", "components/gate", "components/gate-client", diff --git a/README.md b/README.md index 0bc9b8b..98809d9 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ A collection of utility components that remix wasi:http types and interfaces. ## Components +- [`authorization-injector`](./components/authorization-injector/): adds an `authorization` header to requests sent with `wasi:http/client`, by origin, with every standard method except TRACE, `authorization-injector-core` with [`authorization-injector-method-config`](./components/authorization-injector-method-config/) +- [`authorization-injector-core`](./components/authorization-injector-core/): adds an `authorization` header to requests sent with `wasi:http/client`, by origin and method, configured with `wasi:config/store` - [`client`](./components/client/): a higher-level HTTP client that delegates to `wasi:http/client` - [`status-codes`](./components/status-codes/): constants for HTTP status codes diff --git a/components/authorization-injector-core/Cargo.toml b/components/authorization-injector-core/Cargo.toml new file mode 100644 index 0000000..3ea5e70 --- /dev/null +++ b/components/authorization-injector-core/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "authorization-injector-core" +version = "0.1.0" +edition = "2024" +license = "Apache-2.0" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +http-utils = { workspace = true } +# spawn forwards the result of transmitting the request with the authorization header +wit-bindgen = { workspace = true, features = ["async-spawn"] } + +[dev-dependencies] +http = { workspace = true } +test-harness = { workspace = true } +tokio = { workspace = true } +wasmtime = { workspace = true } diff --git a/components/authorization-injector-core/README.md b/components/authorization-injector-core/README.md new file mode 100644 index 0000000..c2bd953 --- /dev/null +++ b/components/authorization-injector-core/README.md @@ -0,0 +1,64 @@ +# `authorization-injector-core` + +Adds an `authorization` header to requests sent with `wasi:http/client` that don't already have one, by the request's origin and method. + +A request that already has an `authorization` header, whose method isn't allowed, or whose origin no key matches, is sent unchanged. [`authorization-injector`](../authorization-injector/) is this component with a method config that allows every standard method except TRACE. + +## Credentials + +The value for each origin is read from `wasi:config/store`. The key is the origin, `scheme://host[:port]`, and the value is the `authorization` header to send. + +```properties +https://api.example.com=Bearer 0123456789abcdef +https://*.example.com=Basic dXNlcjpwYXNz +http://localhost:*=Bearer dev-token +https://public.example.com= +``` + +- Without a port, a key matches the scheme's default port, e.g. `https://example.com` matches `https://example.com:443` but not `https://example.com:8443`. +- A host starting with `*.` matches the domain and every subdomain, e.g. `*.example.com` matches `example.com`, `foo.example.com` and `a.b.example.com`, but not `notexample.com`. `*` is only allowed as the first label of the host. +- A port of `*` matches any port, including the default. +- Schemes and hosts match case-insensitively. Userinfo in the request authority is ignored. +- When several keys match, the most specific one is used: an exact host before a wildcard host, a longer wildcard domain before a shorter one, then an exact port before `*`. +- An empty value sends no `authorization` header, e.g. to exclude `public.example.com` from `*.example.com` above. + +## Methods + +Whether the authorization is added to a request with each method is read from the `methods` import, a `wasi:config/store`. The key is the lowercase method, e.g. `get` or `post`, the value is `deferred` or `denied`. A method without a key uses the `*` key, and is denied without it, so without a method config no authorization is added. + +```properties +get=deferred +post=deferred +trace=denied +*=denied +``` + +Deny `trace`: the response to a TRACE request echoes the request back, an added header would reveal the credential to the caller. + +## Invalid config + +Every credential key must be a valid origin, no two keys may match the same origins, e.g. `https://example.com` and `https://example.com:443`, and every credential must be a valid header value. Every method value must be `deferred`, an empty value, or `denied`. Otherwise the cause is logged at the `critical` level and every request fails with `internal-error`, so a typo is noticed rather than ignored. A credential is never logged: + +``` +Invalid config COMPONENT=authorization-injector-core CONFIG=credentials KEY=api.example.com ERROR=expected 'scheme://host[:port]' +Invalid config COMPONENT=authorization-injector-core CONFIG=methods KEY=post VALUE=allowed ERROR=expected one of: 'deferred', 'denied' +``` + +> [!CAUTION] +> The configured values are credentials. Any request the wrapped component sends to a matching origin with an allowed method carries the credential, whatever its path. Configure the narrowest keys and methods you can, and prefer `https`. + +The headers of a `wasi:http` request are immutable, so a request that gets an `authorization` header is sent as a new request, with a copy of its headers, the same method, target and options, and its body and trailers. The result of transmitting the new request is forwarded to the original request. + +## Interfaces + +Imports: + +- `wasi:config/store@0.2.0-rc.1`: the authorization for each origin +- `methods: wasi:config/store@0.2.0-rc.1`: whether the authorization is added for each method +- `wasi:logging/logging@0.1.0-draft`: logs an invalid config +- `wasi:http/types@0.3.0`: the requests being sent +- `wasi:http/client@0.3.0`: the client the authorization injector wraps + +Exports: + +- `wasi:http/client@0.3.0`: client that adds the configured authorization diff --git a/components/authorization-injector-core/src/lib.rs b/components/authorization-injector-core/src/lib.rs new file mode 100644 index 0000000..3b8e726 --- /dev/null +++ b/components/authorization-injector-core/src/lib.rs @@ -0,0 +1,137 @@ +mod rules; + +use crate::{ + exports::wasi::http::client::{Guest, Request, Response}, + rules::{MethodRules, Origin, Rules}, + wasi::{ + config::store, + http::{ + client, + types::{ErrorCode, Scheme}, + }, + logging::logging::{Level, log}, + }, +}; + +const COMPONENT_NAME: &str = "authorization-injector-core"; + +const LOG_CONTEXT: &str = "componentized-authorization-injector"; + +const AUTHORIZATION: &str = "authorization"; + +struct AuthorizationInjector {} + +impl Guest for AuthorizationInjector { + #[doc = "/ This function may be used to either send an outgoing request over the"] + #[doc = "/ network or to forward it to another component."] + #[allow(async_fn_in_trait)] + async fn send(request: Request) -> Result { + match authorization(&request)? { + Some(authorization) => client::send(with_authorization(request, &authorization)?).await, + None => client::send(request).await, + } + } +} + +/// The authorization to add to the request, `None` when the request already has an authorization +/// header, its method is not allowed, or none is configured for its origin. +fn authorization(request: &Request) -> Result, ErrorCode> { + if request.get_headers().has(AUTHORIZATION) { + return Ok(None); + } + let methods = load_config( + "methods", + methods::get_all().map_err(|err| match err { + methods::Error::Upstream(message) => format!("ERROR=upstream: {message}"), + methods::Error::Io(message) => format!("ERROR=io: {message}"), + }), + MethodRules::parse, + )?; + let method = http_utils::format_http_method!(wasi::http::types::Method, request.get_method()); + if !methods.allows(&method) { + return Ok(None); + } + let rules = load_config( + "credentials", + store::get_all().map_err(|err| match err { + store::Error::Upstream(message) => format!("ERROR=upstream: {message}"), + store::Error::Io(message) => format!("ERROR=io: {message}"), + }), + Rules::parse, + )?; + let (Some(scheme), Some(authority)) = (request.get_scheme(), request.get_authority()) else { + return Ok(None); + }; + let scheme = http_utils::format_http_scheme!(Scheme, &scheme); + Ok(Origin::new(&scheme, &authority) + .and_then(|origin| rules.authorization(&origin).map(str::to_string))) +} + +/// Parse the entries of a config, `credentials` for `wasi:config/store` or `methods` for the +/// `methods` import. An invalid config fails every request, so a typo is noticed rather than +/// ignored. +fn load_config( + config: &str, + entries: Result, String>, + parse: impl FnOnce(Vec<(String, String)>) -> Result, +) -> Result { + entries.and_then(parse).map_err(|message| { + log( + Level::Critical, + LOG_CONTEXT, + &format!("Invalid config COMPONENT={COMPONENT_NAME} CONFIG={config} {message}"), + ); + ErrorCode::InternalError(Some(format!("invalid-config<{COMPONENT_NAME}>"))) + }) +} + +/// The request with the authorization header added. +/// +/// The headers of a request are immutable, so a new request is made with a copy of the headers, +/// the same method, target and options, and the body and trailers of the original request. The +/// result of transmitting the new request is forwarded to the original request. +fn with_authorization(request: Request, authorization: &str) -> Result { + let headers = request.get_headers().clone(); + headers + .set(AUTHORIZATION, &[authorization.as_bytes().to_vec()]) + .map_err(|err| internal_error(format!("failed to set the authorization header: {err:?}")))?; + // a child of the request, dropped before the request is consumed + let options = request.get_options().map(|options| options.clone()); + let method = request.get_method(); + let scheme = request.get_scheme(); + let authority = request.get_authority(); + let path_with_query = request.get_path_with_query(); + + let (res_tx, res_rx) = wit_future::new(|| Ok(())); + let (contents, trailers) = Request::consume_body(request, res_rx); + let (request, transmitted) = Request::new(headers, Some(contents), trailers, options); + wit_bindgen::spawn_local(async move { + let _ = res_tx.write(transmitted.await).await; + }); + + let invalid = |part: &str| internal_error(format!("failed to copy the request {part}")); + request.set_method(&method).map_err(|()| invalid("method"))?; + request + .set_scheme(scheme.as_ref()) + .map_err(|()| invalid("scheme"))?; + request + .set_authority(authority.as_deref()) + .map_err(|()| invalid("authority"))?; + request + .set_path_with_query(path_with_query.as_deref()) + .map_err(|()| invalid("path with query"))?; + Ok(request) +} + +fn internal_error(message: String) -> ErrorCode { + log(Level::Error, LOG_CONTEXT, &message); + ErrorCode::InternalError(Some(message)) +} + +wit_bindgen::generate!({ + path: "../wit", + world: "authorization-injector", + generate_all +}); + +export!(AuthorizationInjector); diff --git a/components/authorization-injector-core/src/rules.rs b/components/authorization-injector-core/src/rules.rs new file mode 100644 index 0000000..6470815 --- /dev/null +++ b/components/authorization-injector-core/src/rules.rs @@ -0,0 +1,500 @@ +//! Matching a request's origin to the configured authorization, without any WASI imports so it is +//! unit tested on the host. +//! +//! A rule's key is an origin, `scheme://host[:port]`. The host may start with `*.`, matching the +//! domain and every subdomain, e.g. `*.example.com` matches `example.com` and `a.b.example.com`. +//! The port may be `*`, matching any port, without a port the key matches the scheme's default +//! port. + +/// The default port of a scheme, `None` for a scheme without one. +fn default_port(scheme: &str) -> Option { + match scheme { + "http" => Some(80), + "https" => Some(443), + _ => None, + } +} + +/// The origin of a request, normalized for matching. +#[derive(Debug, PartialEq, Eq)] +pub struct Origin { + scheme: String, + host: String, + port: Option, +} + +impl Origin { + /// The origin of a request with the scheme and authority, `None` when the authority is not + /// valid. + /// + /// Userinfo is ignored, the host is lowercase without a trailing dot, and a missing port is + /// the scheme's default port. + pub fn new(scheme: &str, authority: &str) -> Option { + let scheme = scheme.to_ascii_lowercase(); + let host_port = match authority.rsplit_once('@') { + Some((_userinfo, host_port)) => host_port, + None => authority, + }; + let (host, port) = split_host_port(host_port)?; + let host = normalize_host(host)?; + let port = match port { + // `host:` is the default port, RFC 3986 section 3.2.3 + None | Some("") => default_port(&scheme), + Some(port) => Some(parse_port(port)?), + }; + Some(Self { scheme, host, port }) + } +} + +/// The host and port of an authority, `[::1]:8080` is `[::1]` and `8080`. +fn split_host_port(host_port: &str) -> Option<(&str, Option<&str>)> { + if host_port.starts_with('[') { + let end = host_port.find(']')? + 1; + let (host, rest) = host_port.split_at(end); + return match rest { + "" => Some((host, None)), + rest => Some((host, Some(rest.strip_prefix(':')?))), + }; + } + Some(match host_port.split_once(':') { + Some((host, port)) => (host, Some(port)), + None => (host_port, None), + }) +} + +/// A lowercase host without a trailing dot, `None` when empty. +fn normalize_host(host: &str) -> Option { + let host = host.strip_suffix('.').unwrap_or(host); + (!host.is_empty()).then(|| host.to_ascii_lowercase()) +} + +/// A port number, digits only. +fn parse_port(port: &str) -> Option { + if port.is_empty() || !port.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + port.parse().ok() +} + +#[derive(Debug, PartialEq, Eq)] +enum HostPattern { + /// The host, e.g. `example.com`. + Exact(String), + /// The domain and its subdomains, e.g. `example.com` for `*.example.com`. + Domain(String), +} + +impl HostPattern { + fn matches(&self, host: &str) -> bool { + match self { + Self::Exact(exact) => host == exact, + Self::Domain(domain) => { + host == domain + || host + .strip_suffix(domain.as_str()) + .is_some_and(|sub| sub.ends_with('.')) + } + } + } +} + +#[derive(Debug, PartialEq, Eq)] +enum PortPattern { + /// The port, `None` for a scheme without a default port. + Exact(Option), + /// Any port, `*`. + Any, +} + +/// How specific a matching rule is, the most specific rule is used. An exact host is more +/// specific than a domain, a longer domain more than a shorter one, then an exact port more than +/// any port. +type Specificity = (bool, usize, bool); + +/// The authorization for requests to the origins matching the key. +#[derive(Debug)] +struct Rule { + key: String, + scheme: String, + host: HostPattern, + port: PortPattern, + authorization: String, +} + +impl Rule { + fn parse(key: String, authorization: String) -> Result { + let invalid_key = |error: &str| format!("KEY={key} ERROR={error}"); + + let (scheme, host_port) = key + .split_once("://") + .ok_or_else(|| invalid_key("expected 'scheme://host[:port]'"))?; + let valid_scheme = scheme.starts_with(|c: char| c.is_ascii_alphabetic()) + && scheme + .chars() + .all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.')); + if !valid_scheme { + return Err(invalid_key("invalid scheme")); + } + let scheme = scheme.to_ascii_lowercase(); + if host_port.contains(['/', '?', '#']) { + return Err(invalid_key("expected an origin without a path, query or fragment")); + } + if host_port.contains('@') { + return Err(invalid_key("expected an origin without userinfo")); + } + + let (host, port) = split_host_port(host_port).ok_or_else(|| invalid_key("invalid host"))?; + let host = match host.strip_prefix("*.") { + Some(domain) => HostPattern::Domain(domain.to_string()), + None => HostPattern::Exact(host.to_string()), + }; + let host = match host { + HostPattern::Exact(host) | HostPattern::Domain(host) if host.contains('*') => { + return Err(invalid_key( + "a wildcard host must start with '*.', e.g. '*.example.com'", + )); + } + HostPattern::Exact(host) => HostPattern::Exact( + normalize_host(&host).ok_or_else(|| invalid_key("missing host"))?, + ), + HostPattern::Domain(domain) => HostPattern::Domain( + normalize_host(&domain).ok_or_else(|| invalid_key("missing domain after '*.'"))?, + ), + }; + let port = match port { + None | Some("") => PortPattern::Exact(default_port(&scheme)), + Some("*") => PortPattern::Any, + Some(port) => PortPattern::Exact(Some( + parse_port(port).ok_or_else(|| invalid_key("invalid port, expected a number or '*'"))?, + )), + }; + + // the value is a secret, the error never includes it + let valid_value = authorization + .bytes() + .all(|b| b == b'\t' || (0x20..0x7f).contains(&b) || b >= 0x80); + if !valid_value { + return Err(invalid_key("invalid header value")); + } + + Ok(Self { + key, + scheme, + host, + port, + authorization, + }) + } + + fn matches(&self, origin: &Origin) -> bool { + self.scheme == origin.scheme + && self.host.matches(&origin.host) + && match self.port { + PortPattern::Any => true, + PortPattern::Exact(port) => port == origin.port, + } + } + + fn specificity(&self) -> Specificity { + let (exact_host, host_len) = match &self.host { + HostPattern::Exact(host) => (true, host.len()), + HostPattern::Domain(domain) => (false, domain.len()), + }; + (exact_host, host_len, self.port != PortPattern::Any) + } + + /// Whether the rules match exactly the same origins, e.g. `https://example.com` and + /// `https://example.com:443`. + fn same_origins(&self, other: &Rule) -> bool { + self.scheme == other.scheme && self.host == other.host && self.port == other.port + } +} + +/// The authorization configured for each origin. +#[derive(Debug)] +pub struct Rules(Vec); + +impl Rules { + /// Parse the `wasi:config/store` entries, an error describes the invalid entry, e.g. + /// `KEY= ERROR=`, without the value. + pub fn parse(entries: Vec<(String, String)>) -> Result { + let mut rules: Vec = Vec::with_capacity(entries.len()); + for (key, value) in entries { + let rule = Rule::parse(key, value)?; + if let Some(other) = rules.iter().find(|other| other.same_origins(&rule)) { + return Err(format!( + "KEY={} ERROR=matches the same origins as '{}'", + rule.key, other.key + )); + } + rules.push(rule); + } + Ok(Self(rules)) + } + + /// The authorization for the origin, from the most specific matching rule. An empty value + /// is no authorization, e.g. to exclude a host from a domain. + pub fn authorization(&self, origin: &Origin) -> Option<&str> { + self.0 + .iter() + .filter(|rule| rule.matches(origin)) + .max_by_key(|rule| rule.specificity()) + .map(|rule| rule.authorization.as_str()) + .filter(|authorization| !authorization.is_empty()) + } +} + +/// The key a method without a key of its own falls back to. +const ANY_METHOD: &str = "*"; + +/// Whether the authorization is added to requests with each method, e.g. `get=deferred` and +/// `*=denied`. +/// +/// The key is the lowercase method, the value is `deferred`, or empty, or `denied`. A method +/// without a key uses the `*` key, and is denied without it, so a credential is only sent with +/// the methods it is meant for. +#[derive(Debug)] +pub struct MethodRules { + allowed: Vec<(String, bool)>, +} + +impl MethodRules { + /// Parse the `wasi:config/store` entries, an error describes the invalid entry, e.g. + /// `KEY= VALUE= ERROR=`. + pub fn parse(entries: Vec<(String, String)>) -> Result { + let mut allowed = Vec::with_capacity(entries.len()); + for (key, value) in entries { + let is_allowed = match value.as_str() { + "" | "deferred" => true, + "denied" => false, + _ => { + return Err(format!( + "KEY={key} VALUE={value} ERROR=expected one of: 'deferred', 'denied'" + )); + } + }; + allowed.push((key.to_ascii_lowercase(), is_allowed)); + } + Ok(Self { allowed }) + } + + /// Whether the authorization is added to a request with the method, any case. + pub fn allows(&self, method: &str) -> bool { + let method = method.to_ascii_lowercase(); + let lookup = |key: &str| { + // the last value of a key wins, like `wasi:config/store#get` + self.allowed + .iter() + .rev() + .find(|(k, _)| k == key) + .map(|(_, allowed)| *allowed) + }; + lookup(&method).or_else(|| lookup(ANY_METHOD)).unwrap_or(false) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn methods(entries: &[(&str, &str)]) -> MethodRules { + MethodRules::parse( + entries + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(), + ) + .unwrap() + } + + #[test] + fn methods_fall_back_to_any_then_denied() { + let rules = methods(&[("get", "deferred"), ("post", ""), ("trace", "denied")]); + assert!(rules.allows("get")); + assert!(rules.allows("GET")); + assert!(rules.allows("post")); + assert!(!rules.allows("trace")); + assert!(!rules.allows("bogus")); + + let rules = methods(&[("trace", "denied"), ("*", "deferred")]); + assert!(rules.allows("bogus")); + assert!(!rules.allows("trace")); + + assert!(!methods(&[]).allows("get")); + } + + #[test] + fn invalid_method_value() { + assert_eq!( + MethodRules::parse(vec![("post".into(), "allowed".into())]).unwrap_err(), + "KEY=post VALUE=allowed ERROR=expected one of: 'deferred', 'denied'" + ); + } + + fn rules(entries: &[(&str, &str)]) -> Rules { + Rules::parse( + entries + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(), + ) + .unwrap() + } + + fn parse_error(key: &str, value: &str) -> String { + Rules::parse(vec![(key.to_string(), value.to_string())]).unwrap_err() + } + + fn lookup<'a>(rules: &'a Rules, scheme: &str, authority: &str) -> Option<&'a str> { + rules.authorization(&Origin::new(scheme, authority).unwrap()) + } + + #[test] + fn origin_defaults_port_and_normalizes_host() { + assert_eq!( + Origin::new("HTTPS", "user:pass@Example.COM."), + Some(Origin { + scheme: "https".into(), + host: "example.com".into(), + port: Some(443) + }) + ); + assert_eq!(Origin::new("http", "example.com:").unwrap().port, Some(80)); + assert_eq!(Origin::new("ws", "example.com").unwrap().port, None); + assert_eq!( + Origin::new("http", "[::1]:8080"), + Some(Origin { + scheme: "http".into(), + host: "[::1]".into(), + port: Some(8080) + }) + ); + assert_eq!(Origin::new("http", "example.com:http"), None); + assert_eq!(Origin::new("http", ":80"), None); + assert_eq!(Origin::new("http", "[::1"), None); + } + + #[test] + fn exact_origin() { + let rules = rules(&[("https://example.com", "Bearer a")]); + assert_eq!(lookup(&rules, "https", "example.com"), Some("Bearer a")); + assert_eq!(lookup(&rules, "https", "EXAMPLE.com:443"), Some("Bearer a")); + assert_eq!(lookup(&rules, "https", "example.com:8443"), None); + assert_eq!(lookup(&rules, "http", "example.com"), None); + assert_eq!(lookup(&rules, "https", "api.example.com"), None); + assert_eq!(lookup(&rules, "https", "notexample.com"), None); + } + + #[test] + fn explicit_port() { + let rules = rules(&[("http://localhost:8080", "Bearer a")]); + assert_eq!(lookup(&rules, "http", "localhost:8080"), Some("Bearer a")); + assert_eq!(lookup(&rules, "http", "localhost"), None); + } + + #[test] + fn wildcard_domain_matches_domain_and_subdomains() { + let rules = rules(&[("https://*.example.com", "Bearer a")]); + assert_eq!(lookup(&rules, "https", "example.com"), Some("Bearer a")); + assert_eq!(lookup(&rules, "https", "foo.example.com"), Some("Bearer a")); + assert_eq!(lookup(&rules, "https", "a.b.example.com"), Some("Bearer a")); + assert_eq!(lookup(&rules, "https", "notexample.com"), None); + assert_eq!(lookup(&rules, "https", "example.com.evil"), None); + assert_eq!(lookup(&rules, "https", "foo.example.com:8443"), None); + } + + #[test] + fn wildcard_port() { + let rules = rules(&[("http://localhost:*", "Bearer a")]); + assert_eq!(lookup(&rules, "http", "localhost"), Some("Bearer a")); + assert_eq!(lookup(&rules, "http", "localhost:8080"), Some("Bearer a")); + assert_eq!(lookup(&rules, "https", "localhost"), None); + } + + #[test] + fn most_specific_rule_wins() { + let rules = rules(&[ + ("https://*.example.com:*", "any port"), + ("https://*.example.com", "domain"), + ("https://*.api.example.com", "subdomain"), + ("https://api.example.com:*", "exact host any port"), + ("https://api.example.com", "exact"), + ]); + assert_eq!(lookup(&rules, "https", "example.com:8443"), Some("any port")); + assert_eq!(lookup(&rules, "https", "www.example.com"), Some("domain")); + assert_eq!(lookup(&rules, "https", "v1.api.example.com"), Some("subdomain")); + assert_eq!( + lookup(&rules, "https", "api.example.com:8443"), + Some("exact host any port") + ); + assert_eq!(lookup(&rules, "https", "api.example.com"), Some("exact")); + } + + #[test] + fn empty_value_excludes_an_origin() { + let rules = rules(&[ + ("https://*.example.com", "Bearer a"), + ("https://public.example.com", ""), + ]); + assert_eq!(lookup(&rules, "https", "public.example.com"), None); + assert_eq!(lookup(&rules, "https", "api.example.com"), Some("Bearer a")); + } + + #[test] + fn invalid_keys() { + assert_eq!( + parse_error("example.com", "x"), + "KEY=example.com ERROR=expected 'scheme://host[:port]'" + ); + assert_eq!( + parse_error("1http://example.com", "x"), + "KEY=1http://example.com ERROR=invalid scheme" + ); + assert_eq!( + parse_error("https://example.com/path", "x"), + "KEY=https://example.com/path ERROR=expected an origin without a path, query or fragment" + ); + assert_eq!( + parse_error("https://user@example.com", "x"), + "KEY=https://user@example.com ERROR=expected an origin without userinfo" + ); + assert_eq!( + parse_error("https://foo*.example.com", "x"), + "KEY=https://foo*.example.com ERROR=a wildcard host must start with '*.', e.g. '*.example.com'" + ); + assert_eq!( + parse_error("https://*", "x"), + "KEY=https://* ERROR=a wildcard host must start with '*.', e.g. '*.example.com'" + ); + assert_eq!( + parse_error("https://*.", "x"), + "KEY=https://*. ERROR=missing domain after '*.'" + ); + assert_eq!(parse_error("https://", "x"), "KEY=https:// ERROR=missing host"); + assert_eq!( + parse_error("https://example.com:99999", "x"), + "KEY=https://example.com:99999 ERROR=invalid port, expected a number or '*'" + ); + } + + #[test] + fn invalid_value_is_not_logged() { + assert_eq!( + parse_error("https://example.com", "Bearer a\r\nx-injected: b"), + "KEY=https://example.com ERROR=invalid header value" + ); + } + + #[test] + fn duplicate_origins() { + let err = Rules::parse(vec![ + ("https://example.com".into(), "a".into()), + ("https://EXAMPLE.com:443".into(), "b".into()), + ]) + .unwrap_err(); + assert_eq!( + err, + "KEY=https://EXAMPLE.com:443 ERROR=matches the same origins as 'https://example.com'" + ); + } +} diff --git a/components/authorization-injector-core/tests/authorization.rs b/components/authorization-injector-core/tests/authorization.rs new file mode 100644 index 0000000..a9fa8b0 --- /dev/null +++ b/components/authorization-injector-core/tests/authorization.rs @@ -0,0 +1,238 @@ +//! Tests for `authorization-injector-core`, which adds an authorization header to requests sent +//! without one, from `wasi:config/store`. + +use test_harness::{ + Harness, HttpErrorCode, LogEntry, UpstreamRequest, host_request_with, response_status, +}; + +/// Send a POST request with the headers and a body through the client, the status of the +/// response or the error. +async fn send( + client: &mut test_harness::TestSubject, + uri: &str, + headers: &[(&'static str, &'static str)], +) -> wasmtime::Result> { + send_method(client, http::Method::POST, uri, headers).await +} + +/// Send a request with the method, headers and a body through the client, the status of the +/// response or the error. +async fn send_method( + client: &mut test_harness::TestSubject, + method: http::Method, + uri: &str, + headers: &[(&'static str, &'static str)], +) -> wasmtime::Result> { + let uri = uri.to_string(); + let headers = headers.to_vec(); + client + .run(async move |accessor, client| { + let request = + host_request_with(accessor, method, &uri, &headers, b"payload")?; + Ok( + match client.gated_client().call_send(accessor, request).await? { + Ok(response) => Ok(response_status(accessor, &response)?), + Err(err) => Err(err), + }, + ) + }) + .await +} + +/// The client, allowed to add the authorization to requests with any method. +fn harness() -> Harness { + Harness::new("authorization-injector-core").named_config("methods", &[("*", "deferred")]) +} + +fn last_request(client: &test_harness::TestSubject) -> UpstreamRequest { + client + .recorder() + .upstream_requests() + .pop() + .expect("a request was sent upstream") +} + +#[tokio::test(flavor = "multi_thread")] +async fn exports_client() -> wasmtime::Result<()> { + let client = harness().build().await?; + client.exports().gated_client(); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn adds_authorization_for_matching_origin() -> wasmtime::Result<()> { + let mut client = harness() + .config("https://api.example.com", "Bearer secret") + .build() + .await?; + let result = send( + &mut client, + "https://api.example.com/items?page=2", + &[("x-request", "1"), ("content-type", "text/plain")], + ) + .await?; + assert_eq!(result.ok(), Some(200)); + + let request = last_request(&client); + assert_eq!(request.method, "POST"); + assert_eq!(request.uri, "https://api.example.com/items?page=2"); + assert_eq!(request.header("authorization"), vec!["Bearer secret"]); + assert_eq!(request.header("x-request"), vec!["1"]); + assert_eq!(request.header("content-type"), vec!["text/plain"]); + assert_eq!(request.body, b"payload"); + assert_eq!(client.recorder().logs(), vec![]); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn keeps_existing_authorization() -> wasmtime::Result<()> { + let mut client = harness() + .config("https://api.example.com", "Bearer secret") + .build() + .await?; + let result = send( + &mut client, + "https://api.example.com/", + &[("Authorization", "Basic mine")], + ) + .await?; + assert_eq!(result.ok(), Some(200)); + let request = last_request(&client); + assert_eq!(request.header("authorization"), vec!["Basic mine"]); + assert_eq!(request.body, b"payload"); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn adds_authorization_only_to_allowed_methods() -> wasmtime::Result<()> { + let mut client = Harness::new("authorization-injector-core") + .named_config( + "methods", + &[("get", "deferred"), ("post", ""), ("trace", "denied")], + ) + .config("https://api.example.com", "Bearer secret") + .build() + .await?; + for (method, expected) in [ + ("GET", vec!["Bearer secret"]), + ("POST", vec!["Bearer secret"]), + ("TRACE", vec![]), + ("trace", vec![]), + // without a key or `*`, a method is denied + ("PUT", vec![]), + ] { + let method = http::Method::from_bytes(method.as_bytes())?; + let result = send_method(&mut client, method.clone(), "https://api.example.com/", &[]).await?; + assert_eq!(result.ok(), Some(200), "{method}"); + let request = last_request(&client); + assert_eq!(request.method, method.as_str()); + assert_eq!(request.header("authorization"), expected, "{method}"); + assert_eq!(request.body, b"payload"); + } + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn without_method_config_no_authorization_is_added() -> wasmtime::Result<()> { + let mut client = Harness::new("authorization-injector-core") + .named_config("methods", &[]) + .config("https://api.example.com", "Bearer secret") + .build() + .await?; + assert_eq!(send(&mut client, "https://api.example.com/", &[]).await?.ok(), Some(200)); + assert_eq!(last_request(&client).header("authorization"), Vec::<&str>::new()); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn invalid_method_config_fails_every_request() -> wasmtime::Result<()> { + let mut client = Harness::new("authorization-injector-core") + .named_config("methods", &[("post", "allowed")]) + .config("https://api.example.com", "Bearer secret") + .build() + .await?; + let result = send(&mut client, "https://api.example.com/", &[]).await?; + assert!( + matches!( + &result, + Err(HttpErrorCode::InternalError(Some(message))) + if message == "invalid-config" + ), + "{result:?}" + ); + assert_eq!(client.recorder().requests(), vec![]); + assert_eq!( + client.recorder().logs(), + vec![LogEntry::critical( + "componentized-authorization-injector", + "Invalid config COMPONENT=authorization-injector-core CONFIG=methods KEY=post VALUE=allowed ERROR=expected one of: 'deferred', 'denied'" + )] + ); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn unmatched_origin_is_sent_unchanged() -> wasmtime::Result<()> { + let mut client = harness() + .config("https://api.example.com", "Bearer secret") + .build() + .await?; + for uri in [ + "http://api.example.com/", + "https://api.example.com:8443/", + "https://other.example.com/", + ] { + assert_eq!(send(&mut client, uri, &[]).await?.ok(), Some(200)); + let request = last_request(&client); + assert_eq!(request.header("authorization"), Vec::<&str>::new(), "{uri}"); + assert_eq!(request.body, b"payload"); + } + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn wildcard_host_and_port() -> wasmtime::Result<()> { + let mut client = harness() + .config("https://*.example.com", "Bearer domain") + .config("http://localhost:*", "Bearer local") + .build() + .await?; + for (uri, expected) in [ + ("https://example.com/", vec!["Bearer domain"]), + ("https://foo.example.com/", vec!["Bearer domain"]), + ("https://a.b.example.com/", vec!["Bearer domain"]), + ("https://notexample.com/", vec![]), + ("http://localhost:8080/", vec!["Bearer local"]), + ("http://localhost/", vec!["Bearer local"]), + ] { + assert_eq!(send(&mut client, uri, &[]).await?.ok(), Some(200)); + assert_eq!(last_request(&client).header("authorization"), expected, "{uri}"); + } + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn invalid_config_fails_every_request() -> wasmtime::Result<()> { + let mut client = harness() + .config("api.example.com", "Bearer secret") + .build() + .await?; + let result = send(&mut client, "https://api.example.com/", &[]).await?; + assert!( + matches!( + &result, + Err(HttpErrorCode::InternalError(Some(message))) + if message == "invalid-config" + ), + "{result:?}" + ); + assert_eq!(client.recorder().requests(), vec![]); + assert_eq!( + client.recorder().logs(), + vec![LogEntry::critical( + "componentized-authorization-injector", + "Invalid config COMPONENT=authorization-injector-core CONFIG=credentials KEY=api.example.com ERROR=expected 'scheme://host[:port]'" + )] + ); + Ok(()) +} diff --git a/components/authorization-injector-method-config/README.md b/components/authorization-injector-method-config/README.md new file mode 100644 index 0000000..809fd16 --- /dev/null +++ b/components/authorization-injector-method-config/README.md @@ -0,0 +1,23 @@ +# `authorization-injector-method-config` + +Method config for `authorization-injector-core` that adds the authorization to requests with every standard method except TRACE. The response to a TRACE request echoes the request back, an added credential would be revealed to the caller. A method that isn't listed, e.g. a custom method, is denied. + +```properties +get=deferred +head=deferred +query=deferred +options=deferred +post=deferred +put=deferred +patch=deferred +delete=deferred +connect=deferred +trace=denied +*=denied +``` + +## Interfaces + +Exports: + +- `wasi:config/store@0.2.0-rc.1`: the config diff --git a/components/authorization-injector-method-config/authorization-injector-method-config.properties b/components/authorization-injector-method-config/authorization-injector-method-config.properties new file mode 100644 index 0000000..64f8ba0 --- /dev/null +++ b/components/authorization-injector-method-config/authorization-injector-method-config.properties @@ -0,0 +1,11 @@ +get=deferred +head=deferred +query=deferred +options=deferred +post=deferred +put=deferred +patch=deferred +delete=deferred +connect=deferred +trace=denied +*=denied diff --git a/components/authorization-injector/Cargo.toml b/components/authorization-injector/Cargo.toml new file mode 100644 index 0000000..a47e69b --- /dev/null +++ b/components/authorization-injector/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "authorization-injector-tests" +version = "0.1.0" +edition = "2024" +license = "Apache-2.0" +publish = false + +# only tests, the component is built from authorization-injector.wac + +[dev-dependencies] +http = { workspace = true } +test-harness = { workspace = true } +tokio = { workspace = true } +wasmtime = { workspace = true } diff --git a/components/authorization-injector/README.md b/components/authorization-injector/README.md new file mode 100644 index 0000000..74cc927 --- /dev/null +++ b/components/authorization-injector/README.md @@ -0,0 +1,18 @@ +# `authorization-injector` + +Adds an `authorization` header to requests sent with `wasi:http/client` that don't already have one, with every standard method except TRACE. + +`authorization-injector-core` composed with [`authorization-injector-method-config`](../authorization-injector-method-config/) for its `methods` import. The authorization for each origin is read from `wasi:config/store`, see [`authorization-injector-core`](../authorization-injector-core/). + +## Interfaces + +Imports: + +- `wasi:config/store@0.2.0-rc.1`: the authorization for each origin +- `wasi:logging/logging@0.1.0-draft`: logs an invalid config +- `wasi:http/types@0.3.0`: the requests being sent +- `wasi:http/client@0.3.0`: the client the authorization injector wraps + +Exports: + +- `wasi:http/client@0.3.0`: client that adds the configured authorization diff --git a/components/authorization-injector/authorization-injector.wac b/components/authorization-injector/authorization-injector.wac new file mode 100644 index 0000000..6331c79 --- /dev/null +++ b/components/authorization-injector/authorization-injector.wac @@ -0,0 +1,6 @@ +package componentized:http; + +export new local:authorization-injector-core { + methods: new local:authorization-injector-method-config {}.store, + ... +}...; diff --git a/components/authorization-injector/tests/authorization.rs b/components/authorization-injector/tests/authorization.rs new file mode 100644 index 0000000..ef7999e --- /dev/null +++ b/components/authorization-injector/tests/authorization.rs @@ -0,0 +1,45 @@ +//! Tests for `authorization-injector`, composed from `authorization-injector.wac`. + +use test_harness::{Harness, host_request_with, response_status}; + +#[tokio::test(flavor = "multi_thread")] +async fn adds_authorization_to_every_standard_method_but_trace() -> wasmtime::Result<()> { + let mut client = Harness::new("authorization-injector") + .config("https://api.example.com", "Bearer secret") + .build() + .await?; + let cases = [ + ("GET", true), + ("HEAD", true), + ("QUERY", true), + ("OPTIONS", true), + ("POST", true), + ("PUT", true), + ("PATCH", true), + ("DELETE", true), + ("CONNECT", true), + ("TRACE", false), + ("BOGUS", false), + ]; + for (method, authorized) in cases { + let status = client + .run(async move |accessor, client| { + let method = http::Method::from_bytes(method.as_bytes())?; + let request = + host_request_with(accessor, method, "https://api.example.com/", &[], b"")?; + let response = client + .gated_client() + .call_send(accessor, request) + .await? + .map_err(|err| wasmtime::format_err!("{err:?}"))?; + response_status(accessor, &response) + }) + .await?; + assert_eq!(status, 200, "{method}"); + let request = client.recorder().upstream_requests().pop().unwrap(); + assert_eq!(request.method, method); + let expected = if authorized { vec!["Bearer secret"] } else { vec![] }; + assert_eq!(request.header("authorization"), expected, "{method}"); + } + Ok(()) +} diff --git a/components/wit/worlds.wit b/components/wit/worlds.wit index f64ee41..52b4198 100644 --- a/components/wit/worlds.wit +++ b/components/wit/worlds.wit @@ -5,6 +5,16 @@ world client { export componentized:http/client@0.1.1-dev; } +world authorization-injector { + /// the authorization for each origin + import wasi:config/store@0.2.0-rc.1; + /// whether the authorization is added to requests with each method + import methods: wasi:config/store@0.2.0-rc.1; + import wasi:logging/logging@0.1.0-draft; + import wasi:http/client@0.3.0; + export wasi:http/client@0.3.0; +} + world gate-client { import componentized:http/latch@0.1.1-dev; import wasi:config/store@0.2.0-rc.1; diff --git a/crates/test-harness/src/latch.rs b/crates/test-harness/src/latch.rs index 9856425..10bdff0 100644 --- a/crates/test-harness/src/latch.rs +++ b/crates/test-harness/src/latch.rs @@ -168,6 +168,32 @@ impl store::Host for Ctx { } } +/// Define a `wasi:config/store` under the named import, with the values of +/// [`crate::Harness::named_config`]. +pub(crate) fn add_named_config_to_linker(linker: &mut Linker, import: &str) -> Result<()> { + let mut instance = linker.instance(import)?; + let name = import.to_string(); + instance.func_wrap( + "get", + move |store: StoreContextMut<'_, Ctx>, (key,): (String,)| { + let value = store.data().named_config[&name] + .iter() + .rev() + .find(|(k, _)| *k == key) + .map(|(_, v)| v.clone()); + Ok((Ok::<_, store::Error>(value),)) + }, + )?; + let name = import.to_string(); + instance.func_wrap( + "get-all", + move |store: StoreContextMut<'_, Ctx>, (): ()| { + Ok((Ok::<_, store::Error>(store.data().named_config[&name].clone()),)) + }, + )?; + Ok(()) +} + /// Define the host latch, `wasi:config/store`, and the host latch under the named imports of a /// `latch-n` component, e.g. `latch1`. pub(crate) fn add_to_linker(linker: &mut Linker) -> Result<()> { @@ -250,17 +276,36 @@ pub fn host_request( accessor: &Accessor, method: http::Method, uri: &str, +) -> Result> { + host_request_with(accessor, method, uri, &[], &[]) +} + +/// A request created by the host with headers and a body, for a gate's `wasi:http/client` or +/// `wasi:http/handler`. +pub fn host_request_with( + accessor: &Accessor, + method: http::Method, + uri: &str, + headers: &[(&str, &str)], + body: &[u8], ) -> Result> { let uri: http::Uri = uri.parse()?; let parts = uri.into_parts(); - let body = http_body_util::Empty::::new() + let mut fields = wasmtime_wasi_http::FieldMap::new_mutable(usize::MAX); + for (name, value) in headers { + fields + .append_raw(name.parse()?, value.parse()?) + .map_err(|err| format_err!("invalid header {name}: {err}"))?; + } + fields.set_immutable(); + let body = http_body_util::Full::new(bytes::Bytes::copy_from_slice(body)) .map_err(|never| -> wasmtime_wasi_http::Error { match never {} }); let (request, _transmitted) = Request::new( method, parts.scheme, parts.authority, parts.path_and_query, - wasmtime_wasi_http::FieldMap::default(), + fields, None, body, ); diff --git a/crates/test-harness/src/lib.rs b/crates/test-harness/src/lib.rs index b517981..92ba5a7 100644 --- a/crates/test-harness/src/lib.rs +++ b/crates/test-harness/src/lib.rs @@ -5,7 +5,7 @@ //! with either `wasi:http/client` or `wasi:http/handler`, never reach the network, they are //! recorded and answered with a canned response. -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::path::{Path, PathBuf}; use std::pin::Pin; use std::process::Command; @@ -35,7 +35,9 @@ use crate::gate_bindings::exports::wasi::http as gated; mod latch; -pub use latch::{Authorization, HostLatch, Observation, host_request, response_status}; +pub use latch::{ + Authorization, HostLatch, Observation, host_request, host_request_with, response_status, +}; /// Bindings for gates, which export `wasi:http/client` and `wasi:http/handler` with the host's /// `wasi:http/types`, and import a latch. @@ -569,6 +571,7 @@ pub struct Ctx { recorder: Recorder, latch: HostLatch, config: Vec<(String, String)>, + named_config: HashMap>, } impl WasiView for Ctx { @@ -794,6 +797,7 @@ pub struct Harness { latches: Vec, host_latch: Option, config: Vec<(String, String)>, + named_config: HashMap>, } impl Harness { @@ -805,6 +809,7 @@ impl Harness { latches: vec![], host_latch: None, config: vec![], + named_config: HashMap::new(), } } @@ -834,6 +839,19 @@ impl Harness { self } + /// Define a `wasi:config/store` under a named import, e.g. `methods`, with the values, in + /// order. Empty values define an empty store. + pub fn named_config(mut self, import: &str, values: &[(&str, &str)]) -> Self { + self.named_config.insert( + import.to_string(), + values + .iter() + .map(|(key, value)| (key.to_string(), value.to_string())) + .collect(), + ); + self + } + fn compose(&self) -> Result> { let mut names = vec![self.subject.as_str()]; names.extend(self.latches.iter().map(String::as_str)); @@ -905,6 +923,9 @@ impl Harness { upstream_client::add_to_linker::<_, UpstreamClient>(&mut linker, |ctx| ctx)?; logging::add_to_linker::<_, HasSelf>(&mut linker, |ctx| ctx)?; latch::add_to_linker(&mut linker)?; + for import in self.named_config.keys() { + latch::add_named_config_to_linker(&mut linker, import)?; + } let recorder = Recorder::default(); let mut store = Store::new( @@ -920,6 +941,7 @@ impl Harness { recorder: recorder.clone(), latch: self.host_latch.unwrap_or_else(HostLatch::defer), config: self.config, + named_config: self.named_config, }, ); let instance_pre = linker.instantiate_pre(&component)?;