mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-08-02 04:43:10 +00:00
Command data is percent-escaped on the wire because Gitea's job log cannot carry a newline, and the renderer decodes it. 1. Unescaping iterated a map, so order was random per process: `%250A` decoded to a newline instead of a literal `%0A` about two runs in three. Now a precompiled `strings.NewReplacer`. 1. Escape the data of command lines the runner writes itself (`##[error]`, `::group::Run …`), so decoding returns the original text instead of mangling a `%`. Multi-line runner errors also get real line breaks instead of a literal `\n`. 1. Register secrets in escaped form too — one containing `%` reaches the log as `%25…` and was never masked. 1. Decode in `jobLogFormatter`, so `gitea-runner exec` matches the web view. ## Relation to the Gitea PR https://github.com/go-gitea/gitea/pull/38659 makes the renderer decode command data. Point 2 is required by it; the rest stand alone. Either order works — until both land, a new runner on old Gitea shows `%25`, an old runner on new Gitea shows the mangling point 2 fixes. Co-authored-by: bircni <bircni@icloud.com> Reviewed-on: https://gitea.com/gitea/runner/pulls/1120 Reviewed-by: bircni <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io>
199 lines
5.5 KiB
Go
199 lines
5.5 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"
|
|
"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]+$")
|
|
commandPatternADO = regexp.MustCompile("^##\\[([^ ]+)( (.+))?]([^\r\n]*)[\r\n]+$")
|
|
}
|
|
|
|
func tryParseRawActionCommand(line string) (command string, kvPairs map[string]string, arg string, 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]
|
|
ok = true
|
|
}
|
|
return command, kvPairs, arg, ok
|
|
}
|
|
|
|
func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
|
|
logger := common.Logger(ctx)
|
|
resumeCommand := ""
|
|
return func(line string) bool {
|
|
command, kvPairs, arg, ok := tryParseRawActionCommand(line)
|
|
if !ok {
|
|
return true
|
|
}
|
|
|
|
if resumeCommand != "" && command != resumeCommand {
|
|
// There should not be any emojis in the log output for Gitea.
|
|
// The code in the switch statement is the same.
|
|
// 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)
|
|
return true
|
|
}
|
|
arg = UnescapeCommandData(arg)
|
|
kvPairs = unescapeKvPairs(kvPairs)
|
|
switch command {
|
|
case "set-env":
|
|
rc.setEnv(ctx, kvPairs, arg)
|
|
case "set-output":
|
|
rc.setOutput(ctx, kvPairs, arg)
|
|
case "add-path":
|
|
rc.addPath(ctx, arg)
|
|
case "debug":
|
|
logger.Infof("%s", line)
|
|
case "warning":
|
|
logger.Infof("%s", line)
|
|
case "error":
|
|
logger.Infof("%s", line)
|
|
case "add-mask":
|
|
rc.AddMask(arg)
|
|
logger.Infof("%s", "***")
|
|
case "stop-commands":
|
|
resumeCommand = arg
|
|
logger.Infof("%s", line)
|
|
case resumeCommand:
|
|
resumeCommand = ""
|
|
logger.Infof("%s", line)
|
|
case "save-state":
|
|
logger.Infof("%s", line)
|
|
rc.saveState(ctx, kvPairs, arg)
|
|
case "add-matcher":
|
|
logger.Infof("%s", line)
|
|
default:
|
|
logger.Infof("%s", line)
|
|
}
|
|
|
|
// return true to let gitea's logger handle these special outputs also
|
|
return true
|
|
}
|
|
}
|
|
|
|
func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg string) {
|
|
name := kvPairs["name"]
|
|
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) setOutput(ctx context.Context, kvPairs map[string]string, arg string) {
|
|
logger := common.Logger(ctx)
|
|
stepID := rc.CurrentStep
|
|
outputName := kvPairs["name"]
|
|
if outputMapping, ok := rc.OutputMappings[MappableOutput{StepID: stepID, OutputName: outputName}]; ok {
|
|
stepID = outputMapping.StepID
|
|
outputName = outputMapping.OutputName
|
|
}
|
|
|
|
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.Split(kvPair, "=")
|
|
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", ",")
|
|
)
|
|
|
|
// 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)
|
|
}
|
|
|
|
func unescapeKvPairs(kvPairs map[string]string) map[string]string {
|
|
for k, v := range kvPairs {
|
|
kvPairs[k] = unescapeCommandProperty(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
|
|
}
|
|
}
|