branding

package
v0.294.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 24 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 branding is the instance's logo and display name: what an admin uploads on the Account page (docs/branding.md), the images muster serves from /brand/, and the row that keeps them (migration 0105, instance_branding).

This file is the image half: Process turns an upload into the three PNGs muster serves, or refuses it with one of the errors below.

🔴 THE FORMAT IS DECIDED BY THE BYTES, NEVER BY A NAME. The filename and the part's Content-Type are the uploader's claims and are not read at all. The first bytes must be a PNG, JPEG or WebP signature, and each is decoded by ITS OWN decoder (png, jpeg, webp) — never image.Decode, whose registry is whatever the binary happens to import, so a GIF or BMP decoder linked in by some other package would widen what is accepted without a line changing here.

🔴 THE SIZE IS CHECKED BEFORE ANYTHING IS DECODED. A 1 MiB file can declare a 65535×65535 image whose pixels compress to nothing; decoding it would ask for 16 GiB. So the header is read first (DecodeConfig, which allocates no pixels), and the width, height and pixel count are refused there.

🔴 WHAT IS STORED IS RE-ENCODED, NEVER THE UPLOAD. The decoded pixels are drawn onto a fresh canvas and written as a new PNG, so nothing of the file but its pixels survives: no EXIF, no text chunks, no ICC profile, no bytes appended after the image (a PNG/HTML polyglot loses its HTML).

Index

Constants

View Source
const (
	// MaxUploadBytes is the largest upload accepted: 1 MiB.
	MaxUploadBytes = 1 << 20
	// MaxSide is the widest and tallest image accepted, in pixels.
	MaxSide = 4096
	// MaxPixels is the most pixels accepted (width × height): 2048×2048.
	MaxPixels = 2048 * 2048
	// LogoMaxSide is the longest side of the stored logo: a larger upload is
	// scaled down to fit (never up). The header draws it about 28px tall; this
	// keeps a crisp copy for high-density screens without storing megabytes.
	LogoMaxSide = 1024
	// Icon192 and Icon512 are the square icons' sides.
	Icon192 = 192
	Icon512 = 512
)

The limits Process enforces.

🔴 MaxPixels IS THE MEMORY BOUND, AND IT IS SET BY MEASUREMENT. Decoding and scaling cost memory per pixel, whatever the file's size: at the old cap of 16,000,000 pixels a progressive CMYK JPEG of a few hundred KB peaked at 560-580 MB RSS, past the server's 512 MiB limit. At 2048×2048 (4,194,304) the worst accepted input measured ~119 MB peak RSS (the figures are on processMu). A logo is stored at most LogoMaxSide (1024) on a side, so this still takes a 2× source.

⚠ MaxSide STAYS ABOVE √MaxPixels SO THE PIXEL GUARD IS REACHABLE. Were the sides capped at 2048 too, no image could exceed MaxPixels and the guard would never run. At 4096 a wide 4096×1024 banner is accepted, and a 4096×1025 or 2049×2048 image passes the side check and is refused here (TestThePixelCapIs2048Squared).

View Source
const MaxNameRunes = 64

MaxNameRunes is the longest display name, in characters (Unicode code points — what Postgres's char_length counts, so the column's CHECK and this agree).

View Source
const ThemeAuditAction = "instance.theme.changed"

ThemeAuditAction is the audit_log action of a theme change.

Variables

View Source
var (
	ErrEmpty       = &Error{"empty", "the file is empty"}
	ErrTooLarge    = &Error{"too_large", "the file is larger than 1 MiB"}
	ErrSVG         = &Error{"svg_refused", "SVG is not accepted: upload a PNG, JPEG or WebP"}
	ErrUnsupported = &Error{"unsupported_type", "the file is not a PNG, JPEG or WebP image"}
	ErrCorrupt     = &Error{"corrupt", "the image could not be read: it is truncated or damaged"}
	ErrDimensions  = &Error{"too_wide", "the image is wider or taller than 4096 pixels"}
	ErrPixels      = &Error{"too_many_pixels", "the image has more than 4,194,304 pixels (2048×2048)"}
)

The refusals. Each is a distinct value, so a caller (and a test) can tell which guard refused: errors.Is(err, ErrSVG).

View Source
var (
	ErrNameInvalid = &Error{"name_invalid", "the name is not valid UTF-8"}
	ErrNameTooLong = &Error{"name_too_long", "the name is longer than 64 characters"}
	ErrNameControl = &Error{"name_control", "the name contains a control, text-direction or invisible formatting character"}
)

