httpadapter

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpadapter holds request handling shared by the adapters that receive requests over net/http: the reverse proxy and forward-auth.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GRPCCode added in v0.4.0

func GRPCCode(code int) codes.Code

GRPCCode is the gRPC status of a denial with HTTP status code: gRPC's own mapping of HTTP statuses (doc/http-grpc-status-mapping.md), which clients apply to an HTTP answer, so a client sees the same code through every proxy; and RESOURCE_EXHAUSTED for an oversized body (413), which that mapping leaves UNKNOWN.

func GRPCMessage added in v0.4.0

func GRPCMessage(code int, id string) string

GRPCMessage is the grpc-message of a denial with HTTP status code: the status text, as the body of an HTTP denial, and the request ID, which a gRPC client usually logs with the status. It is percent-encoded, as the gRPC spec says.

func IsEvent

func IsEvent(v pipeline.Verdict) bool

IsEvent reports whether a verdict is logged as a security event: blocks and detections, and every request that went uninspected unexpectedly (timeout, error, overload). Client cancellations and requests uninspected by configuration (a skipped route, no policy for the route, inspection suspended) are not events: they are counted in the metrics, and logging each would flood the logs. Neither are the requests an engine denies before it has a policy bundle (bootstrap deny): the engine logs that once.

func IsToken

func IsToken(s string) bool

IsToken reports whether s is an HTTP token (RFC 9110), the syntax of a method.

func LogEvent

func LogEvent(ctx context.Context, logger *slog.Logger, rec *events.Recorder, req *request.Request, path string, v pipeline.Verdict)

LogEvent writes one security event for req, and records it for the operator when rec is set. Lines are rate-limited per policy and rule by rec's limiter; the events for the operator are not. The line says where each rule matched (the variable's name), never what it matched.

func NewRequestID

func NewRequestID() string

NewRequestID returns a random 16-hex-digit request ID.

func PeerAddr

func PeerAddr(remoteAddr string) netip.Addr

PeerAddr parses the address of the direct TCP peer from r.RemoteAddr.

func RequestID

func RequestID(id string) string

RequestID returns id if it is a safe correlation ID (at most 128 of [A-Za-z0-9._:-]), and "" otherwise so that one is generated. The value is client-controlled and ends up in logs and events.

func SnapshotHeaders

func SnapshotHeaders(r *http.Request) http.Header

SnapshotHeaders returns a private copy of r's headers for inspection, with Transfer-Encoding and Content-Length restored.

The copy matters because an evaluation that times out keeps running after the handler has returned, and must not read the live request. Copying costs about 0.3µs, well under 0.1% of an evaluation.

net/http moves Transfer-Encoding out of the header map, and Content-Length can be absent (HTTP/2) even when the length is known. Without them, rules see a POST with neither header and raise a false positive.

func URIPath

func URIPath(rawURI string) (string, error)

URIPath validates a request target received from a proxy and returns its decoded path. The target must be in origin form and must not contain a fragment: rules stop reading the URI at '#' while the application may not.

func WriteDenial added in v0.5.0

func WriteDenial(w http.ResponseWriter, status int, id string, reqHeaders http.Header, jsonOK bool)

WriteDenial answers a denied (or unreadable) request with status: its request ID in X-Request-Id, and DenialBody's body.

func WriteGRPCDenial added in v0.4.0

func WriteGRPCDenial(w http.ResponseWriter, p GRPCProtocol, httpStatus, code int, id string)

WriteGRPCDenial answers a denied gRPC call in the trailers-only form: the gRPC status in the headers, no message. httpStatus is the HTTP status: 200, as a gRPC server answers, unless the proxy needs another to deny (forward-auth); gRPC clients read the gRPC status either way. code is the verdict's HTTP status, which gives the gRPC status.

Types

type Denial added in v0.5.0

type Denial struct {
	ContentType string
	Body        []byte
}

Denial is a denied request's answer, the same from every adapter (docs/troubleshooting.md, "What a denied client sees"): the status, X-Request-Id, and a short body. A client that accepts application/json gets a JSON body instead of text when JSON denials are on (--json-denials).

func DenialBody added in v0.5.0

func DenialBody(status int, id string, reqHeaders http.Header, jsonOK bool) Denial

DenialBody returns the body of a denial with status for the request ID, as JSON when jsonOK and the request's Accept takes application/json.

type GRPCProtocol added in v0.4.0

type GRPCProtocol uint8

GRPCProtocol is the gRPC variant a request asks for by its Content-Type, and so how a denial is answered.

const (
	// NotGRPC is any other request: denied with a plain HTTP answer.
	NotGRPC GRPCProtocol = iota
	// GRPC is gRPC over HTTP/2: application/grpc, with any codec.
	GRPC
	// GRPCWeb is gRPC-Web: application/grpc-web, with any codec.
	GRPCWeb
	// GRPCWebText is gRPC-Web in base64: application/grpc-web-text.
	GRPCWebText
)

func GRPCOf added in v0.4.0

func GRPCOf(h http.Header) GRPCProtocol

GRPCOf returns the gRPC variant a request with headers h asks for: by its one Content-Type, as Envoy tells a gRPC request (application/grpc, alone or with a "+codec" suffix). Unlike the pipeline's test of a gRPC call, the body doesn't matter: this only chooses the form of an answer the client can read.

func (GRPCProtocol) ContentType added in v0.4.0

func (p GRPCProtocol) ContentType() string

ContentType is the media type of a denial's answer: the protocol's, without a codec (a trailers-only answer carries no message).

func (GRPCProtocol) String added in v0.4.0

func (p GRPCProtocol) String() string

Jump to

Keyboard shortcuts

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