Skip to content

Commit 257658e

Browse files
authored
Merge pull request #40 from brycenichols/filter_var_names
Do not allow anything except ^[_A-Za-z][A-Za-z0-9_]*$ for environment…
2 parents d24758b + ab58293 commit 257658e

File tree

6 files changed

+55
-19
lines changed

6 files changed

+55
-19
lines changed

.github/workflows/tag.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ jobs:
3131
with:
3232
go-version: 1.21
3333
id: go
34+
- name: Set up Python 3.13
35+
uses: actions/setup-python@v3
36+
with:
37+
python-version: "3.13"
38+
- name: Install cram
39+
run: |
40+
python -m pip install --upgrade pip
41+
pip install cram
3442
- name: Check out new tag into the Go module directory
3543
uses: actions/checkout@v2
3644
with:

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PROJECT_NAME := buildenv
55

66
all: clean build-deps build
77

8-
test:
8+
test: build-local
99
go test ./...
1010
cram cram_tests
1111

@@ -17,7 +17,7 @@ build: test
1717
for pkg in $$(ls pkg/); do cp CONTRIBUTING.md CONTRIBUTORS.md LICENSE NOTICE pkg/$${pkg}; done
1818
for pkg in $$(ls pkg/); do cd pkg/$${pkg}; tar cvzf "../../$(PROJECT_NAME)-$${pkg}-$(VERSION).tar.gz" *; cd ../..; done
1919

20-
build-local: test
20+
build-local:
2121
CGO_ENABLED=0 go build -ldflags "-X main.version=$(VERSION)" -o $(PROJECT_NAME)
2222

2323
clean:

cram_tests/inject.t

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Setup
2+
3+
$ . "$TESTDIR"/setup.sh
4+
5+
Try injecting quote
6+
7+
$ echo '{"vars":{"Q": "\"; echo bad \""}}' > test.yml
8+
$ be -f test.yml
9+
export Q="\"; echo bad \""
10+
11+
Bad keys
12+
13+
$ echo '{"vars":{"export hi=there; dosomethingevil && ": "hi"}}' > test2.yml
14+
$ be -f test2.yml

cram_tests/regress.t

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Setup
2+
3+
$ . "$TESTDIR"/setup.sh
4+
5+
Regular run
6+
7+
$ buildenv -cf "$TESTDIR"/../no_secrets.yml
8+
# Global Variables
9+
export TEST="no secrets"
10+

cram_tests/setup.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/usr/bin/env bash
2-
alias be="${TESTDIR}/../Buildenv-Tool -f ${TESTDIR}/../no_secrets.yml"
3-
2+
alias be="${TESTDIR}/../buildenv -f ${TESTDIR}/../no_secrets.yml"

reader/reader.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"os"
99
"os/exec"
10+
"regexp"
1011
"slices"
1112
"strings"
1213

@@ -43,6 +44,8 @@ func (e EnvVars) GetOutput() OutputList {
4344

4445
type Secrets map[string]string
4546

47+
var shellvar_regexp = regexp.MustCompile("^[_A-Za-z][A-Za-z0-9_]*$")
48+
4649
func (s Secrets) GetOutput(ctx context.Context, r *Reader) (OutputList, error) {
4750
// Read it like a kv secrets where all keys are "value"
4851
kvSecrets := KVSecrets{}
@@ -245,8 +248,10 @@ func (o OutputList) Exec(shell_cmd string) int {
245248
}
246249

247250
for _, out := range o {
248-
s := fmt.Sprintf("%s=%s", out.Key, out.Value)
249-
cmd.Env = append(cmd.Environ(), s)
251+
if shellvar_regexp.MatchString(out.Key) {
252+
s := fmt.Sprintf("%s=%s", out.Key, out.Value)
253+
cmd.Env = append(cmd.Environ(), s)
254+
}
250255
}
251256

252257
cmd.Stdin = os.Stdin
@@ -265,19 +270,19 @@ func (o OutputList) Exec(shell_cmd string) int {
265270

266271
func (o OutputList) Print(showComments bool) {
267272
for _, out := range o {
268-
keySpace := ""
269-
nl := false
270-
if out.Key != "" {
271-
fmt.Printf("export %s=%q", out.Key, out.Value)
272-
keySpace = " "
273-
nl = true
274-
}
275-
if out.Comment != "" && showComments {
276-
fmt.Printf("%s# %s", keySpace, out.Comment)
277-
nl = true
278-
}
279-
if nl {
280-
fmt.Println()
273+
if out.Key == "" {
274+
if showComments && out.Comment != "" {
275+
fmt.Printf("# %s\n", out.Comment)
276+
}
277+
} else {
278+
/* silently discards variable names that are not shell safe */
279+
if shellvar_regexp.MatchString(out.Key) {
280+
fmt.Printf("export %s=%q", out.Key, out.Value)
281+
if out.Comment != "" && showComments {
282+
fmt.Printf(" # %s", out.Comment)
283+
}
284+
fmt.Println()
285+
}
281286
}
282287
}
283288
}

0 commit comments

Comments
 (0)