Skip to content

Commit 73d8d44

Browse files
committed
skip keys with invalid characters
1 parent bc7ae73 commit 73d8d44

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

helpers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"os"
13+
"regexp"
1314
"strings"
1415
"time"
1516
)
@@ -34,6 +35,8 @@ var envStyleExt = map[EnvStyle]string{
3435
DOTENV: "env",
3536
}
3637

38+
var envNameRegex = "^[a-zA-Z0-9_]*$"
39+
3740
// cleanUpFile removes the given file
3841
func cleanUpFile(filePath string) {
3942
err := os.Remove(filePath)
@@ -94,6 +97,12 @@ func parseConfig(config []byte, store map[string]string, envType EnvStyle) (err
9497
continue
9598
}
9699

100+
// invalidate keys with invalid characters (only alphanumeric and _)
101+
regexpKey := regexp.MustCompile(envNameRegex)
102+
if !regexpKey.MatchString(cfgLine[0]) {
103+
continue
104+
}
105+
97106
store[cfgLine[0]] = strings.Join(cfgLine[1:], delim)
98107
if err == io.EOF {
99108
return nil

helpers_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sicher
22

33
import (
44
"encoding/hex"
5+
"fmt"
56
"os"
67
"testing"
78
)
@@ -70,6 +71,96 @@ URI=localhost
7071
t.Errorf("Expected dotenv style env not be be parseable with yaml envType")
7172
}
7273
}
74+
func TestParseConfig(t *testing.T) {
75+
76+
tests := []struct {
77+
text string
78+
expected map[string]string
79+
envType string
80+
}{
81+
{
82+
text: `
83+
PORT:8080
84+
URI:localhost
85+
#OLD_PORT:5000
86+
`,
87+
expected: map[string]string{
88+
"PORT": "8080",
89+
"URI": "localhost",
90+
},
91+
envType: "yaml",
92+
},
93+
{
94+
text: `
95+
PORT=8080
96+
URI=localhost
97+
#OLD_PORT=5000
98+
`,
99+
expected: map[string]string{
100+
"PORT": "8080",
101+
"URI": "localhost",
102+
},
103+
envType: "dotenv",
104+
},
105+
{
106+
text: `
107+
PORT=8080
108+
URI=localhost
109+
#OLD_PORT=5000
110+
KEY=value=ndsjhjdghdhg
111+
`,
112+
expected: map[string]string{
113+
"PORT": "8080",
114+
"URI": "localhost",
115+
"KEY": "value=ndsjhjdghdhg",
116+
},
117+
envType: "dotenv",
118+
},
119+
{
120+
text: `
121+
PORT:8080
122+
URI=localhost
123+
`,
124+
expected: map[string]string{
125+
"PORT": "8080",
126+
},
127+
envType: "yaml",
128+
},
129+
{
130+
text: `
131+
PORT:8080
132+
URI=localhost
133+
SOME_KEY:somevalue=jsfhjdghdhg
134+
`,
135+
expected: map[string]string{
136+
"URI": "localhost",
137+
},
138+
envType: "dotenv",
139+
},
140+
}
141+
142+
for _, val := range tests {
143+
enMap := make(map[string]string)
144+
if err := parseConfig([]byte(val.text), enMap, EnvStyle(val.envType)); err != nil {
145+
t.Errorf("Unable to parse config; %v", err)
146+
}
147+
148+
t.Run(fmt.Sprintf("Envtype %s", val.envType), func(t *testing.T) {
149+
for key, value := range val.expected {
150+
if enMap[key] != value {
151+
t.Errorf("Expected value to be %s, got %s", value, enMap[key])
152+
}
153+
}
154+
for key, value := range enMap {
155+
if val.expected[key] != value {
156+
t.Errorf("Expected value to be %s, got %s", value, val.expected[key])
157+
}
158+
}
159+
})
160+
}
161+
162+
}
163+
73164
func TestYamlParseConfig(t *testing.T) {
74165
enMap := make(map[string]string)
75166
cfg := []byte(`

0 commit comments

Comments
 (0)