ensureInsideDir verifies that filePath is inside dir. Both paths must be already resolved (no symlinks). Returns an error if the file escapes.
(filePath, dir string)
| 188 | // ensureInsideDir verifies that filePath is inside dir. Both paths must be |
| 189 | // already resolved (no symlinks). Returns an error if the file escapes. |
| 190 | func ensureInsideDir(filePath, dir string) error { |
| 191 | rel, err := filepath.Rel(dir, filePath) |
| 192 | if err != nil { |
| 193 | return fmt.Errorf("path escapes root directory via symlink") |
| 194 | } |
| 195 | |
| 196 | if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { |
| 197 | return fmt.Errorf("path escapes root directory via symlink") |
| 198 | } |
| 199 | |
| 200 | return nil |
| 201 | } |