Skip to content

Commit

Permalink
rename + fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
  • Loading branch information
Wwwsylvia committed Jan 12, 2025
1 parent e0dc271 commit 1a84546
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion content/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3067,14 +3067,17 @@ func TestStore_Dir_OverwriteSymlink_RemovalFailed(t *testing.T) {
content := []byte("hello world")
fileName := "test.txt"
filePath := filepath.Join(dirPath, fileName)
if err := os.WriteFile(filePath, content, 0444); err != nil {
if err := os.WriteFile(filePath, content, 0666); err != nil {
t.Fatal("error calling WriteFile(), error =", err)
}
// create symlink to an absolute path
symlink := filepath.Join(dirPath, "test_symlink")
if err := os.Symlink(filePath, symlink); err != nil {
t.Fatal("error calling Symlink(), error =", err)
}
if err := os.Chmod(symlink, 0444); err != nil {
t.Fatal("error calling Chmod(), error =", err)
}

src, err := New(tempDir)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions content/file/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func extractTarDirectory(dirPath, dirName string, r io.Reader, buf []byte) error

// Name check
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
.
Unresolved path from an archive header, which may point outside the archive root, is used in symlink creation.
filePathRel, err := ensureBasePath(dirPath, dirName, filename)
filePathRel, err := resolveRelToBase(dirPath, dirName, filename)
if err != nil {
return err
}
Expand Down Expand Up @@ -214,10 +214,10 @@ func extractTarDirectory(dirPath, dirName string, r io.Reader, buf []byte) error
}
}

// ensureBasePath ensures the target path is in the base path,
// resolveRelToBase ensures the target path is in the base path,
// returning its relative path to the base path.
// target can be either an absolute path or a relative path.
func ensureBasePath(baseAbs, baseRel, target string) (string, error) {
func resolveRelToBase(baseAbs, baseRel, target string) (string, error) {
base := baseRel
if filepath.IsAbs(target) {
// ensure base and target are consistent
Expand Down Expand Up @@ -257,7 +257,7 @@ func ensureLinkPath(baseAbs, baseRel, link, target string) (string, error) {
path = filepath.Join(filepath.Dir(link), target)
}
// ensure path is under baseAbs or baseRel
if _, err := ensureBasePath(baseAbs, baseRel, path); err != nil {
if _, err := resolveRelToBase(baseAbs, baseRel, path); err != nil {
return "", err
}
return target, nil
Expand Down
2 changes: 1 addition & 1 deletion content/file/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func Test_ensureBasePath(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ensureBasePath(baseAbs, baseRel, tt.target)
got, err := resolveRelToBase(baseAbs, baseRel, tt.target)
if (err != nil) != tt.wantErr {
t.Errorf("ensureBasePath() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit 1a84546

Please sign in to comment.