fix(server): stop 431 on /api for browsers with large session cookies

This commit is contained in:
2026-11-23 14:14:28 +00:00
parent 5a9d104b37
commit 536f35ab55
9 changed files with 122 additions and 9 deletions
+5 -4
View File
@@ -391,10 +391,11 @@ func (a *App) initializeComponents() error {
func (a *App) setupServer() error {
// Create Fiber app
a.app = fiber.New(fiber.Config{
ReadTimeout: a.config.Server.ReadTimeout,
WriteTimeout: a.config.Server.WriteTimeout,
ServerHeader: "GoHoarder",
AppName: "GoHoarder v1.0",
ReadTimeout: a.config.Server.ReadTimeout,
WriteTimeout: a.config.Server.WriteTimeout,
ReadBufferSize: a.config.Server.ReadBufferSize,
ServerHeader: "GoHoarder",
AppName: "GoHoarder v1.0",
})
// Auth middleware factories. Built once, reused on every gated route.
+37
View File
@@ -0,0 +1,37 @@
package app
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/lukaszraczylo/gohoarder/pkg/config"
)
const recentPackagesURL = "/api/packages?page=1&page_size=10&sort=cached_at&order=desc"
// Fiber v3 defaults to an 8192-byte read buffer; OIDC session cookies plus a long Referer exceed it.
func TestLargeRequestHeadersAreAccepted(t *testing.T) {
a := newTestApp(t, nil)
headers := map[string]string{
"Cookie": "session=" + strings.Repeat("a", 5000),
"Referer": "https://dash.example.com/" + strings.Repeat("r", 5000),
}
code, _, body := doRequest(t, a, recentPackagesURL, headers)
if code != http.StatusOK {
t.Fatalf("GET with ~10KB of headers = %d %q, want 200", code, body)
}
}
func TestReadBufferSizeIsConfigurable(t *testing.T) {
if got := config.Default().Server.ReadBufferSize; got != 16384 {
t.Fatalf("default server.read_buffer_size = %d, want 16384", got)
}
a := newTestApp(t, func(c *config.Config) { c.Server.ReadBufferSize = 4096 })
req := httptest.NewRequest(http.MethodGet, recentPackagesURL, nil)
req.Header.Set("Cookie", "session="+strings.Repeat("a", 6000))
if _, err := a.app.Test(req); err == nil || !strings.Contains(err.Error(), "small read buffer") {
t.Errorf("6KB header with a 4096 buffer: err = %v, want a small-read-buffer rejection", err)
}
}
+14 -5
View File
@@ -35,6 +35,7 @@ type ServerConfig struct {
ReadTimeout time.Duration `mapstructure:"read_timeout" json:"read_timeout"`
WriteTimeout time.Duration `mapstructure:"write_timeout" json:"write_timeout"`
IdleTimeout time.Duration `mapstructure:"idle_timeout" json:"idle_timeout"`
ReadBufferSize int `mapstructure:"read_buffer_size" json:"read_buffer_size"` // bytes; caps the request line plus headers
}
// TLSConfig contains TLS/HTTPS configuration
@@ -137,6 +138,10 @@ type MySQLConfig struct {
// release-age gate clients apply, so a cached page never hides the entries the gate reads.
const DefaultMetadataTTL = 24 * time.Hour
// DefaultReadBufferSize is the HTTP server read buffer. Fiber's own 8192 rejects browsers
// that send OIDC session cookies plus a long Referer or URL with 431.
const DefaultReadBufferSize = 16384
// DefaultIdleTTL is how long a cached item can go unrequested before cleanup removes it.
const DefaultIdleTTL = 7 * 24 * time.Hour
@@ -334,11 +339,12 @@ type PyPIHandlerConfig struct {
func Default() *Config {
return &Config{
Server: ServerConfig{
Host: "0.0.0.0",
Port: 8080,
ReadTimeout: 5 * time.Minute,
WriteTimeout: 5 * time.Minute,
IdleTimeout: 2 * time.Minute,
Host: "0.0.0.0",
Port: 8080,
ReadTimeout: 5 * time.Minute,
WriteTimeout: 5 * time.Minute,
IdleTimeout: 2 * time.Minute,
ReadBufferSize: DefaultReadBufferSize,
TLS: TLSConfig{
Enabled: false,
},
@@ -511,6 +517,9 @@ func (c *Config) Validate() error {
}
// Validate cache
if c.Server.ReadBufferSize < 0 {
return fmt.Errorf("server.read_buffer_size cannot be negative")
}
if c.Cache.DefaultTTL < 0 {
return fmt.Errorf("cache.default_ttl cannot be negative")
}