Skip to content

Commit 43f943f

Browse files
committed
feat: enforce formatter name format
Signed-off-by: Brian McGee <[email protected]>
1 parent d82a9fc commit 43f943f

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

cmd/root_test.go

+39-1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,44 @@ func TestSpecifyingFormatters(t *testing.T) {
332332
}),
333333
)
334334
})
335+
336+
t.Run("bad names", func(t *testing.T) {
337+
for _, name := range []string{"foo$", "/bar", "baz%"} {
338+
treefmt(t,
339+
withArgs("--formatters", name),
340+
withError(func(as *require.Assertions, err error) {
341+
as.ErrorContains(err, fmt.Sprintf("formatter name %q is invalid", name))
342+
}),
343+
)
344+
345+
t.Setenv("TREEFMT_FORMATTERS", name)
346+
347+
treefmt(t,
348+
withError(func(as *require.Assertions, err error) {
349+
as.ErrorContains(err, fmt.Sprintf("formatter name %q is invalid", name))
350+
}),
351+
)
352+
353+
t.Setenv("TREEFMT_FORMATTERS", "")
354+
355+
cfg.FormatterConfigs[name] = &config.Formatter{
356+
Command: "echo",
357+
Includes: []string{"*"},
358+
}
359+
360+
test.WriteConfig(t, configPath, cfg)
361+
362+
treefmt(t,
363+
withError(func(as *require.Assertions, err error) {
364+
as.ErrorContains(err, fmt.Sprintf("formatter name %q is invalid", name))
365+
}),
366+
)
367+
368+
delete(cfg.FormatterConfigs, name)
369+
370+
test.WriteConfig(t, configPath, cfg)
371+
}
372+
})
335373
}
336374

337375
func TestIncludesAndExcludes(t *testing.T) {
@@ -1633,7 +1671,7 @@ func TestStdin(t *testing.T) {
16331671
treefmt(t,
16341672
withArgs("--stdin", "../test.nix"),
16351673
withError(func(as *require.Assertions, err error) {
1636-
as.ErrorContains(err, fmt.Sprintf("path ../test.nix not inside the tree root %s", tempDir))
1674+
as.ErrorContains(err, "path ../test.nix not inside the tree root "+tempDir)
16371675
}),
16381676
withStderr(func(out []byte) {
16391677
as.Contains(string(out), "Error: path ../test.nix not inside the tree root")

config/config.go

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"regexp"
78
"strings"
89

910
"github.com/numtide/treefmt/v2/walk"
@@ -210,12 +211,32 @@ func FromViper(v *viper.Viper) (*Config, error) {
210211
cfg.Excludes = cfg.Global.Excludes
211212
}
212213

214+
// validate formatter names do not contain invalid characters
215+
216+
nameRegex := regexp.MustCompile("^[a-zA-Z0-9_-]+$")
217+
218+
for name := range cfg.FormatterConfigs {
219+
if !nameRegex.MatchString(name) {
220+
return nil, fmt.Errorf(
221+
"formatter name %q is invalid, must be of the form %s",
222+
name, nameRegex.String(),
223+
)
224+
}
225+
}
226+
213227
// filter formatters based on provided names
214228
if len(cfg.Formatters) > 0 {
215229
filtered := make(map[string]*Formatter)
216230

217231
// check if the provided names exist in the config
218232
for _, name := range cfg.Formatters {
233+
if !nameRegex.MatchString(name) {
234+
return nil, fmt.Errorf(
235+
"formatter name %q is invalid, must be of the form %s",
236+
name, nameRegex.String(),
237+
)
238+
}
239+
219240
formatterCfg, ok := cfg.FormatterConfigs[name]
220241
if !ok {
221242
return nil, fmt.Errorf("formatter %v not found in config", name)

0 commit comments

Comments
 (0)