urlpolicy

package
v0.19.1306 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: AGPL-3.0 Imports: 6 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 urlpolicy decides whether a URL taken from app config may be used, against allowlists a control plane operator configures per category.

The CLI and ctl-api both evaluate URLs with this package, so a URL that passes `nuon apps validate` is not rejected by the server for a matching reason.

Index

Constants

This section is empty.

Variables

Categories lists every category in a stable order.

Functions

func ReservedRunnerEnvVar

func ReservedRunnerEnvVar(name string) bool

ReservedRunnerEnvVar reports whether a runner env var name could redirect the runner's traffic or credentials, or change which runner build runs.

Types

type Category

type Category string

Category groups app-config URLs by what the URL is trusted to do. The lists are separate so that allowing a template bucket does not also allow that bucket to receive the runner token.

const (
	// CategoryRunnerAPI covers the API the runner sends its token to.
	CategoryRunnerAPI Category = "runner_api"
	// CategoryPublicAPI covers the API the install stack phones home to.
	CategoryPublicAPI Category = "public_api"
	// CategoryRunnerBootstrap covers scripts that run on the runner host or as
	// the phone-home function.
	CategoryRunnerBootstrap Category = "runner_bootstrap"
	// CategoryStackTemplates covers templates the customer's cloud deploys.
	CategoryStackTemplates Category = "stack_templates"
	// CategoryConfigFetch covers files ctl-api fetches while parsing a config.
	CategoryConfigFetch Category = "config_fetch"
)

func (Category) ConfigKey

func (c Category) ConfigKey() string

ConfigKey is the ctl-api config key an operator sets to restrict a category.

type CategoryConfig

type CategoryConfig struct {
	// Entries are the operator's entries. A category with none is not
	// restricted.
	Entries []string
	// Builtins are added once the category is restricted, so an operator does
	// not have to restate the origins a stock app config points at. They never
	// restrict a category on their own.
	Builtins []string
}

CategoryConfig is the input for one category.

type Config

type Config struct {
	// Mode defaults to ModeEnforce.
	Mode       Mode
	AllowHTTP  bool
	Categories map[Category]CategoryConfig
}

Config is the input for New.

type Entry

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

Entry is one allowlist entry: a URL prefix. A bare host is deliberately not the unit of trust, because hosts such as raw.githubusercontent.com or s3.amazonaws.com serve content from anyone.

func ParseEntry

func ParseEntry(raw string, opts ParseOptions) (Entry, error)

ParseEntry parses an allowlist entry. The accepted forms are:

https://bucket.s3.us-west-2.amazonaws.com/        origin plus path prefix
https://raw.githubusercontent.com/acme/runner/    owner-scoped prefix
https://*.example.com/templates/                  any subdomain of example.com
example.com                                       shorthand for https://example.com/

func (Entry) String

func (e Entry) String() string

String returns the entry in canonical form.

type Mode

type Mode string
const (
	// ModeEnforce rejects a violation.
	ModeEnforce Mode = "enforce"
	// ModeWarn reports a violation without rejecting it, for an operator
	// trialling a new list.
	ModeWarn Mode = "warn"
)

type ParseOptions

type ParseOptions struct {
	// AllowHTTP admits http entries. Only local development sets it.
	AllowHTTP bool
}

ParseOptions controls how entries and candidate URLs are parsed.

type Policy

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

Policy is the effective, parsed policy. The zero value and a nil *Policy restrict nothing.

func FromEffective

func FromEffective(mode Mode, entries map[string][]string) (*Policy, error)

FromEffective rebuilds a policy from the effective entries a control plane serves, so a client matches URLs exactly as that control plane does. The entries already include built-ins. Categories this build does not know are skipped: it has no fields mapped to them.

func New

func New(cfg Config) (*Policy, error)

New parses every entry and fails on the first malformed one.

func (*Policy) AnyRestricted

func (p *Policy) AnyRestricted() bool

AnyRestricted reports whether any category has an allowlist.

func (*Policy) Check

func (p *Policy) Check(category Category, field, rawURL string) *Violation

Check returns a violation when the category is restricted and rawURL does not fall under any of its entries. field names the config field, for the message. An empty rawURL is never a violation: the field is unset.

func (*Policy) CheckRunnerEnvVar

func (p *Policy) CheckRunnerEnvVar(field, name string) *Violation

CheckRunnerEnvVar returns a violation for a reserved runner env var. The denylist applies only once the operator has restricted the runner API, which is how they opt into locking the runner down.

func (*Policy) Entries

func (p *Policy) Entries(category Category) []string

Entries returns the effective entries for a category in canonical form: built-ins first, then the operator's. It is empty for an unrestricted category.

func (*Policy) Mode

func (p *Policy) Mode() Mode

Mode returns the policy's mode.

func (*Policy) OnWarn

func (p *Policy) OnWarn(fn func(Violations))

OnWarn registers the function ReportWarnings calls. Set it once at startup, before the policy is shared.

func (*Policy) RedirectCheck

func (p *Policy) RedirectCheck(category Category, field string) func(target *url.URL) error

RedirectCheck returns the check a fetch of an allowed URL runs on each redirect target, so an allowed location cannot send the fetch off-list. In enforce mode an off-list target is an error. In warn mode it is reported through ReportWarnings and the redirect is followed. It returns nil when the category is not restricted.

func (*Policy) ReportWarnings

func (p *Policy) ReportWarnings(violations Violations)

ReportWarnings hands violations found in ModeWarn to the registered function.

func (*Policy) Restricted

func (p *Policy) Restricted(category Category) bool

Restricted reports whether the category has an allowlist. An unrestricted category accepts any URL.

type Violation

type Violation struct {
	Category Category `json:"category"`
	Field    string   `json:"field"`
	Value    string   `json:"value"`
	Reason   string   `json:"reason"`
}

Violation is one config value the policy does not allow.

func (Violation) Error

func (v Violation) Error() string

type Violations

type Violations []Violation

Violations is a list of violations that reads as one error.

func (Violations) Error

func (v Violations) Error() string

Jump to

Keyboard shortcuts

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