Files
archie/internal/channels/webhook/webhook.go
T
sam 543b657472 feat(webhookguard): shared HMAC, rate-limit, and redaction mechanics
archie-core-7d5u.3 -- storage-agnostic building blocks for vetting inbound
webhook-shaped requests, extracted rather than duplicated:

- VerifyHMAC: behavior-preserving extraction of internal/channels/webhook's
  private validateHMAC. internal/channels/webhook now calls it instead of
  keeping its own copy; all 10 of that package's existing tests still pass
  unmodified, and the one now-redundant unit test was removed with a
  comment pointing to where its coverage relocated.
- RateLimiter: per-source token bucket. Source IDs come from a registered,
  operator-controlled set, not unbounded attacker input, so the bucket map
  is not itself bounded -- documented explicitly rather than left implicit.
- RedactPayload: key-name-based redaction, matching gateway/stream.go's
  sensitiveParameterKey marker list. Deliberately does NOT reuse that
  function's actual behavior: it replaces every string value regardless of
  key, correct for a bounded chat summary and wrong here -- a captured
  payload exists to be inspected for field mapping (t2db.3), so only values
  under a sensitive-looking key are touched.

Narrowed from 7d5u.3's original scope, which bundled capture storage and an
approval state machine into this package. Both moved to the beads that own
that state (t2db.1's capture store depends on 7d5u.2's still-open NATS
storage decision; t2db.4's binding is the only consumer of approval state
today) rather than building shared abstractions ahead of a second real
consumer.

Adversarially reviewed by a fresh subagent with zero surviving findings:
token-bucket math hand-traced on two scenarios and race-clean under -race,
the redaction/gateway-stream.go behavioral divergence verified by reading
both functions side by side, both the redaction and rate-limit guards
mutation-tested by disabling each and confirming the suite catches it.

task check exit 0, golangci-lint 0 issues, gofumpt clean.
2026-08-22 01:28:58 +10:00

250 lines
6.2 KiB
Go

