Documentation
¶
Overview ¶
Package ssrfguard provides a net.Dialer.Control callback that blocks connections to private and reserved IP ranges (SSRF protection).
It fires after DNS resolution but before the TCP handshake, which also prevents DNS-rebinding attacks.
Example ¶
package main
import (
"fmt"
"net"
"net/http"
"time"
"github.com/abhinavxd/ssrfguard"
)
func main() {
guard := ssrfguard.New() // no allowlist — block all reserved ranges
client := &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
Control: guard.Control,
}).DialContext,
},
}
// Public addresses work fine.
_, err := client.Get("https://example.com")
fmt.Println("example.com:", err)
// Requests to private/reserved IPs are blocked before any TCP connection.
_, err = client.Get("http://127.0.0.1:8080/admin")
fmt.Println("127.0.0.1:", err)
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReservedRanges ¶
ReservedRanges returns a copy of the built-in reserved IP ranges that the guard blocks by default. The returned slice is a copy callers may inspect it freely without affecting the guard's behaviour.
Types ¶
type Guard ¶
type Guard struct {
// contains filtered or unexported fields
}
Guard blocks connections to private/reserved IP addresses unless explicitly allowed. Use New to create one, then assign Guard.Control to a net.Dialer.
Click to show internal directories.
Click to hide internal directories.