netutil

package
v0.4.5-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package netutil provides HTTP security utilities for safe network operations. This package is designed to be reusable across the codebase without dependencies on domain, application, or infrastructure layers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractHost

func ExtractHost(rawURL string) string

ExtractHost returns just the host:port from a URL.

func FormatSize

func FormatSize(bytes int64) string

FormatSize returns a human-readable size string.

func HasCredentials

func HasCredentials(rawURL string) bool

HasCredentials returns true if the URL contains credentials.

func InsecureTLSConfig

func InsecureTLSConfig() *tls.Config

InsecureTLSConfig returns a TLS configuration that skips certificate verification. This should only be used with explicit user consent (--insecure flag). WARNING: Using this config disables security protections.

func IsHTTPS

func IsHTTPS(rawURL string) bool

IsHTTPS returns true if the URL uses the HTTPS scheme.

func IsOCI

func IsOCI(rawURL string) bool

IsOCI returns true if the URL uses the OCI scheme.

func IsPrivateHost

func IsPrivateHost(host string) (bool, net.IP)

IsPrivateHost resolves a hostname and checks if any of its IPs are private. Returns true if the host resolves to any private IP. Returns false if the host cannot be resolved or has no private IPs.

func IsPrivateIP

func IsPrivateIP(ip net.IP) bool

IsPrivateIP returns true if the given IP address is in a private/reserved range. This includes:

  • 10.0.0.0/8 (Class A private)
  • 172.16.0.0/12 (Class B private)
  • 192.168.0.0/16 (Class C private)
  • 169.254.0.0/16 (link-local)
  • 127.0.0.0/8 (loopback)
  • ::1/128 (IPv6 loopback)
  • fc00::/7 (IPv6 unique local)
  • fe80::/10 (IPv6 link-local)

func IsPrivateIPError

func IsPrivateIPError(err error) bool

IsPrivateIPError returns true if the error is a PrivateIPError.

func IsPrivateIPString

func IsPrivateIPString(ipStr string) bool

IsPrivateIPString parses an IP string and checks if it's private. Returns false if the string is not a valid IP address.

func IsRetryableStatus

func IsRetryableStatus(statusCode int) bool

IsRetryableStatus is exported for testing and external use.

func IsSizeLimitExceededError

func IsSizeLimitExceededError(err error) bool

IsSizeLimitExceededError returns true if the error is a SizeLimitExceededError.

func MinTLSVersion

func MinTLSVersion() uint16

MinTLSVersion returns the minimum required TLS version.

func MinTLSVersionString

func MinTLSVersionString() string

MinTLSVersionString returns the minimum required TLS version as a string.

func NormalizeURL

func NormalizeURL(rawURL string) string

NormalizeURL returns a normalized form of the URL suitable for cache keys. It lowercases the scheme and host, removes default ports, and strips credentials.

func PrivateIPRanges

func PrivateIPRanges() []string

PrivateIPRanges returns a human-readable list of blocked IP ranges. Useful for error messages and documentation.

func StripCredentials

func StripCredentials(rawURL string) string

StripCredentials removes user:password@ from a URL for safe logging. Returns the original string if the URL cannot be parsed.

func TLSConfig

func TLSConfig() *tls.Config

TLSConfig returns a secure TLS configuration with TLS 1.2+ minimum. This enforces Constitution II: TLS Enforcement requirements.

func TLSVersionString

func TLSVersionString(version uint16) string

TLSVersionString returns a human-readable TLS version string.

Types

type LimitedReader

type LimitedReader struct {
	R     io.Reader // underlying reader
	N     int64     // max bytes remaining
	Limit int64     // original limit (for error messages)
	// contains filtered or unexported fields
}

LimitedReader wraps an io.Reader with a maximum size limit. It returns an error when the limit is exceeded.

func NewLimitedReader

func NewLimitedReader(r io.Reader, limit int64) *LimitedReader

NewLimitedReader creates a new LimitedReader that will read at most limit bytes.

func (*LimitedReader) BytesRead

func (l *LimitedReader) BytesRead() int64

BytesRead returns the number of bytes read so far.

func (*LimitedReader) Read

func (l *LimitedReader) Read(p []byte) (n int, err error)

Read implements io.Reader with size limit enforcement.

type PrivateIPError

type PrivateIPError struct {
	IP net.IP
}

PrivateIPError is returned when a connection to a private IP is blocked.

func (*PrivateIPError) Error

func (e *PrivateIPError) Error() string

type RetryTransport

type RetryTransport struct {
	// Base is the underlying transport.
	// Default: http.DefaultTransport if nil.
	Base http.RoundTripper

	// OnRetry is called before each retry attempt.
	// The callback receives the attempt number (1-based) and wait duration.
	OnRetry func(attempt int, waitDuration time.Duration, statusCode int)

	// MaxRetries is the maximum number of retry attempts.
	// Default: 3 if zero.
	MaxRetries int

	// InitialBackoff is the initial backoff duration.
	// Default: 1s if zero.
	InitialBackoff time.Duration

	// MaxBackoff is the maximum backoff duration.
	// Default: 30s if zero.
	MaxBackoff time.Duration
}

RetryTransport wraps an http.RoundTripper with retry logic. It implements exponential backoff and respects Retry-After headers.

func (*RetryTransport) RoundTrip

func (t *RetryTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper with retry logic.

type SecureDialer

type SecureDialer struct {
	OnPrivateIPBlocked  func(ip net.IP)
	OnDNSPinning        func(host string, ip net.IP)
	Resolver            *net.Resolver
	Timeout             time.Duration
	AllowPrivateNetwork bool
}

SecureDialer provides DNS pinning and SSRF protection for network connections. It resolves DNS once and pins the IP for the duration of the connection, preventing DNS rebinding attacks.

func (*SecureDialer) DialContext

func (d *SecureDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext connects to the address with DNS pinning and SSRF protection. It resolves DNS once, validates against private IP ranges, and connects using the pinned IP to prevent DNS rebinding attacks.

type SizeLimitExceededError

type SizeLimitExceededError struct {
	Limit int64
	Read  int64
}

SizeLimitExceededError is returned when the size limit is exceeded.

func (*SizeLimitExceededError) Error

func (e *SizeLimitExceededError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL