Skip to content

Commit a9ea7b8

Browse files
authored
Fix handling of xslt path and possible args pollution for xslt calls (#1066)
1 parent f8cacb6 commit a9ea7b8

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

internal/core/ini.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ var syntaxOpts = map[string]func(string, *ini.Section, *Config) error{
114114
},
115115
"Transform": func(label string, sec *ini.Section, cfg *Config) error { //nolint:unparam
116116
candidate := sec.Key("Transform").String()
117-
cfg.Stylesheets[label] = system.DeterminePath(cfg.Flags.Path, candidate)
117+
cfg.Stylesheets[label] = system.DeterminePath(cfg.ConfigFile(), candidate)
118118
return nil
119119

120120
},

internal/core/ini_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,30 @@ CommentDelimiters = "{/*"
114114
})
115115
}
116116
}
117+
118+
func Test_processConfig_transform(t *testing.T) {
119+
body := `[*.xml]
120+
Transform = transform.xsl
121+
`
122+
uCfg, err := shadowLoad([]byte(body))
123+
if err != nil {
124+
t.Fatal(err)
125+
}
126+
127+
conf, err := NewConfig(&CLIFlags{})
128+
if err != nil {
129+
t.Fatal(err)
130+
}
131+
conf.AddConfigFile("C:\\Source\\project\\.vale.ini")
132+
133+
_, err = processConfig(uCfg, conf, false)
134+
if err != nil {
135+
t.Fatal(err)
136+
}
137+
138+
actual := conf.Stylesheets["*.xml"]
139+
expected := "C:\\Source\\project\\transform.xsl"
140+
if actual != expected {
141+
t.Errorf("expected %v, but got %v", expected, actual)
142+
}
143+
}

internal/lint/xml.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ func (l Linter) lintXML(file *core.File) error {
3333
errors.New("no XSLT transform provided"))
3434
}
3535

36-
xsltArgs = append(xsltArgs, []string{file.Transform, "-"}...)
36+
args := append([]string{}, xsltArgs...)
37+
args = append(args, []string{file.Transform, "-"}...)
3738

38-
cmd := exec.Command(xsltproc, xsltArgs...)
39+
cmd := exec.Command(xsltproc, args...)
3940
cmd.Stdin = strings.NewReader(file.Content)
4041
cmd.Stdout = &out
4142
cmd.Stderr = &eut

internal/lint/xml_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package lint
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestXSLTArgsNotPolluted(t *testing.T) {
8+
// We want to test that xsltArgs in xml.go is not mutated.
9+
initialLen := len(xsltArgs)
10+
11+
// Simulate what lintXML does multiple times
12+
for i := 0; i < 3; i++ {
13+
args := append([]string{}, xsltArgs...)
14+
args = append(args, []string{"transform.xsl", "-"}...)
15+
16+
if len(xsltArgs) != initialLen {
17+
t.Fatalf("xsltArgs was modified! original: %d, current: %d", initialLen, len(xsltArgs))
18+
}
19+
20+
expectedArgsLen := initialLen + 2
21+
if len(args) != expectedArgsLen {
22+
t.Errorf("local args have wrong length: %d, expected %d", len(args), expectedArgsLen)
23+
}
24+
}
25+
}

internal/system/path_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package system
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
)
7+
8+
func TestDeterminePath(t *testing.T) {
9+
tests := []struct {
10+
configPath string
11+
keyPath string
12+
expected string
13+
}{
14+
{
15+
configPath: "C:\\Source\\project\\.vale.ini",
16+
keyPath: "transform.xsl",
17+
expected: "C:\\Source\\project\\transform.xsl",
18+
},
19+
{
20+
configPath: "C:\\Source\\project\\.vale.ini",
21+
keyPath: "styles/transform.xsl",
22+
expected: "C:\\Source\\project\\styles\\transform.xsl",
23+
},
24+
{
25+
configPath: "C:\\Source\\project\\.vale.ini",
26+
keyPath: "C:\\Other\\transform.xsl",
27+
expected: "C:\\Other\\transform.xsl",
28+
},
29+
}
30+
31+
for _, tt := range tests {
32+
actual := DeterminePath(tt.configPath, tt.keyPath)
33+
// Clean the paths to ensure separators match for comparison
34+
actual = filepath.Clean(actual)
35+
expected := filepath.Clean(tt.expected)
36+
if actual != expected {
37+
t.Errorf("DeterminePath(%q, %q) = %q; want %q", tt.configPath, tt.keyPath, actual, expected)
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)