Let WriteFiles write includes that leave the folder when asked

AllowParentPaths is off by default, so WriteFiles still refuses a path
with .. in it. Absolute paths are refused either way.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 23:12:34 +00:00
co-authored by Claude Sonnet 5.5
parent 8134660721
commit 37d2866fe9
3 changed files with 47 additions and 4 deletions
+22
View File
@@ -143,6 +143,28 @@ func TestWriteFilesStaysInFolder(t *testing.T) {
}
}
// TestWriteFilesAllowParentPaths checks that AllowParentPaths lets WriteFiles
// write an include that leaves the folder with ".." (also spelled with a
// backslash, which the resolver reads as a slash), and still refuses
// absolute paths.
func TestWriteFilesAllowParentPaths(t *testing.T) {
anyFile := func(string) ([]byte, error) { return []byte("<region> sample=a.wav\n"), nil }
for include, ok := range map[string]bool{"../Data/a.sfz": true, "../../a.sfz": true, "/evil.sfz": false, "..\\evil.sfz": true} {
doc, err := Parse(strings.NewReader(fmt.Sprintf("#include %q\n", include)), WithResolver(anyFile))
if err != nil {
t.Fatal(err)
}
var written []string
err = doc.WriteFiles("out/main.sfz", func(p string, _ []byte) error { written = append(written, p); return nil }, WriteOptions{AllowParentPaths: true})
if ok && (err != nil || len(written) != 2) {
t.Errorf("%s: err %v, wrote %q", include, err, written)
}
if !ok && (!errors.Is(err, ErrInvalid) || len(written) != 0) {
t.Errorf("%s: err %v, wrote %q", include, err, written)
}
}
}
// TestValidateOutsideFolder checks that Validate reports includes and samples
// that leave the folder.
func TestValidateOutsideFolder(t *testing.T) {