resolveCachePath returns path unchanged when it is empty (in-memory cache) or absolute; otherwise it joins it with parentDir, cleans the result, and refuses any path that escapes parentDir via "..". Note: This is a lexical check only and does not resolve symlinks. An attacker with write access to p
(path, parentDir string)
| 49 | // significant privileges |
| 50 | // 3. The cache only stores agent responses, not sensitive data |
| 51 | func resolveCachePath(path, parentDir string) (string, error) { |
| 52 | if path == "" || filepath.IsAbs(path) { |
| 53 | return path, nil |
| 54 | } |
| 55 | resolved := filepath.Clean(filepath.Join(parentDir, path)) |
| 56 | cleanParent := filepath.Clean(parentDir) + string(filepath.Separator) |
| 57 | if !strings.HasPrefix(resolved+string(filepath.Separator), cleanParent) { |
| 58 | return "", fmt.Errorf("cache path %q escapes parent directory", path) |
| 59 | } |
| 60 | return resolved, nil |
| 61 | } |