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

fix StoreBuilder::inherit_limited_network #2541

Merged
merged 1 commit into from
Jun 10, 2024
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
21 changes: 16 additions & 5 deletions crates/core/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::{anyhow, Result};
use bytes::Bytes;
use cap_primitives::net::Pool;
use cap_std::ipnet::IpNet;
use cap_std::ipnet::{IpNet, Ipv4Net, Ipv6Net};
use std::{
io::{Read, Write},
mem,
net::{Ipv4Addr, Ipv6Addr},
path::{Path, PathBuf},
sync::{Arc, Mutex},
time::{Duration, Instant},
Expand Down Expand Up @@ -201,17 +202,27 @@ impl StoreBuilder {
);
}

/// Inherit the host network with a few hardcoded caveats
pub fn inherit_limited_network(&mut self) {
/// Allow unrestricted outbound access to the host network.
pub fn inherit_network(&mut self) {
self.with_wasi(|wasi| match wasi {
WasiCtxBuilder::Preview1(_) => {
dicej marked this conversation as resolved.
Show resolved Hide resolved
panic!("Enabling network only allowed in preview2")
}
WasiCtxBuilder::Preview2(ctx) => {
WasiCtxBuilder::Preview2(_) => {
// TODO: ctx.allow_udp(false);
ctx.inherit_network();
}
});

// Allow access to 0.0.0.0/0, i.e. all IPv4 addresses
self.net_pool.insert_ip_net_port_any(
IpNet::V4(Ipv4Net::new(Ipv4Addr::new(0, 0, 0, 0), 0).unwrap()),
dicej marked this conversation as resolved.
Show resolved Hide resolved
cap_primitives::ambient_authority(),
);
// Allow access to 0:/0, i.e. all IPv6 addresses
self.net_pool.insert_ip_net_port_any(
IpNet::V6(Ipv6Net::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), 0).unwrap()),
cap_primitives::ambient_authority(),
);
}

/// Sets the WASI `stdin` descriptor to the given [`Read`]er.
Expand Down
6 changes: 2 additions & 4 deletions crates/trigger/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ impl TriggerHooks for Network {
let allowed_hosts =
spin_outbound_networking::AllowedHostsConfig::parse(&hosts, &self.resolver)?;
match allowed_hosts {
spin_outbound_networking::AllowedHostsConfig::All => {
store_builder.inherit_limited_network()
}
spin_outbound_networking::AllowedHostsConfig::All => store_builder.inherit_network(),
spin_outbound_networking::AllowedHostsConfig::SpecificHosts(configs) => {
for config in configs {
if config.scheme().allows_any() {
match config.host() {
spin_outbound_networking::HostConfig::Any => {
store_builder.inherit_limited_network()
store_builder.inherit_network()
}
spin_outbound_networking::HostConfig::AnySubdomain(_) => continue,
spin_outbound_networking::HostConfig::ToSelf => {}
Expand Down
Loading