Skip to content

Commit a849639

Browse files
author
Nicholas M. Iodice
authored
Prevent index out of bound operation when validating build path (#288)
This change adds additional logic inside the build path validation code. This logic prevents a panic that occurs when validating an empty string.
1 parent 13a8ee8 commit a849639

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

azuredevops/utils/validate/file_path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Path(i interface{}, k string) (warnings []string, errors []error) {
2020
errors = append(errors, fmt.Errorf("path can not be empty"))
2121
}
2222

23-
if v[:1] != `\` {
23+
if len(v) >= 1 && v[:1] != `\` {
2424
errors = append(errors, fmt.Errorf("path must start with backslash"))
2525
}
2626

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// +build all utils path
2+
3+
package validate
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
)
9+
10+
func TestPathValidation(t *testing.T) {
11+
type TestCase struct {
12+
Value string
13+
TestName string
14+
ErrCount int
15+
}
16+
cases := []TestCase{
17+
{
18+
Value: `\`,
19+
TestName: "Default Path",
20+
ErrCount: 0,
21+
},
22+
{
23+
Value: "",
24+
TestName: "Empty Path",
25+
ErrCount: 1,
26+
},
27+
{
28+
Value: "A",
29+
TestName: "Wrong Starting Character",
30+
ErrCount: 1,
31+
},
32+
}
33+
34+
illegalChars := []string{"<", ">", "|", ":", "$", "@", `"`, "/", "%", "+", "*", "?"}
35+
for _, c := range illegalChars {
36+
cases = append(cases, TestCase{
37+
Value: fmt.Sprintf(`\%s`, c),
38+
TestName: fmt.Sprintf("Illegal Character - %s", c),
39+
ErrCount: 1,
40+
})
41+
}
42+
43+
for _, tc := range cases {
44+
t.Run(tc.TestName, func(t *testing.T) {
45+
_, errors := Path(tc.Value, tc.TestName)
46+
if len(errors) != tc.ErrCount {
47+
t.Fatalf("Expected TestPathValidation to have %d not %d errors for %q", tc.ErrCount, len(errors), tc.TestName)
48+
}
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)