Help us shape the future of Go! Take the 2026 Go Developer Survey

allowlist

package
v1.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Package allowlist holds the values that may change on every request (time, tokens). Their value is not compared, but their format still is, and every entry carries a reason.

Index

Constants

View Source
const (
	CheckNoRequest  = "no-request"          // the rule applies to no compared request
	CheckNoPlace    = "no-place"            // it reaches nothing in the baseline bodies of its requests
	CheckAnyLength  = "any-length-values"   // any_length without format whose glob reaches values
	CheckRawRequest = "raw-without-request" // a $raw rule for every request
	CheckBroad      = "broad"               // see broad
	CheckShadowed   = "shadowed"            // an earlier rule always decides first
	CheckDuplicate  = "duplicate"           // the same rule again
)

Checks of driftgate allowlist check: what a rule does that its author probably did not mean. Lint finds those that need only the list; the command adds those that need the catalog and a baseline.

View Source
const RawPath = "$raw"

RawPath is the path of a body rule for the whole body: when the bytes of a body that is not JSON differ, the current body as text must match the format, and then it is not compared further. It is taken literally, not as a glob, so a wildcard path never stands for the whole body.

View Source
const Version = 1

Variables

This section is empty.

Functions

func IsCellPath

func IsCellPath(path string) bool

IsCellPath reports whether a rule's path names cells of a spreadsheet: it starts with xlsx: or csv!. Only such rules apply to cells; a path that starts with a wildcard (*) stays with JSON and HTML, as before cells had rules.

func SameParts

func SameParts(rules []*BodyRule, base, cur string) bool

SameParts reports whether two values are the same once every part a rule with parts matches is set aside. Each value is cut into literal pieces and matched parts (rule by rule, a later rule only within the literal pieces an earlier one left); the literal pieces must be equal, and a matched part stands for any text the rule matches.

Types

type ArrayRule

type ArrayRule struct {
	Target   string `yaml:"target,omitempty"`
	Request  string `yaml:"request,omitempty"`
	Path     string `yaml:"path"` // glob over JSON paths of arrays
	Key      string `yaml:"key"`  // a member of every item, or a path into it (user.id)
	AnyOrder bool   `yaml:"any_order,omitempty"`
	Reason   string `yaml:"reason"`
	// contains filtered or unexported fields
}

ArrayRule pairs the items of a JSON array by a key instead of by position: an item inserted at the front is one new item, not a change of every item after it. Items that are new or gone are drifts (unless an any_length rule lets the array change length), and so is a changed order of the items both sides have, unless AnyOrder.

func (*ArrayRule) KeySteps

func (r *ArrayRule) KeySteps() []string

KeySteps is the key of an array rule as member names (user.id: user, id).

func (*ArrayRule) Reaches

func (r *ArrayRule) Reaches(path string) bool

Reaches reports whether the rule's path matches the path of an array.

type BodyRule

type BodyRule struct {
	Target    string `yaml:"target,omitempty"`  // only requests of this service
	Request   string `yaml:"request,omitempty"` // glob over request IDs; empty means all
	Path      string `yaml:"path"`              // glob over body paths
	Format    string `yaml:"format,omitempty"`
	AnyLength bool   `yaml:"any_length,omitempty"` // arrays at this path may change length; values need a format
	// Parts: the format matches parts of the value (a hash in an asset URL, a
	// build id inside a script); only those parts may change, the rest of
	// the value is still compared. Without it the format matches the whole
	// value.
	Parts  bool   `yaml:"parts,omitempty"`
	Reason string `yaml:"reason"`
	// contains filtered or unexported fields
}

