Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

security: fix SSRF vulnerability in orchestrator health checks

Mario16537a235df7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0235b3dc..04349ee4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,9 @@
- **WebSocket proxy standards** — Improved HTTP header formatting using `fmt.Fprintf` and `textproto.CanonicalMIMEHeaderKey` (#50)
- **Integration test stability** — Added retry logic, increased timeouts, and better error reporting for flaky tests (#50)
+### Security
+- **SSRF Prevention** — Added port validation to prevent Server-Side Request Forgery in orchestrator health checks. Blocks sensitive ports and validates port range (1024-65535) (#50)
+
### Docs
- **Definition of Done** — Added DEFINITION_OF_DONE.md and GitHub PR template with streamlined, actionable checklist (PR #48)
diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.go
index fb356396..05f4dcc5 100644
--- a/internal/orchestrator/handlers.go
+++ b/internal/orchestrator/handlers.go
@@ -104,6 +104,12 @@ func (o *Orchestrator) handleLaunchByName(w http.ResponseWriter, r *http.Request
return
}
+ // Validate port to prevent SSRF
+ if err := ValidatePort(req.Port); err != nil {
+ web.Error(w, 400, fmt.Errorf("invalid port: %w", err))
+ return
+ }
+
inst, err := o.Launch(req.Name, req.Port, req.Headless)
if err != nil {
web.Error(w, 409, err)
diff --git a/internal/orchestrator/health.go b/internal/orchestrator/health.go
index 1f1fc4da..5b71b27b 100644
--- a/internal/orchestrator/health.go
+++ b/internal/orchestrator/health.go
@@ -36,10 +36,7 @@ func (o *Orchestrator) monitor(inst *InstanceInternal) {
time.Sleep(instanceHealthPollInterval)
for _, baseURL := range instanceBaseURLs(inst.Port) {
- // Suppress CodeQL alert: baseURL comes from trusted orchestrator configuration
- // (port-based child instance list), not user input. This is intentional design:
- // agents request the orchestrator to probe known child instances.
- // lgtm[go/request-forgery]
+ // Validate URL components to prevent SSRF
req, reqErr := http.NewRequest(http.MethodGet, baseURL+"/health", nil)
if reqErr != nil {
lastProbe = fmt.Sprintf("%s -> %s", baseURL, reqErr.Error())
@@ -141,6 +138,12 @@ func isInstanceHealthyStatus(code int) bool {
}
func instanceBaseURLs(port string) []string {
+ // Port should already be validated, but double-check
+ if err := ValidatePort(port); err != nil {
+ // Return empty slice if port is invalid
+ return []string{}
+ }
+
return []string{
fmt.Sprintf("http://127.0.0.1:%s", port),
fmt.Sprintf("http://[::1]:%s", port),
diff --git a/internal/orchestrator/validation.go b/internal/orchestrator/validation.go
new file mode 100644
index 00000000..0d0ff405
--- /dev/null
+++ b/internal/orchestrator/validation.go
@@ -0,0 +1,74 @@
+package orchestrator
+
+import (
+ "fmt"
+ "net"
+ "strconv"
+)
+
+// ValidatePort ensures the port is valid and safe to use
+func ValidatePort(port string) error {
+ if port == "" {
+ return fmt.Errorf("port cannot be empty")
+ }
+
+ // Parse port number
+ portNum, err := strconv.Atoi(port)
+ if err != nil {
+ return fmt.Errorf("invalid port number: %s", port)
+ }
+
+ // Check valid port range
+ if portNum < 1024 || portNum > 65535 {
+ return fmt.Errorf("port must be between 1024 and 65535")
+ }
+
+ // Disallow common sensitive ports
+ blockedPorts := []int{
+ 3306, // MySQL
+ 5432, // PostgreSQL
+ 6379, // Redis
+ 9200, // Elasticsearch
+ 27017, // MongoDB
+ 11211, // Memcached
+ 2049, // NFS
+ 111, // RPC
+ 22, // SSH (if somehow in high range)
+ 25, // SMTP
+ 110, // POP3
+ 143, // IMAP
+ }
+
+ for _, blocked := range blockedPorts {
+ if portNum == blocked {
+ return fmt.Errorf("port %d is not allowed", portNum)
+ }
+ }
+
+ return nil
+}
+
+// ValidateHost ensures only localhost connections are allowed
+func ValidateHost(host string) error {
+ // Only allow localhost connections
+ allowedHosts := []string{
+ "localhost",
+ "127.0.0.1",
+ "[::1]",
+ "::1",
+ }
+
+ for _, allowed := range allowedHosts {
+ if host == allowed {
+ return nil
+ }
+ }
+
+ // Also check if it's a loopback IP
+ ip := net.ParseIP(host)
+ if ip != nil && ip.IsLoopback() {
+ return nil
+ }
+
+ return fmt.Errorf("only localhost connections are allowed")
+}
diff --git a/internal/orchestrator/validation_test.go b/internal/orchestrator/validation_test.go
new file mode 100644
index 00000000..55852647
--- /dev/null
+++ b/internal/orchestrator/validation_test.go
@@ -0,0 +1,59 @@
+package orchestrator
+
+import (
+ "testing"
+)
+
+func TestValidatePort(t *testing.T) {
+ tests := []struct {
+ name string
+ port string
+ wantErr bool
+ }{
+ {"valid port", "8080", false},
+ {"valid high port", "65000", false},
+ {"empty port", "", true},
+ {"non-numeric", "abc", true},
+ {"too low", "80", true},
+ {"too high", "70000", true},
+ {"mysql port", "3306", true},
+ {"redis port", "6379", true},
+ {"postgres port", "5432", true},
+ {"elasticsearch", "9200", true},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := ValidatePort(tt.port)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("ValidatePort(%q) error = %v, wantErr %v", tt.port, err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestValidateHost(t *testing.T) {
+ tests := []struct {
+ name string
+ host string
+ wantErr bool
+ }{
+ {"localhost", "localhost", false},
+ {"ipv4 loopback", "127.0.0.1", false},
+ {"ipv6 loopback bracket", "[::1]", false},
+ {"ipv6 loopback", "::1", false},
+ {"other loopback", "127.0.0.2", false},
+ {"external ip", "192.168.1.1", true},
+ {"domain", "example.com", true},
+ {"public ip", "8.8.8.8", true},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := ValidateHost(tt.host)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("ValidateHost(%q) error = %v, wantErr %v", tt.host, err, tt.wantErr)
+ }
+ })
+ }
+}