Binding a server to 0.0.0.0 (or [::] for IPv6) exposes it on every available network interface, including public ones. This is a common misconfiguration in containerized Rust services and the default in many tutorials. Bind to a specific interface (e.g. 127.0.0.1 for local-only access, or a private interface address) unless the service is intentionally public.
usestd::net::{TcpListener,SocketAddr,SocketAddrV4,SocketAddrV6,Ipv4Addr,Ipv6Addr};usetokio::net::TcpListenerasTokioListener;useactix_web::HttpServer;asyncfnbad()-> std::io::Result<()>{// std bind to all IPv4 interfaces
let_=TcpListener::bind("0.0.0.0:8080")?;// tokio bind to all IPv4 interfaces
let_=TokioListener::bind("0.0.0.0:3000").await?;// actix-web .bind() chain
let_=HttpServer::new(||()).bind("0.0.0.0:3001")?;// IPv6 unspecified
let_=TcpListener::bind("[::]:50051")?;// raw string literal still matches
let_=TcpListener::bind(r"0.0.0.0:9000")?;// tonic parse-then-bind idiom
let_addr: SocketAddr="0.0.0.0:50051".parse().unwrap();// Programmatic construction via UNSPECIFIED constants
let_v4=SocketAddrV4::new(Ipv4Addr::UNSPECIFIED,7000);let_v6=SocketAddrV6::new(std::net::Ipv6Addr::UNSPECIFIED,7001,0,0);Ok(())}
Compliant Code Examples
usestd::net::{TcpListener,SocketAddr,SocketAddrV4,Ipv4Addr};fnok()-> std::io::Result<()>{// Loopback (local only)
let_=TcpListener::bind("127.0.0.1:8080")?;let_=TcpListener::bind("[::1]:8080")?;// Specific internal interface
let_=TcpListener::bind("192.168.1.10:9000")?;// Parsed as SocketAddr but to loopback
let_addr: SocketAddr="127.0.0.1:9000".parse().unwrap();// Localhost constant — must not match (predicate filters UNSPECIFIED)
let_addr=SocketAddrV4::new(Ipv4Addr::LOCALHOST,7000);// Non-bind method that happens to take a 0.0.0.0 string — must not match
let_=parse_address("0.0.0.0:8080");Ok(())}
Seamless integrations. Try Datadog Code Security
Datadog Code Security
Try this rule and analyze your code with Datadog Code Security
How to use this rule
1
2
rulesets:- rust-security # Rules to enforce Rust security.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Security scans to your CI pipelines