Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code scanning alert no. 49: Arbitrary file access during archive extraction ("Zip Slip") #368

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
ErrPathAlreadyExists = errors.New("path already exists")
ErrCopyingEqualPaths = errors.New("failed copying files: source is the same as destination")
ErrMovingEqualPaths = errors.New("failed moving files: source is the same as destination")
ErrInvalidFilePath = errors.New("invalid file path")

// Cryptography errors
ErrIntegrityCheckFailed = errors.New("checksum verification failed")
Expand Down
10 changes: 10 additions & 0 deletions cmd/installer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ func (p *PackType) validate() error {
p.Subfolder = filepath.Dir(file.Name)
}

// Ensure the file path does not contain ".."
if strings.Contains(file.Name, "..") {
log.Errorf("File \"%s\" invalid file path", file.Name)
return errs.ErrInvalidFilePath
}

// Read pack's pdsc
tmpPdscFileName := filepath.Join(os.TempDir(), utils.RandStringBytes(10))
defer os.RemoveAll(tmpPdscFileName)
Expand Down Expand Up @@ -198,6 +204,10 @@ func (p *PackType) validate() error {

p.Pdsc.FileName = file.Name
return nil
} else {
if strings.Contains(file.Name, "..") {
return errs.ErrInsecureZipFileName
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions cmd/installer/root_pack_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,25 @@ func TestAddPack(t *testing.T) {
assert.False(utils.FileExists(installer.Installation.PackIdx))
})

t.Run("test installing a pack with .. in pdsc name", func(t *testing.T) {
localTestingDir := "test-add-pack-with-dot-dot-name"
assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot))
installer.UnlockPackRoot()
installer.Installation.WebDir = filepath.Join(testDir, "public_index")
defer removePackRoot(localTestingDir)

packPath := packWithParentDirectoryFiles

err := installer.AddPack(packPath, !CheckEula, !ExtractEula, !ForceReinstall, !NoRequirements, Timeout)

// Sanity check
assert.NotNil(err)
assert.Equal(errs.ErrInvalidFilePath, err)

// Make sure pack.idx never got touched
assert.False(utils.FileExists(installer.Installation.PackIdx))
})

t.Run("test installing a pack with version not present in the pdsc file", func(t *testing.T) {
localTestingDir := "test-add-pack-with-version-not-present-in-the-pdsc-file"
assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot))
Expand Down
1 change: 1 addition & 0 deletions cmd/installer/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ var (
packWithMalformedURL = "http://:malformed-url*/TheVendor.PackName.1.2.3.pack"
packWithoutPdscFileInside = filepath.Join(testDir, "PackWithout.PdscFileInside.1.2.3.pack")
packWithTaintedCompressedFiles = filepath.Join(testDir, "PackWith.TaintedFiles.1.2.3.pack")
packWithParentDirectoryFiles = filepath.Join(testDir, "PackWith.ParentDirectoryFiles.1.2.3.pack")

// Packs with packid names only
publicRemotePackPackID = "TheVendor.PublicRemotePack"
Expand Down
Binary file not shown.
Loading