@@ -111,17 +111,20 @@ impl Factor for OutboundNetworkingFactor {
111
111
wasi_builder. outbound_socket_addr_check ( move |addr, addr_use| {
112
112
let allowed_hosts = allowed_hosts. clone ( ) ;
113
113
async move {
114
- // TODO: validate against existing spin-core behavior
115
114
let scheme = match addr_use {
116
115
SocketAddrUse :: TcpBind => return false ,
117
116
SocketAddrUse :: TcpConnect => "tcp" ,
118
- SocketAddrUse :: UdpBind | SocketAddrUse :: UdpConnect | SocketAddrUse :: UdpOutgoingDatagram => "udp" ,
117
+ SocketAddrUse :: UdpBind
118
+ | SocketAddrUse :: UdpConnect
119
+ | SocketAddrUse :: UdpOutgoingDatagram => "udp" ,
119
120
} ;
120
- allowed_hosts. check_url ( & addr. to_string ( ) , scheme) . await . unwrap_or_else ( |err| {
121
- // TODO: should this trap (somehow)?
122
- tracing:: error!( %err, "allowed_outbound_hosts variable resolution failed" ) ;
123
- false
124
- } )
121
+ allowed_hosts
122
+ . check_url ( & addr. to_string ( ) , scheme)
123
+ . await
124
+ . unwrap_or (
125
+ // TODO: should this trap (somehow)?
126
+ false ,
127
+ )
125
128
}
126
129
} ) ;
127
130
}
@@ -174,7 +177,7 @@ impl FactorInstanceBuilder for InstanceBuilder {
174
177
}
175
178
}
176
179
177
- // TODO: Refactor w/ spin-outbound-networking crate to simplify
180
+ /// A check for whether a URL is allowed by the outbound networking configuration.
178
181
#[ derive( Clone ) ]
179
182
pub struct OutboundAllowedHosts {
180
183
allowed_hosts_future : SharedFutureResult < AllowedHostsConfig > ,
@@ -184,31 +187,41 @@ pub struct OutboundAllowedHosts {
184
187
impl OutboundAllowedHosts {
185
188
/// Checks address against allowed hosts
186
189
///
187
- /// Calls the [`DisallowedHostCallback`] if set and URL is disallowed.
190
+ /// Calls the [`DisallowedHostHandler`] if set and URL is disallowed.
191
+ /// If `url` cannot be parsed, `{scheme}://` is prepended to `url` and retried.
188
192
pub async fn check_url ( & self , url : & str , scheme : & str ) -> anyhow:: Result < bool > {
189
- let Ok ( url) = OutboundUrl :: parse ( url, scheme) else {
190
- tracing:: warn!(
191
- "A component tried to make a request to a url that could not be parsed: {url}" ,
192
- ) ;
193
- return Ok ( false ) ;
193
+ tracing:: debug!( "Checking outbound networking request to '{url}'" ) ;
194
+ let url = match OutboundUrl :: parse ( url, scheme) {
195
+ Ok ( url) => url,
196
+ Err ( err) => {
197
+ tracing:: warn!( %err,
198
+ "A component tried to make a request to a url that could not be parsed: {url}" ,
199
+ ) ;
200
+ return Ok ( false ) ;
201
+ }
194
202
} ;
195
203
196
204
let allowed_hosts = self . resolve ( ) . await ?;
197
205
let is_allowed = allowed_hosts. allows ( & url) ;
198
206
if !is_allowed {
207
+ tracing:: debug!( "Disallowed outbound networking request to '{url}'" ) ;
199
208
self . report_disallowed_host ( url. scheme ( ) , & url. authority ( ) ) ;
200
209
}
201
210
Ok ( is_allowed)
202
211
}
203
212
204
213
/// Checks if allowed hosts permit relative requests
205
214
///
206
- /// Calls the [`DisallowedHostCallback `] if set and relative requests are
215
+ /// Calls the [`DisallowedHostHandler `] if set and relative requests are
207
216
/// disallowed.
208
217
pub async fn check_relative_url ( & self , schemes : & [ & str ] ) -> anyhow:: Result < bool > {
218
+ tracing:: debug!( "Checking relative outbound networking request with schemes {schemes:?}" ) ;
209
219
let allowed_hosts = self . resolve ( ) . await ?;
210
220
let is_allowed = allowed_hosts. allows_relative_url ( schemes) ;
211
221
if !is_allowed {
222
+ tracing:: debug!(
223
+ "Disallowed relative outbound networking request with schemes {schemes:?}"
224
+ ) ;
212
225
let scheme = schemes. first ( ) . unwrap_or ( & "" ) ;
213
226
self . report_disallowed_host ( scheme, "self" ) ;
214
227
}
@@ -217,7 +230,7 @@ impl OutboundAllowedHosts {
217
230
218
231
async fn resolve ( & self ) -> anyhow:: Result < Arc < AllowedHostsConfig > > {
219
232
self . allowed_hosts_future . clone ( ) . await . map_err ( |err| {
220
- tracing:: error!( "Error resolving allowed_outbound_hosts variables: {err} " ) ;
233
+ tracing:: error!( %err , "Error resolving variables when checking request against allowed outbound hosts " ) ;
221
234
anyhow:: Error :: msg ( err)
222
235
} )
223
236
}
0 commit comments