requesttransform

package
v0.5.9 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 26 Imported by: ? 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 requesttransform evaluates a declared request transform: the signing, nonce and body encryption a target's own client applies to every request, written as data (an ordered list of operations) and never as code.

The schema, the checks and the semantics are gimorra's (packages/validators/src/request-transform.ts). Both evaluators read the same conformance vectors (testdata/request-transform.json, a byte-for-byte copy of gimorra's packages/validators/conformance/request-transform.json) in their tests, so one transform answers the same bytes in either.

A transform cannot change where a request goes: no operation writes the URL's scheme, host, port or path, and the header names that route or frame a request (DestinationHeaders) are refused at decode.

Keys are references: hmac-header and encrypt-body name a credential ({"credential": "<name>"}) that Env.Key resolves at evaluation; a transform document never holds a key value.

Index

Constants

View Source
const KeyEnvPrefix = "VIGOLIUM_TRANSFORM_KEY_"

KeyEnvPrefix prefixes the environment variable a key reference resolves from first: {"credential": "api-key"} reads VIGOLIUM_TRANSFORM_KEY_API_KEY.

View Source
const MaxOps = 32

MaxOps is the most operations one transform may hold.

Variables

View Source
var DestinationHeaders = map[string]bool{
	"host": true, ":authority": true, ":scheme": true, ":path": true, ":method": true,
	"forwarded": true, "x-forwarded-host": true, "x-forwarded-proto": true, "x-forwarded-port": true,
	"x-forwarded-server": true, "x-host": true, "x-original-url": true, "x-rewrite-url": true,
	"x-original-host": true, "x-http-method-override": true, "x-http-method": true,
	"x-method-override": true, "content-length": true, "transfer-encoding": true, "connection": true,
	"te": true, "upgrade": true, "proxy-connection": true,
}

DestinationHeaders are the header names that decide where a request goes or how it is framed; a transform never writes one.

View Source
var Ops = []string{"timestamp", "nonce", "hash", "truncate", "hmac-header", "set-header", "set-query", "encrypt-body"}

Ops is the closed vocabulary, in the order the refusal message lists it.

Functions

func Activate

func Activate(s *Set)

Activate installs the set every request this process sends is checked against (nil clears it).

func Decode

func Decode(data []byte) (*Transform, *Refusal)

Decode decodes and checks one transform document, answering the FIRST refusal (the same one gimorra's decodeTransform answers).

func DecodeWith

func DecodeWith(data []byte, predefined []string) (*Transform, *Refusal)

DecodeWith is Decode with names defined before the first op.

func Evaluate

func Evaluate(t *Transform, req Request, env Env) (Request, map[string]string, *Refusal)

Evaluate applies a checked transform to one request. The URL that comes back keeps the request's origin byte for byte: no op writes it.

func KeyEnvName

func KeyEnvName(credential string) string

KeyEnvName is the environment variable a credential name resolves from: the prefix plus the name uppercased, every non-alphanumeric byte as `_`.

func Matches

func Matches(t *Transform, rawURL string) bool

Matches reports whether t applies to rawURL: its host (and port, when the transform names one) and its path prefix.

func PercentEncode

func PercentEncode(v string) string

PercentEncode encodes every byte outside RFC 3986's unreserved set as %XX, uppercase.

func QueryValue

func QueryValue(query, name string) (string, bool)

QueryValue is one raw query's first value for name, decoded.

Types

type Applies

type Applies struct {
	Host          string
	PathPrefix    string
	HasPathPrefix bool
}

Applies is where a transform applies: one host (hostname, or hostname:port), optionally a path prefix.

type Env

type Env struct {
	// Now is milliseconds since the epoch.
	Now func() int64
	// Key resolves a credential reference; false when the store holds none.
	Key func(credential string) (string, bool)
	// Random returns n random bytes; nil uses crypto/rand.
	Random func(n int) []byte
	// Fixed pins a timestamp or nonce op's value by name.
	Fixed map[string]string
	// Vars are values defined before the first op.
	Vars map[string]string
}

Env is what one evaluation draws on.

type KeyRef

type KeyRef struct {
	Credential string
}

KeyRef names a credential of the caller's key store, never its value.

type Op

type Op struct {
	Op string

	Name    string
	HasName bool

	Unit      string
	Length    int
	HasLength bool
	Alphabet  string

	Alg         string
	Over        []string
	Join        string
	Encoding    string
	From        string
	Keep        string
	Header      string
	Key         KeyRef
	KeyEncoding string
	Prefix      string
	Value       string
	Param       string
	IV          string
	Layout      string

	Envelope       string
	HasEnvelope    bool
	ContentType    string
	HasContentType bool
}

Op is one decoded operation; which fields are meaningful depends on Op.

type Refusal

type Refusal struct {
	Code   string
	Detail string
	Field  string
}

Refusal says why a transform was refused or could not be evaluated. Code is the stable vocabulary both evaluators share (the vectors assert it).

func (*Refusal) Error

func (r *Refusal) Error() string

type Request

type Request struct {
	Method  string
	URL     string
	Headers map[string]string
	Body    *string
}

Request is what a transform reads and writes: the FINAL request, with every header the wire carries (lowercased names) and the body.

type Set

type Set struct {
	Transforms []*Transform
	// contains filtered or unexported fields
}

Set is the transforms one invocation applies, with the key store their references resolve against.

func Active

func Active() *Set

Active is the installed set, or nil.

func Load

func Load(path, keysPath string) (*Set, error)

Load reads a transform file and, when keysPath is set, its keys file - a JSON object of credential name to value, readable by its owner alone.

func Parse

func Parse(data []byte, keys map[string]string) (*Set, error)

Parse decodes a transform file: one transform object or an array of them. keys is the parsed keys file (nil for none).

func (*Set) Apply

func (s *Set) Apply(req Request) (Request, *Transform, *Refusal)

Apply evaluates the transform that applies to req.URL with the real clock and crypto/rand. It answers req unchanged (and a nil transform) when none applies.

func (*Set) For

func (s *Set) For(rawURL string) *Transform

For is the transform that applies to rawURL, or nil.

func (*Set) Key

func (s *Set) Key(credential string) (string, bool)

Key resolves a credential reference: the environment first, then the keys file.

type Transform

type Transform struct {
	ID      string
	Applies Applies
	Ops     []Op
}

Transform is a decoded, checked transform.

func For

func For(list []*Transform, rawURL string) *Transform

For picks the transform that applies to rawURL; the longest path prefix wins, the first among equals.

type URLParts

type URLParts struct {
	Origin   string
	Host     string
	Path     string
	Query    string
	HasQuery bool
	Fragment string
}

URLParts is a URL split by string slicing - never a URL parser, because two parsers normalise a path differently and a signature over a normalised path signs bytes that never leave.

func SplitURL

func SplitURL(raw string) URLParts

SplitURL is gimorra's urlParts.

Jump to

Keyboard shortcuts

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