Skip to content

Commit 843b49c

Browse files
committed
fix: removed path validation
1 parent de8f157 commit 843b49c

File tree

2 files changed

+19
-87
lines changed

2 files changed

+19
-87
lines changed

internal/util/path.go

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ type PathValidationOptions struct {
2525
Existence ExistenceType
2626
}
2727

28-
// Common dangerous characters that could be used for injection attacks
29-
var dangerousChars = []string{";", "&", "|", "`", "$", "\"", "'", "\n", "\r", "\t", "*"}
30-
3128
// ValidatePath validates any path for security with customizable requirements
3229
func ValidatePath(path types.FilePath, options PathValidationOptions) error {
3330
pathStr := strings.TrimSpace(string(path))
@@ -38,12 +35,12 @@ func ValidatePath(path types.FilePath, options PathValidationOptions) error {
3835
return fmt.Errorf("path cannot be empty, got: '%s'", string(path))
3936
}
4037

41-
// 1. Check for dangerous characters
42-
if err := validateDangerousCharacters(pathStr); err != nil {
38+
// Validate Windows UNC admin share paths
39+
if err := validateWindowsUNCAdminShare(pathStr); err != nil {
4340
return err
4441
}
4542

46-
// 2. Validate path existence based on requirements
43+
// Validate path existence based on requirements
4744
if err := validatePathExistence(pathStr, options.Existence); err != nil {
4845
return err
4946
}
@@ -78,8 +75,7 @@ func ValidatePathStrict(path types.FilePath) error {
7875
// ValidatePathForStorage validates a path for storage purposes without requiring the path to exist.
7976
// This function is used when storing paths where the path may not exist yet
8077
// (e.g., user-configured paths for future use, paths during data migration, or storage keys).
81-
// It performs security validation (dangerous characters, path traversal) but allows empty paths
82-
// and doesn't check if the path actually exists on the filesystem.
78+
// It allows empty paths and doesn't check if the path actually exists on the filesystem.
8379
func ValidatePathForStorage(path types.FilePath) error {
8480
options := PathValidationOptions{
8581
AllowEmpty: true,
@@ -103,30 +99,27 @@ func PathKey(p types.FilePath) types.FilePath {
10399
return ""
104100
}
105101

106-
if err := validateDangerousCharacters(s); err != nil {
107-
return ""
108-
}
109-
110102
// Normalize the path using filepath.Clean()
111103
s = filepath.Clean(s)
112104

113105
return types.FilePath(s)
114106
}
115107

116-
// validateDangerousCharacters checks for dangerous characters in a string
117-
func validateDangerousCharacters(input string) error {
118-
for _, char := range dangerousChars {
119-
if !strings.Contains(input, char) {
120-
continue
121-
}
122-
123-
// Special case: $ is allowed in Windows UNC administrative share paths (e.g., \\server\C$\path)
124-
if char == "$" && isWindowsUNCAdminShare(input) {
125-
continue
108+
// validateWindowsUNCAdminShare validates Windows UNC admin share paths
109+
// These paths have the format \\server\C$\... where C$ is the administrative share
110+
func validateWindowsUNCAdminShare(path string) error {
111+
// Check if path looks like a UNC path (starts with \\ or //)
112+
if strings.HasPrefix(path, "\\\\") || strings.HasPrefix(path, "//") {
113+
// If it's a UNC path, verify it's a valid admin share format
114+
if !isWindowsUNCAdminShare(path) {
115+
// If it looks like UNC but isn't a valid admin share, that's okay
116+
// We just want to ensure admin shares are properly recognized
117+
return nil
126118
}
127-
128-
return fmt.Errorf("dangerous character detected in '%s': %s", input, char)
119+
// Valid admin share path - no error
120+
return nil
129121
}
122+
// Not a UNC path - no validation needed
130123
return nil
131124
}
132125

internal/util/path_test.go

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ func TestPathKey(t *testing.T) {
6969
expected: types.FilePath(filepath.Clean("/Users/foo/./../bar")),
7070
},
7171
{
72-
name: "Invalid path with command injection",
72+
name: "Path with semicolon (normalized)",
7373
input: "/Users/foo; rm -rf /",
74-
expected: "",
74+
expected: types.FilePath(filepath.Clean("/Users/foo; rm -rf /")),
7575
},
7676
{
7777
name: "Relative path",
@@ -122,48 +122,6 @@ func createCommonTestCases(tempDir string) []testCase {
122122
input: types.FilePath(tempDir),
123123
expectError: false,
124124
},
125-
{
126-
name: "Command injection semicolon",
127-
input: "/Users/foo; rm -rf /",
128-
expectError: true,
129-
errorMsg: "dangerous character detected in",
130-
},
131-
{
132-
name: "Command injection ampersand",
133-
input: "/Users/foo & echo pwned",
134-
expectError: true,
135-
errorMsg: "dangerous character detected in",
136-
},
137-
{
138-
name: "Command injection backtick",
139-
input: "/Users/foo `whoami`",
140-
expectError: true,
141-
errorMsg: "dangerous character detected in",
142-
},
143-
{
144-
name: "Command injection dollar",
145-
input: "/Users/foo $(whoami)",
146-
expectError: true,
147-
errorMsg: "dangerous character detected in",
148-
},
149-
{
150-
name: "Command injection double quote",
151-
input: "/Users/foo\" && rm -rf /",
152-
expectError: true,
153-
errorMsg: "dangerous character detected in",
154-
},
155-
{
156-
name: "Command injection single quote",
157-
input: "/Users/foo' && rm -rf /",
158-
expectError: true,
159-
errorMsg: "dangerous character detected in",
160-
},
161-
{
162-
name: "Dollar sign in non-UNC path should fail",
163-
input: "/Users/foo$bar/test",
164-
expectError: true,
165-
errorMsg: "dangerous character detected in",
166-
},
167125
}
168126
}
169127

@@ -180,25 +138,6 @@ func TestValidatePathLenient(t *testing.T) {
180138
expectError: false,
181139
})
182140

183-
// Add Windows UNC admin share test cases (only for lenient, as they don't exist on non-Windows)
184-
testCases = append(testCases,
185-
testCase{
186-
name: "Windows UNC admin share C$",
187-
input: "\\\\localhost\\C$\\Users\\test",
188-
expectError: false,
189-
},
190-
testCase{
191-
name: "Windows UNC admin share D$",
192-
input: "\\\\server\\D$\\path\\to\\file",
193-
expectError: false,
194-
},
195-
testCase{
196-
name: "Windows UNC admin share with forward slashes",
197-
input: "//localhost/C$/Users/test",
198-
expectError: false,
199-
},
200-
)
201-
202141
runValidationTest(t, ValidatePathLenient, testCases)
203142
}
204143

0 commit comments

Comments
 (0)