Skip to content

Find, verify, and analyze leaked credentials

Cap 7z LZMA and LZMA2 dictionary sizes and keep scanning past refused entries (#5483)

Bryan Beverlyee5d0d227b53
diff --git a/go.mod b/go.mod
index a927411f6..c359adf26 100644
--- a/go.mod
+++ b/go.mod
@@ -99,6 +99,7 @@ require (
github.com/testcontainers/testcontainers-go/modules/postgres v0.42.1-0.20260423004847-9fc0246c3859
github.com/trufflesecurity/disk-buffer-reader v0.2.1
github.com/trufflesecurity/ldap-verify v0.0.0-20261012152128-4a15fafb6e19
+ github.com/ulikunitz/xz v0.5.15
github.com/wasilibs/go-re2 v1.12.0
github.com/xo/dburl v0.23.8
gitlab.com/gitlab-org/api/client-go v1.12.0
@@ -287,7 +288,6 @@ require (
github.com/tklauser/go-sysconf v0.4.0 // indirect
github.com/tklauser/numcpus v0.12.0 // indirect
github.com/trufflesecurity/touchfile v0.1.1 // indirect
- github.com/ulikunitz/xz v0.5.15 // indirect
github.com/vbatts/tar-split v0.12.1 // indirect
github.com/wasilibs/wazero-helpers v0.0.0-20250123031827-cd30c44769bb // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
diff --git a/pkg/handlers/archive.go b/pkg/handlers/archive.go
index f71c78991..02cb4ee07 100644
--- a/pkg/handlers/archive.go
+++ b/pkg/handlers/archive.go
@@ -201,9 +201,17 @@ func (h *archiveHandler) extractorHandler(dataOrErrChan chan DataOrErr) func(con
return nil
}
+ // Opening an entry can fail for a reason confined to that entry, such as
+ // a 7z coder that is unsupported or declares more memory than the limit
+ // in sevenzip_coders.go allows. Returning the error would make the
+ // extractor stop and skip every entry after this one, so report it as a
+ // warning and move on to the next entry.
f, err := file.Open()
if err != nil {
- return fmt.Errorf("error opening file %s: %w", file.Name(), err)
+ h.metrics.incErrors()
+ return common.CancellableWrite(ctx, dataOrErrChan, DataOrErr{
+ Err: fmt.Errorf("%w: error opening file %s: %v", ErrProcessingWarning, file.Name(), err),
+ })
}
defer func() { _ = f.Close() }()
diff --git a/pkg/handlers/archive_test.go b/pkg/handlers/archive_test.go
index dcb25563e..dcacf8a70 100644
--- a/pkg/handlers/archive_test.go
+++ b/pkg/handlers/archive_test.go
@@ -8,11 +8,15 @@ import (
"hash/crc32"
"io"
"net/http"
+ "os"
+ "path/filepath"
"strings"
"testing"
"unicode/utf16"
+ "github.com/bodgit/sevenzip"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context"
)
@@ -183,17 +187,207 @@ func sevenZipWithEmptyFolder(files []string) []byte {
h = append(h, names.Bytes()...)
h = append(h, 0x00, 0x00) // end of FilesInfo, end of Header
+ return sevenZipFile(append(data.Bytes(), unused...), h)
+}
+
+// sevenZipFile wraps packed stream data and an unencoded 7z header in the
+// signature header that points at it.
+func sevenZipFile(packed, header []byte) []byte {
start := make([]byte, 20)
- binary.LittleEndian.PutUint64(start[0:], uint64(data.Len()+len(unused)))
- binary.LittleEndian.PutUint64(start[8:], uint64(len(h)))
- binary.LittleEndian.PutUint32(start[16:], crc32.ChecksumIEEE(h))
+ binary.LittleEndian.PutUint64(start[0:], uint64(len(packed)))
+ binary.LittleEndian.PutUint64(start[8:], uint64(len(header)))
+ binary.LittleEndian.PutUint32(start[16:], crc32.ChecksumIEEE(header))
out := []byte{'7', 'z', 0xBC, 0xAF, 0x27, 0x1C, 0, 4}
out = binary.LittleEndian.AppendUint32(out, crc32.ChecksumIEEE(start))
out = append(out, start...)
- out = append(out, data.Bytes()...)
- out = append(out, unused...)
- return append(out, h...)
+ out = append(out, packed...)
+ return append(out, header...)
+}
+
+// append7zNumber appends v in the 7z variable-length number encoding. The
+// count of leading one bits in the first byte gives the number of
+// little-endian bytes that follow, and the rest of the first byte holds the
+// value's highest bits.
+func append7zNumber(b []byte, v uint64) []byte {
+ for extra := 0; extra < 8; extra++ {
+ if v < 1<<(7*(extra+1)) {
+ b = append(b, ^byte(0xFF>>extra)|byte(v>>(8*extra)))
+ for i := 0; i < extra; i++ {
+ b = append(b, byte(v>>(8*i)))
+ }
+ return b
+ }
+ }
+ b = append(b, 0xFF)
+ return binary.LittleEndian.AppendUint64(b, v)
+}
+
+// sevenZipEntry is one file stored alone in its own folder and decoded by a
+// single coder, so its packed and unpacked sizes are those of the folder.
+type sevenZipEntry struct {
+ name string
+ methodID []byte
+ props []byte
+ packed []byte
+ unpacked uint64
+}
+
+// sevenZipArchive builds a 7z archive holding the given entries. The coder
+// method, properties and unpack size of each entry are written as given, so
+// the header can declare values that a real compressor would not produce.
+func sevenZipArchive(entries []sevenZipEntry) []byte {
+ n := uint64(len(entries))
+ var packed []byte
+
+ h := []byte{0x01, 0x04, 0x06, 0} // Header, MainStreamsInfo, PackInfo at position 0
+ h = append7zNumber(h, n)
+ h = append(h, 0x09) // pack stream sizes
+ for _, e := range entries {
+ packed = append(packed, e.packed...)
+ h = append7zNumber(h, uint64(len(e.packed)))
+ }
+ h = append(h, 0x00, 0x07, 0x0B) // end of PackInfo, UnpackInfo, folders
+ h = append7zNumber(h, n)
+ h = append(h, 0) // folders are stored inline
+ for _, e := range entries {
+ // One coder per folder. The flag byte holds the method ID length
+ // and, when the coder has properties, bit 0x20.
+ flag := byte(len(e.methodID))
+ if len(e.props) > 0 {
+ flag |= 0x20
+ }
+ h = append(h, 1, flag)
+ h = append(h, e.methodID...)
+ if len(e.props) > 0 {
+ h = append7zNumber(h, uint64(len(e.props)))
+ h = append(h, e.props...)
+ }
+ }
+ h = append(h, 0x0C) // folder unpack sizes
+ for _, e := range entries {
+ h = append7zNumber(h, e.unpacked)
+ }
+ h = append(h, 0x00, 0x08, 0x0D) // end of UnpackInfo, SubStreamsInfo, files per folder
+ for range entries {
+ h = append(h, 1)
+ }
+ h = append(h, 0x00, 0x00, 0x05) // end of SubStreamsInfo, end of MainStreamsInfo, FilesInfo
+ h = append7zNumber(h, n)
+
+ var names bytes.Buffer
+ names.WriteByte(0) // names are stored inline
+ for _, e := range entries {
+ for _, u := range utf16.Encode([]rune(e.name + "\x00")) {
+ _ = binary.Write(&names, binary.LittleEndian, u)
+ }
+ }
+ h = append(h, 0x11)
+ h = append7zNumber(h, uint64(names.Len()))
+ h = append(h, names.Bytes()...)
+ h = append(h, 0x00, 0x00) // end of FilesInfo, end of Header
+
+ return sevenZipFile(packed, h)
+}
+
+// handleArchive runs the archive handler over data and collects the content
+// of every chunk it emits along with every error.
+func handleArchive(t *testing.T, data []byte) (chunks [][]byte, errs []error) {
+ t.Helper()
+ ctx := logContext.Background()
+ rdr, err := newFileReader(ctx, io.NopCloser(bytes.NewReader(data)))
+ require.NoError(t, err)
+ defer func() { _ = rdr.Close() }()
+
+ for dataOrErr := range newArchiveHandler().HandleFile(ctx, rdr) {
+ if dataOrErr.Err != nil {
+ errs = append(errs, dataOrErr.Err)
+ continue
+ }
+ chunks = append(chunks, dataOrErr.Data)
+ }
+ return chunks, errs
+}
+
+// TestArchiveHandler7zCoders extracts archives written by 7-Zip with each of
+// the coders that sevenzip_coders.go replaces. The expected content comes from
+// the CRC-32 of each file recorded in the archive header, so it does not
+// depend on the decoders under test.
+func TestArchiveHandler7zCoders(t *testing.T) {
+ for _, name := range []string{"ppmd.7z", "lzma.7z", "lzma2.7z"} {
+ t.Run(name, func(t *testing.T) {
+ data, err := os.ReadFile(filepath.Join("testdata", name))
+ require.NoError(t, err)
+
+ r, err := sevenzip.NewReader(bytes.NewReader(data), int64(len(data)))
+ require.NoError(t, err)
+ var want []uint32
+ for _, f := range r.File {
+ if !f.FileInfo().IsDir() {
+ want = append(want, f.CRC32)
+ }
+ }
+ require.NotEmpty(t, want)
+
+ chunks, errs := handleArchive(t, data)
+ assert.Empty(t, errs)
+ var got []uint32
+ for _, c := range chunks {
+ got = append(got, crc32.ChecksumIEEE(c))
+ }
+ assert.ElementsMatch(t, want, got)
+ })
+ }
+}
+
+// TestArchiveHandler7zContinuesPastRefusedEntry checks that an entry whose
+// coder declares more memory than maxCoderMemory is reported as a warning and
+// that the entries after it are still scanned.
+func TestArchiveHandler7zContinuesPastRefusedEntry(t *testing.T) {
+ const declared = 512 << 20 // above maxCoderMemory, below the handler's file size limit
+ tests := []struct {
+ name string
+ methodID []byte
+ props []byte
+ }{
+ {"ppmd", ppmdMethodID, binary.LittleEndian.AppendUint32([]byte{6}, declared)},
+ {"lzma", lzmaMethodID, binary.LittleEndian.AppendUint32([]byte{0x5d}, declared)},
+ {"lzma2", lzma2MethodID, []byte{lzma2DictProp39}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ archive := sevenZipArchive([]sevenZipEntry{
+ {name: "a.txt", methodID: tt.methodID, props: tt.props, packed: make([]byte, 16), unpacked: declared},
+ {name: "b.txt", methodID: []byte{0x00}, packed: []byte("file b\n"), unpacked: 7},
+ })
+
+ chunks, errs := handleArchive(t, archive)
+ require.Len(t, errs, 1)
+ assert.ErrorIs(t, errs[0], ErrProcessingWarning)
+ assert.ErrorContains(t, errs[0], "exceeds limit")
+ require.Len(t, chunks, 1)
+ assert.Equal(t, "file b\n", string(chunks[0]))
+ })
+ }
+}
+
+// TestArchiveHandler7zLargeDeclaredDictionary checks that an LZMA2 entry
+// declaring a dictionary far above maxCoderMemory still extracts when the
+// data it holds is small.
+func TestArchiveHandler7zLargeDeclaredDictionary(t *testing.T) {
+ content := []byte("file a\n")
+ archive := sevenZipArchive([]sevenZipEntry{{
+ name: "a.txt",
+ methodID: lzma2MethodID,
+ props: []byte{lzma2DictProp39},
+ packed: encodeLZMA2(t, content),
+ unpacked: uint64(len(content)),
+ }})
+
+ chunks, errs := handleArchive(t, archive)
+ assert.Empty(t, errs)
+ require.Len(t, chunks, 1)
+ assert.Equal(t, content, chunks[0])
}
func TestArchiveHandler7zEmptyFolder(t *testing.T) {
diff --git a/pkg/handlers/sevenzip_coders.go b/pkg/handlers/sevenzip_coders.go
new file mode 100644
index 000000000..353b3b2ba
--- /dev/null
+++ b/pkg/handlers/sevenzip_coders.go
@@ -0,0 +1,144 @@
+package handlers
+
+import (
+ "bufio"
+ "bytes"
+ "encoding/binary"
+ "fmt"
+ "io"
+
+ "github.com/bodgit/sevenzip"
+ "github.com/stangelandcl/ppmd"
+ "github.com/ulikunitz/xz/lzma"
+)
+
+// maxCoderMemory caps the working memory that a PPMd, LZMA or LZMA2 coder in a
+// 7z archive may make the decoder allocate. Each of these decoders allocates
+// the size declared in the archive header in one call when an entry is opened,
+// before any archive size limit applies, so without a cap a small crafted
+// archive can force an allocation of up to 4 GiB. 7-Zip's highest presets use
+// 192 MiB for the PPMd model and 64 MiB for LZMA and LZMA2 dictionaries.
+const maxCoderMemory = 256 << 20
+
+// 7z method IDs of the coders registered below.
+var (
+ ppmdMethodID = []byte{0x03, 0x04, 0x01}
+ lzmaMethodID = []byte{0x03, 0x01, 0x01}
+ lzma2MethodID = []byte{0x21}
+)
+
+// sevenzip registers its own decompressors for these methods in its init, which
+// runs before this one because this package imports it. Registering again
+// replaces them for every 7z reader in the process.
+func init() {
+ sevenzip.RegisterDecompressor(ppmdMethodID, newCappedPPMdReader)
+ sevenzip.RegisterDecompressor(lzmaMethodID, newCappedLZMAReader)
+ sevenzip.RegisterDecompressor(lzma2MethodID, newCappedLZMA2Reader)
+}
+
+// newCappedPPMdReader is a 7z PPMd decompressor that rejects coders declaring
+// more than maxCoderMemory. Otherwise it decodes the same way as the PPMd
+// decompressor that sevenzip registers. Unlike a dictionary, the PPMd model
+// size changes how the data decodes, so it cannot be reduced to fit the data.
+func newCappedPPMdReader(props []byte, uncompressedSize uint64, readers []io.ReadCloser) (io.ReadCloser, error) {
+ if len(readers) != 1 {
+ return nil, fmt.Errorf("ppmd: need exactly one reader")
+ }
+ if len(props) != 5 {
+ return nil, fmt.Errorf("ppmd: need 5 property bytes, got %d", len(props))
+ }
+ order := int(props[0])
+ memory := binary.LittleEndian.Uint32(props[1:])
+ if memory > maxCoderMemory {
+ return nil, fmt.Errorf("ppmd: model memory %d exceeds limit %d", memory, maxCoderMemory)
+ }
+
+ r, err := ppmd.NewH7zReader(readers[0], order, int(memory), int(uncompressedSize))
+ if err != nil {
+ return nil, fmt.Errorf("ppmd: error creating reader: %w", err)
+ }
+ return struct {
+ io.Reader
+ io.Closer
+ }{&r, readers[0]}, nil
+}
+
+// newCappedLZMAReader is a 7z LZMA decompressor that sizes the dictionary with
+// cappedDictSize. Otherwise it decodes the same way as the LZMA decompressor
+// that sevenzip registers, which prepends the coder properties and the
+// uncompressed size to the stream to form a classic LZMA header.
+func newCappedLZMAReader(props []byte, uncompressedSize uint64, readers []io.ReadCloser) (io.ReadCloser, error) {
+ if len(readers) != 1 {
+ return nil, fmt.Errorf("lzma: need exactly one reader")
+ }
+ if len(props) != 5 {
+ return nil, fmt.Errorf("lzma: need 5 property bytes, got %d", len(props))
+ }
+ dictSize, err := cappedDictSize(uint64(binary.LittleEndian.Uint32(props[1:])), uncompressedSize)
+ if err != nil {
+ return nil, fmt.Errorf("lzma: %w", err)
+ }
+
+ header := make([]byte, 0, lzma.HeaderLen)
+ header = append(header, props[0])
+ header = binary.LittleEndian.AppendUint32(header, uint32(dictSize))
+ header = binary.LittleEndian.AppendUint64(header, uncompressedSize)
+
+ // The LZMA decoder reads byte by byte, and a reader without ReadByte is
+ // wrapped in one that issues a Read call per byte. The input belongs to
+ // this coder alone, so buffering past the end of the stream is harmless.
+ r, err := lzma.NewReader(bufio.NewReader(io.MultiReader(bytes.NewReader(header), readers[0])))
+ if err != nil {
+ return nil, fmt.Errorf("lzma: error creating reader: %w", err)
+ }
+ return struct {
+ io.Reader
+ io.Closer
+ }{r, readers[0]}, nil
+}
+
+// newCappedLZMA2Reader is a 7z LZMA2 decompressor that sizes the dictionary
+// with cappedDictSize. Otherwise it decodes the same way as the LZMA2
+// decompressor that sevenzip registers.
+func newCappedLZMA2Reader(props []byte, uncompressedSize uint64, readers []io.ReadCloser) (io.ReadCloser, error) {
+ if len(readers) != 1 {
+ return nil, fmt.Errorf("lzma2: need exactly one reader")
+ }
+ if len(props) != 1 {
+ return nil, fmt.Errorf("lzma2: need 1 property byte, got %d", len(props))
+ }
+ if props[0] > 40 {
+ return nil, fmt.Errorf("lzma2: invalid dictionary size property %d", props[0])
+ }
+ // The property byte encodes the dictionary size as 2 or 3 times a power of
+ // two, the same decoding as Lzma2Dec.c in the 7-Zip sources.
+ declared := uint64(2|props[0]&1) << (props[0]/2 + 11)
+ dictSize, err := cappedDictSize(declared, uncompressedSize)
+ if err != nil {
+ return nil, fmt.Errorf("lzma2: %w", err)
+ }
+
+ config := lzma.Reader2Config{DictCap: int(dictSize)}
+ r, err := config.NewReader2(readers[0])
+ if err != nil {
+ return nil, fmt.Errorf("lzma2: error creating reader: %w", err)
+ }
+ return struct {
+ io.Reader
+ io.Closer
+ }{r, readers[0]}, nil
+}
+
+// cappedDictSize returns the dictionary size to decode an LZMA or LZMA2 stream
+// with, or an error if it exceeds maxCoderMemory. A back-reference never
+// reaches further than the data decoded so far, so a dictionary larger than
+// the stream's uncompressed size is never filled. Decoding with one reduced to
+// that size gives the same output, and it means an archive that declares a
+// large dictionary for a small amount of data is not rejected.
+func cappedDictSize(declared, uncompressedSize uint64) (uint64, error) {
+ size := max(min(declared, uncompressedSize), lzma.MinDictCap)
+ if size > maxCoderMemory {
+ return 0, fmt.Errorf("dictionary size %d exceeds limit %d", size, maxCoderMemory)
+ }
+ return size, nil
+}
diff --git a/pkg/handlers/sevenzip_coders_test.go b/pkg/handlers/sevenzip_coders_test.go
new file mode 100644
index 000000000..ec664fd67
--- /dev/null
+++ b/pkg/handlers/sevenzip_coders_test.go
@@ -0,0 +1,154 @@
+package handlers
+
+import (
+ "bytes"
+ "encoding/binary"
+ "io"
+ "runtime"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+ "github.com/ulikunitz/xz/lzma"
+)
+
+// largeDeclaredDict is a dictionary size far above maxCoderMemory that a
+// crafted 7z header can declare.
+const largeDeclaredDict = 3 << 30
+
+// lzma2DictProp39 is the LZMA2 property byte that declares a 3 GiB dictionary.
+const lzma2DictProp39 = 39
+
+func oneReader(data []byte) []io.ReadCloser {
+ return []io.ReadCloser{io.NopCloser(bytes.NewReader(data))}
+}
+
+// encodeLZMA compresses data as a raw 7z LZMA stream and returns its 5
+// property bytes with the dictionary size replaced by dictSize.
+func encodeLZMA(t *testing.T, data []byte, dictSize uint32) (props, stream []byte) {
+ t.Helper()
+ var buf bytes.Buffer
+ w, err := lzma.WriterConfig{Size: int64(len(data))}.NewWriter(&buf)
+ require.NoError(t, err)
+ _, err = w.Write(data)
+ require.NoError(t, err)
+ require.NoError(t, w.Close())
+
+ // A classic LZMA stream starts with a 13-byte header: the properties
+ // byte, the dictionary size and the uncompressed size. 7z stores the
+ // first two as coder properties and the size in the archive header.
+ out := buf.Bytes()
+ props = append([]byte{out[0]}, binary.LittleEndian.AppendUint32(nil, dictSize)...)
+ return props, out[lzma.HeaderLen:]
+}
+
+// encodeLZMA2 compresses data as a raw LZMA2 stream.
+func encodeLZMA2(t *testing.T, data []byte) []byte {
+ t.Helper()
+ var buf bytes.Buffer
+ w, err := lzma.Writer2Config{}.NewWriter2(&buf)
+ require.NoError(t, err)
+ _, err = w.Write(data)
+ require.NoError(t, err)
+ require.NoError(t, w.Close())
+ return buf.Bytes()
+}
+
+// heapGrowth reports how much the live heap grows while open runs and the
+// reader it returns is still reachable.
+func heapGrowth(t *testing.T, open func() (io.ReadCloser, error)) (io.ReadCloser, uint64) {
+ t.Helper()
+ var before, after runtime.MemStats
+ runtime.GC()
+ runtime.ReadMemStats(&before)
+ rc, err := open()
+ require.NoError(t, err)
+ runtime.ReadMemStats(&after)
+ if after.HeapAlloc < before.HeapAlloc {
+ return rc, 0
+ }
+ return rc, after.HeapAlloc - before.HeapAlloc
+}
+
+func TestCappedPPMdReaderRejectsLargeModelMemory(t *testing.T) {
+ props := []byte{6, 0, 0, 0, 0}
+
+ binary.LittleEndian.PutUint32(props[1:], 0xF0000000)
+ _, err := newCappedPPMdReader(props, 10, oneReader(make([]byte, 16)))
+ assert.ErrorContains(t, err, "exceeds limit")
+
+ binary.LittleEndian.PutUint32(props[1:], 1<<20)
+ rc, err := newCappedPPMdReader(props, 10, oneReader(make([]byte, 16)))
+ assert.NoError(t, err)
+ assert.NoError(t, rc.Close())
+}
+
+func TestCappedLZMAReaderShrinksDictionaryToData(t *testing.T) {
+ data := []byte("file a\nThe LZMA header declares a dictionary far larger than this.\n")
+ props, stream := encodeLZMA(t, data, largeDeclaredDict)
+
+ rc, grown := heapGrowth(t, func() (io.ReadCloser, error) {
+ return newCappedLZMAReader(props, uint64(len(data)), oneReader(stream))
+ })
+ assert.Less(t, grown, uint64(64<<20), "dictionary should be sized to the data, not the header")
+
+ got, err := io.ReadAll(rc)
+ require.NoError(t, err)
+ assert.Equal(t, data, got)
+ assert.NoError(t, rc.Close())
+}
+
+func TestCappedLZMAReaderRejectsLargeDictionary(t *testing.T) {
+ props, stream := encodeLZMA(t, []byte("unused"), largeDeclaredDict)
+ _, err := newCappedLZMAReader(props, 1<<32, oneReader(stream))
+ assert.ErrorContains(t, err, "exceeds limit")
+}
+
+func TestCappedLZMA2ReaderShrinksDictionaryToData(t *testing.T) {
+ data := []byte("file a\nThe LZMA2 property declares a dictionary far larger than this.\n")
+ stream := encodeLZMA2(t, data)
+
+ rc, grown := heapGrowth(t, func() (io.ReadCloser, error) {
+ return newCappedLZMA2Reader([]byte{lzma2DictProp39}, uint64(len(data)), oneReader(stream))
+ })
+ assert.Less(t, grown, uint64(64<<20), "dictionary should be sized to the data, not the property")
+
+ got, err := io.ReadAll(rc)
+ require.NoError(t, err)
+ assert.Equal(t, data, got)
+ assert.NoError(t, rc.Close())
+}
+
+func TestCappedLZMA2ReaderRejectsLargeDictionary(t *testing.T) {
+ _, err := newCappedLZMA2Reader([]byte{lzma2DictProp39}, 1<<32, oneReader(nil))
+ assert.ErrorContains(t, err, "exceeds limit")
+}
+
+func TestCappedCodersRejectMalformedProperties(t *testing.T) {
+ tests := []struct {
+ name string
+ open func() (io.ReadCloser, error)
+ }{
+ {"ppmd short properties", func() (io.ReadCloser, error) {
+ return newCappedPPMdReader([]byte{6}, 10, oneReader(nil))
+ }},
+ {"lzma short properties", func() (io.ReadCloser, error) {
+ return newCappedLZMAReader([]byte{0x5d}, 10, oneReader(nil))
+ }},
+ {"lzma2 missing property", func() (io.ReadCloser, error) {
+ return newCappedLZMA2Reader(nil, 10, oneReader(nil))
+ }},
+ {"lzma2 property out of range", func() (io.ReadCloser, error) {
+ return newCappedLZMA2Reader([]byte{41}, 10, oneReader(nil))
+ }},
+ {"lzma two readers", func() (io.ReadCloser, error) {
+ return newCappedLZMAReader(make([]byte, 5), 10, append(oneReader(nil), oneReader(nil)...))
+ }},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ _, err := tt.open()
+ assert.Error(t, err)
+ })
+ }
+}
diff --git a/pkg/handlers/sevenzip_ppmd.go b/pkg/handlers/sevenzip_ppmd.go
deleted file mode 100644
index f6f033169..000000000
--- a/pkg/handlers/sevenzip_ppmd.go
+++ /dev/null
@@ -1,50 +0,0 @@
-package handlers
-
-import (
- "encoding/binary"
- "fmt"
- "io"
-
- "github.com/bodgit/sevenzip"
- "github.com/stangelandcl/ppmd"
-)
-
-// maxPPMdMemory caps the model memory that a PPMd coder in a 7z archive may
-// declare. The PPMd decoder allocates the declared size in one call when an
-// entry is opened, before any archive size limit applies, so without a cap a
-// small crafted archive could force an allocation of almost 4 GiB. 7-Zip's
-// highest PPMd preset uses 192 MiB.
-const maxPPMdMemory = 256 << 20
-
-// ppmdMethodID is the 7z method ID of the PPMd coder.
-var ppmdMethodID = []byte{0x03, 0x04, 0x01}
-
-func init() {
- sevenzip.RegisterDecompressor(ppmdMethodID, newCappedPPMdReader)
-}
-
-// newCappedPPMdReader is a 7z PPMd decompressor that rejects coders declaring
-// more than maxPPMdMemory. Otherwise it decodes the same way as the PPMd
-// decompressor that sevenzip registers.
-func newCappedPPMdReader(props []byte, uncompressedSize uint64, readers []io.ReadCloser) (io.ReadCloser, error) {
- if len(readers) != 1 {
- return nil, fmt.Errorf("ppmd: need exactly one reader")
- }
- if len(props) != 5 {
- return nil, fmt.Errorf("ppmd: need 5 property bytes, got %d", len(props))
- }
- order := int(props[0])
- memory := binary.LittleEndian.Uint32(props[1:])
- if memory > maxPPMdMemory {
- return nil, fmt.Errorf("ppmd: model memory %d exceeds limit %d", memory, maxPPMdMemory)
- }
-
- r, err := ppmd.NewH7zReader(readers[0], order, int(memory), int(uncompressedSize))
- if err != nil {
- return nil, fmt.Errorf("ppmd: error creating reader: %w", err)
- }
- return struct {
- io.Reader
- io.Closer
- }{&r, readers[0]}, nil
-}
diff --git a/pkg/handlers/sevenzip_ppmd_test.go b/pkg/handlers/sevenzip_ppmd_test.go
deleted file mode 100644
index 41a0ba070..000000000
--- a/pkg/handlers/sevenzip_ppmd_test.go
+++ /dev/null
@@ -1,24 +0,0 @@
-package handlers
-
-import (
- "bytes"
- "encoding/binary"
- "io"
- "testing"
-
- "github.com/stretchr/testify/assert"
-)
-
-func TestCappedPPMdReaderRejectsLargeModelMemory(t *testing.T) {
- props := []byte{6, 0, 0, 0, 0}
- readers := func() []io.ReadCloser { return []io.ReadCloser{io.NopCloser(bytes.NewReader(make([]byte, 16)))} }
-
- binary.LittleEndian.PutUint32(props[1:], 0xF0000000)
- _, err := newCappedPPMdReader(props, 10, readers())
- assert.ErrorContains(t, err, "exceeds limit")
-
- binary.LittleEndian.PutUint32(props[1:], 1<<20)
- rc, err := newCappedPPMdReader(props, 10, readers())
- assert.NoError(t, err)
- assert.NoError(t, rc.Close())
-}
diff --git a/pkg/handlers/testdata/lzma.7z b/pkg/handlers/testdata/lzma.7z
new file mode 100644
index 000000000..cfd4a7f94
Binary files /dev/null and b/pkg/handlers/testdata/lzma.7z differ
diff --git a/pkg/handlers/testdata/lzma2.7z b/pkg/handlers/testdata/lzma2.7z
new file mode 100644
index 000000000..d08f4b579
Binary files /dev/null and b/pkg/handlers/testdata/lzma2.7z differ
diff --git a/pkg/handlers/testdata/ppmd.7z b/pkg/handlers/testdata/ppmd.7z
new file mode 100644
index 000000000..1d3bc8708
Binary files /dev/null and b/pkg/handlers/testdata/ppmd.7z differ