Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ RATE_LIMIT_VERIFIED_RPM=120
# Cleanup interval for stale buckets (seconds)
RATE_LIMIT_CLEANUP_INTERVAL=300

# Request Body Size Configuration
# Maximum request body size in MB (default: 10)
MAX_REQUEST_BODY_MB=10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Document MAX_REQUEST_BODY_MB in config references

This introduces a new operator-facing gateway setting, but only .env.example mentions it; the main README configuration table, gateway/README.md optional-variable table, and .env.production.example still omit it. In production/setup contexts users will not discover the new limit knob or know its default/unit, so please keep those config references aligned with this new env var.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# Request Timeout Configuration
# Global request timeout (seconds)
REQUEST_TIMEOUT_SECONDS=60
Expand Down
10 changes: 5 additions & 5 deletions gateway/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,26 @@ func CacheMiddleware() gin.HandlerFunc {

// Read request body to generate cache key
// Check Content-Length first to reject oversized requests immediately
const maxBodySize = 10 * 1024 * 1024
maxSize := getMaxBodySize()
// ContentLength == -1 means unknown (chunked encoding or no header), proceed to MaxBytesReader
if c.Request.ContentLength > maxBodySize {
if c.Request.ContentLength > maxSize {
c.Header("Connection", "close")
c.JSON(413, gin.H{"error": "Payload too large", "max_size": "10MB"})
c.JSON(413, gin.H{"error": "Payload too large", "max_size": fmt.Sprintf("%dMB", maxSize/1024/1024)})
c.Abort()
return
}

var requestBody []byte
var err error
if c.Request.Body != nil {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(maxBodySize))
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxSize)
requestBody, err = io.ReadAll(c.Request.Body)
if err != nil {
// If body too large, MaxBytesReader returns error
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
c.Header("Connection", "close")
c.JSON(413, gin.H{"error": "Payload too large", "max_size": "10MB"})
c.JSON(413, gin.H{"error": "Payload too large", "max_size": fmt.Sprintf("%dMB", maxSize/1024/1024)})
c.Abort()
return
}
Expand Down
11 changes: 11 additions & 0 deletions gateway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,14 @@ func getVerifierTimeout() time.Duration { return getPositiveTimeout("VERIFIER_TI
func getHealthCheckTimeout() time.Duration {
return getPositiveTimeout("HEALTH_CHECK_TIMEOUT_SECONDS", 2)
}

// getMaxBodySize returns the maximum request body size in bytes, configured via
// the MAX_REQUEST_BODY_MB environment variable. Defaults to 10MB.
func getMaxBodySize() int64 {
mb := getEnvAsInt("MAX_REQUEST_BODY_MB", 10)
Comment thread
AnkanMisra marked this conversation as resolved.
Outdated
if mb <= 0 {
log.Printf("Warning: MAX_REQUEST_BODY_MB must be positive, using default 10")
mb = 10
}
return int64(mb) * 1024 * 1024
Comment thread
AnkanMisra marked this conversation as resolved.
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
6 changes: 3 additions & 3 deletions gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,13 @@ func handleSummarize(c *gin.Context) {
// Read body if not already available
if requestBody == nil {
// Read body with limit (only if middleware didn't process it)
const maxBodySize = 10 * 1024 * 1024
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(maxBodySize))
maxSize := getMaxBodySize()
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxSize)
requestBody, err = io.ReadAll(c.Request.Body)
if err != nil {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
c.JSON(413, gin.H{"error": "Payload too large", "max_size": "10MB"})
c.JSON(413, gin.H{"error": "Payload too large", "max_size": fmt.Sprintf("%dMB", maxSize/1024/1024)})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
} else {
respondError(c, 500, "request_body_read_failed", err)
}
Expand Down
Loading
Loading