Documentation
¶
Overview ¶
Package security holds the checks that decide whether a connection from outside is allowed in, and the masking of credentials in text bound for logs and error reports.
Index ¶
Constants ¶
const AllowedFileURLNetworksEnv = "JARGO_ALLOWED_FILE_URL_NETWORKS"
AllowedFileURLNetworksEnv is the environment variable naming the networks a server is trusted to fetch files from, beyond the public internet, as a comma-separated list of CIDR ranges.
const AllowedOriginsEnv = "JARGO_ALLOWED_ORIGINS"
AllowedOriginsEnv is the environment variable naming the origins a browser client may open a socket from, as a comma-separated list. It is the default for every endpoint that takes an origin list, so a deployment can set the policy once instead of threading it through each one.
const Redacted = "[REDACTED]"
Redacted is what RedactSecrets puts in place of a credential.
Variables ¶
This section is empty.
Functions ¶
func DefaultAllowedFileURLNetworks ¶ added in v1.3.0
func DefaultAllowedFileURLNetworks() []string
DefaultAllowedFileURLNetworks returns the CIDR ranges AllowedFileURLNetworksEnv names (for example 10.0.0.0/8,192.168.0.0/16), or none when it is unset or empty, which trusts only the public internet.
func DefaultAllowedOrigins ¶
func DefaultAllowedOrigins() []string
DefaultAllowedOrigins returns the origins AllowedOriginsEnv names, or none when it is unset or empty, which allows every origin. Blank entries are dropped, so a trailing comma does not turn into an origin nothing matches.
func IsGlobal ¶ added in v1.3.0
IsGlobal reports whether ip is globally reachable, as the IANA IPv4 and IPv6 special-purpose address registries say. An IPv4-mapped IPv6 address is judged as the IPv4 address it maps, and the shared address space 100.64.0.0/10 is not global.
func IsOriginAllowed ¶
IsOriginAllowed reports whether origin, the value of a request's Origin header, is permitted by allowed.
An empty allowed list permits every origin, including a request that carries no Origin header at all: that is the default, and it is what a telephony provider streaming a call needs, since it is not a browser and sends no origin. A non-empty list permits only the origins it names, matched whole and without regard to case, and rejects a request whose origin is missing.
It is the guard against a page on another site opening a WebSocket to this one on a visitor's behalf: the browser sends the page's origin, and a server that never looks at it accepts the connection as readily as its own client's.
func RedactSecrets ¶
RedactSecrets masks the credentials in text, leaving the rest as written.
Error messages often quote whatever a failure carried: an SDK error that prints the headers it sent, a URL signed for one download, a provider body echoing the key it rejected. RedactSecrets masks the credential and keeps the rest, so a message still says what went wrong. It masks three things:
- The value of any name=value or name: value pair (or :=, =>), quoted or not, whose name marks a credential (api_key, Authorization, X-Amz-Signature, apiKey...). This covers query strings, printed header maps and plain prose alike. An auth scheme (Bearer, Basic...) ahead of the value is kept. An exception class name followed by its message (KeyError: ...) is not a credential name.
- A URL's password (scheme://user:password@host).
- Tokens recognizable by their format alone: provider key prefixes, AWS access key IDs, JWTs and PEM private keys.
Each is replaced by Redacted. Redacting text twice reads the same as redacting it once.
Types ¶
type URLReachability ¶ added in v1.3.0
type URLReachability string
URLReachability says who can safely reach a URL's host. It guards against server-side request forgery through URLs a client supplies.
const ( // ReachabilityPublic is a publicly routable host: safe to hand to a third // party to fetch itself. ReachabilityPublic URLReachability = "public" // ReachabilityAllowed is a host within a trusted private network: safe for // this server to fetch directly, but not to hand off to a third party that // cannot reach it. ReachabilityAllowed URLReachability = "allowed" // ReachabilityBlocked is a host nobody should fetch. ReachabilityBlocked URLReachability = "blocked" )
func ClassifyURLReachability ¶ added in v1.3.0
func ClassifyURLReachability(ctx context.Context, rawURL string, allowed []netip.Prefix) URLReachability
ClassifyURLReachability classifies who can safely reach rawURL's host: the public internet, this server, or no one.
Resolution happens once, up front; it does not defend against a target that resolves differently between this check and the actual connection (DNS rebinding).
It returns ReachabilityPublic when every resolved address is publicly routable, which is safe to hand to a third party (an LLM provider, for one) to fetch itself; ReachabilityAllowed when they are not all publicly routable but every address falls within allowed, which is safe for this server to fetch directly but not to hand off; and ReachabilityBlocked otherwise: loopback, link-local (the cloud metadata range 169.254.0.0/16 among them), private or unique-local addresses outside allowed, or a host that fails to resolve.