Reject special files before preparing inputs

This commit is contained in:
2026-08-29 16:13:36 +00:00
parent a68e8e31a4
commit e7e3bef1e4
4 changed files with 95 additions and 4 deletions

View File

@@ -635,6 +635,13 @@ func requireRegularReadableFile(path string, label string) error {
if strings.TrimSpace(path) == "" {
return fmt.Errorf("%s path is required", label)
}
declared, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("inspect %s %q: %w", label, path, err)
}
if declared.Mode()&os.ModeSymlink != 0 || !declared.Mode().IsRegular() {
return fmt.Errorf("%s %q is not a regular file without symlinks", label, path)
}
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("open %s %q: %w", label, path, err)
@@ -644,8 +651,13 @@ func requireRegularReadableFile(path string, label string) error {
if statErr != nil {
return fmt.Errorf("stat %s %q: %w", label, path, statErr)
}
if !info.Mode().IsRegular() {
return fmt.Errorf("%s %q is not a regular file", label, path)
current, currentErr := os.Lstat(path)
if currentErr != nil {
return fmt.Errorf("reinspect %s %q: %w", label, path, currentErr)
}
if !info.Mode().IsRegular() || current.Mode()&os.ModeSymlink != 0 || !current.Mode().IsRegular() ||
!os.SameFile(info, declared) || !os.SameFile(info, current) {
return fmt.Errorf("%s %q changed while being opened", label, path)
}
if closeErr != nil {
return fmt.Errorf("close %s %q: %w", label, path, closeErr)