Use Go rules for Windows filename components
This commit is contained in:
@@ -258,27 +258,16 @@ func validFilenameComponent(value string) bool {
|
||||
return false
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
return validWindowsFilenameComponent(value)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func validWindowsFilenameComponent(value string) bool {
|
||||
if strings.ContainsAny(value, `<>:"|?*`) || strings.HasSuffix(value, ".") || strings.HasSuffix(value, " ") {
|
||||
return false
|
||||
}
|
||||
for _, character := range value {
|
||||
if character < 32 {
|
||||
if !filepath.IsLocal(value) || strings.ContainsAny(value, `<>:"|?*`) || strings.HasSuffix(value, ".") || strings.HasSuffix(value, " ") {
|
||||
return false
|
||||
}
|
||||
for _, character := range value {
|
||||
if character < 32 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
base := strings.ToUpper(strings.TrimSuffix(value, filepath.Ext(value)))
|
||||
switch base {
|
||||
case "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM¹", "COM²", "COM³", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "LPT¹", "LPT²", "LPT³":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var errUnixSystemPresetUnavailable = errors.New("Unix system directories are unavailable on this platform")
|
||||
|
||||
@@ -129,31 +129,33 @@ func TestFilenameComponentValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
for _, value := range []string{"CON", "nul.txt", "name.", "bad:name"} {
|
||||
for _, value := range []string{"CON", "CONIN$", "CONOUT$", "name.", "bad:name", "bad\x01name"} {
|
||||
if validFilenameComponent(value) {
|
||||
t.Errorf("Windows component %q unexpectedly accepted", value)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, value := range []string{"name with spaces", "naïve"} {
|
||||
if !validFilenameComponent(value) {
|
||||
t.Errorf("host-valid component %q rejected", value)
|
||||
// Device names with extensions depend on the Windows version. Match
|
||||
// Go's platform-aware decision instead of imposing a blanket ban.
|
||||
for _, value := range []string{"nul.txt", "COM¹.log", "LPT³.txt", "CON.extra.yml"} {
|
||||
if got, want := validFilenameComponent(value), filepath.IsLocal(value); got != want {
|
||||
t.Errorf("Windows component %q validity = %v, want Go's %v", value, got, want)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, value := range []string{"CONIN$", "CONOUT$", "nul.txt"} {
|
||||
if !validFilenameComponent(value) {
|
||||
t.Errorf("Windows component restriction applied to this host: %q", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, value := range []string{"name with spaces", "naïve"} {
|
||||
if !validFilenameComponent(value) {
|
||||
t.Errorf("host-valid component %q rejected", value)
|
||||
}
|
||||
}
|
||||
if diagnostics := validateSearch(SearchDirectory("configs", DirectorySearchOptions{Filenames: []FilenameGroup{CustomFilenames("bad/name")}}), ""); len(diagnostics) == 0 {
|
||||
t.Fatal("invalid custom filename was not retained for validation")
|
||||
}
|
||||
for _, value := range []string{"CON", "nul.txt", "COM¹.log", "LPT³.txt", "name.", "bad:name", "bad\x01name"} {
|
||||
if validWindowsFilenameComponent(value) {
|
||||
t.Errorf("Windows component %q unexpectedly accepted", value)
|
||||
}
|
||||
}
|
||||
for _, value := range []string{"name with spaces", "naïve"} {
|
||||
if !validWindowsFilenameComponent(value) {
|
||||
t.Errorf("valid Windows component %q rejected", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchCandidatesPreserveDirectoryAndGroupOrder(t *testing.T) {
|
||||
|
||||
32
internal/engine/discovery_windows_test.go
Normal file
32
internal/engine/discovery_windows_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
//go:build windows
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWindowsSearchRejectsReservedComponentsBeforeAcquisition(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
root := t.TempDir()
|
||||
first := writeImportFile(t, root, "first.yml", "value: first\n")
|
||||
for _, name := range []string{"CONIN$", "CONOUT$"} {
|
||||
for _, source := range []Source{
|
||||
SearchConfig(name, Searches(Paths(first), WorkingDirectory())),
|
||||
SearchConfig("", Searches(Paths(first), SearchDirectory(root, DirectorySearchOptions{
|
||||
Filenames: []FilenameGroup{CustomFilenames(name)},
|
||||
}))),
|
||||
} {
|
||||
result, err := testLoad[discoveryLoadConfig](source, Options[discoveryLoadConfig]{})
|
||||
var failure *LoadError
|
||||
if result != nil || !errors.As(err, &failure) || len(failure.Diagnostics()) != 1 || failure.Diagnostics()[0].Code != CodeOptionsInvalid {
|
||||
t.Fatalf("reserved component %q accepted: %#v, %v", name, result, err)
|
||||
}
|
||||
if len(failure.Sources()) != 0 {
|
||||
t.Fatalf("reserved component %q was checked after acquisition: %#v", name, failure.Sources())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user