The name's refusals.

View Source
var (
	ErrThemePreset   = &Error{"theme_preset_unknown", "there is no such theme preset"}
	ErrThemeAccent   = &Error{"theme_accent_invalid", "an accent colour is written #RRGGBB"}
	ErrThemeContrast = &Error{"theme_contrast", "those colours do not meet the contrast table, so text or controls would be hard to read"}
)

The theme's refusals, as branding Errors so the routes answer them with their own codes. A theme that fails the contrast table is refused with a *ThemeContrastError, which unwraps to ErrThemeContrast.

View Source

ErrAppIconNeedsLogo refuses an app icon on a server with no logo: the app icon replaces the logo's icons, it is not a brand on its own.

View Source
var ErrEmptyChange = errors.New("branding: a change must set or clear something (and not both set and clear the logo or the app icon)")

ErrEmptyChange refuses a Change that changes nothing, and one that both sets and clears the logo or the app icon, or sets an app icon while clearing the logo.

View Source
var ErrNotAdmin = errors.New("branding: only an admin may change the instance's branding")

ErrNotAdmin refuses a write whose ctx is not an admin's (authz.IsAdmin).

Functions

func CheckNameText added in v0.224.0

func CheckNameText(s string) error

CheckNameText is THE character rule for a name a person reads as a name: the instance's name (NormalizeName) and every name stored in users.display_name — a person's (users.RedeemInvite) and a team's (users.CreateTeam). It refuses invalid UTF-8 (ErrNameInvalid: a byte that is not text cannot be stored, and Postgres would refuse it only after a writer had done its other work), and, as a *NameCharError naming the first one,

  • a control character: C0 (U+0000–U+001F, tab and newline included), DEL and C1 (U+007F–U+009F) — unicode.IsControl;
  • an invisible or text-direction formatting character (nameFormatChar).

It checks characters only: trimming, emptiness and length are each caller's. One rule, one place: a writer of a name calls this, never a copy of it (internal/users' TestEveryDisplayNameWriterIsChecked holds the writers to it).

func CheckTheme added in v0.267.0

func CheckTheme(c theme.Choice) (theme.Choice, error)

CheckTheme is c normalised (theme.Choice.Normalize) once it has passed the contrast table, or the refusal that names why not. It is the save's one gate: Next calls it, so every store refuses what it refuses.

func ETagOf

func ETagOf(logo, icon192, icon512 []byte) string

ETagOf is the ETag of a stored logo and icons (Images.ETag).

func NormalizeName

func NormalizeName(raw string) (string, error)

NormalizeName is raw as stored: trimmed of surrounding white space, or refused. An empty result (raw was empty or only white space) means "clear the name", and is not an error.

🔴 TEXT-DIRECTION CONTROLS ARE REFUSED WITH THE C0/C1 CONTROLS. The name is drawn beside the logo on every page and on the sign-in form, where a right-to-left override (U+202E) could make it read as something it is not.

Types

type AppIcon added in v0.200.0

type AppIcon struct {
	Icon192 []byte
	Icon512 []byte
}

AppIcon is an accepted app-icon upload as muster stores it: the two square icons drawn from it, in place of the ones drawn from the logo. Nothing else of it is kept.

func ProcessIcon added in v0.200.0

func ProcessIcon(data []byte) (AppIcon, error)

ProcessIcon is Process for an app icon: the same checks in the same order (the same errors), the same decode onto a fresh canvas, and the same square icons — scaled to fit and centred on a transparent background, nothing cropped — drawn from the upload (scaled to LogoMaxSide first, as the logo is) instead of from the logo.

type Branding

