Skip to content

Commit

Permalink
Merge pull request #23 from eranbrodet/XRAY-13114
Browse files Browse the repository at this point in the history
Allow skipping IsFolders check in DebArchiver.ExtractArchive()
  • Loading branch information
eranbrodet authored Feb 7, 2024
2 parents a080b05 + a6bc33e commit badd9da
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
14 changes: 13 additions & 1 deletion archive_extractor/deb_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type DebArchiver struct {
MaxNumberOfEntries int
}

const DebArchiverSkipFoldersCheckParamsKey = "DebArchiverSkipFoldersCheckParamsKey"

func (da DebArchiver) ExtractArchive(path string,
processingFunc func(*ArchiveHeader, map[string]interface{}) error, params map[string]interface{}) error {
maxBytesLimit, err := maxBytesLimit(path, da.MaxCompressRatio)
Expand All @@ -33,6 +35,7 @@ func (da DebArchiver) ExtractArchive(path string,
if rc == nil {
return errors.New(fmt.Sprintf("Failed to open deb file : %s", path))
}

entriesCount := 0
for {
if da.MaxNumberOfEntries != 0 && entriesCount > da.MaxNumberOfEntries {
Expand All @@ -49,7 +52,7 @@ func (da DebArchiver) ExtractArchive(path string,
if archiveEntry == nil {
return errors.New(fmt.Sprintf("Failed to open file : %s", path))
}
if !utils.IsFolder(archiveEntry.Name) {
if skipFolderCheck(params) || !utils.IsFolder(archiveEntry.Name) {
limitingReader := provider.CreateLimitAggregatingReadCloser(rc)
archiveHeader := NewArchiveHeader(limitingReader, archiveEntry.Name, archiveEntry.ModTime.Unix(), archiveEntry.Size)
err = processingFunc(archiveHeader, params)
Expand All @@ -60,3 +63,12 @@ func (da DebArchiver) ExtractArchive(path string,
}
return nil
}

func skipFolderCheck(params map[string]interface{}) bool {
value, found := params[DebArchiverSkipFoldersCheckParamsKey]
if !found {
return false
}
boolValue, ok := value.(bool)
return ok && boolValue
}
33 changes: 33 additions & 0 deletions archive_extractor/deb_archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,36 @@ func TestDebArchiverMaxRatioNotReached(t *testing.T) {
err := za.ExtractArchive("./fixtures/test.deb", processingReadingFunc, params())
assert.NoError(t, err)
}

func TestDebArchiverSkipFoldersCheck(t *testing.T) {
za := &DebArchiver{
MaxCompressRatio: 1,
MaxNumberOfEntries: 3,
}
paramsMap := params()

var entries []string
processor := func(header *ArchiveHeader, params map[string]interface{}) error {
entries = append(entries, header.Name)
return nil
}

archivePath := "./fixtures/testslashesinentrynames.deb"

// Default behaviour, skip entries that look like folders
err := za.ExtractArchive(archivePath, processor, paramsMap)
assert.NoError(t, err)
assert.Equal(t, 0, len(entries))

// Explicitly skip entries that look like folders
paramsMap[DebArchiverSkipFoldersCheckParamsKey] = false
err = za.ExtractArchive(archivePath, processor, paramsMap)
assert.NoError(t, err)
assert.Equal(t, 0, len(entries))

// Don't skip entries that look like folders
paramsMap[DebArchiverSkipFoldersCheckParamsKey] = true
err = za.ExtractArchive(archivePath, processor, paramsMap)
assert.NoError(t, err)
assert.Equal(t, 3, len(entries))
}
Binary file not shown.

0 comments on commit badd9da

Please sign in to comment.