webhook

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package webhook verifies GitHub webhook deliveries.

GitHub signs each webhook delivery with HMAC-SHA256 over the raw request body using the secret configured on the webhook. This package verifies the X-Hub-Signature-256 header in constant time and exposes an http.Handler middleware for ergonomic integration.

See https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries.

Index

Constants

View Source
const (
	SignatureHeader = "X-Hub-Signature-256"
	EventHeader     = "X-GitHub-Event"
	DeliveryHeader  = "X-GitHub-Delivery"
)

Header names GitHub sets on every webhook delivery.

View Source
const DefaultMaxPayloadSize int64 = 25 * 1024 * 1024

DefaultMaxPayloadSize matches GitHub's documented 25 MiB delivery cap.

Variables

View Source
var (
	ErrMissingSignature       = errors.New("webhook: missing signature header")
	ErrInvalidSignatureFormat = errors.New("webhook: invalid signature format")
	ErrSignatureMismatch      = errors.New("webhook: signature mismatch")
)

Sentinel errors returned by Verify. Callers can branch with errors.Is.

Functions

func Middleware

func Middleware(secret []byte, opts ...MiddlewareOpt) func(http.Handler) http.Handler

Middleware returns net/http middleware that verifies the signature header against secret before invoking next. Failed verifications short-circuit with 401 Unauthorized; bodies larger than the configured cap return 413. The request body is restored for downstream handlers.

func Verify

func Verify(secret, body []byte, signature string) error

Verify reports whether signature is a valid HMAC-SHA256 of body using secret. signature must be in GitHub's "sha256=<hex>" form, as delivered in the X-Hub-Signature-256 header. Comparison runs in constant time.

Types

type MiddlewareOpt

type MiddlewareOpt func(*middlewareConfig)

MiddlewareOpt configures Middleware.

func WithErrorHandler

func WithErrorHandler(fn func(http.ResponseWriter, *http.Request, error)) MiddlewareOpt

WithErrorHandler overrides how verification failures are reported. The default writes 401 Unauthorized (or 413 for oversized bodies) with no body.

func WithMaxPayloadSize

func WithMaxPayloadSize(n int64) MiddlewareOpt

WithMaxPayloadSize overrides the request body size cap. A non-positive value disables the cap, which is not recommended in production.

Jump to

Keyboard shortcuts

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