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:
@@ -34,6 +34,12 @@ type WriteOptions struct {
|
||||
// as a file of its own, as it would need to be written differently each
|
||||
// time, and [Document.WriteFiles] returns an error for it.
|
||||
ExpandVariables bool
|
||||
// AllowParentPaths lets [Document.WriteFiles] write an included file whose
|
||||
// path leaves the folder of the main file with "..", as the DrumGizmo kits'
|
||||
// "../Data" layout does. Absolute paths are refused either way. The writer
|
||||
// is then trusted with files outside the folder, so set it only for
|
||||
// documents whose includes are known to be safe.
|
||||
AllowParentPaths bool
|
||||
// LineEnding is the line break of the text written. The zero value is
|
||||
// [KeepLineEndings].
|
||||
LineEnding LineEnding
|
||||
@@ -71,7 +77,8 @@ func (d *Document) hasIncludes() bool {
|
||||
// WriteFiles writes the main file with the path root, and each file it includes
|
||||
// at its own path in the folder of root, using fw. It writes nothing, and
|
||||
// returns an error wrapping [ErrInvalid], for an include whose path is absolute
|
||||
// or leaves the folder, and for a file that is included more than once and
|
||||
// or leaves the folder (unless AllowParentPaths is set, which permits leaving
|
||||
// it with ".."), and for a file that is included more than once and
|
||||
// would be written differently each time.
|
||||
func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) error {
|
||||
r := &renderer{opts: opts, defines: defines{}, outputs: map[string]string{}, collect: true}
|
||||
@@ -83,7 +90,7 @@ func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) err
|
||||
for p := range r.outputs {
|
||||
// An include path comes from the text, so it can't be trusted to stay in
|
||||
// the folder.
|
||||
if !fs.ValidPath(p) {
|
||||
if !fs.ValidPath(p) && !(opts.AllowParentPaths && isParentPath(p)) {
|
||||
return fmt.Errorf("%w: the include %q is outside the folder of the main file", ErrInvalid, p)
|
||||
}
|
||||
paths = append(paths, p)
|
||||
@@ -101,6 +108,20 @@ func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) err
|
||||
return nil
|
||||
}
|
||||
|
||||
// isParentPath reports whether p is a relative, clean path that leaves its
|
||||
// folder with "..", and otherwise has no empty, "." or ".." elements after the
|
||||
// leading ones.
|
||||
func isParentPath(p string) bool {
|
||||
if p == "" || strings.Contains(p, "\\") || path.Clean(p) != p || path.IsAbs(p) {
|
||||
return false
|
||||
}
|
||||
rest := p
|
||||
for strings.HasPrefix(rest, "../") {
|
||||
rest = rest[3:]
|
||||
}
|
||||
return rest == ".." || fs.ValidPath(rest)
|
||||
}
|
||||
|
||||
// renderer writes the files of a document as the options say.
|
||||
type renderer struct {
|
||||
opts WriteOptions
|
||||
|
||||
Reference in New Issue
Block a user