Skip to content

Commit 13a133f

Browse files
authored
fix StoreBuilder::inherit_limited_network (#2541)
* fix `StoreBuilder::inherit_limited_network` Previously, this called `WasiCtxBuilder::inherit_network`, but that had no effect since `StoreBuilder::build_with_data` later overwrites that setting by calling `WasiCtxBuilder::socket_addr_check` with a lambda that uses `StoreBuilder::net_pool` to check addresses. In this cases, `StoreBuilder::net_pool` has not had any subnets added to it, so it denies everything, which is the opposite of what we intended. The solution is to have `StoreBuilder::inherit_limited_network` update `net_pool` to allow all IPv4 and IPv6 networks. Signed-off-by: Joel Dice <[email protected]> * address review feedback - Rename `StoreBuilder::inherit_limited_network` to `inherit_network` - Add comments explaining use of `Pool` and CIDR addresses Signed-off-by: Joel Dice <[email protected]> --------- Signed-off-by: Joel Dice <[email protected]>
1 parent 7864d69 commit 13a133f

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

crates/core/src/store.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use anyhow::{anyhow, Result};
22
use bytes::Bytes;
33
use cap_primitives::net::Pool;
4-
use cap_std::ipnet::IpNet;
4+
use cap_std::ipnet::{IpNet, Ipv4Net, Ipv6Net};
55
use std::{
66
io::{Read, Write},
77
mem,
8+
net::{Ipv4Addr, Ipv6Addr},
89
path::{Path, PathBuf},
910
sync::{Arc, Mutex},
1011
time::{Duration, Instant},
@@ -201,17 +202,27 @@ impl StoreBuilder {
201202
);
202203
}
203204

204-
/// Inherit the host network with a few hardcoded caveats
205-
pub fn inherit_limited_network(&mut self) {
205+
/// Allow unrestricted outbound access to the host network.
206+
pub fn inherit_network(&mut self) {
206207
self.with_wasi(|wasi| match wasi {
207208
WasiCtxBuilder::Preview1(_) => {
208209
panic!("Enabling network only allowed in preview2")
209210
}
210-
WasiCtxBuilder::Preview2(ctx) => {
211+
WasiCtxBuilder::Preview2(_) => {
211212
// TODO: ctx.allow_udp(false);
212-
ctx.inherit_network();
213213
}
214214
});
215+
216+
// Allow access to 0.0.0.0/0, i.e. all IPv4 addresses
217+
self.net_pool.insert_ip_net_port_any(
218+
IpNet::V4(Ipv4Net::new(Ipv4Addr::new(0, 0, 0, 0), 0).unwrap()),
219+
cap_primitives::ambient_authority(),
220+
);
221+
// Allow access to 0:/0, i.e. all IPv6 addresses
222+
self.net_pool.insert_ip_net_port_any(
223+
IpNet::V6(Ipv6Net::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), 0).unwrap()),
224+
cap_primitives::ambient_authority(),
225+
);
215226
}
216227

217228
/// Sets the WASI `stdin` descriptor to the given [`Read`]er.

crates/trigger/src/network.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ impl TriggerHooks for Network {
2929
let allowed_hosts =
3030
spin_outbound_networking::AllowedHostsConfig::parse(&hosts, &self.resolver)?;
3131
match allowed_hosts {
32-
spin_outbound_networking::AllowedHostsConfig::All => {
33-
store_builder.inherit_limited_network()
34-
}
32+
spin_outbound_networking::AllowedHostsConfig::All => store_builder.inherit_network(),
3533
spin_outbound_networking::AllowedHostsConfig::SpecificHosts(configs) => {
3634
for config in configs {
3735
if config.scheme().allows_any() {
3836
match config.host() {
3937
spin_outbound_networking::HostConfig::Any => {
40-
store_builder.inherit_limited_network()
38+
store_builder.inherit_network()
4139
}
4240
spin_outbound_networking::HostConfig::AnySubdomain(_) => continue,
4341
spin_outbound_networking::HostConfig::ToSelf => {}

0 commit comments

Comments
 (0)