File size: 9,860 Bytes
6a7089a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | package config
import (
"fmt"
"strconv"
"strings"
)
// ValidationError represents a configuration validation error.
type ValidationError struct {
Field string
Message string
}
func (e ValidationError) Error() string {
return fmt.Sprintf("%s: %s", e.Field, e.Message)
}
// ValidateFileConfig validates a FileConfig and returns all errors found.
func ValidateFileConfig(fc *FileConfig) []error {
var errs []error
// Server validation
if fc.Server.Port != "" {
if err := validatePort(fc.Server.Port, "server.port"); err != nil {
errs = append(errs, err)
}
}
if fc.Server.Bind != "" {
if err := validateBind(fc.Server.Bind, "server.bind"); err != nil {
errs = append(errs, err)
}
}
if fc.MultiInstance.InstancePortStart != nil && fc.MultiInstance.InstancePortEnd != nil {
if *fc.MultiInstance.InstancePortStart > *fc.MultiInstance.InstancePortEnd {
errs = append(errs, ValidationError{
Field: "multiInstance.instancePortStart/End",
Message: fmt.Sprintf("start port (%d) must be <= end port (%d)", *fc.MultiInstance.InstancePortStart, *fc.MultiInstance.InstancePortEnd),
})
}
}
if fc.MultiInstance.Restart.MaxRestarts != nil {
if *fc.MultiInstance.Restart.MaxRestarts < -1 {
errs = append(errs, ValidationError{
Field: "multiInstance.restart.maxRestarts",
Message: fmt.Sprintf("must be >= 0 or -1 for unlimited (got %d)", *fc.MultiInstance.Restart.MaxRestarts),
})
}
}
if fc.MultiInstance.Restart.InitBackoffSec != nil && *fc.MultiInstance.Restart.InitBackoffSec < 1 {
errs = append(errs, ValidationError{
Field: "multiInstance.restart.initBackoffSec",
Message: fmt.Sprintf("must be >= 1 (got %d)", *fc.MultiInstance.Restart.InitBackoffSec),
})
}
if fc.MultiInstance.Restart.MaxBackoffSec != nil && *fc.MultiInstance.Restart.MaxBackoffSec < 1 {
errs = append(errs, ValidationError{
Field: "multiInstance.restart.maxBackoffSec",
Message: fmt.Sprintf("must be >= 1 (got %d)", *fc.MultiInstance.Restart.MaxBackoffSec),
})
}
if fc.MultiInstance.Restart.StableAfterSec != nil && *fc.MultiInstance.Restart.StableAfterSec < 1 {
errs = append(errs, ValidationError{
Field: "multiInstance.restart.stableAfterSec",
Message: fmt.Sprintf("must be >= 1 (got %d)", *fc.MultiInstance.Restart.StableAfterSec),
})
}
if fc.MultiInstance.Restart.InitBackoffSec != nil && fc.MultiInstance.Restart.MaxBackoffSec != nil &&
*fc.MultiInstance.Restart.InitBackoffSec > *fc.MultiInstance.Restart.MaxBackoffSec {
errs = append(errs, ValidationError{
Field: "multiInstance.restart.initBackoffSec/maxBackoffSec",
Message: fmt.Sprintf("init backoff (%d) must be <= max backoff (%d)", *fc.MultiInstance.Restart.InitBackoffSec, *fc.MultiInstance.Restart.MaxBackoffSec),
})
}
// Instance defaults validation
if fc.InstanceDefaults.Mode != "" && fc.InstanceDefaults.Mode != "headless" && fc.InstanceDefaults.Mode != "headed" {
errs = append(errs, ValidationError{
Field: "instanceDefaults.mode",
Message: fmt.Sprintf("invalid value %q (must be headless or headed)", fc.InstanceDefaults.Mode),
})
}
if fc.InstanceDefaults.StealthLevel != "" {
if !isValidStealthLevel(fc.InstanceDefaults.StealthLevel) {
errs = append(errs, ValidationError{
Field: "instanceDefaults.stealthLevel",
Message: fmt.Sprintf("invalid value %q (must be light, medium, or full)", fc.InstanceDefaults.StealthLevel),
})
}
}
if fc.InstanceDefaults.TabEvictionPolicy != "" {
if !isValidEvictionPolicy(fc.InstanceDefaults.TabEvictionPolicy) {
errs = append(errs, ValidationError{
Field: "instanceDefaults.tabEvictionPolicy",
Message: fmt.Sprintf("invalid value %q (must be reject, close_oldest, or close_lru)", fc.InstanceDefaults.TabEvictionPolicy),
})
}
}
if fc.InstanceDefaults.MaxTabs != nil && *fc.InstanceDefaults.MaxTabs < 1 {
errs = append(errs, ValidationError{
Field: "instanceDefaults.maxTabs",
Message: fmt.Sprintf("must be >= 1 (got %d)", *fc.InstanceDefaults.MaxTabs),
})
}
if fc.InstanceDefaults.MaxParallelTabs != nil && *fc.InstanceDefaults.MaxParallelTabs < 0 {
errs = append(errs, ValidationError{
Field: "instanceDefaults.maxParallelTabs",
Message: fmt.Sprintf("must be >= 0 (got %d)", *fc.InstanceDefaults.MaxParallelTabs),
})
}
// Multi-instance validation
if fc.MultiInstance.Strategy != "" {
if !isValidStrategy(fc.MultiInstance.Strategy) {
errs = append(errs, ValidationError{
Field: "multiInstance.strategy",
Message: fmt.Sprintf("invalid value %q (must be simple, explicit, simple-autorestart, or always-on)", fc.MultiInstance.Strategy),
})
}
}
if fc.MultiInstance.AllocationPolicy != "" {
if !isValidAllocationPolicy(fc.MultiInstance.AllocationPolicy) {
errs = append(errs, ValidationError{
Field: "multiInstance.allocationPolicy",
Message: fmt.Sprintf("invalid value %q (must be fcfs, round_robin, or random)", fc.MultiInstance.AllocationPolicy),
})
}
}
// Attach validation
for _, scheme := range fc.Security.Attach.AllowSchemes {
if !isValidAttachScheme(scheme) {
errs = append(errs, ValidationError{
Field: "security.attach.allowSchemes",
Message: fmt.Sprintf("invalid value %q (must be ws, wss, http, or https)", scheme),
})
}
}
// IDPI validation
errs = append(errs, validateIDPIConfig(fc.Security.IDPI)...)
// Timeouts validation
if fc.Timeouts.ActionSec < 0 {
errs = append(errs, ValidationError{
Field: "timeouts.actionSec",
Message: fmt.Sprintf("must be >= 0 (got %d)", fc.Timeouts.ActionSec),
})
}
if fc.Timeouts.NavigateSec < 0 {
errs = append(errs, ValidationError{
Field: "timeouts.navigateSec",
Message: fmt.Sprintf("must be >= 0 (got %d)", fc.Timeouts.NavigateSec),
})
}
if fc.Timeouts.ShutdownSec < 0 {
errs = append(errs, ValidationError{
Field: "timeouts.shutdownSec",
Message: fmt.Sprintf("must be >= 0 (got %d)", fc.Timeouts.ShutdownSec),
})
}
if fc.Timeouts.WaitNavMs < 0 {
errs = append(errs, ValidationError{
Field: "timeouts.waitNavMs",
Message: fmt.Sprintf("must be >= 0 (got %d)", fc.Timeouts.WaitNavMs),
})
}
return errs
}
func validatePort(port string, field string) error {
p, err := strconv.Atoi(port)
if err != nil {
return ValidationError{
Field: field,
Message: fmt.Sprintf("invalid port %q (must be a number)", port),
}
}
if p < 1 || p > 65535 {
return ValidationError{
Field: field,
Message: fmt.Sprintf("port %d out of range (must be 1-65535)", p),
}
}
return nil
}
func validateBind(bind string, field string) error {
// Accept common bind addresses
validBinds := map[string]bool{
"127.0.0.1": true,
"0.0.0.0": true,
"localhost": true,
"::1": true,
"::": true,
}
if validBinds[bind] {
return nil
}
// Basic IP format check (not exhaustive, just sanity)
// If it contains a dot, assume it's an IPv4 attempt
// If it contains a colon, assume it's an IPv6 attempt
// This is intentionally loose — the OS will reject truly invalid addresses
return nil
}
func isValidStealthLevel(level string) bool {
switch level {
case "light", "full":
return true
default:
return false
}
}
func isValidEvictionPolicy(policy string) bool {
switch policy {
case "reject", "close_oldest", "close_lru":
return true
default:
return false
}
}
func isValidStrategy(strategy string) bool {
switch strategy {
case "simple", "explicit", "simple-autorestart", "always-on":
return true
default:
return false
}
}
func isValidAllocationPolicy(policy string) bool {
switch policy {
case "fcfs", "round_robin", "random":
return true
default:
return false
}
}
func isValidAttachScheme(scheme string) bool {
switch scheme {
case "ws", "wss", "http", "https":
return true
default:
return false
}
}
func ValidStealthLevels() []string {
return []string{"light", "full"}
}
func ValidEvictionPolicies() []string {
return []string{"reject", "close_oldest", "close_lru"}
}
func ValidStrategies() []string {
return []string{"simple", "explicit", "simple-autorestart", "always-on"}
}
// validateIDPIConfig validates the security.idpi sub-section.
// Validation is skipped when IDPI is disabled; a zero-value IDPIConfig is always valid.
func validateIDPIConfig(cfg IDPIConfig) []error {
if !cfg.Enabled {
return nil
}
var errs []error
for _, domain := range cfg.AllowedDomains {
trimmed := strings.TrimSpace(domain)
if trimmed == "" {
errs = append(errs, ValidationError{
Field: "security.idpi.allowedDomains",
Message: "domain pattern must not be empty or whitespace-only",
})
continue
}
if strings.ContainsAny(trimmed, " \t") {
errs = append(errs, ValidationError{
Field: "security.idpi.allowedDomains",
Message: fmt.Sprintf("domain pattern %q must not contain whitespace", trimmed),
})
}
if strings.HasPrefix(trimmed, "file://") {
errs = append(errs, ValidationError{
Field: "security.idpi.allowedDomains",
Message: fmt.Sprintf("domain pattern %q must not use the file:// scheme; use a hostname", trimmed),
})
}
}
for _, p := range cfg.CustomPatterns {
if strings.TrimSpace(p) == "" {
errs = append(errs, ValidationError{
Field: "security.idpi.customPatterns",
Message: "custom pattern must not be empty or whitespace-only",
})
}
}
if cfg.ScanTimeoutSec < 0 {
errs = append(errs, ValidationError{
Field: "security.idpi.scanTimeoutSec",
Message: "scanTimeoutSec must not be negative",
})
}
return errs
}
// ValidAllocationPolicies returns all valid allocation policy values.
func ValidAllocationPolicies() []string {
return []string{"fcfs", "round_robin", "random"}
}
// ValidAttachSchemes returns all valid attach URL schemes.
func ValidAttachSchemes() []string {
return []string{"ws", "wss", "http", "https"}
}
|