Jelajahi Sumber

fix: list HMAC webhook auth types in one place

The unsigned-argument check and its tests repeated the HMAC auth type
names, and DevSkim flagged each mention of the older hash as a weak
hash (DS126858). The names now live in config.HmacWebhookAuthTypes, on
one line with a DevSkim suppression that explains the older hash is
only accepted to check signatures from senders that still use it. A
test checks that every listed type has a verifier.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 1 hari lalu
induk
melakukan
24bccc6db9

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

@@ -112,6 +112,16 @@ func IsKnownWebhookTemplate(name string) bool {
 	return slices.Contains(WebhookTemplateNames, name)
 }
 
+// HmacWebhookAuthTypes sign only the request body. The older of the two hash
+// functions is only accepted so that OliveTin can check signatures from
+// senders that still use it, such as older GitHub webhooks; OliveTin does not
+// use it for anything else.
+var HmacWebhookAuthTypes = []string{"hmac-sha256", "hmac-sha1"} // DevSkim: ignore DS126858
+
+func IsHmacWebhookAuthType(authType string) bool {
+	return slices.Contains(HmacWebhookAuthTypes, authType)
+}
+
 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"

+ 3 - 6
service/internal/configcheck/rebuild.go

@@ -650,14 +650,11 @@ func actionHasBodyOnlySignedWebhook(action *config.Action) bool {
 }
 
 func webhookSignsBodyOnly(webhook config.WebhookConfig) bool {
-	switch webhook.AuthType {
-	case "hmac-sha256", "hmac-sha1":
-		return true
-	case "":
+	if webhook.AuthType == "" {
 		return config.IsKnownWebhookTemplate(webhook.Template)
-	default:
-		return false
 	}
+
+	return config.IsHmacWebhookAuthType(webhook.AuthType)
 }
 
 func isUnsignedWebhookArgument(name string) bool {

+ 5 - 2
service/internal/configcheck/webhook_unsigned_args_test.go

@@ -46,8 +46,11 @@ func TestRebuildWarnsForUnsignedArgumentsOnHmacWebhook(t *testing.T) {
 	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)
+func TestRebuildWarnsForUnsignedArgumentsOnEveryHmacAuthTypeAndTemplate(t *testing.T) {
+	for _, authType := range config.HmacWebhookAuthTypes {
+		assert.Len(t, unsignedArgIssues(config.WebhookConfig{AuthType: authType, Secret: "s"}, "webhook_query"), 1, authType)
+	}
+
 	assert.Len(t, unsignedArgIssues(config.WebhookConfig{Template: "github-push", Secret: "s"}, "webhook_query"), 1)
 }
 

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

@@ -87,3 +87,12 @@ func TestKnownWebhookTemplateNamesMatchTemplateAppliers(t *testing.T) {
 		assert.True(t, config.IsKnownWebhookTemplate(name), "template %q must be listed in config.WebhookTemplateNames", name)
 	}
 }
+
+func TestHmacWebhookAuthTypesAreVerified(t *testing.T) {
+	for _, authType := range config.HmacWebhookAuthTypes {
+		verifier := NewAuthVerifier(config.WebhookConfig{AuthType: authType, Secret: "s3cret"})
+
+		assert.NotNil(t, verifier.getVerifier(), "HMAC auth type %q must have a verifier", authType)
+		assert.False(t, verifier.Verify(unauthenticatedWebhookRequest(t), []byte(`{}`)), "unsigned request must be rejected for %q", authType)
+	}
+}