Quellcode durchsuchen

fix: report unknown webhook templates and only exempt known ones

A webhook with any template was exempt from the authType check, so a
misspelt template name, which sets no authType, gave no Diagnostics
error while every request was rejected. Only known templates are now
exempt, and an unknown template name is reported as a
webhook_template_unknown error. The known template names live in the
config package, with a test that keeps them in step with the webhook
template appliers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread vor 1 Tag
Ursprung
Commit
4746cd785a

+ 1 - 1
docs/modules/ROOT/pages/action_execution/onwebhook.adoc

@@ -230,7 +230,7 @@ OliveTin supports several authentication methods to verify webhook requests:
 [#every-webhook-needs-an-authtype]
 === Every webhook needs an `authType`
 
-Every webhook must set an `authType`. A webhook without one is rejected, and the xref:advanced_configuration/diagnostics.adoc[Diagnostics] page shows an error for it (code `webhook_auth_type_unset`). Webhooks that use a GitHub `template` choose their own `authType`. See xref:upgrade/upgrade_notes.adoc#webhook-auth-type-required[Upgrade Notes].
+Every webhook must set an `authType`. A webhook without one is rejected, and the xref:advanced_configuration/diagnostics.adoc[Diagnostics] page shows an error for it (code `webhook_auth_type_unset`). Webhooks that use one of the xref:action_execution/onwebhook_github.adoc[GitHub templates] choose their own `authType`. A `template` name that OliveTin does not know is ignored, and Diagnostics shows an error for it (code `webhook_template_unknown`). See xref:upgrade/upgrade_notes.adoc#webhook-auth-type-required[Upgrade Notes].
 
 `matchHeaders`, `matchQuery` and `matchPath` only decide which action a request is for. They are not secrets, so use one of the authentication methods below unless anyone who can reach `/webhooks` may run the action. Webhook-triggered actions run as the `system` usergroup, not as the caller (see xref:security/acl.adoc#acls-for-triggers[ACLs for triggers]).
 

+ 16 - 0
service/internal/config/config.go

@@ -2,6 +2,7 @@ package config
 
 import (
 	"fmt"
+	"slices"
 )
 
 // ReservedArgumentNamePrefix is reserved for OliveTin-injected system arguments.
@@ -96,6 +97,21 @@ type RateSpec struct {
 // unauthenticated requests.
 const WebhookAuthTypeNone = "none"
 
+// WebhookTemplateNames are the webhook templates OliveTin knows. Each one sets
+// its own authType, so a webhook that uses one does not need an authType in
+// the config.
+var WebhookTemplateNames = []string{
+	"github-push",
+	"github-pr",
+	"github-pull-request",
+	"github-release",
+	"github-workflow",
+}
+
+func IsKnownWebhookTemplate(name string) bool {
+	return slices.Contains(WebhookTemplateNames, name)
+}
+
 type WebhookConfig struct {
 	Secret        string            `koanf:"secret"`        // Optional: secret for signature verification
 	AuthType      string            `koanf:"authType"`      // Required unless set by a template: "hmac-sha256", "hmac-sha1", "bearer", "basic", "none"

+ 26 - 9
service/internal/configcheck/rebuild.go

@@ -590,20 +590,37 @@ func collectWebhookAuthIssues(cfg *config.Config) []configissues.Issue {
 	return out
 }
 
-// Webhook templates choose their own authType, so only webhooks without a
-// template need one in the config.
+// Known webhook templates choose their own authType, so only webhooks without
+// one need an authType in the config. An unknown template name sets nothing,
+// so it is reported, and does not excuse a missing authType.
 func webhookAuthIssuesForAction(action *config.Action) []configissues.Issue {
 	out := make([]configissues.Issue, 0)
 
 	for index, webhook := range action.ExecOnWebhook {
-		if webhook.AuthType != "" || webhook.Template != "" {
-			continue
-		}
-
-		out = append(out, actionIssue(action, configissues.SeverityError, configissues.CodeWebhookAuthTypeUnset,
-			"Webhook has no authType, so its requests are rejected. Set an authType such as hmac-sha256 or bearer, or authType: none to allow unauthenticated requests",
-			fmt.Sprintf("execOnWebhook[%d]", index), ""))
+		source := fmt.Sprintf("execOnWebhook[%d]", index)
+		out = append(out, webhookTemplateIssues(action, webhook, source)...)
+		out = append(out, webhookAuthTypeIssues(action, webhook, source)...)
 	}
 
 	return out
 }
+
+func webhookTemplateIssues(action *config.Action, webhook config.WebhookConfig, source string) []configissues.Issue {
+	if webhook.Template == "" || config.IsKnownWebhookTemplate(webhook.Template) {
+		return nil
+	}
+
+	return []configissues.Issue{actionIssue(action, configissues.SeverityError, configissues.CodeWebhookTemplateUnknown,
+		fmt.Sprintf("Webhook template %q is not known, so it is ignored. Known templates are: %s", webhook.Template, strings.Join(config.WebhookTemplateNames, ", ")),
+		source, "")}
+}
+
+func webhookAuthTypeIssues(action *config.Action, webhook config.WebhookConfig, source string) []configissues.Issue {
+	if webhook.AuthType != "" || config.IsKnownWebhookTemplate(webhook.Template) {
+		return nil
+	}
+
+	return []configissues.Issue{actionIssue(action, configissues.SeverityError, configissues.CodeWebhookAuthTypeUnset,
+		"Webhook has no authType, so its requests are rejected. Set an authType such as hmac-sha256 or bearer, or authType: none to allow unauthenticated requests",
+		source, "")}
+}

+ 26 - 0
service/internal/configcheck/webhook_auth_test.go

@@ -47,3 +47,29 @@ func TestRebuildReportsEachWebhookWithoutAuthType(t *testing.T) {
 	assert.Equal(t, "execOnWebhook[0]", issues[0].Source)
 	assert.Equal(t, "execOnWebhook[2]", issues[1].Source)
 }
+
+func TestRebuildReportsUnknownWebhookTemplateWithoutAuthType(t *testing.T) {
+	webhook := config.WebhookConfig{Template: "github-pus", Secret: "s3cret"}
+
+	assert.Len(t, webhookAuthIssues(webhook), 1, "a misspelt template must not excuse a missing authType")
+
+	templateIssues := issuesWithCode(configissues.List(), configissues.CodeWebhookTemplateUnknown)
+	require.Len(t, templateIssues, 1)
+	assert.Contains(t, templateIssues[0].Message, "github-pus")
+	assert.Contains(t, templateIssues[0].Message, "github-push")
+	assert.Equal(t, "execOnWebhook[0]", templateIssues[0].Source)
+}
+
+func TestRebuildReportsUnknownWebhookTemplateEvenWithAuthType(t *testing.T) {
+	webhook := config.WebhookConfig{Template: "gitlab-push", AuthType: "bearer", Secret: "token"}
+
+	assert.Empty(t, webhookAuthIssues(webhook))
+	assert.Len(t, issuesWithCode(configissues.List(), configissues.CodeWebhookTemplateUnknown), 1)
+}
+
+func TestRebuildAcceptsEveryKnownWebhookTemplate(t *testing.T) {
+	for _, name := range config.WebhookTemplateNames {
+		assert.Empty(t, webhookAuthIssues(config.WebhookConfig{Template: name, Secret: "s3cret"}), name)
+		assert.Empty(t, issuesWithCode(configissues.List(), configissues.CodeWebhookTemplateUnknown), name)
+	}
+}

+ 1 - 0
service/internal/configissues/issue.go

@@ -26,6 +26,7 @@ const (
 	CodeOnClickUnknown         = "onclick_unknown"
 	CodeDashboardTypeUnknown   = "dashboard_type_unknown"
 	CodeWebhookAuthTypeUnset   = "webhook_auth_type_unset"
+	CodeWebhookTemplateUnknown = "webhook_template_unknown"
 )
 
 // Issue is a configuration warning or error surfaced on Diagnostics.

+ 13 - 0
service/internal/webhooks/auth_type_required_test.go

@@ -74,3 +74,16 @@ func TestWebhookHandlerDoesNotRunActionWithoutAuthType(t *testing.T) {
 func TestWebhookHandlerRunsActionWithAuthTypeNone(t *testing.T) {
 	assert.Equal(t, 1, webhookExecutionCount(t, config.WebhookAuthTypeNone))
 }
+
+func TestKnownWebhookTemplateNamesMatchTemplateAppliers(t *testing.T) {
+	for _, name := range config.WebhookTemplateNames {
+		webhook := config.WebhookConfig{Template: name}
+		ApplyGitHubTemplate(&webhook, name)
+
+		assert.NotEmpty(t, webhook.AuthType, "known template %q must set an authType", name)
+	}
+
+	for name := range templateAppliers() {
+		assert.True(t, config.IsKnownWebhookTemplate(name), "template %q must be listed in config.WebhookTemplateNames", name)
+	}
+}

+ 8 - 3
service/internal/webhooks/github.go

@@ -15,15 +15,20 @@ func ApplyGitHubTemplate(cfg *config.WebhookConfig, template string) {
 
 type templateApplier func(*config.WebhookConfig)
 
-func getTemplateApplier(template string) templateApplier {
-	templateMap := map[string]templateApplier{
+// The names here must match config.WebhookTemplateNames, which the config
+// check uses to decide whether a webhook needs its own authType.
+func templateAppliers() map[string]templateApplier {
+	return map[string]templateApplier{
 		"github-push":         applyGitHubPushTemplate,
 		"github-pr":           applyGitHubPRTemplate,
 		"github-pull-request": applyGitHubPRTemplate,
 		"github-release":      applyGitHubReleaseTemplate,
 		"github-workflow":     applyGitHubWorkflowTemplate,
 	}
-	return templateMap[template]
+}
+
+func getTemplateApplier(template string) templateApplier {
+	return templateAppliers()[template]
 }
 
 func setDefaultAuth(cfg *config.WebhookConfig) {