mirror of
https://gitea.com/gitea/runner.git
synced 2026-08-27 22:37:46 +00:00
12dc9d26a2
Fixes 51 bugs discovered via comparison with `actions/runner`. Every fix has test coverage.
### Secrets
- A short secret registered no shifted-base64 form, so `base64("user:$TOKEN")` printed in the clear
- Encoded forms came only from the whole trimmed value, missing padded and per-line spellings
- Masks split only on `\n`, so `::add-mask::a%0Db` registered neither half
- Adds XML, expression-string and quote-trimming encoders
### Workflow commands
- Split at the last `::` or `]` rather than the first, so `::add-mask::a::b` registered no mask
- A command on the last line without a newline was ignored, and `::ADD-MASK::` did nothing
- `##[...]` did not decode `%3B`/`%5D`, properties lost anything after a second `=`
- `$GITHUB_ENV` and `::set-env::` now refuse `NODE_OPTIONS`
### Status
- `continue-on-error` reported failed, a cancelled job reported success, an `if:` error reported cancelled
- File commands ran after `continue-on-error`, failing the job while the step stayed green
- A bad job output aborted the whole run instead of that job
### Steps and actions
- `${{ matrix.* }}` and `${{ strategy.* }}` were empty inside composite actions
- Composite inputs leaked into nested actions as `INPUT_*`, `with:` matched case-sensitively, `pre` failures were dropped
- Docker actions dropped `runs.env` when the caller passed `with: args:`, and caller `args`/`entrypoint` beat the manifest
- An implicit shell ran with `pipefail`, a `shell:` without `{0}` passed without running
- `container.env` overrode job env and every `$GITHUB_ENV` write, heredocs lost leading blank lines, `$GITHUB_PATH` was not BOM-decoded
Written by Claude Opus 5.
Reviewed-on: https://gitea.com/gitea/runner/pulls/1194
Reviewed-by: bircni <bircni@icloud.com>
Co-authored-by: silverwind <me@silverwind.io>
120 lines
4.1 KiB
Go
120 lines
4.1 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package runner
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"fmt"
|
|
"maps"
|
|
"path"
|
|
"strings"
|
|
|
|
"gitea.com/gitea/runner/act/common"
|
|
"gitea.com/gitea/runner/act/container"
|
|
)
|
|
|
|
// GitHub's job-hook variables, read as a fallback when the settings are unset.
|
|
const (
|
|
jobStartedHookEnv = "ACTIONS_RUNNER_HOOK_JOB_STARTED"
|
|
jobCompletedHookEnv = "ACTIONS_RUNNER_HOOK_JOB_COMPLETED"
|
|
)
|
|
|
|
// Kept apart from the per-step file-command files, which are truncated on every step.
|
|
const (
|
|
hookEnvFileCommand = "workflow/hook-envs.txt"
|
|
hookPathFileCommand = "workflow/hook-path.txt"
|
|
)
|
|
|
|
func (rc *RunContext) runJobStartedHook(ctx context.Context) error {
|
|
return rc.runJobHook(ctx, cmp.Or(rc.Config.JobStartedHook, rc.Config.Env[jobStartedHookEnv]), "job started")
|
|
}
|
|
|
|
func (rc *RunContext) runJobCompletedHook(ctx context.Context) error {
|
|
return rc.runJobHook(ctx, cmp.Or(rc.Config.JobCompletedHook, rc.Config.Env[jobCompletedHookEnv]), "job completed")
|
|
}
|
|
|
|
// runJobHook runs one hook in the job environment. Either hook failing fails the job, as
|
|
// on GitHub, where the operator is responsible for the hook's own resilience.
|
|
func (rc *RunContext) runJobHook(ctx context.Context, hookPath, name string) error {
|
|
if hookPath == "" {
|
|
return nil
|
|
}
|
|
|
|
cmd, shell := hookCommand(hookPath)
|
|
rawLogger := common.Logger(ctx).WithField(rawOutputField, true)
|
|
defer rawLogger.Infof("::endgroup::")
|
|
rawLogger.Infof("::group::Run '%s'", EscapeCommandData(hookPath))
|
|
rawLogger.Infof("A %s hook has been configured by the runner administrator", name)
|
|
if shell != "" {
|
|
rawLogger.Infof("shell: %s", shell)
|
|
}
|
|
|
|
env := map[string]string{}
|
|
if jobContainer := rc.Run.Job().Container(); jobContainer != nil {
|
|
maps.Copy(env, jobContainer.Env)
|
|
}
|
|
maps.Copy(env, rc.GetEnv())
|
|
rc.withGithubEnv(ctx, rc.getGithubContext(ctx), env)
|
|
rc.ApplyExtraPath(ctx, &env)
|
|
|
|
err := rc.setupHookFileCommands(ctx, env)
|
|
if err == nil {
|
|
err = rc.JobContainer.Exec(cmd, env, "", "")(ctx)
|
|
}
|
|
// Processed even on failure, so a hook that exports what it managed to set up before
|
|
// failing still hands it to the job.
|
|
if processErr := rc.processHookFileCommands(ctx); err == nil {
|
|
err = processErr
|
|
}
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
|
|
err = fmt.Errorf("the %s hook %q failed: %w", name, hookPath, err)
|
|
// Flip the job status the way a failing pre step does, so success()-default main steps
|
|
// skip and the task is reported failed.
|
|
reportStepError(ctx, rc, err)
|
|
return err
|
|
}
|
|
|
|
// setupHookFileCommands points the hook at its GITHUB_ENV and GITHUB_PATH files, so it can
|
|
// export to the job's steps, and truncates them so the second hook does not re-read what
|
|
// the first one wrote.
|
|
func (rc *RunContext) setupHookFileCommands(ctx context.Context, env map[string]string) error {
|
|
actPath := rc.JobContainer.GetActPath()
|
|
env["GITHUB_ENV"] = path.Join(actPath, hookEnvFileCommand)
|
|
env["GITHUB_PATH"] = path.Join(actPath, hookPathFileCommand)
|
|
env["GITEA_ENV"] = env["GITHUB_ENV"]
|
|
env["GITEA_PATH"] = env["GITHUB_PATH"]
|
|
|
|
return rc.JobContainer.Copy(actPath,
|
|
&container.FileEntry{Name: hookEnvFileCommand, Mode: 0o666},
|
|
&container.FileEntry{Name: hookPathFileCommand, Mode: 0o666},
|
|
)(ctx)
|
|
}
|
|
|
|
func (rc *RunContext) processHookFileCommands(ctx context.Context) error {
|
|
err := processRunnerEnvFileCommand(ctx, hookEnvFileCommand, rc, rc.setEnvFile)
|
|
if pathErr := rc.UpdateExtraPath(ctx, path.Join(rc.JobContainer.GetActPath(), hookPathFileCommand)); pathErr != nil && err == nil {
|
|
err = pathErr
|
|
}
|
|
return err
|
|
}
|
|
|
|
// hookCommand mirrors actions/runner, which deliberately does not apply the shell flags it
|
|
// gives `run:` steps — a hook sets its own. See docs/adrs/1751-runner-job-hooks.md there.
|
|
// The second return value is how the invocation is shown in the log, empty when the file is
|
|
// executed directly.
|
|
func hookCommand(hookPath string) (cmd []string, shell string) {
|
|
switch strings.ToLower(path.Ext(hookPath)) {
|
|
case ".sh":
|
|
return []string{"bash", "-e", hookPath}, "bash -e {0}"
|
|
case ".ps1":
|
|
return []string{"pwsh", "-command", ". '" + hookPath + "'"}, `pwsh -command ". '{0}'"`
|
|
default:
|
|
return []string{hookPath}, ""
|
|
}
|
|
}
|