fix(http): handle invalid gzip stream errors (#9767)

* fix(http): handle invalid gzip stream errors

Explain:

- problem: ReadUrlAsStream could panic when a response claimed gzip encoding but the body was not a valid gzip stream.

- root cause: the gzip reader error was ignored and a nil reader was deferred and read from.

- fix: return the gzip.NewReader error before registering Close or reading.

- validation: go test ./weed/util/http -run TestReadUrlAsStreamReturnsGzipReaderError -count=1; git diff --check.

* test: avoid closing shared global HTTP client in unit test
This commit is contained in:
7y-9
2026-06-01 12:21:19 -07:00
committed by GitHub
parent 1a19683ee6
commit 5ea75dcc67
2 changed files with 25 additions and 1 deletions
@@ -397,6 +397,9 @@ func ReadUrlAsStream(ctx context.Context, fileUrl, jwt string, cipherKey []byte,
switch contentEncoding {
case "gzip":
reader, err = gzip.NewReader(r.Body)
if err != nil {
return true, err
}
defer reader.Close()
default:
reader = r.Body
+22 -1
View File
@@ -1,6 +1,11 @@
package http
import "testing"
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestAppendQueryParameter(t *testing.T) {
testCases := []struct {
@@ -70,3 +75,19 @@ func TestAppendQueryParameter(t *testing.T) {
})
}
}
func TestReadUrlAsStreamReturnsGzipReaderError(t *testing.T) {
InitGlobalHttpClient()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Encoding", "gzip")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("not gzip"))
}))
defer server.Close()
_, err := ReadUrlAsStream(context.Background(), server.URL, "", nil, false, true, 0, 0, func(data []byte) {})
if err == nil {
t.Fatal("ReadUrlAsStream returned nil error for invalid gzip response")
}
}