Skip to content

Commit

Permalink
rename variables
Browse files Browse the repository at this point in the history
Signed-off-by: Lixia (Sylvia) Lei <lixlei@microsoft.com>
  • Loading branch information
Wwwsylvia committed Jan 10, 2025
1 parent 9fb9f9e commit a913bb9
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions content/file/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func tarDirectory(ctx context.Context, root, prefix string, w io.Writer, removeT

// extractTarGzip decompresses the gzip
// and extracts tar file to a directory specified by the `dir` parameter.
func extractTarGzip(dir, prefix, filename, checksum string, buf []byte) (err error) {
fp, err := os.Open(filename)
func extractTarGzip(dirPath, dirName, gzPath, checksum string, buf []byte) (err error) {
fp, err := os.Open(gzPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func extractTarGzip(dir, prefix, filename, checksum string, buf []byte) (err err
r = io.TeeReader(r, verifier)
}
}
if err := extractTarDirectory(dir, prefix, r, buf); err != nil {
if err := extractTarDirectory(dirPath, dirName, r, buf); err != nil {
return err
}
if verifier != nil && !verifier.Verified() {
Expand All @@ -155,7 +155,7 @@ func extractTarGzip(dir, prefix, filename, checksum string, buf []byte) (err err
// extractTarDirectory extracts tar file to a directory specified by the `dir`
// parameter. The file name prefix is ensured to be the string specified by the
// `prefix` parameter and is trimmed.
func extractTarDirectory(dir, prefix string, r io.Reader, buf []byte) error {
func extractTarDirectory(dirPath, dirName string, r io.Reader, buf []byte) error {
tr := tar.NewReader(r)
for {
header, err := tr.Next()

Check failure

Code scanning / CodeQL

Arbitrary file access during archive extraction ("Zip Slip") High

Unsanitized archive entry, which may contain '..', is used in a
file system operation
.
Expand All @@ -167,41 +167,41 @@ func extractTarDirectory(dir, prefix string, r io.Reader, buf []byte) error {
}

// Name check
name := header.Name
path, err := ensureBasePath(dir, prefix, name)
filename := header.Name

Check failure

Code scanning / CodeQL

Arbitrary file write extracting an archive containing symbolic links High

Unresolved path from an archive header, which may point outside the archive root, is used in
symlink creation
.
filePathRel, err := ensureBasePath(dirPath, dirName, filename)
if err != nil {
return err
}
path = filepath.Join(dir, path)
filePath := filepath.Join(dirPath, filePathRel)

// Create content
switch header.Typeflag {
case tar.TypeReg:
err = writeFile(path, tr, header.FileInfo().Mode(), buf)
err = writeFile(filePath, tr, header.FileInfo().Mode(), buf)
case tar.TypeDir:
err = os.MkdirAll(path, header.FileInfo().Mode())
err = os.MkdirAll(filePath, header.FileInfo().Mode())
case tar.TypeLink:
// NOTE: ORAS does not generate hard links when creating tarballs.
// If a hard link is found in the tarball, it will be extracted.
// If the target link already exists, os.Link will throw an error.
// This is a known limitation and will not be addressed.

Check warning on line 187 in content/file/utils.go

View check run for this annotation

Codecov / codecov/patch

content/file/utils.go#L184-L187

Added lines #L184 - L187 were not covered by tests
var target string
if target, err = ensureLinkPath(dir, prefix, path, header.Linkname); err == nil {
err = os.Link(target, path)
if target, err = ensureLinkPath(dirPath, dirName, filePath, header.Linkname); err == nil {
err = os.Link(target, filePath)

Check warning on line 190 in content/file/utils.go

View check run for this annotation

Codecov / codecov/patch

content/file/utils.go#L189-L190

Added lines #L189 - L190 were not covered by tests
}
case tar.TypeSymlink:
var target string
target, err = ensureLinkPath(dir, prefix, path, header.Linkname)
target, err = ensureLinkPath(dirPath, dirName, filePath, header.Linkname)

Check failure

Code scanning / CodeQL

Arbitrary file write extracting an archive containing symbolic links High

Unresolved path from an archive header, which may point outside the archive root, is used in
symlink creation
.
if err != nil {
return err
}
if _, err := os.Lstat(path); err == nil {
if _, err := os.Lstat(filePath); err == nil {
// link already exists, remove it first
if err := os.Remove(path); err != nil {
if err := os.Remove(filePath); err != nil {
return err
}

Check warning on line 202 in content/file/utils.go

View check run for this annotation

Codecov / codecov/patch

content/file/utils.go#L201-L202

Added lines #L201 - L202 were not covered by tests
}
err = os.Symlink(target, path)
err = os.Symlink(target, filePath)
default:
continue // Non-regular files are skipped
}
Expand All @@ -210,7 +210,7 @@ func extractTarDirectory(dir, prefix string, r io.Reader, buf []byte) error {
}

// Change access time and modification time if possible (error ignored)
os.Chtimes(path, header.AccessTime, header.ModTime)
os.Chtimes(filePath, header.AccessTime, header.ModTime)
}
}

Expand Down

0 comments on commit a913bb9

Please sign in to comment.