Просмотр исходного кода

security: GHSA-gjm6-x9mg-qxpj (MED) keep webhook shell restrictions across triggers

Triggered actions were rebuilt with only the "trigger" tag, dropping the
"webhook" tag, so shell: and shellAfterCompleted actions reached through
triggers from a webhook action bypassed the webhook shell guard.

Triggered requests now inherit the webhook tag, so the guard applies at
every depth of a trigger chain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 2 дней назад
Родитель
Сommit
51f10e9295

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

@@ -7,7 +7,7 @@ OliveTin provides a dedicated webhook endpoint at `/webhooks` that can receive w
 
 == Basic Configuration
 
-To configure an action to run on a webhook, add the `execOnWebhook` property to your action. Webhook-triggered actions **must** use `exec:` (not `shell:` or `shellAfterCompleted`).
+To configure an action to run on a webhook, add the `execOnWebhook` property to your action. Webhook-triggered actions **must** use `exec:` (not `shell:` or `shellAfterCompleted`). This also applies to any action started through `triggers` from a webhook-triggered action, at any depth of the trigger chain.
 
 [source,yaml]
 .`config.yaml`

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

@@ -19,7 +19,7 @@ User-supplied **argument** values are type-checked (and some types are blocked w
 
 Those values are substituted into `shell` / `shellAfterCompleted` as-is. OliveTin assumes they are **server-controlled** (entity files and the OliveTin process environment under the operator's control). The author of the config is responsible for ensuring that data is trustworthy, or for using `exec` and careful quoting when it might not be.
 
-Webhooks cannot use `shell:` or `shellAfterCompleted`; webhook-triggered actions must use `exec:` only. See xref:action_execution/onwebhook.adoc[Execute on webhook].
+Webhooks cannot use `shell:` or `shellAfterCompleted`; webhook-triggered actions, and any actions they start through `triggers`, must use `exec:` only. See xref:action_execution/onwebhook.adoc[Execute on webhook].
 
 The way that you specify these two types of execution is different - `shell` expects a single string, while `exec` expects a list of strings (the first being the command, the rest being the arguments).
 

+ 13 - 1
service/internal/executor/executor.go

@@ -1482,7 +1482,7 @@ func triggerLoop(req *ExecutionRequest) {
 		trigger := &ExecutionRequest{
 			Binding:           binding,
 			TrackingID:        uuid.NewString(),
-			Tags:              []string{"trigger"},
+			Tags:              triggeredRequestTags(req),
 			AuthenticatedUser: req.AuthenticatedUser,
 			Arguments:         req.Arguments,
 			Cfg:               req.Cfg,
@@ -1494,6 +1494,18 @@ func triggerLoop(req *ExecutionRequest) {
 	}
 }
 
+// Triggered actions inherit the webhook tag so that the webhook shell
+// restrictions apply to the whole trigger chain, not just the first action.
+func triggeredRequestTags(req *ExecutionRequest) []string {
+	tags := []string{"trigger"}
+
+	if hasWebhookTag(req) {
+		tags = append(tags, "webhook")
+	}
+
+	return tags
+}
+
 func stepSaveLog(req *ExecutionRequest) bool {
 	if !canSaveExecutionLog(req) {
 		log.Warnf("Cannot save execution log; missing request, log entry, binding/action, or config")

+ 138 - 0
service/internal/executor/executor_test.go

@@ -3,6 +3,7 @@ package executor
 import (
 	"os"
 	"path/filepath"
+	"slices"
 	"strings"
 	"testing"
 	"time"
@@ -1240,3 +1241,140 @@ func TestRebuildActionMapSkipsNilActions(t *testing.T) {
 	assert.NotPanics(t, e.RebuildActionMap)
 	assert.Len(t, e.MapActionBindings, 1)
 }
+
+type finishedExecution struct {
+	title    string
+	output   string
+	exitCode int32
+	tags     []string
+}
+
+type executionResultCollector struct {
+	ch chan finishedExecution
+}
+
+func (c *executionResultCollector) OnExecutionStarted(_ *InternalLogEntry) {}
+
+func (c *executionResultCollector) OnExecutionFinished(entry *InternalLogEntry) {
+	c.ch <- finishedExecution{
+		title:    entry.ActionTitle,
+		output:   entry.Output,
+		exitCode: entry.ExitCode,
+		tags:     slices.Clone(entry.Tags),
+	}
+}
+
+func (c *executionResultCollector) OnOutputChunk(_ []byte, _ string) {}
+
+func (c *executionResultCollector) OnActionMapRebuilt() {}
+
+func runWebhookTriggerChain(t *testing.T, actions []*config.Action, expectedExecutions int) map[string]finishedExecution {
+	t.Helper()
+
+	cfg := config.DefaultConfig()
+	e := DefaultExecutor(cfg)
+	cfg.Actions = append(cfg.Actions, actions...)
+	cfg.Sanitize()
+	e.RebuildActionMap()
+
+	results := make(chan finishedExecution, expectedExecutions+2)
+	e.AddListener(&executionResultCollector{ch: results})
+
+	req := &ExecutionRequest{
+		Tags:              []string{"webhook"},
+		AuthenticatedUser: auth.UserFromSystem(cfg, "webhook"),
+		Cfg:               cfg,
+		Arguments:         map[string]string{"msg": "hello"},
+		Binding:           e.FindBindingWithNoEntity(actions[0]),
+	}
+	wg, _ := e.ExecRequest(req)
+	wg.Wait()
+
+	got := make(map[string]finishedExecution)
+	for i := range expectedExecutions {
+		select {
+		case result := <-results:
+			got[result.title] = result
+		case <-time.After(2 * time.Second):
+			t.Fatalf("timed out waiting for execution %d; got %v", i+1, got)
+		}
+	}
+
+	return got
+}
+
+func TestWebhookTriggerRejectsShellExecution(t *testing.T) {
+	marker := filepath.Join(t.TempDir(), "triggered-shell-ran")
+
+	got := runWebhookTriggerChain(t, []*config.Action{
+		{
+			Title:    "Webhook entry",
+			Exec:     []string{"echo", "entry"},
+			Triggers: []string{"Triggered shell"},
+		},
+		{
+			Title: "Triggered shell",
+			Shell: "touch " + marker,
+		},
+	}, 2)
+
+	triggered := got["Triggered shell"]
+	assert.Equal(t, int32(-1337), triggered.exitCode)
+	assert.Contains(t, triggered.output, "webhooks cannot use Shell execution")
+	assert.Contains(t, triggered.tags, "trigger")
+	assert.Contains(t, triggered.tags, "webhook")
+	assert.NoFileExists(t, marker, "triggered shell command must not run")
+}
+
+func TestWebhookTriggerRejectsShellExecutionAtDepth(t *testing.T) {
+	marker := filepath.Join(t.TempDir(), "nested-shell-ran")
+
+	got := runWebhookTriggerChain(t, []*config.Action{
+		{
+			Title:    "Webhook entry",
+			Exec:     []string{"echo", "entry"},
+			Triggers: []string{"Middle exec"},
+		},
+		{
+			Title:    "Middle exec",
+			Exec:     []string{"echo", "middle"},
+			Triggers: []string{"Nested shell"},
+		},
+		{
+			Title: "Nested shell",
+			Shell: "touch " + marker,
+		},
+	}, 3)
+
+	nested := got["Nested shell"]
+	assert.Equal(t, int32(-1337), nested.exitCode)
+	assert.Contains(t, nested.output, "webhooks cannot use Shell execution")
+	assert.NoFileExists(t, marker, "nested triggered shell command must not run")
+}
+
+func TestWebhookTriggerRejectsShellAfterCompleted(t *testing.T) {
+	marker := filepath.Join(t.TempDir(), "triggered-after-ran")
+
+	got := runWebhookTriggerChain(t, []*config.Action{
+		{
+			Title:    "Webhook entry",
+			Exec:     []string{"echo", "entry"},
+			Triggers: []string{"Triggered after completed"},
+		},
+		{
+			Title:               "Triggered after completed",
+			Exec:                []string{"echo", "triggered"},
+			ShellAfterCompleted: "touch " + marker,
+		},
+	}, 2)
+
+	assert.Contains(t, got["Triggered after completed"].output, "webhooks cannot use shellAfterCompleted")
+	assert.NoFileExists(t, marker, "triggered shellAfterCompleted must not run")
+}
+
+func TestTriggeredRequestTags(t *testing.T) {
+	assert.Equal(t, []string{"trigger"}, triggeredRequestTags(&ExecutionRequest{}))
+	assert.Equal(t, []string{"trigger"}, triggeredRequestTags(&ExecutionRequest{Tags: []string{"trigger"}}))
+	assert.Equal(t, []string{"trigger", "webhook"}, triggeredRequestTags(&ExecutionRequest{Tags: []string{"webhook"}}))
+	assert.Equal(t, []string{"trigger", "webhook"}, triggeredRequestTags(&ExecutionRequest{Tags: []string{"trigger", "webhook"}}))
+}