BodyRule allows a value inside a body to change while it keeps its format. The path is a JSON path ($.token), a path in an HTML page (html>body>p@class, html>body>script#__NEXT_DATA__$.buildId) or in its RSC payload (self.__next_f$.0.b), a cell of a spreadsheet (xlsx:Users!A1, csv!A1), or a glob over them. The path RawPath names the whole body of a response that is not JSON (see RawRule).

func (*BodyRule) Matches

func (r *BodyRule) Matches(text string) bool

Matches reports whether a value keeps the rule's format; a rule without a format (any_length) matches no value.

func (*BodyRule) Reaches

func (r *BodyRule) Reaches(path string) bool

Reaches reports whether the rule's path matches a body path; the start and end of its glob rule most paths out before the glob is matched. A cell rule matches the worksheet, the column and the row apart (cellGlob): what it matches is always a part of what its glob does.

type Finding

type Finding struct {
	RuleRef
	Check  string `json:"check"`
	Detail string `json:"detail"`
}

Finding is one problem of a rule.

func (Finding) String

func (f Finding) String() string

type HeaderRule

type HeaderRule struct {
	Target     string `yaml:"target,omitempty"`  // only requests of this service
	Request    string `yaml:"request,omitempty"` // glob over request IDs; empty means all
	Name       string `yaml:"name"`
	Format     string `yaml:"format,omitempty"`
	IgnoreCase bool   `yaml:"ignore_case,omitempty"`
	Reason     string `yaml:"reason"`
	// contains filtered or unexported fields
}

HeaderRule allows a header's value to change while it keeps its format, or, with IgnoreCase, only in letter case (charset and media type names are case-insensitive, RFC 9110).

func (*HeaderRule) Matches

func (r *HeaderRule) Matches(value string) bool

Matches reports whether a header value keeps the rule's format.

type List

type List struct {
	Version int          `yaml:"version"`
	Headers []HeaderRule `yaml:"headers"`
	Body    []BodyRule   `yaml:"body"`
	Arrays  []ArrayRule  `yaml:"arrays,omitempty"`
	// contains filtered or unexported fields
}

List is the parsed allow-list file.

func Empty

func Empty() *List

Empty returns a list with no rules.

func Load

func Load(path string) (*List, error)

Load reads an allow-list; a missing file is an empty list.

func Parse

func Parse(raw []byte) (*List, error)

Parse decodes and validates an allow-list.

func (*List) AnyLength

func (l *List) AnyLength(request, path string) bool

AnyLength reports whether a rule with any_length lets the JSON array at a body path of a request change its length (asked for an array that did).

func (*List) ArrayKey

func (l *List) ArrayKey(request, path string) *ArrayRule

ArrayKey returns the first array rule that applies to the JSON array at a body path of a request, or nil: its items are then paired by key.

func (*List) CellRules

func (l *List) CellRules(request, path string) (*BodyRule, []*BodyRule)

CellRules is ValueRules for a cell of a spreadsheet: only rules whose path names cells (IsCellPath) apply to them.

func (*List) Copy

func (l *List) Copy() *List

Copy is ForRequest without leaving out the rules of other requests.

func (*List) Done

func (l *List) Done(request string, values, bodies bool)

Done adds what the rules of a list for one request did to the Usage of the list it was made from. values is whether the comparison went as far as values (where header and value rules apply), bodies whether the request's bodies were compared (not when a private body is not on this machine): a rule that had no chance is not counted for the request.

func (*List) ForRequest

func (l *List) ForRequest(request string) *List

ForRequest is the list of rules that can apply to one request: the body rules for other requests are left out once, instead of being matched against the request at every value of its body, and the rest are sorted by kind. The list is the request's own: it counts which rules applied to a change (Done).

func (*List) ForTarget

func (l *List) ForTarget(target string) *List

ForTarget is the list of rules that apply to requests of one service: the rules of that service and the rules that name none.

func (*List) HasCellRules

func (l *List) HasCellRules() bool

HasCellRules reports whether a rule can apply to cells of a spreadsheet: a body rule whose path starts with xlsx: or csv! (IsCellPath).

func (*List) HasRawRule

func (l *List) HasRawRule(request string) bool

HasRawRule reports whether a whole-body rule applies to the request, without counting it as used.

func (*List) Header

func (l *List) Header(name string) *HeaderRule

Header returns the first rule for a header name (case-insensitive) that applies to every request.

func (*List) HeaderFor

func (l *List) HeaderFor(request, name string) *HeaderRule

HeaderFor returns the first rule for a header name that applies to the request: a rule without request applies to all. In a list for one request the rule counts as used (Track).

func (*List) Lint

func (l *List) Lint() []Finding

Lint checks the rules of a list by themselves: a $raw rule without request, rules that reach far more than a value (broad), and rules that never decide because an earlier one always does (shadowed, duplicate).

func (*List) Marshal

func (l *List) Marshal() ([]byte, error)

Marshal writes the list as YAML.

func (*List) RawRule

func (l *List) RawRule(request string) *BodyRule

RawRule returns the first whole-body rule (path RawPath) that applies to the request, or nil. The caller asks only when the rule would apply (no body is JSON): in a list for one request it then counts as used.

func (*List) TextRules

func (l *List) TextRules(request, path string) (*BodyRule, []*BodyRule)

TextRules is ValueRules; texts and attributes of a page are values.

func (*List) Track

func (l *List) Track() *List

Track returns the list with a new Usage and its rules numbered: lists made from it (ForTarget, ForRequest) count into it.

func (*List) Unused

func (l *List) Unused(selected bool) []Unused

Unused lists the rules of a tracked list (Track) that applied to no change in the comparison; with selected, only those that apply to a compared request (a comparison of some requests says nothing about the rules of the others).

func (*List) Validate

func (l *List) Validate() error

Validate checks every rule and compiles its format.

func (*List) ValueRules

func (l *List) ValueRules(request, path string) (*BodyRule, []*BodyRule)

ValueRules returns the rules for a value (a JSON scalar, a text or an attribute of an HTML page) at a body path of a request: the first matching rule with a whole-value format, which then decides alone, and otherwise every matching rule with parts. A rule without a format (any_length) is about the length of JSON arrays and says nothing about a value, so it is passed over. The caller asks for a value that changed: in a list for one request the rules returned count as used.

type RuleRef

type RuleRef struct {
	Kind    string `json:"kind"` // header, body or array
	Rule    int    `json:"rule"` // its position among the rules of its kind, from 1
	Name    string `json:"name,omitempty"`
	Path    string `json:"path,omitempty"`
	Request string `json:"request,omitempty"`
	Target  string `json:"target,omitempty"`
}

RuleRef names a rule of the list in a report: its kind, its position and what it is about.

func (RuleRef) String

func (r RuleRef) String() string

String is "body rule 3 ($.token, request get-*)".

type Unused

type Unused struct {
	RuleRef
	// Why: "no request" (it applies to none of the compared requests) or
	// "no change" (nothing it applies to changed).
	Why string `json:"why"`
}

Unused is a rule that did nothing in a comparison.

type Usage

type Usage struct {
	// contains filtered or unexported fields
}

Usage counts, over one comparison, the compared requests every rule applies to and those where it applied to a change. Requests are compared in parallel; each counts in its own list (ForRequest) and adds once, at the end (Done).

Jump to

Keyboard shortcuts

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