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

gzip: Error out if reserved bits are set #891

Merged
merged 1 commit into from
Nov 28, 2023
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
5 changes: 5 additions & 0 deletions gzip/gunzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ func (z *Reader) readHeader() (hdr Header, err error) {
}
}

// Reserved FLG bits must be zero.
if flg>>5 != 0 {
return hdr, ErrHeader
}

z.digest = 0
if z.decompressor == nil {
z.decompressor = flate.NewReader(z.r)
Expand Down
23 changes: 21 additions & 2 deletions gzip/gunzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
oldgz "compress/gzip"
"crypto/rand"
"errors"
"io"
"os"
"strings"
Expand Down Expand Up @@ -408,11 +409,14 @@ func TestDecompressor(t *testing.T) {
}

func TestIssue6550(t *testing.T) {
f, err := os.Open("testdata/issue6550.gz")
b, err := os.ReadFile("testdata/issue6550.gz")
if err != nil {
t.Fatal(err)
}
gzip, err := NewReader(f)
// issue6550.gz has invalid flag bit set.
// TestReservedBits tests that. Fix it up for this test.
b[3] &= 0x1f
gzip, err := NewReader(bytes.NewReader(b))
if err != nil {
t.Fatalf("NewReader(testdata/issue6550.gz): %v", err)
}
Expand All @@ -435,6 +439,21 @@ func TestIssue6550(t *testing.T) {
}
}

func TestReservedBits(t *testing.T) {
f, err := os.Open("testdata/issue6550.gz")
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = NewReader(f)
if err == nil {
t.Fatalf("Reserved bits did not result in error")
}
if !errors.Is(err, ErrHeader) {
t.Fatal("Expected ErrHeader, got", err)
}
}

func TestInitialReset(t *testing.T) {
var r Reader
if err := r.Reset(bytes.NewReader(gunzipTests[1].gzip)); err != nil {
Expand Down
Loading