type Branding struct {
	Name string

Branding is the instance's branding as stored. A zero value is "nothing set": no name and no logo.

func (b Branding) HasLogo() bool

HasLogo reports whether a logo is set.

type Change

type Change struct {
	// Name, when set, replaces the name; "" clears it. It must already be
	// NormalizeName's output.
	Name *string
	// icon already set stays, and is still what is served.
	Logo *Images
	// exclusive, and so are it and AppIcon.
	ClearLogo bool
	// AppIcon, when set, replaces the served icons with these. It needs a
	// logo: one already set, or Logo in the same change (ErrAppIconNeedsLogo).
	AppIcon *AppIcon
	// ClearAppIcon removes the app icon, so the logo's icons are served
	// again. It and AppIcon are exclusive.
	ClearAppIcon bool
	// LogoShowsName, when set, replaces Branding.LogoShowsName.
	LogoShowsName *bool
	// Theme, when set, replaces the theme. 🔴 It is refused unless it passes
	// the contrast table (theme.Choice.Validate): Next checks it, so no store
	// can write a theme that fails it, whatever route sent it.
	Theme *theme.Choice
}

Change is one write. A nil field is left as it is.

func (Change) Empty added in v0.200.0

func (c Change) Empty() bool

Empty reports whether c changes nothing or contradicts itself (ErrEmptyChange).

type Error

type Error struct {
	Code string
	Msg  string
}

Error is one reason an upload or a name is refused. Code is stable (the API returns it, and a client may branch on it); Msg is for a person.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

Is lets errors.Is match an *Error by its Code, so the unsupported-type refusal (which carries the sniffed type in its message) still matches ErrUnsupported.

type Images

type Images struct {
	Icon192 []byte
	Icon512 []byte
	// ETag is the first 16 hex digits of the sha256 over the three PNGs. It
	// changes whenever any of them does, so it is the cache key everywhere:
	// the /brand/ responses' ETag, the ?v= on every link to them, and the
	// field the apps compare.
	ETag string
}

Images is an accepted upload as muster stores and serves it: the logo and two square icons, each a PNG, and the ETag that names all three.

func Process

func Process(data []byte) (Images, error)

Process validates data and returns the images to store, or one of the errors above. The checks run in this order, each before any work the next one guards: size, signature, header (DecodeConfig), sides, pixel count, decode, re-encode.

type NameCharError added in v0.224.0

type NameCharError struct{ Rune rune }

NameCharError is CheckNameText's refusal: the first refused character. It is ErrNameControl (errors.Is, and errors.As to *Error), so a caller that answers with the code and message keeps working; Rune names the character, for a sentence that says what was refused.

func (*NameCharError) Error added in v0.224.0

func (e *NameCharError) Error() string

func (*NameCharError) Unwrap added in v0.224.0

func (e *NameCharError) Unwrap() error

type PG

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

PG is Store on instance_branding (migrations 0105, 0114).

func NewPG

func NewPG(pool *pgxpool.Pool) *PG

NewPG is Store over pool.

func (*PG) Apply

func (p *PG) Apply(ctx context.Context, c Change) (Branding, error)

Apply implements Store: it reads the row FOR UPDATE, applies c in Go (Stored.Next), and writes the whole row back with the ETag of what it now serves (Stored.Resolve), in one transaction.

🔴 IT CHECKS THE CALLER IS AN ADMIN BEFORE IT ACTS AS THE SYSTEM. The write needs the system scope (row-level security admits nothing else), and a store that elevated any caller would make the route's admin check the only one; this keeps a handler wired without it from writing.

func (*PG) Load

func (p *PG) Load(ctx context.Context) (Branding, error)

Load implements Store. It reads as authz.System: the branding is the server's, read on the sign-in page and by the public /brand/ images, where there is no user, and row-level security admits only the system scope (0105).

type Store

type Store interface {
	// Load is the branding; a zero Branding when nothing was ever set.
	Load(ctx context.Context) (Branding, error)
	// Apply writes c as the admin ctx names, and returns the result.
	Apply(ctx context.Context, c Change) (Branding, error)
}

Store keeps the instance's branding.

type Stored added in v0.200.0

type Stored struct {
	Name                           string

Stored is the row as written: the logo's own icons and the app icon's kept apart. Resolve is the Branding it serves.

func (Stored) Next added in v0.200.0

func (s Stored) Next(c Change) (Stored, error)

Next is s with c applied, or ErrAppIconNeedsLogo, or a theme refusal (CheckTheme). It is the one statement of what each Change field does, used by PG.Apply (and by test stores).

func (Stored) Resolve added in v0.200.0

func (s Stored) Resolve() Branding

Resolve is the Branding s serves: the app icon's icons when it has one, the logo's otherwise, and the ETag over the logo and THOSE icons.

type ThemeContrastError added in v0.267.0

type ThemeContrastError struct{ Failures []string }

ThemeContrastError refuses a theme that fails the contrast table, naming each failing pair (theme.Failures).

func (*ThemeContrastError) Error added in v0.267.0

func (e *ThemeContrastError) Error() string

func (*ThemeContrastError) Unwrap added in v0.267.0

func (e *ThemeContrastError) Unwrap() error

Jump to

Keyboard shortcuts

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