@@ -57,18 +57,17 @@ pub async fn run_with_shutdown<F>(config: ServerConfig, shutdown: F) -> anyhow::
5757where
5858 F : std:: future:: Future < Output = ( ) > + Send + ' static ,
5959{
60+ let addr: SocketAddr = config. listen_addr . parse ( ) ?;
61+
6062 // Warn if authentication is disabled
6163 if !config. auth . enabled {
62- warn ! ( "Server running without authentication!" ) ;
63- warn ! ( "Anyone on the network can access this server." ) ;
64- warn ! ( "Use --auth to enable authentication." ) ;
64+ warn_auth_disabled ( addr) ;
6565 }
6666
6767 let state = Arc :: new ( AppState :: new ( config. clone ( ) ) . await ?) ;
6868 let state_for_cleanup = Arc :: clone ( & state) ;
6969 let app = create_router_with_state ( state) ;
7070
71- let addr: SocketAddr = config. listen_addr . parse ( ) ?;
7271 info ! ( "Starting Cortex server on {}" , addr) ;
7372
7473 // Start mDNS publisher if enabled
@@ -121,6 +120,20 @@ where
121120 Ok ( ( ) )
122121}
123122
123+ fn warn_auth_disabled ( addr : SocketAddr ) {
124+ warn ! ( "Server running without authentication!" ) ;
125+ warn ! ( "{}" , auth_disabled_exposure_warning( addr) ) ;
126+ warn ! ( "Use --auth to enable authentication." ) ;
127+ }
128+
129+ fn auth_disabled_exposure_warning ( addr : SocketAddr ) -> & ' static str {
130+ if addr. ip ( ) . is_loopback ( ) {
131+ "Only local processes can access this server."
132+ } else {
133+ "Anyone on the network can access this server."
134+ }
135+ }
136+
124137/// Create the application router.
125138pub fn create_router ( state : AppState ) -> Router {
126139 create_router_with_state ( Arc :: new ( state) )
@@ -143,3 +156,29 @@ pub fn create_router_with_state(state: Arc<AppState>) -> Router {
143156 . layer ( CorsLayer :: permissive ( ) )
144157 . with_state ( state)
145158}
159+
160+ #[ cfg( test) ]
161+ mod tests {
162+ use super :: auth_disabled_exposure_warning;
163+ use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr , SocketAddr } ;
164+
165+ #[ test]
166+ fn test_auth_disabled_exposure_warning_for_loopback_and_network_binds ( ) {
167+ let ipv4_loopback = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: LOCALHOST ) , 3000 ) ;
168+ let ipv6_loopback = SocketAddr :: new ( IpAddr :: V6 ( Ipv6Addr :: LOCALHOST ) , 3000 ) ;
169+ let wildcard = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: UNSPECIFIED ) , 3000 ) ;
170+
171+ assert_eq ! (
172+ auth_disabled_exposure_warning( ipv4_loopback) ,
173+ "Only local processes can access this server."
174+ ) ;
175+ assert_eq ! (
176+ auth_disabled_exposure_warning( ipv6_loopback) ,
177+ "Only local processes can access this server."
178+ ) ;
179+ assert_eq ! (
180+ auth_disabled_exposure_warning( wildcard) ,
181+ "Anyone on the network can access this server."
182+ ) ;
183+ }
184+ }
0 commit comments