Skip to content

Commit

Permalink
fix: adjust minimum file size check for compressed archive types
Browse files Browse the repository at this point in the history
* when a file is smaller than the block size and padded with zeros, compression can remove the padding, resulting in a file size less than the block size.
* for compressed archive types, the check now only ensures the size is sufficient to detect the corresponding magic numbers, rather than strictly adhering to block size.

Signed-off-by: Tony Chen <[email protected]>
  • Loading branch information
Nahemah1022 committed Aug 21, 2024
1 parent 7324344 commit 5c94da9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cmn/archive/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

const TarBlockSize = 512 // Size of each block in a tar stream

const FmtErrShortFile = "%s file is too short, should have at least %d size"

// assorted errors
type (
ErrUnknownMime struct{ detail string }
Expand Down
18 changes: 13 additions & 5 deletions cmn/archive/mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func MimeFile(file cos.LomReader, smm *memsys.MMSA, mime, archname string) (m st
n int
buf, slab = smm.AllocSize(sizeDetectMime)
)
m, n, err = _detect(file, archname, buf)
m, n, err = _detect(file, archname, m, buf)
if n > 0 {
fh, ok := file.(*os.File)
cos.Assertf(ok, "expecting os.File, got %T", file)
Expand Down Expand Up @@ -160,19 +160,27 @@ func MimeFQN(smm *memsys.MMSA, mime, archname string) (m string, err error) {
return "", err
}
buf, slab := smm.AllocSize(sizeDetectMime)
m, _, err = _detect(fh, archname, buf)
m, _, err = _detect(fh, archname, m, buf)
slab.Free(buf)
cos.Close(fh)
return
}

func _detect(file cos.LomReader, archname string, buf []byte) (m string, n int, err error) {
func _detect(file cos.LomReader, archname, mine string, buf []byte) (m string, n int, err error) {
n, err = file.Read(buf)
if err != nil {
return
}
if n < sizeDetectMime {
err = NewErrUnknownFileExt(archname, "file is too short")
if mine == ExtTar && n < sizeDetectMime {
err = NewErrUnknownFileExt(archname, fmt.Sprintf(FmtErrShortFile, ExtTar, sizeDetectMime))
return
}
if mine == ExtTarGz && n < magicGzip.offset+len(magicGzip.sig) {
err = NewErrUnknownFileExt(archname, fmt.Sprintf(FmtErrShortFile, ExtTarGz, magicGzip.offset+len(magicGzip.sig)))
return
}
if mine == ExtTarLz4 && n < magicLz4.offset+len(magicLz4.sig) {
err = NewErrUnknownFileExt(archname, fmt.Sprintf(FmtErrShortFile, ExtTarGz, magicLz4.offset+len(magicLz4.sig)))
return
}
for _, magic := range allMagics {
Expand Down

0 comments on commit 5c94da9

Please sign in to comment.