# How do I stop my server from being tricked into fetching internal addresses (SSRF)?
Source: https://ruststudio.org/community/t/85/how-do-i-stop-my-server-from-being-tricked-into-fetching-internal-addresses-ssrf
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: security, web, ssrf | Views: 0

## Short answer
When a feature fetches a URL from a user, allow only http and https, resolve the host yourself, and refuse loopback, private, link-local and unique-local addresses before connecting to the address you checked. Prefer an allow-list of hosts, and check again after every redirect.

## Answer
## Answer
Features such as "import from URL", webhooks or link previews make your server send requests on behalf of a user. If the user can name any address, they may point it at something only your server can reach: a database admin port, an internal service, or the cloud provider's metadata address (169.254.169.254 on common platforms). That is server-side request forgery.

The strongest defence is an **allow-list** of the hosts a feature may call. When that is not possible, check the address that you will actually connect to against a block-list of non-public ranges. The function below does that for an `IpAddr`; it has no network access and only demonstrates the classification.

```rust title=src/main.rs
use std::net::{IpAddr, Ipv6Addr};

/// True for addresses a server-side fetch must not be allowed to reach.
fn is_blocked(ip: IpAddr) -> bool {
    match ip {
        IpAddr::V4(v4) => {
            v4.is_loopback() || v4.is_private() || v4.is_link_local() // 169.254.0.0/16 holds cloud metadata services
                || v4.is_unspecified() || v4.is_broadcast() || v4.is_multicast()
        }
        IpAddr::V6(v6) => {
            if let Some(v4) = v6.to_ipv4_mapped() {
                return is_blocked(IpAddr::V4(v4)); // ::ffff:127.0.0.1 is still loopback
            }
            let seg0 = v6.segments()[0];
            v6.is_loopback() || v6.is_unspecified() || v6.is_multicast()
                || (seg0 & 0xfe00) == 0xfc00 // fc00::/7 unique local
                || (seg0 & 0xffc0) == 0xfe80 // fe80::/10 link local
        }
    }
}

fn main() {
    for s in ["127.0.0.1", "10.1.2.3", "192.168.0.9", "169.254.169.254", "93.184.216.34", "::1", "::ffff:127.0.0.1", "fd00::1", "2001:db8::1"] {
        let ip: IpAddr = s.parse().unwrap();
        println!("{s:20} blocked: {}", is_blocked(ip));
    }
    let _ = Ipv6Addr::LOCALHOST;
}
```

```text
127.0.0.1            blocked: true
10.1.2.3             blocked: true
192.168.0.9          blocked: true
169.254.169.254      blocked: true
93.184.216.34        blocked: false
::1                  blocked: true
::ffff:127.0.0.1     blocked: true
fd00::1              blocked: true
2001:db8::1          blocked: false
```

## How to do it
1. Accept only `http` and `https`, and only default or expected ports.
2. Resolve the name yourself, check **every** returned address, and connect to the address you checked. If you check the name and then let the HTTP client resolve it again, a hostile DNS server can answer differently the second time (DNS rebinding).
3. Disable automatic redirects or re-run the check on each redirect target.
4. Set short timeouts and a maximum response size (see the thread on limiting input).
5. Run the fetcher with network rules that block internal ranges as well; code checks are one layer, not the only one.
6. Do not return the fetched content or detailed errors to the user unless the feature needs it.

## Common mistakes
- Checking the host string for "localhost" only. Addresses can be written in many forms, and IPv6 and IPv4-mapped IPv6 forms (`::ffff:127.0.0.1`) are handled in the code above for this reason.
- Trusting a check done once, at input time, when the request happens later.
- Assuming a firewall at the edge protects services that the server itself can reach.

## Related questions
### Should I trust the response of an allowed host?
Treat it as untrusted input like any other: limit its size, parse it into typed data and never pass it on unescaped.
### Is the standard library's `is_private` enough?
It covers the IPv4 private ranges only, so the IPv6 ranges need their own checks. The code above matches the unique-local (`fc00::/7`) and link-local (`fe80::/10`) ranges with bit masks. In rustc 1.93.0 the standard library also has `Ipv6Addr::is_unique_local` and `is_unicast_link_local` (a one-line program calling them compiled and ran); prefer those if your minimum Rust version has them.

## Sources
- OWASP Server-Side Request Forgery Prevention Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html
- Standard library, `Ipv4Addr`: https://doc.rust-lang.org/std/net/struct.Ipv4Addr.html
- Standard library, `Ipv6Addr`: https://doc.rust-lang.org/std/net/struct.Ipv6Addr.html
