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

fix: check required justifications on StartActionByGet and ignore a client provider header (#1189)

James Read 1 день назад
Родитель
Сommit
051f20db18

+ 17 - 0
.github/SECURITY_ADVISORY_DUPLICATES.md

@@ -183,6 +183,23 @@ Previous decisions, for reference:
 
 ---
 
+## Required justification skipped
+
+**Canonical:** [GHSA-5jwv-37px-7hmc](https://github.com/OliveTin/OliveTin/security/advisories/GHSA-5jwv-37px-7hmc) — reporter **Khattak761** (2026-09-04)
+
+Ways to run an action that requires a justification without giving one. Not accepted as a vulnerability: the justification is a note in the logs, not an access control. The user already has `exec` on the action, and is still logged under their own username.
+
+| GHSA | Reporter | Status | Notes |
+|------|----------|--------|-------|
+| GHSA-5jwv-37px-7hmc | Khattak761 | closed (canonical) | `StartActionByGet` / `StartActionByGetAndWait` skip the check |
+| GHSA-cwqr-6xvr-ch27 | joostgrunwald | closed | Same endpoints, plus a client-supplied `provider: system` header with trusted-header auth |
+
+**Fix (public, as a bug):** the `StartActionByGet*` methods check the justification like `StartAction`, and trusted-header auth no longer reads a `provider` header.
+
+**Why not accepted:** no change to who can run what; ACLs still match the real username and usergroup. Close new reports here unless they show a justification bypass that also grants a permission the user did not have.
+
+---
+
 ## Easy to confuse (not duplicates)
 
 | Topic | Advisories | Distinction |

+ 2 - 0
docs/modules/ROOT/pages/api/method_StartActionByGet.adoc

@@ -7,6 +7,8 @@
 Despite its name, this method no longer supports `GET` requests. The OliveTin 2k style of putting the action ID in the URL (eg: `GET /api/StartActionByGet/pingGithub`) returns `404` in OliveTin 3k, and is not coming back.
 ====
 
+This method cannot send a justification. If the action requires one, the call is refused with `invalid_argument`, unless the action's justification template fills it in. Use `StartAction` with a `justification` instead. See xref:upgrade/upgrade_notes.adoc#start-action-by-get-justification[Upgrade Notes].
+
 == Migrating to `StartAction`
 
 `StartActionByGet` takes only an `actionId`, and returns an Execution Tracking ID. `StartAction` does the same with `bindingId` - for a normal action, the binding ID is the same as the action `id` from your `config.yaml` (see xref:api/start_action.adoc#api-ids[Action IDs vs Binding IDs]).

+ 2 - 0
docs/modules/ROOT/pages/api/method_StartActionByGetAndWait.adoc

@@ -7,6 +7,8 @@
 Despite its name, this method no longer supports `GET` requests. The OliveTin 2k style of putting the action ID in the URL (eg: `GET /api/StartActionByGetAndWait/pingGithub`) returns `404` in OliveTin 3k, and is not coming back.
 ====
 
+This method cannot send a justification. If the action requires one, the call is refused with `invalid_argument`, unless the action's justification template fills it in. Use `StartActionAndWait` with a `justification` instead. See xref:upgrade/upgrade_notes.adoc#start-action-by-get-justification[Upgrade Notes].
+
 == Migrating to `StartActionAndWait`
 
 `StartActionAndWait` takes the same `actionId` field, and returns the same Log Entry, so you only need to change the method name. It also accepts `arguments` and a `justification`.

+ 2 - 0
docs/modules/ROOT/pages/security/trusted_header.adoc

@@ -24,6 +24,8 @@ The value of `X-Username` and `X-Usergroup` can be whatever you like, as long as
 
 NOTE: You *must* set `authHttpHeaderUsername` to some value, even if you only intend to use `authHttpHeaderUserGroup`, otherwise usergroups will be ignored.
 
+Users logged in this way always have the provider `trusted-header`. OliveTin does not read the provider from a request header. See xref:upgrade/upgrade_notes.adoc#trusted-header-provider[Upgrade Notes].
+
 == Multiple usergroups
 
 OliveTin will automatically detect multiple usergroups in the `authHttpHeaderUserGroup` header if they are separated by a space. You can also set a configuration option to use a different separator string with `authHttpHeaderUserGroupSep`. For example, if you set `authHttpHeaderUserGroupSep` to `,`, then the header `X-Usergroup: group1,group2` will be interpreted as two usergroups: `group1` and `group2`.

+ 12 - 0
docs/modules/ROOT/pages/upgrade/upgrade_notes.adoc

@@ -54,6 +54,18 @@ OliveTin now keeps up to 2000 execution logs in memory, and removes the oldest f
 
 If you want a longer history in the web interface, raise `logHistoryLimit` in your config. Logs saved to disk with `saveLogs` are not affected. See xref:logs/saving.adoc#log-history-limit[How many logs are kept in memory].
 
+[#start-action-by-get-justification]
+=== `StartActionByGet` and `StartActionByGetAndWait` now check required justifications
+
+The deprecated `StartActionByGet` and `StartActionByGetAndWait` API methods used to run actions that require a justification without one. They now refuse them with "justification is required for this action", in the same way as `StartAction` and `StartActionAndWait`, unless the action's justification template fills one in. These methods cannot send a justification, so if a script calls them for such an action, change it to call `StartAction` or `StartActionAndWait` with a `justification`. See xref:api/method_StartActionByGetAndWait.adoc[StartActionByGetAndWait].
+
+[#trusted-header-provider]
+=== Trusted header users always have the provider `trusted-header`
+
+Users logged in with trusted header authentication (`authHttpHeaderUsername`) now always have the provider `trusted-header`. OliveTin used to copy a `provider` request header into the user's provider, which let a client claim to be a `system` user and skip a required justification.
+
+This affects you if something reads the provider from the `WhoAmI` or `Init` API for these users. Change it to expect `trusted-header`. It also affects you if a client sends a `provider: system` header to call `StartAction` or `StartActionAndWait` for an action that requires a justification, without sending one. Those calls now fail with "justification is required for this action", unless the action's justification template fills one in. Send a `justification` with these calls. `RestartAction` is not affected, because it always reuses the justification stored with the original run.
+
 == 2024.08.14
 
 === Navigation change - Subpaths no longer supported

+ 18 - 23
service/internal/api/api.go

@@ -331,16 +331,17 @@ func (api *oliveTinAPI) StartActionByGet(ctx ctx.Context, req *connect.Request[a
 	}
 
 	user := auth.UserFromApiCall(ctx, req, api.cfg)
-	if err := api.errUnlessStartEntityAccessAllowed(user, binding, map[string]string{}); err != nil {
+	args := make(map[string]string)
+	justification, err := api.startByGetJustification(user, binding, args)
+	if err != nil {
 		return nil, err
 	}
 
-	args := make(map[string]string)
-
 	execReq := executor.ExecutionRequest{
 		Binding:           binding,
 		TrackingID:        uuid.NewString(),
 		Arguments:         args,
+		Justification:     justification,
 		AuthenticatedUser: user,
 		Cfg:               api.cfg,
 	}
@@ -352,27 +353,19 @@ func (api *oliveTinAPI) StartActionByGet(ctx ctx.Context, req *connect.Request[a
 	}), nil
 }
 
-func (api *oliveTinAPI) runBindingAndWait(binding *executor.ActionBinding, args map[string]string, user *authpublic.AuthenticatedUser) (*executor.InternalLogEntry, bool) {
-	execReq := executor.ExecutionRequest{
-		Binding:           binding,
-		TrackingID:        uuid.NewString(),
-		Arguments:         args,
-		AuthenticatedUser: user,
-		Cfg:               api.cfg,
+// The deprecated StartActionByGet methods take no arguments or justification, so they only
+// run an action that requires a justification when its justification template resolves to text.
+func (api *oliveTinAPI) startByGetJustification(user *authpublic.AuthenticatedUser, binding *executor.ActionBinding, args map[string]string) (string, error) {
+	if err := api.errUnlessStartEntityAccessAllowed(user, binding, args); err != nil {
+		return "", err
 	}
 
-	wg, _ := api.executor.ExecRequest(&execReq)
-	wg.Wait()
-
-	return api.executor.GetLog(execReq.TrackingID)
-}
-
-func (api *oliveTinAPI) startActionByGetAndWaitLogEntry(binding *executor.ActionBinding, user *authpublic.AuthenticatedUser) (*apiv1.LogEntry, error) {
-	internalLogEntry, ok := api.runBindingAndWait(binding, map[string]string{}, user)
-	if !ok {
-		return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("execution not found"))
+	justification := resolveStartJustification(binding.Action, binding, "", args)
+	if err := validateJustificationRequired(binding.Action, justification, user); err != nil {
+		return "", connectInvalidJustification(err)
 	}
-	return api.logEntryForAllowedViewer(internalLogEntry, user)
+
+	return justification, nil
 }
 
 func (api *oliveTinAPI) StartActionByGetAndWait(ctx ctx.Context, req *connect.Request[apiv1.StartActionByGetAndWaitRequest]) (*connect.Response[apiv1.StartActionByGetAndWaitResponse], error) {
@@ -384,11 +377,13 @@ func (api *oliveTinAPI) StartActionByGetAndWait(ctx ctx.Context, req *connect.Re
 	}
 
 	user := auth.UserFromApiCall(ctx, req, api.cfg)
-	if err := api.errUnlessStartEntityAccessAllowed(user, binding, map[string]string{}); err != nil {
+	args := make(map[string]string)
+	justification, err := api.startByGetJustification(user, binding, args)
+	if err != nil {
 		return nil, err
 	}
 
-	logEntry, err := api.startActionByGetAndWaitLogEntry(binding, user)
+	logEntry, err := api.startActionAndWaitLogEntry(binding, args, justification, user)
 	if err != nil {
 		return nil, err
 	}

+ 77 - 0
service/internal/api/api_justification_test.go

@@ -10,6 +10,7 @@ import (
 	"github.com/stretchr/testify/require"
 
 	apiv1 "github.com/OliveTin/OliveTin/gen/olivetin/api/v1"
+	apiv1connect "github.com/OliveTin/OliveTin/gen/olivetin/api/v1/apiv1connect"
 	"github.com/OliveTin/OliveTin/internal/auth"
 	config "github.com/OliveTin/OliveTin/internal/config"
 	"github.com/OliveTin/OliveTin/internal/executor"
@@ -164,3 +165,79 @@ func TestValidateJustificationRequiredAllowsSystemUser(t *testing.T) {
 	err := validateJustificationRequired(action, "", auth.UserFromSystem(cfg, "cron"))
 	require.NoError(t, err)
 }
+
+func newJustificationRequiredTestServer(t *testing.T, cfg *config.Config) (apiv1connect.OliveTinApiServiceClient, func()) {
+	t.Helper()
+
+	cfg.Actions = append(cfg.Actions, &config.Action{
+		Title:         "Approve change",
+		ID:            "approve",
+		Justification: config.JustificationRequiredNoTemplate,
+		Shell:         "echo approved",
+	})
+
+	ex := executor.DefaultExecutor(cfg)
+	ex.RebuildActionMap()
+
+	ts, client := getNewTestServerAndClientWithExecutor(cfg, ex)
+	return client, ts.Close
+}
+
+func TestStartActionByGetRequiresJustification(t *testing.T) {
+	client, closeServer := newJustificationRequiredTestServer(t, config.DefaultConfig())
+	defer closeServer()
+
+	//nolint:staticcheck // Deliberately calling the deprecated RPC.
+	_, err := client.StartActionByGet(context.Background(), connect.NewRequest(&apiv1.StartActionByGetRequest{ActionId: "approve"}))
+	require.Error(t, err)
+	assert.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))
+}
+
+func TestStartActionByGetAndWaitRequiresJustification(t *testing.T) {
+	client, closeServer := newJustificationRequiredTestServer(t, config.DefaultConfig())
+	defer closeServer()
+
+	//nolint:staticcheck // Deliberately calling the deprecated RPC.
+	_, err := client.StartActionByGetAndWait(context.Background(), connect.NewRequest(&apiv1.StartActionByGetAndWaitRequest{ActionId: "approve"}))
+	require.Error(t, err)
+	assert.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))
+}
+
+func TestStartActionByGetUsesJustificationTemplate(t *testing.T) {
+	cfg := config.DefaultConfig()
+	cfg.Actions = append(cfg.Actions, &config.Action{
+		Title:         "Templated",
+		ID:            "templated",
+		Justification: "Scheduled maintenance",
+		Shell:         "echo hi",
+	})
+
+	ex := executor.DefaultExecutor(cfg)
+	ex.RebuildActionMap()
+	ts, client := getNewTestServerAndClientWithExecutor(cfg, ex)
+	defer ts.Close()
+
+	//nolint:staticcheck // Deliberately calling the deprecated RPC.
+	resp, err := client.StartActionByGet(context.Background(), connect.NewRequest(&apiv1.StartActionByGetRequest{ActionId: "templated"}))
+	require.NoError(t, err)
+
+	waitForLogJustification(t, ex, resp.Msg.ExecutionTrackingId, "Scheduled maintenance")
+}
+
+func TestTrustedHeaderProviderSystemDoesNotSkipJustification(t *testing.T) {
+	cfg := config.DefaultConfig()
+	cfg.AuthHttpHeaderUsername = "X-Username"
+	cfg.AuthHttpHeaderUserGroup = "X-Usergroup"
+
+	client, closeServer := newJustificationRequiredTestServer(t, cfg)
+	defer closeServer()
+
+	req := connect.NewRequest(&apiv1.StartActionAndWaitRequest{ActionId: "approve"})
+	req.Header().Set("X-Username", "alice")
+	req.Header().Set("X-Usergroup", "operators")
+	req.Header().Set("provider", "system")
+
+	_, err := client.StartActionAndWait(context.Background(), req)
+	require.Error(t, err)
+	assert.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))
+}

+ 2 - 3
service/internal/auth/trusted-headers.go

@@ -18,9 +18,8 @@ func checkUserFromHeaders(context *types.AuthCheckingContext) *types.Authenticat
 		u.UsergroupLine = getHeaderKeyOrEmpty(context.Request.Header, context.Config.AuthHttpHeaderUserGroup)
 	}
 
-	if prov := getHeaderKeyOrEmpty(context.Request.Header, "provider"); prov != "" {
-		u.Provider = prov
-	}
+	u.Provider = "trusted-header"
+
 	return u
 }
 

+ 28 - 0
service/internal/auth/trusted-headers_test.go

@@ -0,0 +1,28 @@
+package auth
+
+import (
+	"net/http"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+
+	types "github.com/OliveTin/OliveTin/internal/auth/authpublic"
+	"github.com/OliveTin/OliveTin/internal/config"
+)
+
+func TestCheckUserFromHeadersIgnoresClientProviderHeader(t *testing.T) {
+	cfg := config.DefaultConfig()
+	cfg.AuthHttpHeaderUsername = "X-Username"
+
+	header := http.Header{}
+	header.Set("X-Username", "alice")
+	header.Set("provider", "system")
+
+	user := checkUserFromHeaders(&types.AuthCheckingContext{
+		Request: &http.Request{Header: header},
+		Config:  cfg,
+	})
+
+	assert.Equal(t, "alice", user.Username)
+	assert.Equal(t, "trusted-header", user.Provider)
+}