wasm

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 23 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 wasm hosts source plugins compiled to WebAssembly, on Extism and wazero (pure Go, no CGO). It is the Phase 0 prototype of angelia-dev/angelia#59; the manifest, the exports and the host functions are a draft (ADR 0011).

A plugin reaches the network only through the http_request host function, which sends every hop through the injected netguard client after checking it against allowed_hosts: the manifest's list plus the operator's (Options.AllowedHosts). A request to a host that is not allowed, or one netguard's SSRF guard blocks, fails the whole call even if the plugin ignores the error.

Credentials (Options.Credentials) are added by the host to the hops to the host and port each one is bound to, over https only. The host never hands them to the plugin; echoes from the upstream are redacted, best effort (ADR 0011, amendment of #145).

Index

Constants

View Source
const (
	DefaultMaxRequestsPerCall = 100
	DefaultCallTimeout        = 2 * time.Minute
	DefaultValidateTimeout    = 2 * time.Second
	DefaultMaxMemoryPages     = 1024 // 64 MiB
	DefaultMaxResponseBytes   = 10 << 20
	DefaultMaxOutputBytes     = 16 << 20
)

Defaults applied by New to zero Options fields.

View Source
const HostNamespace = "angelia:host/v0"

HostNamespace is the Wasm import module of the host functions.

Variables

View Source
var ErrHostNotAllowed = errors.New("wasm: host not in allowed_hosts")

ErrHostNotAllowed is wrapped by the error of a call in which the plugin tried to reach a host that is not in its manifest's allowed_hosts, whatever the plugin did with the http_request error.

View Source
var ErrInvalidManifest = errors.New("wasm: invalid plugin manifest")

ErrInvalidManifest is wrapped by every manifest validation error.

View Source
var ErrPolicy = errors.New("wasm: plugin violated host policy")

ErrPolicy is wrapped by the other host-side refusals that fail a call even if the plugin ignores them: network access during Validate and an exhausted request budget.

Functions

This section is empty.

Types

type Credential

type Credential struct {
	// Host is "host" or "host:port": a hostname or IP literal in the merged
	// allowed_hosts, optionally with the port (an IPv6 literal with a port
	// is "[addr]:port"). Without a port the credential is sent on port 443
	// only.
	Host string
	// Header is the request header that carries the token. Empty means
	// Authorization. Otherwise it must be a header the netguard client
	// counts as a credential (PRIVATE-TOKEN, or one of its
	// Config.ExtraAuthHeaders).
	Header string
	// Scheme, if set, is written before the token: "Bearer" gives
	// "Authorization: Bearer <token>". Empty sends the token as is. Only
	// with the Authorization header.
	Scheme string
	// Token returns the token. It is called for every hop to Host; it is
	// the seam for the secret store (#75). A rotated value is only picked
	// up if the function reads it at call time: the instance config reads
	// token_env once, at load. Its error must not contain the token.
	Token func(ctx context.Context) (string, error)
}

Credential is a token the operator configured when installing a plugin, bound to one of its allowed hosts (ADR 0011, amendments of #145 and #184). The host adds it as the Authorization header (or Header) of every hop to that host and port, over https only. It is never in the plugin's config map. The host does not hand it to the plugin, but an upstream that reflects the header can: redacting such echoes is best effort, so bind a credential only to a host that does not reflect it.

type CredentialHeaders

type CredentialHeaders interface {
	IsCredentialHeader(name string) bool
}

CredentialHeaders is implemented by the transport of the netguard client (*netguard.Client): it reports whether a header name puts a request on a host's authenticated budget. The host reserves those headers for the credentials it injects (ADR 0011, amendment of #184).

type HostGrant

type HostGrant struct {
	Host string `json:"host"`
	// Manifest and Operator say who allowed the host; both may be true.
	Manifest bool `json:"manifest"`
	Operator bool `json:"operator"`
	// CredentialPorts lists the ports on which the host injects a
	// credential for this host, sorted. Empty: none.
	CredentialPorts []string `json:"credential_ports,omitempty"`
}

HostGrant is one entry of a plugin's merged allowed_hosts, as Hosts shows it when the plugin is installed.

type HostNotAllowedError

type HostNotAllowedError struct {
	Host    string
	Allowed []string
}

HostNotAllowedError reports a request, or a redirect hop, to a host that is not in allowed_hosts. It wraps ErrHostNotAllowed.

func (*HostNotAllowedError) Error

func (e *HostNotAllowedError) Error() string

func (*HostNotAllowedError) Unwrap

func (e *HostNotAllowedError) Unwrap() error

Unwrap lets errors.Is match ErrHostNotAllowed.

type Manifest

type Manifest struct {
	// Kind is the source kind the plugin registers, for example "crates".
	Kind string `json:"kind"`
	// ConfigSchema is the JSON Schema of the plugin config map (Options.Config).
	// The host only checks that every config key is one of its top-level
	// "properties"; full JSON Schema validation is not done yet.
	ConfigSchema json.RawMessage `json:"config_schema,omitempty"`
	// AllowedHosts lists the hostnames or IP literals the plugin may reach
	// through http_request. Matching is exact and case-insensitive, on every
	// hop including redirects, on any port. There are no wildcards.
	AllowedHosts []string `json:"allowed_hosts"`
}

Manifest is the draft plugin manifest (architecture.md section 3.6). It is a prototype and not yet a public contract.

func ParseManifest

func ParseManifest(data []byte) (Manifest, error)

ParseManifest decodes and validates a JSON manifest. Unknown fields are rejected so that a typo cannot silently widen or drop a restriction.

func (Manifest) Validate

func (m Manifest) Validate() error

Validate checks the manifest.

type Options

type Options struct {
	// HTTPClient sends every hop of every http_request. It must be the
	// netguard client (netguard.Client.HTTPClient), so that the SSRF guard,
	// the per-host rate limits and the retries apply: its Transport must
	// be the *netguard.Client, whose CredentialHeaders method decides the headers reserved for
	// host-injected credentials. The host wraps the Transport to enforce
	// allowed_hosts on every hop and drops its Jar. Required.
	HTTPClient *http.Client
	// Config is the plugin's config map (Extism config). Every key must be
	// declared in the manifest's config_schema properties. It is visible
	// to the plugin: put tokens in Credentials, not here.
	Config map[string]string
	// AllowedHosts extends the manifest's allowed_hosts with hosts the
	// operator configured when installing the plugin, for example a
	// registry mirror. Same rules as the manifest's entries. Optional.
	AllowedHosts []string
	// Credentials are injected by the host into the requests to the hosts
	// they are bound to; the host never hands them to the plugin. Optional.
	Credentials []Credential
	// Logger receives the plugin's log host function calls and the host's
	// warnings. Default: discard.
	Logger *slog.Logger
	// MaxRequestsPerCall bounds the HTTP requests of one List call:
	// every http_request and every redirect hop it follows (pagination
	// included). Default 100.
	MaxRequestsPerCall int
	// CallTimeout bounds one List call, HTTP included; the plugin is
	// interrupted when it expires. Default 2m.
	CallTimeout time.Duration
	// ValidateTimeout bounds one Validate call, which has no network
	// access and should be quick. Default 2s.
	ValidateTimeout time.Duration
	// MaxMemoryPages caps the plugin's linear memory in 64 KiB pages.
	// Default 1024 (64 MiB).
	MaxMemoryPages uint32
	// MaxResponseBytes caps one response body handed to the plugin.
	// Default 10 MiB.
	MaxResponseBytes int64
	// MaxOutputBytes caps the output of one call. Default 16 MiB.
	MaxOutputBytes int
	// MaxIdle is the number of idle plugin instances kept for reuse.
	// Default GOMAXPROCS.
	MaxIdle int
	// CompilationCache, if set, is shared by the Sources of a process so
	// that a plugin's machine code is compiled once. Optional.
	CompilationCache wazero.CompilationCache
}

Options configures a Source.

type Source

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

Source is a source.Source implemented by a WASM plugin. It is safe for concurrent use: each call runs on its own plugin instance, taken from a small pool.

func New

func New(ctx context.Context, m Manifest, wasm []byte, opts Options) (*Source, error)

New compiles the plugin. The context only bounds compilation.

func (*Source) Close

func (s *Source) Close(ctx context.Context) error

Close releases the compiled plugin and its idle instances. Calls in flight fail.

func (*Source) Hosts

func (s *Source) Hosts() []HostGrant

Hosts returns the merged allowed_hosts, sorted, with where each entry comes from and the ports a credential is bound to. It never holds a token.

func (*Source) Kind

func (s *Source) Kind() string

Kind returns the manifest's kind.

func (*Source) List

List calls the plugin's list export.

func (*Source) Validate

func (s *Source) Validate(t source.Target) error

Validate rejects a target of another kind, then asks the plugin's validate export. The plugin gets no network access.

Directories

Path Synopsis
Package wasmtest holds test helpers for the WASM plugin host: a fake crates.io API with synthetic data, used by the host's tests and by benchmark 4 (bench/wasm).
Package wasmtest holds test helpers for the WASM plugin host: a fake crates.io API with synthetic data, used by the host's tests and by benchmark 4 (bench/wasm).

Jump to

Keyboard shortcuts

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