Join an absolute path with elements to create a new Absolute path, or error. A PathTraversalError will be returned if the joined path would traverse outside of the base Absolute path. Note that this does not handle symlinks.
(elem ...string)
| 36 | // A PathTraversalError will be returned if the joined path would traverse outside of |
| 37 | // the base Absolute path. Note that this does not handle symlinks. |
| 38 | func (a Absolute) Join(elem ...string) (Absolute, error) { |
| 39 | joinedAbsolutePath, err := ParseAbsolute(filepath.Join(append([]string{a.path}, elem...)...)) |
| 40 | if err != nil { |
| 41 | return Absolute{}, fmt.Errorf("failed to parse joined path: %w", err) |
| 42 | } |
| 43 | |
| 44 | isSubpath, err := joinedAbsolutePath.isSubpathOf(a) |
| 45 | if err != nil { |
| 46 | return Absolute{}, err |
| 47 | } |
| 48 | |
| 49 | if !isSubpath { |
| 50 | return Absolute{}, PathTraversalError{ |
| 51 | Base: a, |
| 52 | Elems: elem, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return joinedAbsolutePath, nil |
| 57 | } |
| 58 | |
| 59 | func (a Absolute) isSubpathOf(dir Absolute) (bool, error) { |
| 60 | relativePath, err := filepath.Rel(dir.path, a.path) |