// Package webhook implements gateway.Gateway for inbound HTTP webhooks.
// External services push messages into archie-core's gateway for LLM
// processing. Outbound delivery is not supported (deliver-only mode).
package webhook
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"strings"
"sync"
"time"
"github.com/samcharles93/archie-core/internal/channels"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/webhookguard"
)
// Gateway is a deliver-only webhook gateway. It listens for HTTP POST
// requests, validates their HMAC signatures, and routes the extracted
// text through the gateway.Router for LLM processing.
type Gateway struct {
Host string
Port int
Routes []RouteConfig
log *slog.Logger
server *http.Server
router *gateway.Router
mu sync.Mutex
}
// RouteConfig defines one webhook endpoint.
type RouteConfig struct {
// Path is the URL path (e.g. "/github").
Path string
// Secret is the HMAC-SHA256 secret for payload validation.
// Empty = no validation.
Secret string
// Template extracts the message text from the JSON payload.
// Uses dot-notation: "issue.title" or "comment.body".
// Empty = use the raw body as message text.
Template string
// DeliverTo routes replies: "origin" (HTTP response) or empty (no reply).
DeliverTo string
}
// New returns an unstarted Gateway. Call Start to begin listening.
func New(host string, port int, routes []RouteConfig, log *slog.Logger) *Gateway {
if host == "" {
host = "0.0.0.0"
}
return &Gateway{
Host: host,
Port: port,
Routes: routes,
log: log.With("component", "gateway-webhook"),
}
}
func (g *Gateway) Name() string { return "webhook" }
// Start begins listening on the configured host:port. Blocks until ctx
// is cancelled.
func (g *Gateway) Start(ctx context.Context, router *gateway.Router) error {
g.mu.Lock()
g.router = router
mux := http.NewServeMux()
for i := range g.Routes {
route := g.Routes[i] // capture
mux.HandleFunc(route.Path, g.handleWebhook(&route))
}
addr := fmt.Sprintf("%s:%d", g.Host, g.Port)
g.server = &http.Server{Addr: addr, Handler: mux}
g.mu.Unlock()
g.log.Info("webhook gateway listening", "addr", addr)
go func() {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()
_ = g.server.Shutdown(shutdownCtx)
}()
err := g.server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
return fmt.Errorf("webhook: listen: %w", err)
}
return nil
}
// Stop gracefully shuts down the HTTP server.
func (g *Gateway) Stop(ctx context.Context) error {
g.mu.Lock()
srv := g.server
g.mu.Unlock()
if srv == nil {
return nil
}
return srv.Shutdown(ctx)
}
// WebhookHandler returns the HTTP handler for testing.
func (g *Gateway) WebhookHandler() http.Handler {
mux := http.NewServeMux()
for i := range g.Routes {
route := g.Routes[i]
mux.HandleFunc(route.Path, g.handleWebhook(&route))
}
return mux
}
func (g *Gateway) handleWebhook(route *RouteConfig) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
body, err := io.ReadAll(io.LimitReader(r.Body, 1<<20)) // 1 MB
if err != nil {
g.log.Error("webhook read body", "err", err)
http.Error(w, "read error", http.StatusBadRequest)
return
}
// HMAC validation.
if route.Secret != "" {
sig := r.Header.Get("X-Hub-Signature-256")
if sig == "" {
sig = r.Header.Get("X-Signature-256")
}
if !webhookguard.VerifyHMAC(body, sig, route.Secret) {
g.log.Warn("webhook invalid signature", "path", route.Path)
http.Error(w, "invalid signature", http.StatusUnauthorized)
return
}
}
// Extract message text.
text := extractText(body, route.Template)
if text == "" {
http.Error(w, "empty message", http.StatusBadRequest)
return
}
// Route through the gateway.
g.mu.Lock()
router := g.router
g.mu.Unlock()
if router == nil {
http.Error(w, "not started", http.StatusServiceUnavailable)
return
}
msg := gateway.Message{
ChannelID: route.Path,
From: "webhook",
Text: text,
}
reply, err := router.Route(r.Context(), msg)
if err != nil {
g.log.Error("webhook route", "err", err, "path", route.Path)
http.Error(w, "route error", http.StatusInternalServerError)
return
}
if route.DeliverTo == "origin" && reply != "" {
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write([]byte(reply))
} else {
w.WriteHeader(http.StatusAccepted)
}
}
}
// ConfigSchema returns the JSON Schema for the webhook channel config.
func (g *Gateway) ConfigSchema() json.RawMessage {
return json.RawMessage(`{
"type": "object",
"required": ["host", "port"],
"properties": {
"host": {
"type": "string",
"description": "Listen address (e.g. 0.0.0.0)"
},
"port": {
"type": "integer",
"description": "Listen port (e.g. 8644)"
},
"routes": {
"type": "array",
"items": {
"type": "object",
"properties": {
"path": { "type": "string" },
"secret": { "type": "string" },
"template": { "type": "string" }
}
}
}
}
}`)
}
// ValidateConfig checks the webhook channel configuration.
func (g *Gateway) ValidateConfig(cfg map[string]any) error {
if cfg == nil {
return fmt.Errorf("webhook config is required")
}
if host, _ := cfg["host"].(string); host == "" {
return fmt.Errorf("webhook.host is required")
}
return nil
}
// Compile-time guard.
var _ channels.Channel = (*Gateway)(nil)
// extractText pulls a value from a JSON body using dot-notation path.
// "issue.title" → body["issue"]["title"]. Empty path = raw body.
func extractText(body []byte, path string) string {
if path == "" {
return strings.TrimSpace(string(body))
}
var data map[string]any
if err := json.Unmarshal(body, &data); err != nil {
return strings.TrimSpace(string(body))
}
parts := strings.Split(path, ".")
var current any = data
for _, p := range parts {
m, ok := current.(map[string]any)
if !ok {
return ""
}
current = m[p]
}
if s, ok := current.(string); ok {
return strings.TrimSpace(s)
}
return ""
}