Debian Codegraph @

pkg/runner/simple_test.go

raw symbols SCIP
1 package runner
2
3 import (
4 "bytes"
5 "compress/gzip"
6 "context"
7 "net/http"
8 "net/http/httptest"
9 "strconv"
10 "testing"
11
12 "github.com/ffuf/ffuf/v2/pkg/ffuf"
13 )
14
15 // gzipBomb returns a gzip stream that decompresses to `decompressed` bytes of
16 // zeros. The compressed form is tiny (zeros compress ~1000x), so the test moves
17 // almost no data over the wire while forcing a large decompression.
18 func gzipBomb(t *testing.T, decompressed int) []byte {
19 t.Helper()
20 var buf bytes.Buffer
21 zw, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
22 chunk := make([]byte, 1<<20) // 1 MiB of zeros, reused
23 for written := 0; written < decompressed; {
24 n := len(chunk)
25 if r := decompressed - written; r < n {
26 n = r
27 }
28 if _, err := zw.Write(chunk[:n]); err != nil {
29 t.Fatalf("gzip write: %v", err)
30 }
31 written += n
32 }
33 if err := zw.Close(); err != nil {
34 t.Fatalf("gzip close: %v", err)
35 }
36 return buf.Bytes()
37 }
38
39 func runnerFor(t *testing.T, url string) (ffuf.RunnerProvider, ffuf.Request) {
40 t.Helper()
41 ctx, cancel := context.WithCancel(context.Background())
42 t.Cleanup(cancel)
43 conf := ffuf.NewConfig(ctx, cancel)
44 conf.Method = "GET"
45 runner := NewSimpleRunner(&conf, false)
46 req := ffuf.NewRequest(&conf)
47 req.Method = "GET"
48 req.Url = url
49 return runner, req
50 }
51
52 // A malicious server returning a decompression bomb must not be read into memory
53 // unbounded. The response is capped at MAX_DOWNLOAD_SIZE and marked Cancelled
54 // rather than OOM-killing the process. Regression test for the gzip-bomb DoS
55 // (CWE-409): before the fix, io.ReadAll on the decompressed stream had no bound.
56 func TestExecute_GzipBombIsCapped(t *testing.T) {
57 const decompressed = 64 << 20 // 64 MiB, well over the 5 MiB cap
58 bomb := gzipBomb(t, decompressed)
59
60 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61 w.Header().Set("Content-Encoding", "gzip")
62 w.Header().Set("Content-Length", strconv.Itoa(len(bomb)))
63 w.WriteHeader(http.StatusOK)
64 _, _ = w.Write(bomb)
65 }))
66 defer srv.Close()
67
68 // Path 1: default client. The transport adds Accept-Encoding: gzip itself,
69 // transparently decodes, and strips Content-Encoding/Content-Length, so the
70 // header-based guard never fires. The LimitReader is what saves us.
71 t.Run("transparent transport decode", func(t *testing.T) {
72 runner, req := runnerFor(t, srv.URL+"/")
73 resp, err := runner.Execute(&req)
74 if err != nil {
75 t.Fatalf("execute: %v", err)
76 }
77 if !resp.Cancelled {
78 t.Fatalf("expected cancelled response, got %d bytes of data", len(resp.Data))
79 }
80 if len(resp.Data) > MAX_DOWNLOAD_SIZE {
81 t.Fatalf("read %d bytes, must not exceed MAX_DOWNLOAD_SIZE (%d)", len(resp.Data), MAX_DOWNLOAD_SIZE)
82 }
83 })
84
85 // Path 2: client sends its own Accept-Encoding, so the transport does NOT
86 // auto-decode. Content-Encoding: gzip survives and Content-Length reflects the
87 // small compressed size, passing the guard, so the manual decoder + LimitReader
88 // must catch it.
89 t.Run("manual decode with preserved headers", func(t *testing.T) {
90 runner, req := runnerFor(t, srv.URL+"/")
91 req.Headers["Accept-Encoding"] = "gzip"
92 resp, err := runner.Execute(&req)
93 if err != nil {
94 t.Fatalf("execute: %v", err)
95 }
96 if !resp.Cancelled {
97 t.Fatalf("expected cancelled response, got %d bytes of data", len(resp.Data))
98 }
99 if len(resp.Data) > MAX_DOWNLOAD_SIZE {
100 t.Fatalf("read %d bytes, must not exceed MAX_DOWNLOAD_SIZE (%d)", len(resp.Data), MAX_DOWNLOAD_SIZE)
101 }
102 })
103 }
104
105 // A normal, small response must still be read in full and not cancelled, so the
106 // size cap does not regress ordinary fuzzing behaviour.
107 func TestExecute_NormalResponseUnaffected(t *testing.T) {
108 body := []byte("hello world")
109 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
110 _, _ = w.Write(body)
111 }))
112 defer srv.Close()
113
114 runner, req := runnerFor(t, srv.URL+"/")
115 resp, err := runner.Execute(&req)
116 if err != nil {
117 t.Fatalf("execute: %v", err)
118 }
119 if resp.Cancelled {
120 t.Fatal("normal response must not be cancelled")
121 }
122 if !bytes.Equal(resp.Data, body) {
123 t.Fatalf("body = %q, want %q", resp.Data, body)
124 }
125 }