Documentation
¶
Overview ¶
Package xzlimit guards the xz decoder against streams that declare an oversized dictionary: the decoder allocates whatever the header asks for, up to 4 GiB, before reading any data. NewReader vets every block header against MaxDictSize first and decodes a single stream; CheckDictCap does the same for a stream already in memory.
Example ¶
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"github.com/ulikunitz/xz"
"github.com/M0Rf30/pkgformat/xzlimit"
)
func main() {
var packed bytes.Buffer
w, _ := xz.NewWriter(&packed)
_, _ = w.Write([]byte("Packages index"))
_ = w.Close()
r, err := xzlimit.NewReader(bufio.NewReader(&packed))
if err != nil {
fmt.Println(err)
return
}
out, _ := io.ReadAll(r)
fmt.Println(string(out))
}
Output: Packages index
Index ¶
Examples ¶
Constants ¶
const MaxDictSize = 64 << 20
MaxDictSize bounds the LZ window (xz dictionary / zstd window) an untrusted stream may declare. The decoders allocate it up front, so a tiny crafted stream could otherwise demand gigabytes; 64 MiB covers `xz -9`.
Variables ¶
var ErrDictTooLarge = errors.New("xz dictionary size exceeds limit")
ErrDictTooLarge is returned for an xz stream declaring an oversized dictionary.
Functions ¶
func CheckDictCap ¶
CheckDictCap walks the blocks of the (first) xz stream in raw and rejects it when any block's LZMA2 filter declares a dictionary larger than MaxDictSize. Anything it cannot parse is left to the decoder to reject. It does not inspect further streams; use NewReader for untrusted streaming input.
func NewReader ¶
NewReader returns an xz decoder over br. Every block header of the stream is inspected before the decoder sees it, and a block declaring a dictionary above MaxDictSize is rejected with ErrDictTooLarge. The decoder is limited to a single stream (trailing data or a second stream is an error), because the decoder raises its dictionary to whatever each later stream or block declares.
Types ¶
This section is empty.