Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip trailing slash in allowed_outbound_hosts config. #2548

Merged
merged 1 commit into from
Jun 7, 2024
Merged
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
47 changes: 47 additions & 0 deletions crates/outbound-networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ impl HostConfig {
bail!("Invalid allowed host {host}: wildcards are allowed only as subdomains");
}

// Remove trailing slashes
host = host.trim_end_matches('/');

Ok(Self::List(vec![host.into()]))
}

Expand Down Expand Up @@ -502,6 +505,37 @@ mod test {
use super::*;
use std::net::{Ipv4Addr, Ipv6Addr};

#[test]
fn test_allowed_hosts_accepts_url_without_port() {
assert_eq!(
AllowedHostConfig::new(
SchemeConfig::new("http"),
HostConfig::new("spin.fermyon.dev"),
PortConfig::new(80)
),
AllowedHostConfig::parse("http://spin.fermyon.dev").unwrap()
);

assert_eq!(
AllowedHostConfig::new(
SchemeConfig::new("http"),
// Trailing slash is removed
HostConfig::new("spin.fermyon.dev"),
PortConfig::new(80)
),
AllowedHostConfig::parse("http://spin.fermyon.dev/").unwrap()
);

assert_eq!(
AllowedHostConfig::new(
SchemeConfig::new("https"),
HostConfig::new("spin.fermyon.dev"),
PortConfig::new(443)
),
AllowedHostConfig::parse("https://spin.fermyon.dev").unwrap()
);
}

#[test]
fn test_allowed_hosts_accepts_url_with_port() {
assert_eq!(
Expand Down Expand Up @@ -667,6 +701,9 @@ mod test {

#[test]
fn test_allowed_hosts_rejects_path() {
// An empty path is allowed
assert!(AllowedHostConfig::parse("http://spin.fermyon.dev/").is_ok());
// All other paths are not allowed
assert!(AllowedHostConfig::parse("http://spin.fermyon.dev/a").is_err());
assert!(AllowedHostConfig::parse("http://spin.fermyon.dev:6666/a/b").is_err());
assert!(AllowedHostConfig::parse("http://*.fermyon.dev/a").is_err());
Expand Down Expand Up @@ -700,13 +737,23 @@ mod test {
assert!(
allowed.allows(&OutboundUrl::parse("http://example.com:8383/foo/bar", "http").unwrap())
);
// Allow urls with and without a trailing slash
assert!(allowed.allows(&OutboundUrl::parse("https://spin.fermyon.dev", "https").unwrap()));
assert!(allowed.allows(&OutboundUrl::parse("https://spin.fermyon.dev/", "https").unwrap()));
assert!(!allowed.allows(&OutboundUrl::parse("http://example.com/", "http").unwrap()));
assert!(!allowed.allows(&OutboundUrl::parse("http://google.com/", "http").unwrap()));
assert!(allowed.allows(&OutboundUrl::parse("spin.fermyon.dev:443", "https").unwrap()));
assert!(allowed.allows(&OutboundUrl::parse("example.com:8383", "http").unwrap()));
}

#[test]
fn test_allowed_hosts_with_trailing_slash() {
let allowed =
AllowedHostsConfig::parse(&["https://my.api.com/"], &dummy_resolver()).unwrap();
assert!(allowed.allows(&OutboundUrl::parse("https://my.api.com", "https").unwrap()));
assert!(allowed.allows(&OutboundUrl::parse("https://my.api.com/", "https").unwrap()));
}

#[test]
fn test_allowed_hosts_can_be_subdomain_wildcards() {
let allowed = AllowedHostsConfig::parse(
Expand Down
Loading