mirror of
https://github.com/project-zot/zot.git
synced 2026-06-17 12:58:02 +08:00
Improve pathRel to handle edge cases correctly
- Handle case where basepath equals targpath by returning "." - Ensure basepath is treated as directory with trailing slash to avoid false prefix matches - Fix linter issue by using += operator Addresses code review feedback. Co-authored-by: rchincha <45800463+rchincha@users.noreply.github.com>
This commit is contained in:
@@ -80,14 +80,23 @@ func (is *ImageStore) pathRel(basepath, targpath string) (string, error) {
|
||||
basepath = path.Clean(basepath)
|
||||
targpath = path.Clean(targpath)
|
||||
|
||||
// Handle case where paths are equal
|
||||
if basepath == targpath {
|
||||
return ".", nil
|
||||
}
|
||||
|
||||
// Ensure basepath is treated as a directory by adding trailing slash
|
||||
if !strings.HasSuffix(basepath, "/") {
|
||||
basepath += "/"
|
||||
}
|
||||
|
||||
// Check if targpath starts with basepath
|
||||
if !strings.HasPrefix(targpath, basepath) {
|
||||
return "", fmt.Errorf("%w: path %s is not under base path %s", zerr.ErrBadConfig, targpath, basepath)
|
||||
}
|
||||
|
||||
// Remove basepath prefix and leading slash
|
||||
// Remove basepath prefix (which now includes trailing slash)
|
||||
rel := strings.TrimPrefix(targpath, basepath)
|
||||
rel = strings.TrimPrefix(rel, "/")
|
||||
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user