@@ -9,7 +9,7 @@ use spin_locked_app::MetadataKey;
99use spin_world:: v1:: redis:: { self as v1, RedisParameter , RedisResult } ;
1010use spin_world:: v2:: redis:: { self as v2, Connection as RedisConnection , Error } ;
1111
12- pub const ALLOWED_REDIS_HOSTS_KEY : MetadataKey < Vec < String > > =
12+ pub const ALLOWED_REDIS_HOSTS_KEY : MetadataKey < Option < HashSet < String > > > =
1313 MetadataKey :: new ( "allowed_redis_hosts" ) ;
1414
1515pub use host_component:: OutboundRedisComponent ;
@@ -35,7 +35,7 @@ impl FromRedisValue for RedisResults {
3535}
3636
3737pub struct OutboundRedis {
38- allowed_hosts : HashSet < String > ,
38+ allowed_hosts : Option < HashSet < String > > ,
3939 connections : table:: Table < Connection > ,
4040}
4141
@@ -49,6 +49,24 @@ impl Default for OutboundRedis {
4949}
5050
5151impl OutboundRedis {
52+ fn is_address_allowed ( & self , address : & str , default : bool ) -> bool {
53+ fn do_check ( allowed_hosts : Option < & HashSet < String > > , address : & str , default : bool ) -> bool {
54+ let Some ( allowed_hosts) = allowed_hosts else {
55+ return default;
56+ } ;
57+ allowed_hosts. contains ( address)
58+ }
59+
60+ let response = do_check ( self . allowed_hosts . as_ref ( ) , address, default) ;
61+ if !response {
62+ terminal:: warn!(
63+ "A component tried to make a HTTP request to non-allowed address {address:?}."
64+ ) ;
65+ eprintln ! ( "To allow requests, add 'allowed_redis_hosts = [{address:?}]' to the manifest component section." ) ;
66+ }
67+ response
68+ }
69+
5270 async fn establish_connection (
5371 & mut self ,
5472 address : String ,
@@ -73,11 +91,7 @@ impl v2::Host for OutboundRedis {}
7391#[ async_trait]
7492impl v2:: HostConnection for OutboundRedis {
7593 async fn open ( & mut self , address : String ) -> Result < Result < Resource < RedisConnection > , Error > > {
76- if !self . allowed_hosts . contains ( & address) {
77- terminal:: warn!(
78- "A component tried to make a HTTP request to non-allowed address '{address}'."
79- ) ;
80- eprintln ! ( "To allow requests, add 'allowed_redis_hosts = [\" {address}\" ]' to the manifest component section." ) ;
94+ if !self . is_address_allowed ( & address, false ) {
8195 return Ok ( Err ( Error :: InvalidAddress ) ) ;
8296 }
8397
@@ -239,6 +253,9 @@ fn other_error(e: impl std::fmt::Display) -> Error {
239253/// Delegate a function call to the v2::HostConnection implementation
240254macro_rules! delegate {
241255 ( $self: ident. $name: ident( $address: expr, $( $arg: expr) ,* ) ) => { {
256+ if !$self. is_address_allowed( & $address, true ) {
257+ return Ok ( Err ( v1:: Error :: Error ) ) ;
258+ }
242259 let connection = match $self. establish_connection( $address) . await ? {
243260 Ok ( c) => c,
244261 Err ( _) => return Ok ( Err ( v1:: Error :: Error ) ) ,
0 commit comments