Skip to content

Commit

Permalink
feat: req and resp body max size limit
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Dec 25, 2024
1 parent c1f1dd5 commit aa7ec9d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
37 changes: 36 additions & 1 deletion service/aiproxy/common/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -13,6 +14,31 @@ import (

type RequestBodyKey struct{}

const (
MaxRequestBodySize = 1024 * 1024 * 50 // 50MB
)

func LimitReader(r io.Reader, n int64) io.Reader { return &LimitedReader{r, n} }

type LimitedReader struct {
R io.Reader
N int64
}

var ErrLimitedReaderExceeded = errors.New("limited reader exceeded")

func (l *LimitedReader) Read(p []byte) (n int, err error) {
if l.N <= 0 {
return 0, ErrLimitedReaderExceeded
}
if int64(len(p)) > l.N {
p = p[0:l.N]
}
n, err = l.R.Read(p)
l.N -= int64(n)
return
}

func GetRequestBody(req *http.Request) ([]byte, error) {
requestBody := req.Context().Value(RequestBodyKey{})
if requestBody != nil {
Expand All @@ -27,8 +53,17 @@ func GetRequestBody(req *http.Request) ([]byte, error) {
}
}()
if req.ContentLength <= 0 || req.Header.Get("Content-Type") != "application/json" {
buf, err = io.ReadAll(req.Body)
buf, err = io.ReadAll(LimitReader(req.Body, MaxRequestBodySize))
if err != nil {
if errors.Is(err, ErrLimitedReaderExceeded) {
return nil, fmt.Errorf("request body too large, max: %d", MaxRequestBodySize)
}
return nil, fmt.Errorf("request body read failed: %w", err)
}
} else {
if req.ContentLength > MaxRequestBodySize {
return nil, fmt.Errorf("request body too large: %d, max: %d", req.ContentLength, MaxRequestBodySize)
}
buf = make([]byte, req.ContentLength)
_, err = io.ReadFull(req.Body, buf)
}
Expand Down
16 changes: 15 additions & 1 deletion service/aiproxy/common/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

// import webp decoder
"github.com/labring/sealos/service/aiproxy/common"
_ "golang.org/x/image/webp"
)

Expand Down Expand Up @@ -56,6 +57,10 @@ func GetImageSizeFromURL(url string) (width int, height int, err error) {
return img.Width, img.Height, nil
}

const (
MaxImageSize = 1024 * 1024 * 5 // 5MB
)

func GetImageFromURL(ctx context.Context, url string) (string, string, error) {
// Check if the URL is a data URL
matches := dataURLPattern.FindStringSubmatch(url)
Expand All @@ -82,8 +87,17 @@ func GetImageFromURL(ctx context.Context, url string) (string, string, error) {
}
var buf []byte
if resp.ContentLength <= 0 {
buf, err = io.ReadAll(resp.Body)
buf, err = io.ReadAll(common.LimitReader(resp.Body, MaxImageSize))
if err != nil {
if errors.Is(err, common.ErrLimitedReaderExceeded) {
return "", "", fmt.Errorf("image too large, max: %d", MaxImageSize)
}
return "", "", fmt.Errorf("image read failed: %w", err)
}
} else {
if resp.ContentLength > MaxImageSize {
return "", "", fmt.Errorf("image too large: %d, max: %d", resp.ContentLength, MaxImageSize)
}
buf = make([]byte, resp.ContentLength)
_, err = io.ReadFull(resp.Body, buf)
}
Expand Down

0 comments on commit aa7ec9d

Please sign in to comment.