From 1fe6b9af48679813e88f788c4592e65643ea6fd4 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 10:22:54 +1000 Subject: [PATCH] feat(channels): wire chat.webhook route config through to the gateway internal/channels/webhook's RouteConfig implements and tests HMAC signature validation, payload template extraction, and reply delivery, but setupWebhookGateway built []webhook.RouteConfig{{Path: "/webhook"}} literally -- no config field could reach Secret/Template/DeliverTo, so the gateway accepted any unsigned POST despite its package doc claiming signature validation. Adds config.ChatConfig.Webhook (WebhookRoute: path, secret as the existing SecretRef pattern, template, deliver_to) and webhookRoutes(), the pure translation into []webhook.RouteConfig that setupWebhookGateway now calls. A secret ref that fails to resolve logs and falls back to no validation rather than refusing to start, since signature validation stays opt-in (an empty secret is documented, valid config) unlike the mandatory forge webhook secret. --- config.example.toml | 23 ++++++++-- internal/app/archied/bootstrap.go | 25 ++++++++++- internal/app/archied/webhook_route_test.go | 50 ++++++++++++++++++++++ internal/config/config.go | 30 ++++++++++++- 4 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 internal/app/archied/webhook_route_test.go diff --git a/config.example.toml b/config.example.toml index deaa28e4e..275da0d33 100644 --- a/config.example.toml +++ b/config.example.toml @@ -160,11 +160,26 @@ class = "ollama" # relay_addr = "smtp.example.com:587" # outbound relay for replies # Generic inbound webhook gateway: external services push messages into -# chat for LLM processing (deliver-only, no outbound reply channel). This -# is a distinct surface from [forge].webhook_addr (forge issue events) and -# [capture] (unbound event storage for later binding) -- three different -# inbound HTTP concerns that happen to share the word "webhook". +# chat for LLM processing. This is a distinct surface from +# [forge].webhook_addr (forge issue events) and [capture] (unbound event +# storage for later binding) -- three different inbound HTTP concerns that +# happen to share the word "webhook". # webhook_addr = "0.0.0.0:8646" # under [chat], not [chat.telegram] +# +# [chat.webhook] configures the single route the gateway serves. Every +# field is optional; an absent section runs with the pre-existing +# behaviour: path "/webhook", no signature validation, raw body as message +# text, no reply. +# [chat.webhook] +# path = "/webhook" +# secret = { engine = "bws", key = "CHAT_WEBHOOK_SECRET" } # verifies +# # X-Hub-Signature-256 on inbound payloads. Empty (the default) accepts +# # any unsigned POST -- set this to make the gateway actually verify +# # what it claims to. +# template = "issue.title" # dot-notation extraction of the message text +# # from the JSON payload. Empty uses the raw body. +# deliver_to = "origin" # "origin" writes the reply as the HTTP +# # response; empty (the default) sends no reply. # Per-(channel, sender) inbound rate limiting, shared by every chat channel: # Telegram keys by numeric user ID, email by SMTP from address, webhook by diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 8e4e256a7..535c14d73 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -697,9 +697,14 @@ func (b *boot) setupWebhookGateway(ctx context.Context, cfg config.Config, log * return } host, port := parseListenAddr(cfg.Chat.WebhookAddr, "0.0.0.0", 8644) + secretValue, err := b.secrets.Resolve(cfg.Chat.Webhook.Secret) + if err != nil { + log.Error("webhook gateway secret unresolvable; starting with signature validation disabled", + "engine", cfg.Chat.Webhook.Secret.Engine, "key", cfg.Chat.Webhook.Secret.Key, "err", err) + } wh := webhook.New( host, port, - []webhook.RouteConfig{{Path: "/webhook"}}, + webhookRoutes(cfg.Chat.Webhook, secretValue), log, ) whRouter := gateway.NewRouter(b.stateStore, nil, "webhook") @@ -722,6 +727,24 @@ func (b *boot) setupWebhookGateway(ctx context.Context, cfg config.Config, log * }) } +// webhookRoutes translates the configured [chat.webhook] route into the +// gateway's RouteConfig. It is the composition site config.WebhookRoute +// exists for: the HMAC/template/deliver-to fields have been implemented and +// tested in internal/channels/webhook since it was written, reachable only +// through this translation. +func webhookRoutes(route config.WebhookRoute, secretValue string) []webhook.RouteConfig { + path := route.Path + if path == "" { + path = "/webhook" + } + return []webhook.RouteConfig{{ + Path: path, + Secret: secretValue, + Template: route.Template, + DeliverTo: route.DeliverTo, + }} +} + // loadWorkflows builds the workflow registry from the skill catalog. // Plugin-defined workflows override built-ins of the same name; // built-ins fill gaps. diff --git a/internal/app/archied/webhook_route_test.go b/internal/app/archied/webhook_route_test.go new file mode 100644 index 000000000..a13e51fa9 --- /dev/null +++ b/internal/app/archied/webhook_route_test.go @@ -0,0 +1,50 @@ +package archied + +import ( + "testing" + + "github.com/samcharles93/archie-core/internal/config" +) + +// TestWebhookRoutesAppliesConfiguredFields is the red case for +// channels-platform-3: RouteConfig.Secret/Template/DeliverTo are +// implemented and tested in internal/channels/webhook, but the only +// production construction (setupWebhookGateway) built +// []webhook.RouteConfig{{Path: "/webhook"}} literally, so no config value +// could ever reach them and HMAC validation never ran. webhookRoutes is the +// translation site; this pins that a configured [chat.webhook] route +// actually reaches the gateway's RouteConfig. +func TestWebhookRoutesAppliesConfiguredFields(t *testing.T) { + route := config.WebhookRoute{ + Path: "/github", + Template: "issue.title", + DeliverTo: "origin", + } + got := webhookRoutes(route, "shhh") + if len(got) != 1 { + t.Fatalf("webhookRoutes() returned %d routes, want 1", len(got)) + } + want := got[0] + if want.Path != "/github" { + t.Errorf("Path = %q, want %q", want.Path, "/github") + } + if want.Secret != "shhh" { + t.Errorf("Secret = %q, want %q", want.Secret, "shhh") + } + if want.Template != "issue.title" { + t.Errorf("Template = %q, want %q", want.Template, "issue.title") + } + if want.DeliverTo != "origin" { + t.Errorf("DeliverTo = %q, want %q", want.DeliverTo, "origin") + } +} + +// TestWebhookRoutesDefaultsPath proves an unset route path keeps the +// gateway's pre-existing default of "/webhook" rather than becoming +// unreachable. +func TestWebhookRoutesDefaultsPath(t *testing.T) { + got := webhookRoutes(config.WebhookRoute{}, "") + if len(got) != 1 || got[0].Path != "/webhook" { + t.Fatalf("webhookRoutes(zero value) = %+v, want Path=/webhook", got) + } +} diff --git a/internal/config/config.go b/internal/config/config.go index d3638604e..92dc1504b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -910,8 +910,14 @@ type ChatConfig struct { Email EmailConfig `toml:"email" yaml:"email"` // WebhookAddr is the host:port for the inbound webhook gateway. // Empty disables the webhook gateway. - WebhookAddr string `toml:"webhook_addr" yaml:"webhook_addr"` - Telegram TelegramConfig `toml:"telegram" yaml:"telegram"` + WebhookAddr string `toml:"webhook_addr" yaml:"webhook_addr"` + // Webhook configures the single route the inbound webhook gateway + // serves: its path, HMAC validation, payload text extraction, and + // reply delivery. The gateway implementation (internal/channels/webhook) + // has carried these fields since it was written; this is what lets a + // config value reach them. + Webhook WebhookRoute `toml:"webhook" yaml:"webhook"` + Telegram TelegramConfig `toml:"telegram" yaml:"telegram"` // RateLimit budgets inbound messages per (channel, sender) across // every chat channel. Zero MaxRequests (the default) leaves rate // limiting off entirely -- it is an opt-in control, not a default @@ -972,6 +978,26 @@ type EmailConfig struct { RelayAddr string `toml:"relay_addr" yaml:"relay_addr"` } +// WebhookRoute configures the inbound webhook gateway's single route. +// Every field is optional: an absent [chat.webhook] section runs the +// gateway exactly as before this existed -- default path, no signature +// validation, raw body as message text, no reply. +type WebhookRoute struct { + // Path is the URL path the gateway listens on. Empty means "/webhook". + Path string `toml:"path" yaml:"path"` + // Secret is the HMAC-SHA256 secret validating inbound payloads. Empty + // (the zero SecretRef) disables signature validation, so any unsigned + // POST is accepted -- set this to make the gateway actually verify + // what it claims to. + Secret SecretRef `toml:"secret" yaml:"secret"` + // Template extracts the message text from the JSON payload via + // dot-notation (e.g. "issue.title"). Empty uses the raw body. + Template string `toml:"template" yaml:"template"` + // DeliverTo routes replies: "origin" sends the reply back as the HTTP + // response; empty sends no reply. + DeliverTo string `toml:"deliver_to" yaml:"deliver_to"` +} + // Notify configures outbound notifications (n8n webhook → email etc.). type Notify struct { // Webhook receives JSON POSTs for events that need a human (e.g.