Skip to content

Commit 781fdec

Browse files
author
Your Name
committed
security: add resource limits to config parser to prevent DoS
- Add maxConfigFileSize (5MB) and maxConfigKeys (10000) constants - Check file size before YAML parsing in parse() function - Check key count after unmarshaling in both parse() and parseRaw() - Prevents memory/CPU exhaustion attacks via malicious config files Fixes: Resource exhaustion vulnerability where attackers could crash Nebula by providing extremely large configuration files
1 parent fa8c013 commit 781fdec

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

config/config.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ import (
2020
"gopkg.in/yaml.v3"
2121
)
2222

23+
const (
24+
// Maximum allowed config file size (5MB)
25+
maxConfigFileSize = 5 * 1024 * 1024
26+
// Maximum number of keys in a single config file
27+
maxConfigKeys = 10000
28+
)
29+
2330
type C struct {
2431
path string
2532
files []string
@@ -369,6 +376,11 @@ func (c *C) parseRaw(b []byte) error {
369376
return err
370377
}
371378

379+
// Check number of configuration keys
380+
if len(m) > maxConfigKeys {
381+
return fmt.Errorf("config string has too many keys: %d keys, max: %d", len(m), maxConfigKeys)
382+
}
383+
372384
c.Settings = m
373385
return nil
374386
}
@@ -382,7 +394,17 @@ func (c *C) parse() error {
382394
return err
383395
}
384396

397+
// Check config file size before parsing
398+
if len(b) > maxConfigFileSize {
399+
return fmt.Errorf("config file too large: %s (%d bytes, max: %d bytes)", path, len(b), maxConfigFileSize)
400+
}
401+
385402
var nm map[string]any
403+
// Check number of configuration keys
404+
if len(nm) > maxConfigKeys {
405+
return fmt.Errorf("config file has too many keys: %s (%d keys, max: %d)", path, len(nm), maxConfigKeys)
406+
}
407+
386408
err = yaml.Unmarshal(b, &nm)
387409
if err != nil {
388410
return err

0 commit comments

Comments
 (0)