webhook

package
v0.0.0-...-bd25097 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package webhook delivers repository events to outside URLs, signed with HMAC-SHA256 (D78).

Index

Constants

View Source
const Cursor = "webhook"

Cursor is the outbox reader name of the dispatcher.

View Source
const (

	// Tick is how often serve looks for new events when nothing woke it; retry timing comes from webhook.retry_seconds.
	Tick = 5 * time.Second
)

Internal batch sizes: how much one tick reads and sends. Not product values; the limits a person tunes are settings.

Variables

View Source
var (
	ErrBadURL  = errors.New("webhook: URL must be http(s)://host/... without user or password")
	ErrPrivate = errors.New("webhook: address is private, loopback or link-local")
)

Errors a form maps to messages.

View Source
var Kinds = []string{"push", "issues", "issue_comment", "pull_request", "pull_request_review", "pull_request_review_comment", "status", "wiki"}

Kinds are the event kinds a webhook can subscribe to, in form order. Field names follow GitHub's where the meaning is the same (full_name, html_url, ref/before/after, number), so existing receivers need little change.

Functions

func GUID

func GUID() string

GUID is a random delivery id (X-Codr-Delivery).

func ParseHosts

func ParseHosts(v string) []string

ParseHosts reads the comma list of webhook.allow_hosts.

func Sign

func Sign(secret string, body []byte) string

Sign is the value of X-Codr-Signature-256 (and X-Hub-Signature-256): "sha256=" + hex HMAC of the exact body.

func ValidKinds

func ValidKinds(in []string) []string

ValidKinds keeps the known kinds of a form submission, in canonical order; empty means "everything".

func Verify

func Verify(secret string, body []byte, header string) bool

Verify checks a signature header in constant time (for receivers written in Go, and for the tests).

Types

type Builder

type Builder struct {
	Store    *store.Store
	HTTPHost string // forge base URL, e.g. https://git.codr.my
	SSHHost  string
	Git      func(repoID int64) gitread.Repo
	// PushCommits caps the commits listed in a push payload (setting webhook.push_commits).
	PushCommits int
}

Builder turns an outbox event into the JSON body a webhook receives.

func (Builder) Event

func (b Builder) Event(ctx context.Context, r store.Repo, e store.Event) ([]byte, error)

Event builds the body for one outbox event. The event's own data (a snapshot taken when it happened) is placed under the key its kind names; push events get their commit list here, from the repository on disk.

func (Builder) Ping

func (b Builder) Ping(ctx context.Context, r store.Repo, w store.Webhook, actorID int64) ([]byte, error)

Ping is the body sent when a webhook is created or tested.

type Dispatcher

type Dispatcher struct {
	Store   *store.Store
	Key     secret.Key // auth.secret_key: unseals each webhook's signing secret
	Builder Builder
	Log     *slog.Logger
	// Client overrides the HTTP client (tests); nil = a client built from the settings with the address guard.
	Client *http.Client
	// contains filtered or unexported fields
}

Dispatcher fans outbox events out to webhooks and delivers them. One runs inside `serve`.

func (*Dispatcher) Kick

func (d *Dispatcher) Kick()

Kick asks the loop to run now instead of at the next tick. It never blocks.

func (*Dispatcher) Run

func (d *Dispatcher) Run(ctx context.Context, tick time.Duration)

Run ticks until ctx ends.

func (*Dispatcher) Tick

func (d *Dispatcher) Tick(ctx context.Context) error

Tick fans out new events and makes every due attempt once.

type Policy

type Policy struct {
	AllowPrivate bool     // setting webhook.allow_private
	AllowHosts   []string // setting webhook.allow_hosts: host names that may resolve to private addresses
}

Policy decides which addresses a webhook may reach. Anyone who administers a repository can add a webhook, so without it a webhook is a way to make this server call its own neighbours (the LLM router, the database, cloud metadata). The check runs on the address actually dialled, after DNS, so a name that resolves to 127.0.0.1 or changes its answer between check and dial is still refused.

func PolicyFrom

func PolicyFrom(ctx context.Context, st *store.Store) Policy

PolicyFrom reads the address rules from the settings.

func (Policy) CheckURL

func (p Policy) CheckURL(raw string) (*url.URL, error)

CheckURL validates a webhook URL before it is saved: scheme, host, no credentials, and a literal IP must pass the address rule. Names are checked again at every dial.

Jump to

Keyboard shortcuts

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