~comcloudway/builds.sr.ht

1ef9f3e64135c82471194e6f464566a61dc5ea4e — Drew DeVault 5 years ago 5f7dea9
sh escape environment variables in manifests
1 files changed, 15 insertions(+), 1 deletions(-)

M worker/tasks.go
M worker/tasks.go => worker/tasks.go +15 -1
@@ 9,6 9,7 @@ import (
	"os"
	"os/exec"
	"path"
	"regexp"
	"strconv"
	"strings"
	"syscall"


@@ 161,6 162,19 @@ func (ctx *JobContext) SendTasks() error {
	return nil
}

var shunsafe = regexp.MustCompile(`[^\w@%+=:,./-]`)

func shquote(v string) string {
	// Algorithm aped from shlex.py
	if v == "" {
		return "''"
	}
	if !shunsafe.MatchString(v) {
		return v
	}
	return "'" + strings.ReplaceAll(v, "'", "'\"'\"'") + "'"
}

func (ctx *JobContext) SendEnv() error {
	const home = "/home/build"
	ctx.Log.Println("Sending build environment")


@@ 174,7 188,7 @@ export JOB_ID=%d
	for key, value := range ctx.Manifest.Environment {
		switch v := value.(type) {
		case string:
			env += fmt.Sprintf("export %s=%s\n", key, v)
			env += fmt.Sprintf("export %s=%s\n", key, shquote(v))
		case float64:
			env += fmt.Sprintf("export %s=%g\n", key, v)
		case []interface{}: