originfuncs

package
v0.0.0-...-8fa2440 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2016 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchAll

func MatchAll(legs []Leg, legFunc LegFunc) bool

Returns true if all of the legs match the predicate.

func MatchAllCIDR

func MatchAllCIDR(legs []Leg, n ...net.IPNet) bool

Returns true if all of the source IPs of all of the legs match one of given prefixes.

Since ALL legs must match, this can be useful for whitelisting, e.g. to ensure a request originates from within your network.

func MatchAny

func MatchAny(legs []Leg, legFunc LegFunc) bool

Returns true if any of the legs match the predicate.

func MatchAnyCIDR

func MatchAnyCIDR(legs []Leg, n ...net.IPNet) bool

Returns true if any of the source IPs of any of the legs match one of the given prefixes.

Since this examines untrusted legs, this should be used for blacklisting and never whitelisting. i.e. it allows clients to volunteer to be blacklisted, e.g. by ISP transparent proxies. But it should never result in *increased* access.

func TrustLast

func TrustLast(leg *Leg, distance int) bool

Trust last leg only.

func TrustXRealIP

func TrustXRealIP(leg *Leg, distance int) bool

Trust X-Real-IP.

Types

type Leg

type Leg struct {
	SourceIP        net.IP  // nil if unknown
	SourcePort      uint16  // 0 if unknown
	DestinationIP   net.IP  // nil if unknown
	DestinationPort uint16  // 0 if unknown
	Scheme          string  // "" (unknown) or "http" or "https"
	Host            string  // HTTP host
	From            LegFrom // information source ("X-Forwarded-For" or "X-Real-IP" or "Forwarded" or "local")
}

A leg represents a leg of a request. A leg has source and destination IPs and ports and uses either HTTP or HTTPS.

func Parse

func Parse(req *http.Request) []Leg

func ParseLocal

func ParseLocal(req *http.Request) (legs []Leg, err error)

Return the local request leg.

func ParseRFC7239

func ParseRFC7239(hdr http.Header) (legs []Leg, err error)

Parse the RFC 7239 "Forwarded" header and returns the legs described in the header.

func ParseXForwardedFor

func ParseXForwardedFor(hdr http.Header) (legs []Leg, err error)

Parse the "X-Forwarded-For" header.

func ParseXRealIP

func ParseXRealIP(hdr http.Header) (legs []Leg, err error)

Parse the "X-Real-IP"/"X-Real-Protocol"/"X-Local-IP" header.

func TrustedLegs

func TrustedLegs(legs []Leg, trustLegFunc LegFunc) []Leg

Returns a slice of the given slice which is the span of trusted legs.

A leg is trusted if trustLegFunc returns true. By definition, all trusted legs are contiguously at the end of the slice, because these represent legs closer to this machine. For example:

                                                    Trusted                   Trusted
                                                   _________                 _________
{Client Machine} <- Leg -> {ISP Transparent Proxy} <- Leg -> {Load Balancer} <- Leg -> {This Machine}

The information provided by machines you do not control, such as the ISP Transparent Proxy, is untrusted. The information provided by your load balancer, and the local information on the 'physical' connection to this machine are trusted.

In general, you want to trust the first trusted leg. Thus you inspect TrustedLegs(...)[0].

type LegFrom

type LegFrom byte
const (
	FromForwarded LegFrom = iota
	FromXForwardedFor
	FromXRealIP
	FromLocal
)

type LegFunc

type LegFunc func(leg *Leg, distance int) bool

Predicate function type for leg trust decisions. Returns true iff a leg is trusted.

distance is the number of legs between the leg specified and the local (physical connection) leg. The local leg has a distance of 0, the preceding leg has a distance of 1, etc.

The predicates defined in this package currently do not use distance.

func TrustForwardedN

func TrustForwardedN(maxDistance int) LegFunc

Trust any Forwarded headers which have a distance of at most d.

Jump to

Keyboard shortcuts

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