88package securejoin
99
1010import (
11- "errors"
1211 "os"
1312 "path/filepath"
1413 "strings"
@@ -17,40 +16,6 @@ import (
1716
1817const maxSymlinkLimit = 255
1918
20- // IsNotExist tells you if err is an error that implies that either the path
21- // accessed does not exist (or path components don't exist). This is
22- // effectively a more broad version of [os.IsNotExist].
23- func IsNotExist (err error ) bool {
24- // Check that it's not actually an ENOTDIR, which in some cases is a more
25- // convoluted case of ENOENT (usually involving weird paths).
26- return errors .Is (err , os .ErrNotExist ) || errors .Is (err , syscall .ENOTDIR ) || errors .Is (err , syscall .ENOENT )
27- }
28-
29- // errUnsafeRoot is returned if the user provides SecureJoinVFS with a path
30- // that contains ".." components.
31- var errUnsafeRoot = errors .New ("root path provided to SecureJoin contains '..' components" )
32-
33- // stripVolume just gets rid of the Windows volume included in a path. Based on
34- // some godbolt tests, the Go compiler is smart enough to make this a no-op on
35- // Linux.
36- func stripVolume (path string ) string {
37- return path [len (filepath .VolumeName (path )):]
38- }
39-
40- // hasDotDot checks if the path contains ".." components in a platform-agnostic
41- // way.
42- func hasDotDot (path string ) bool {
43- // If we are on Windows, strip any volume letters. It turns out that
44- // C:..\foo may (or may not) be a valid pathname and we need to handle that
45- // leading "..".
46- path = stripVolume (path )
47- // Look for "/../" in the path, but we need to handle leading and trailing
48- // ".."s by adding separators. Doing this with filepath.Separator is ugly
49- // so just convert to Unix-style "/" first.
50- path = filepath .ToSlash (path )
51- return strings .Contains ("/" + path + "/" , "/../" )
52- }
53-
5419// SecureJoinVFS joins the two given path components (similar to [filepath.Join]) except
5520// that the returned path is guaranteed to be scoped inside the provided root
5621// path (when evaluated). Any symbolic links in the path are evaluated with the
0 commit comments