1
0
Fork 0

доза: ограничьте прочитанный размер тела

This commit is contained in:
Arthur K. 2026-01-27 15:44:52 +03:00
parent 7f7d10aeee
commit d087ebf369
Signed by: wzray
GPG key ID: B97F30FDC4636357

View file

@ -13,6 +13,8 @@ import (
"strings" "strings"
) )
const MAX_SIZE = 1 << 22 // 4MB
func main() { func main() {
proxy := &httputil.ReverseProxy{ proxy := &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) { Rewrite: func(r *httputil.ProxyRequest) {
@ -38,17 +40,18 @@ func main() {
// Read response body into data. If body is encoded, decode it. // Read response body into data. If body is encoded, decode it.
var data []byte var data []byte
switch r.Header.Get("Content-Encoding") { switch r.Header.Get("Content-Encoding") {
case "gzip": case "gzip":
reader, _ := gzip.NewReader(r.Body) reader, _ := gzip.NewReader(r.Body)
data, _ = io.ReadAll(reader) data, _ = io.ReadAll(io.LimitReader(reader, MAX_SIZE))
r.Body.Close() reader.Close()
default: default:
data, _ = io.ReadAll(r.Body) data, _ = io.ReadAll(io.LimitReader(r.Body, MAX_SIZE))
r.Body.Close()
} }
io.Copy(io.Discard, r.Body)
r.Body.Close()
// Rewrite 30x redirect location // Rewrite 30x redirect location
locHeader := r.Header.Get("Location") locHeader := r.Header.Get("Location")
if locHeader != "" { if locHeader != "" {