mirror of
https://gitea.com/gitea/runner.git
synced 2026-08-27 14:27: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>
273 lines
8.8 KiB
Go
273 lines
8.8 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package runner
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"gitea.com/gitea/runner/act/common"
|
|
)
|
|
|
|
var commandPatternGA *regexp.Regexp
|
|
|
|
var commandPatternADO *regexp.Regexp
|
|
|
|
func init() {
|
|
commandPatternGA = regexp.MustCompile("^::([^ ]+?)( (.+?))?::([^\r\n]*)[\r\n]*$")
|
|
// excluding ']' ends the command info at the first bracket, as GitHub does
|
|
commandPatternADO = regexp.MustCompile("^##\\[([^ \\]]+)( ([^\\]]*))?]([^\r\n]*)[\r\n]*$")
|
|
}
|
|
|
|
func tryParseRawActionCommand(line string) (command string, kvPairs map[string]string, arg string, ok bool) {
|
|
command, kvPairs, arg, _, ok = tryParseActionCommand(line)
|
|
return command, kvPairs, arg, ok
|
|
}
|
|
|
|
func tryParseActionCommand(line string) (command string, kvPairs map[string]string, arg string, legacy, ok bool) {
|
|
if m := commandPatternGA.FindStringSubmatch(line); m != nil {
|
|
command = m[1]
|
|
kvPairs = parseKeyValuePairs(m[3], ",")
|
|
arg = m[4]
|
|
ok = true
|
|
} else if m := commandPatternADO.FindStringSubmatch(line); m != nil {
|
|
command = m[1]
|
|
kvPairs = parseKeyValuePairs(m[3], ";")
|
|
arg = m[4]
|
|
legacy = true
|
|
ok = true
|
|
}
|
|
return command, kvPairs, arg, legacy, ok
|
|
}
|
|
|
|
func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
|
|
logger := common.Logger(ctx)
|
|
resumeCommand := ""
|
|
return func(line string) bool {
|
|
command, kvPairs, arg, legacy, ok := tryParseActionCommand(line)
|
|
if !ok {
|
|
return true
|
|
}
|
|
command = strings.ToLower(command)
|
|
|
|
if resumeCommand != "" {
|
|
// There should not be any emojis in the log output for Gitea.
|
|
// Return true (not false) so the line still reaches the raw_output
|
|
// log handler; otherwise everything between ::stop-commands:: and
|
|
// its end token is silently dropped from the step log.
|
|
logger.Infof("%s", line)
|
|
// Resumed here rather than from the switch, because the end token is arbitrary
|
|
// and a token naming a real command would otherwise never resume.
|
|
if strings.EqualFold(command, resumeCommand) {
|
|
resumeCommand = ""
|
|
}
|
|
return true
|
|
}
|
|
if legacy {
|
|
arg = UnescapeLegacyCommand(arg)
|
|
kvPairs = unescapeKvPairs(kvPairs, UnescapeLegacyCommand)
|
|
} else {
|
|
arg = UnescapeCommandData(arg)
|
|
kvPairs = unescapeKvPairs(kvPairs, unescapeCommandProperty)
|
|
}
|
|
if (command == "set-env" || command == "add-path") && rc.refuseUnsecureCommand(ctx, command) {
|
|
return true
|
|
}
|
|
switch command {
|
|
case "set-env":
|
|
rc.setEnv(ctx, kvPairs, arg, true)
|
|
case "set-output":
|
|
rc.setOutput(ctx, kvPairs, arg)
|
|
case "add-path":
|
|
rc.addPath(ctx, arg)
|
|
case "add-mask":
|
|
rc.AddMask(arg)
|
|
logger.Infof("%s", "***")
|
|
// The raw line is still forwarded, carrying the secret: that is how the reporter
|
|
// learns the mask, and it drops the row rather than writing it out.
|
|
case "stop-commands":
|
|
resumeCommand = arg
|
|
logger.Infof("%s", line)
|
|
case "save-state":
|
|
logger.Infof("%s", line)
|
|
rc.saveState(ctx, kvPairs, arg)
|
|
default:
|
|
// ::debug::, ::error::, ::warning::, ::add-matcher:: and anything unrecognised are
|
|
// passed through for the reporter and Gitea's web UI to render.
|
|
logger.Infof("%s", line)
|
|
}
|
|
|
|
// return true to let gitea's logger handle these special outputs also
|
|
return true
|
|
}
|
|
}
|
|
|
|
const allowUnsecureCommandsVar = "ACTIONS_ALLOW_UNSECURE_COMMANDS"
|
|
|
|
// refuseUnsecureCommand reports whether a deprecated ::set-env:: or ::add-path:: command must
|
|
// not run, recording the error that fails the step. GitHub disabled both because a step that
|
|
// echoes untrusted content can use them to set NODE_OPTIONS or PATH for every later step.
|
|
func (rc *RunContext) refuseUnsecureCommand(ctx context.Context, command string) bool {
|
|
if rc.allowUnsecureCommandsOptIn() {
|
|
return false
|
|
}
|
|
|
|
// The step executor logs the failure itself, so keep this line's wording distinct.
|
|
common.Logger(ctx).WithField(rawOutputField, true).Errorf("##[error]%s", EscapeCommandData(fmt.Sprintf(
|
|
"The `%s` command is disabled: it can set the environment of every later step from untrusted output. "+
|
|
"Write to $GITHUB_ENV or $GITHUB_PATH instead, or set ACTIONS_ALLOW_UNSECURE_COMMANDS to allow it",
|
|
command)))
|
|
|
|
rc.unsecureCommandMu.Lock()
|
|
defer rc.unsecureCommandMu.Unlock()
|
|
if rc.unsecureCommandErr == nil {
|
|
rc.unsecureCommandErr = fmt.Errorf("the `%s` workflow command is disabled", command)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// allowUnsecureCommandsOptIn reports whether the workflow itself asked for the deprecated
|
|
// commands, from any env scope, as it can on GitHub.
|
|
func (rc *RunContext) allowUnsecureCommandsOptIn() bool {
|
|
return isTruthyEnv(rc.currentStepEnv()[allowUnsecureCommandsVar]) ||
|
|
isTruthyEnv(rc.Env[allowUnsecureCommandsVar]) ||
|
|
isTruthyEnv(rc.GlobalEnv[allowUnsecureCommandsVar])
|
|
}
|
|
|
|
// isTruthyEnv mirrors GitHub's bool.TryParse: only "true", in any casing.
|
|
func isTruthyEnv(v string) bool {
|
|
return strings.EqualFold(strings.TrimSpace(v), "true")
|
|
}
|
|
|
|
// takeUnsecureCommandError returns and clears the error left by a refused command.
|
|
func (rc *RunContext) takeUnsecureCommandError() error {
|
|
rc.unsecureCommandMu.Lock()
|
|
defer rc.unsecureCommandMu.Unlock()
|
|
err := rc.unsecureCommandErr
|
|
rc.unsecureCommandErr = nil
|
|
return err
|
|
}
|
|
|
|
func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg string, fromCommand bool) {
|
|
name := kvPairs["name"]
|
|
if strings.EqualFold(name, "NODE_OPTIONS") {
|
|
message := "Can't store NODE_OPTIONS output parameter using '$GITHUB_ENV' command."
|
|
if fromCommand {
|
|
message = "Can't update NODE_OPTIONS environment variable using ::set-env:: command."
|
|
}
|
|
common.Logger(ctx).WithField(rawOutputField, true).Errorf("##[error]%s", EscapeCommandData(message))
|
|
return
|
|
}
|
|
common.Logger(ctx).Infof("::set-env:: %s=%s", name, arg)
|
|
if rc.Env == nil {
|
|
rc.Env = make(map[string]string)
|
|
}
|
|
if rc.GlobalEnv == nil {
|
|
rc.GlobalEnv = map[string]string{}
|
|
}
|
|
newenv := map[string]string{
|
|
name: arg,
|
|
}
|
|
mergeIntoMap := mergeIntoMapCaseSensitive
|
|
if rc.JobContainer != nil && rc.JobContainer.IsEnvironmentCaseInsensitive() {
|
|
mergeIntoMap = mergeIntoMapCaseInsensitive
|
|
}
|
|
mergeIntoMap(rc.Env, newenv)
|
|
mergeIntoMap(rc.GlobalEnv, newenv)
|
|
}
|
|
|
|
func (rc *RunContext) setEnvFile(ctx context.Context, kvPairs map[string]string, arg string) {
|
|
rc.setEnv(ctx, kvPairs, arg, false)
|
|
}
|
|
|
|
func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string, arg string) {
|
|
logger := common.Logger(ctx)
|
|
stepID := rc.CurrentStep
|
|
outputName := kvPairs["name"]
|
|
|
|
result, ok := rc.StepResults[stepID]
|
|
if !ok {
|
|
logger.Infof("No outputs registered for step '%s'", stepID)
|
|
return
|
|
}
|
|
|
|
logger.Infof("::set-output:: %s=%s", outputName, arg)
|
|
result.Outputs[outputName] = arg
|
|
}
|
|
|
|
func (rc *RunContext) addPath(ctx context.Context, arg string) {
|
|
common.Logger(ctx).Infof("::add-path:: %s", arg)
|
|
extraPath := []string{arg}
|
|
for _, v := range rc.ExtraPath {
|
|
if v != arg {
|
|
extraPath = append(extraPath, v)
|
|
}
|
|
}
|
|
rc.ExtraPath = extraPath
|
|
}
|
|
|
|
func parseKeyValuePairs(kvPairs, separator string) map[string]string {
|
|
rtn := make(map[string]string)
|
|
kvPairList := strings.SplitSeq(kvPairs, separator)
|
|
for kvPair := range kvPairList {
|
|
kv := strings.SplitN(kvPair, "=", 2)
|
|
if len(kv) == 2 {
|
|
rtn[kv[0]] = kv[1]
|
|
}
|
|
}
|
|
return rtn
|
|
}
|
|
|
|
// A Replacer never rescans what it wrote, so "%250A" stays a literal "%0A".
|
|
var (
|
|
commandDataEscaper = strings.NewReplacer("%", "%25", "\r", "%0D", "\n", "%0A")
|
|
commandDataUnescaper = strings.NewReplacer("%25", "%", "%0D", "\r", "%0A", "\n")
|
|
commandPropertyUnescaper = strings.NewReplacer("%25", "%", "%0D", "\r", "%0A", "\n", "%3A", ":", "%2C", ",")
|
|
legacyCommandUnescaper = strings.NewReplacer("%3B", ";", "%0D", "\r", "%0A", "\n", "%5D", "]", "%25", "%")
|
|
)
|
|
|
|
// EscapeCommandData encodes the data part of a "::cmd::" or "##[cmd]" line the runner writes itself,
|
|
// so the log renderer decodes it back. Lines forwarded from step output are already escaped.
|
|
func EscapeCommandData(arg string) string {
|
|
return commandDataEscaper.Replace(arg)
|
|
}
|
|
|
|
func UnescapeCommandData(arg string) string {
|
|
return commandDataUnescaper.Replace(arg)
|
|
}
|
|
|
|
func unescapeCommandProperty(arg string) string {
|
|
return commandPropertyUnescaper.Replace(arg)
|
|
}
|
|
|
|
// UnescapeLegacyCommand decodes a "##[cmd]" line, which also spells ";" and "]" escaped.
|
|
func UnescapeLegacyCommand(arg string) string {
|
|
return legacyCommandUnescaper.Replace(arg)
|
|
}
|
|
|
|
func unescapeKvPairs(kvPairs map[string]string, unescape func(string) string) map[string]string {
|
|
for k, v := range kvPairs {
|
|
kvPairs[k] = unescape(v)
|
|
}
|
|
return kvPairs
|
|
}
|
|
|
|
func (rc *RunContext) saveState(_ context.Context, kvPairs map[string]string, arg string) {
|
|
stepID := rc.CurrentStep
|
|
if stepID != "" {
|
|
if rc.IntraActionState == nil {
|
|
rc.IntraActionState = map[string]map[string]string{}
|
|
}
|
|
state, ok := rc.IntraActionState[stepID]
|
|
if !ok {
|
|
state = map[string]string{}
|
|
rc.IntraActionState[stepID] = state
|
|
}
|
|
state[kvPairs["name"]] = arg
|
|
}
|
|
}
|