Skip to content

Commit

Permalink
IND-1810 Validation function created.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanmanikanta2299 committed Feb 11, 2025
1 parent 399d392 commit 249550c
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,13 @@ func (p *Packer) validSymlink(root, path, target string) (bool, error) {
}

// Target falls within root.
rel, err := filepath.Rel(absRoot, absTarget)
rel, err := TargetWithinRoot(absRoot, absTarget)
if err != nil {
return false, fmt.Errorf("couldn't find relative path : %w", err)
}

if rel != ".." && !(len(rel) >= 3 && rel[:3] == "../") {
return true, nil
return false, err
} else {
if rel {
return true, nil
}
}

// The link target is outside of root. Check if it is allowed.
Expand All @@ -549,13 +549,14 @@ func (p *Packer) validSymlink(root, path, target string) (bool, error) {
prefix += "/"
}

rel, err := filepath.Rel(prefix, absTarget)
// Target falls within root.
rel, err := TargetWithinRoot(prefix, absTarget)
if err != nil {
return false, fmt.Errorf("couldn't find relative path : %w", err)
}

if rel != ".." && !(len(rel) >= 3 && rel[:3] == "../") {
return true, nil
return false, err
} else {
if rel {
return true, nil
}
}
}

Expand All @@ -567,6 +568,17 @@ func (p *Packer) validSymlink(root, path, target string) (bool, error) {
}
}

func TargetWithinRoot(root string, target string) (bool, error) {
rel, err := filepath.Rel(root, target)
if err != nil {
return false, fmt.Errorf("couldn't find relative path : %w", err)
}
if strings.HasPrefix(rel, "..") {
return false, nil
}
return true, nil
}

// checkFileMode is used to examine an os.FileMode and determine if it should
// be included in the archive, and if it has a data body which needs writing.
func checkFileMode(m os.FileMode) (keep, body bool) {
Expand Down

0 comments on commit 249550c

Please sign in to comment.