Sfoglia il codice sorgente

feat: warn when HMAC webhook actions use unsigned request arguments

HMAC webhook signatures only cover the request body, but the request
path, query string and headers are passed to webhook_path,
webhook_query and webhook_header_* arguments. Diagnostics now warns
(webhook_unsigned_argument) when an action with an HMAC webhook,
including the GitHub templates, declares one of those arguments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 1 giorno fa
parent
commit
de40724693

+ 55 - 0
service/internal/configcheck/rebuild.go

@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"fmt"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
+	"slices"
 	"strings"
 	"strings"
 
 
 	"github.com/OliveTin/OliveTin/internal/config"
 	"github.com/OliveTin/OliveTin/internal/config"
@@ -41,6 +42,7 @@ func Rebuild(cfg *config.Config, extra ...configissues.Issue) {
 	collected = append(collected, collectWatcherPathIssues(cfg)...)
 	collected = append(collected, collectWatcherPathIssues(cfg)...)
 	collected = append(collected, collectDashboardTypeIssues(cfg)...)
 	collected = append(collected, collectDashboardTypeIssues(cfg)...)
 	collected = append(collected, collectWebhookAuthIssues(cfg)...)
 	collected = append(collected, collectWebhookAuthIssues(cfg)...)
+	collected = append(collected, collectWebhookUnsignedArgumentIssues(cfg)...)
 	collected = append(collected, extra...)
 	collected = append(collected, extra...)
 	configissues.Replace(collected)
 	configissues.Replace(collected)
 }
 }
@@ -624,3 +626,56 @@ func webhookAuthTypeIssues(action *config.Action, webhook config.WebhookConfig,
 		"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",
 		"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, "")}
 		source, "")}
 }
 }
+
+func collectWebhookUnsignedArgumentIssues(cfg *config.Config) []configissues.Issue {
+	out := make([]configissues.Issue, 0)
+
+	for _, action := range cfg.Actions {
+		if action == nil || !actionHasBodyOnlySignedWebhook(action) {
+			continue
+		}
+
+		out = append(out, unsignedWebhookArgumentIssues(action)...)
+	}
+
+	return out
+}
+
+// HMAC webhook signatures, including the GitHub templates, only cover the
+// request body. The path, query string and headers that OliveTin passes to
+// webhook_path, webhook_query and webhook_header_* arguments are not signed,
+// so anyone who has captured one signed request can change them.
+func actionHasBodyOnlySignedWebhook(action *config.Action) bool {
+	return slices.ContainsFunc(action.ExecOnWebhook, webhookSignsBodyOnly)
+}
+
+func webhookSignsBodyOnly(webhook config.WebhookConfig) bool {
+	switch webhook.AuthType {
+	case "hmac-sha256", "hmac-sha1":
+		return true
+	case "":
+		return config.IsKnownWebhookTemplate(webhook.Template)
+	default:
+		return false
+	}
+}
+
+func isUnsignedWebhookArgument(name string) bool {
+	return name == "webhook_path" || name == "webhook_query" || strings.HasPrefix(name, "webhook_header_")
+}
+
+func unsignedWebhookArgumentIssues(action *config.Action) []configissues.Issue {
+	out := make([]configissues.Issue, 0)
+
+	for _, arg := range action.Arguments {
+		if !isUnsignedWebhookArgument(arg.Name) {
+			continue
+		}
+
+		out = append(out, actionIssue(action, configissues.SeverityWarning, configissues.CodeWebhookUnsignedArg,
+			"Argument comes from the webhook request's path, query string or headers, which HMAC webhook signatures do not cover, so its value is not authenticated. Take values you need to trust from the request body with extract instead",
+			arg.Name, arg.Name))
+	}
+
+	return out
+}

+ 70 - 0
service/internal/configcheck/webhook_unsigned_args_test.go

@@ -0,0 +1,70 @@
+package configcheck_test
+
+import (
+	"testing"
+
+	"github.com/OliveTin/OliveTin/internal/config"
+	"github.com/OliveTin/OliveTin/internal/configcheck"
+	"github.com/OliveTin/OliveTin/internal/configissues"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+func unsignedArgIssues(webhook config.WebhookConfig, argNames ...string) []configissues.Issue {
+	configissues.BeginConfigLoad()
+
+	args := make([]config.ActionArgument, 0, len(argNames))
+	for _, name := range argNames {
+		args = append(args, config.ActionArgument{Name: name, Type: "ascii_identifier"})
+	}
+
+	cfg := config.DefaultConfig()
+	cfg.Actions = []*config.Action{{
+		ID:            "deploy",
+		Title:         "Deploy",
+		Exec:          []string{"true"},
+		Arguments:     args,
+		ExecOnWebhook: []config.WebhookConfig{webhook},
+	}}
+
+	configcheck.Rebuild(cfg)
+
+	return issuesWithCode(configissues.List(), configissues.CodeWebhookUnsignedArg)
+}
+
+func hmacWebhook() config.WebhookConfig {
+	return config.WebhookConfig{AuthType: "hmac-sha256", Secret: "s3cret"}
+}
+
+func TestRebuildWarnsForUnsignedArgumentsOnHmacWebhook(t *testing.T) {
+	issues := unsignedArgIssues(hmacWebhook(), "webhook_header_x-target", "webhook_query", "webhook_path", "branch")
+
+	require.Len(t, issues, 3)
+	names := []string{issues[0].ArgumentName, issues[1].ArgumentName, issues[2].ArgumentName}
+	assert.ElementsMatch(t, []string{"webhook_header_x-target", "webhook_query", "webhook_path"}, names)
+	assert.Equal(t, configissues.SeverityWarning, issues[0].Severity)
+	assert.Contains(t, issues[0].Message, "extract")
+}
+
+func TestRebuildWarnsForUnsignedArgumentsOnHmacSha1AndTemplateWebhooks(t *testing.T) {
+	assert.Len(t, unsignedArgIssues(config.WebhookConfig{AuthType: "hmac-sha1", Secret: "s"}, "webhook_query"), 1)
+	assert.Len(t, unsignedArgIssues(config.WebhookConfig{Template: "github-push", Secret: "s"}, "webhook_query"), 1)
+}
+
+func TestRebuildDoesNotWarnForArgumentsTakenFromTheBody(t *testing.T) {
+	assert.Empty(t, unsignedArgIssues(hmacWebhook(), "branch", "webhook_method"))
+}
+
+func TestRebuildDoesNotWarnForWebhooksThatAuthenticateTheWholeRequest(t *testing.T) {
+	for _, webhook := range []config.WebhookConfig{
+		{AuthType: "bearer", Secret: "token"},
+		{AuthType: "basic", Secret: "user:pass"},
+		{AuthType: config.WebhookAuthTypeNone},
+	} {
+		assert.Empty(t, unsignedArgIssues(webhook, "webhook_query"), webhook.AuthType)
+	}
+}
+
+func TestRebuildDoesNotTreatUnknownWebhookTemplateAsHmac(t *testing.T) {
+	assert.Empty(t, unsignedArgIssues(config.WebhookConfig{Template: "github-pus", Secret: "s"}, "webhook_query"))
+}

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

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