embedhosts

package
v1.166.2 Latest Latest
Warning

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

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

Documentation

Overview

Package embedhosts is the list of origins an HTML asset may frame and load media from (#2069): a video player (Wistia, a deployment's own) or the CDN a clip is served from. The share viewer's Content-Security-Policy and the portal's asset frame both read it, so a clip that plays in the portal plays on a share link, and one removed from the list is blocked on both.

The list is written into a policy, so each entry is held to the one form a policy source takes for a host: https, a host name or a leading `*.` label, an optional port, and nothing else. Anything that could end a directive or widen the policy is refused by name.

Index

Constants

View Source
const MaxHosts = 50

MaxHosts bounds the list, so the policy header it is written into stays a header.

View Source
const SettingsSection = "portal_embeds"

SettingsSection is the platform_settings section an administrator's list is stored under.

Variables

View Source
var Default = []string{
	"https://fast.wistia.net",
	"https://fast.wistia.com",
	"https://www.youtube-nocookie.com",
	"https://www.youtube.com",
	"https://player.vimeo.com",
}

Default is the list a deployment that sets none gets: the Wistia player, which plays inside the asset frame, and the YouTube and Vimeo players, which do not run in its opaque origin and draw as a link card instead (#2069, #2396). Listing them keeps a card's poster and an embed that a later frame can play under one setting.

View Source
var ErrInvalid = errors.New("invalid embed host")

ErrInvalid is what every refusal of an entry is (errors.Is).

Functions

func Defaults

func Defaults() []string

Defaults returns a copy of Default, so a caller cannot change it.

func From

func From(ctx context.Context, read func(context.Context) []string) []string

From is the list a reader function gives, the default when there is no reader, and never nil, so a list turned off reads as [].

func Normalize

func Normalize(entries []string) ([]string, error)

Normalize returns the list in its stored form, each entry `https://host[:port]`, lower case, without duplicates and in the order given. A bare host gains the https scheme. An empty list is valid and turns embeds off.

func RegisterSettings

func RegisterSettings(mux *http.ServeMux, wrap func(http.Handler) http.Handler, r *Resolver, author func(*http.Request) string)

RegisterSettings mounts the admin routes behind wrap.

func Sources

func Sources(hosts []string) string

Sources renders the list as the space-separated sources a directive takes, "" for an empty list. Every entry is normalized again, so a value that did not pass through Normalize cannot reach a header.

Types

type Config

type Config struct {
	Hosts *[]string `yaml:"hosts"`
}

Config is the `portal.embeds` section of the configuration file. Hosts nil takes Default; an empty list turns embeds off. An administrator's list, once written, is used instead.

func (Config) Configured

func (c Config) Configured() []string

Configured returns the configured list in its stored form, or Default when none is configured. An invalid list, which Validate refused at startup, returns Default.

func (Config) Validate

func (c Config) Validate() error

Validate refuses a configured list an administrator could not save.

type InvalidError

type InvalidError struct {
	Entry  string
	Reason string
}

InvalidError is the refusal of one entry, or of the list when Entry is empty, naming what is wrong with it.

func (*InvalidError) Error

func (e *InvalidError) Error() string

func (*InvalidError) Is

func (*InvalidError) Is(target error) bool

Is reports that the refusal is an ErrInvalid.

type Request

type Request struct {
	Hosts []string `json:"hosts"`
}

Request is a settings write.

type Resolver

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

Resolver answers the list in force: the administrator's, else the configuration file's, else Default.

func NewResolver

func NewResolver(section Section, cfg Config) *Resolver

NewResolver builds a resolver over the stored section (nil reads none) and the configuration file's section.

func (*Resolver) Configured

func (r *Resolver) Configured() []string

Configured is the list the configuration file gives, which an administrator's list replaces.

func (*Resolver) Hosts

func (r *Resolver) Hosts(ctx context.Context) []string

Hosts returns the list in force. A failed or invalid read serves the configured list rather than opening or closing every embed on a fault.

func (*Resolver) Write

func (r *Resolver) Write(ctx context.Context, hosts []string, author string) ([]string, error)

Write stores an administrator's list, normalized, and serves it on this replica at once.

type Response

type Response struct {
	// Hosts is the list in force.
	Hosts []string `json:"hosts" example:"https://fast.wistia.net,https://cdn.example.com"`
	// Configured is the list the configuration file gives (portal.embeds.hosts,
	// else the platform's default), which a saved list replaces.
	Configured []string `json:"configured" example:"https://fast.wistia.net"`
}

Response is what the settings routes answer.

type Section

type Section interface {
	// Read returns the stored settings and whether an administrator has
	// written them.
	Read(ctx context.Context) (Settings, bool, error)
	Write(ctx context.Context, s Settings, author string) error
}

Section is the stored administrator list as the resolver reads and writes it. settingsjson.Section satisfies it through an adapter in the wiring.

type Settings

type Settings struct {
	// Hosts are the origins, in their stored form.
	Hosts []string `json:"hosts"`
}

Settings is the stored section.

func (Settings) Validate

func (s Settings) Validate() error

Validate holds the stored list to the policy-source form. A value read back that fails it is not used.

Jump to

Keyboard shortcuts

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