| 1 | package runnerscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/" |
| 5 | "compress/gzipscip-go gomod github.com/golang/go/src go1.18 `compress/gzip`/" |
| 6 | "contextscip-go gomod github.com/golang/go/src go1.18 context/" |
| 7 | "net/httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/" |
| 8 | "net/http/httptestscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/" |
| 9 | "strconvscip-go gomod github.com/golang/go/src go1.18 strconv/" |
| 10 | "testingscip-go gomod github.com/golang/go/src go1.18 testing/" |
| 11 | |
| 12 | "github.com/ffuf/ffuf/v2/pkg/ffufscip-go gomod github.com/ffuf/ffuf/v2 . `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 gzipBombscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/gzipBomb().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, decompressed int) []byte { |
| 19 | t.Helperscip-go gomod github.com/golang/go/src go1.18 testing/common#Helper().() |
| 20 | var buf bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer# |
| 21 | zw, _ := gzipscip-go gomod github.com/golang/go/src go1.18 `compress/gzip`/.NewWriterLevelscip-go gomod github.com/golang/go/src go1.18 `compress/gzip`/NewWriterLevel().(&buf, gzipscip-go gomod github.com/golang/go/src go1.18 `compress/gzip`/.BestCompressionscip-go gomod github.com/golang/go/src go1.18 `compress/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.Writescip-go gomod github.com/golang/go/src go1.18 `compress/gzip`/Writer#Write().(chunk[:n]); err != nil { |
| 29 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("gzip write: %v", err) |
| 30 | } |
| 31 | written += n |
| 32 | } |
| 33 | if err := zw.Closescip-go gomod github.com/golang/go/src go1.18 `compress/gzip`/Writer#Close().(); err != nil { |
| 34 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("gzip close: %v", err) |
| 35 | } |
| 36 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Bytes().() |
| 37 | } |
| 38 | |
| 39 | func runnerForscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/runnerFor().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, url string) (ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.RunnerProviderscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/RunnerProvider#, ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.Requestscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Request#) { |
| 40 | t.Helperscip-go gomod github.com/golang/go/src go1.18 testing/common#Helper().() |
| 41 | ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.18 context/.WithCancelscip-go gomod github.com/golang/go/src go1.18 context/WithCancel().(contextscip-go gomod github.com/golang/go/src go1.18 context/.Backgroundscip-go gomod github.com/golang/go/src go1.18 context/Background().()) |
| 42 | t.Cleanupscip-go gomod github.com/golang/go/src go1.18 testing/common#Cleanup().(cancel) |
| 43 | conf := ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.NewConfigscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/NewConfig().(ctx, cancel) |
| 44 | conf.Methodscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Config#Method. = "GET" |
| 45 | runner := NewSimpleRunnerscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/NewSimpleRunner().(&conf, false) |
| 46 | req := ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.NewRequestscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/NewRequest().(&conf) |
| 47 | req.Methodscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Request#Method. = "GET" |
| 48 | req.Urlscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Request#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_GzipBombIsCappedscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/TestExecute_GzipBombIsCapped().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 57 | const decompressed = 64 << 20 // 64 MiB, well over the 5 MiB cap |
| 58 | bomb := gzipBombscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/gzipBomb().(t, decompressed) |
| 59 | |
| 60 | srv := httptestscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/NewServer().(httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.18 `net/http`/HandlerFunc#(func(w httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.18 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#) { |
| 61 | w.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Set().("Content-Encoding", "gzip") |
| 62 | w.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Set().("Content-Length", strconvscip-go gomod github.com/golang/go/src go1.18 strconv/.Itoascip-go gomod github.com/golang/go/src go1.18 strconv/Itoa().(len(bomb))) |
| 63 | w.WriteHeaderscip-go gomod github.com/golang/go/src go1.18 `net/http`/ResponseWriter#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.18 `net/http`/StatusOK.) |
| 64 | _, _ = w.Writescip-go gomod github.com/golang/go/src go1.18 `net/http`/ResponseWriter#Write().(bomb) |
| 65 | })) |
| 66 | defer srv.Closescip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/Server#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.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().("transparent transport decode", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 72 | runner, req := runnerForscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/runnerFor().(t, srv.URLscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/Server#URL.+"/") |
| 73 | resp, err := runner.Executescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/RunnerProvider#Execute.(&req) |
| 74 | if err != nil { |
| 75 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("execute: %v", err) |
| 76 | } |
| 77 | if !resp.Cancelledscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Cancelled. { |
| 78 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("expected cancelled response, got %d bytes of data", len(resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data.)) |
| 79 | } |
| 80 | if len(resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data.) > MAX_DOWNLOAD_SIZEscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/MAX_DOWNLOAD_SIZE. { |
| 81 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("read %d bytes, must not exceed MAX_DOWNLOAD_SIZE (%d)", len(resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data.), MAX_DOWNLOAD_SIZEscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/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.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().("manual decode with preserved headers", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 90 | runner, req := runnerForscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/runnerFor().(t, srv.URLscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/Server#URL.+"/") |
| 91 | req.Headersscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Request#Headers.["Accept-Encoding"] = "gzip" |
| 92 | resp, err := runner.Executescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/RunnerProvider#Execute.(&req) |
| 93 | if err != nil { |
| 94 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("execute: %v", err) |
| 95 | } |
| 96 | if !resp.Cancelledscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Cancelled. { |
| 97 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("expected cancelled response, got %d bytes of data", len(resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data.)) |
| 98 | } |
| 99 | if len(resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data.) > MAX_DOWNLOAD_SIZEscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/MAX_DOWNLOAD_SIZE. { |
| 100 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("read %d bytes, must not exceed MAX_DOWNLOAD_SIZE (%d)", len(resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data.), MAX_DOWNLOAD_SIZEscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/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_NormalResponseUnaffectedscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/TestExecute_NormalResponseUnaffected().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 108 | body := []byte("hello world") |
| 109 | srv := httptestscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/NewServer().(httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.18 `net/http`/HandlerFunc#(func(w httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.18 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#) { |
| 110 | _, _ = w.Writescip-go gomod github.com/golang/go/src go1.18 `net/http`/ResponseWriter#Write().(body) |
| 111 | })) |
| 112 | defer srv.Closescip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/Server#Close().() |
| 113 | |
| 114 | runner, req := runnerForscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/runner`/runnerFor().(t, srv.URLscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/Server#URL.+"/") |
| 115 | resp, err := runner.Executescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/RunnerProvider#Execute.(&req) |
| 116 | if err != nil { |
| 117 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("execute: %v", err) |
| 118 | } |
| 119 | if resp.Cancelledscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Cancelled. { |
| 120 | t.Fatalscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatal().("normal response must not be cancelled") |
| 121 | } |
| 122 | if !bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Equalscip-go gomod github.com/golang/go/src go1.18 bytes/Equal().(resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data., body) { |
| 123 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("body = %q, want %q", resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data., body) |
| 124 | } |
| 125 | } |