Skip to content

Commit

Permalink
Add ReadHeaderTimeout to prevent Slowloris attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Sep 24, 2023
1 parent 6563cee commit 2b6f503
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
6 changes: 3 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,9 @@ var runCmd = &cobra.Command{

// Create a new metrics server.
metricsServer = &http.Server{
Addr: metricsConfig.Address,
Handler: handler,
Addr: metricsConfig.Address,
Handler: handler,
ReadHeaderTimeout: metricsConfig.GetReadHeaderTimeout(),
}

// Start the metrics server.
Expand All @@ -414,7 +415,6 @@ var runCmd = &cobra.Command{
}(conf.Global.Metrics[config.Default], logger)

// This is a notification hook, so we don't care about the result.
// TODO: Use a context with a timeout
if data, ok := conf.GlobalKoanf.Get("loggers").(map[string]interface{}); ok {
_, err = pluginRegistry.Run(
pluginTimeoutCtx, data, v1.HookName_HOOK_NAME_ON_NEW_LOGGER)
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ func (c *Config) LoadDefaults(ctx context.Context) {
}

defaultMetric := Metrics{
Enabled: true,
Address: DefaultMetricsAddress,
Path: DefaultMetricsPath,
Enabled: true,
Address: DefaultMetricsAddress,
Path: DefaultMetricsPath,
ReadHeaderTimeout: DefaultReadHeaderTimeout,
}

defaultClient := Client{
Expand Down
5 changes: 3 additions & 2 deletions config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ const (
ChecksumBufferSize = 65536

// Metrics constants.
DefaultMetricsAddress = "localhost:9090"
DefaultMetricsPath = "/metrics"
DefaultMetricsAddress = "localhost:9090"
DefaultMetricsPath = "/metrics"
DefaultReadHeaderTimeout = 10 * time.Second

// Sentry constants.
DefaultTraceSampleRate = 0.2
Expand Down
7 changes: 7 additions & 0 deletions config/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,10 @@ func GetDefaultConfigFilePath(filename string) string {
// The fallback is the current directory.
return filepath.Join("./", filename)
}

func (m Metrics) GetReadHeaderTimeout() time.Duration {
if m.ReadHeaderTimeout <= 0 {
return DefaultReadHeaderTimeout
}
return m.ReadHeaderTimeout
}
6 changes: 6 additions & 0 deletions config/getters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,9 @@ func TestGetPlugins(t *testing.T) {
func TestGetDefaultConfigFilePath(t *testing.T) {
assert.Equal(t, GlobalConfigFilename, GetDefaultConfigFilePath(GlobalConfigFilename))
}

// TestGetReadTimeout tests the GetReadTimeout function.
func TestGetReadHeaderTimeout(t *testing.T) {
metrics := Metrics{}
assert.Equal(t, DefaultReadHeaderTimeout, metrics.GetReadHeaderTimeout())
}
7 changes: 4 additions & 3 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ type Logger struct {
}

type Metrics struct {
Enabled bool `json:"enabled"`
Address string `json:"address"`
Path string `json:"path"`
Enabled bool `json:"enabled"`
Address string `json:"address"`
Path string `json:"path"`
ReadHeaderTimeout time.Duration `json:"readHeaderTimeout" jsonschema:"oneof_type=string;integer"`
}

type Pool struct {
Expand Down
1 change: 1 addition & 0 deletions gatewayd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ metrics:
enabled: True
address: localhost:9090
path: /metrics
readHeaderTimeout: 10s # duration, prevents Slowloris attacks

clients:
default:
Expand Down

0 comments on commit 2b6f503

Please sign in to comment.