High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
test: add security integration tests for path traversal and SSRF prevention
Mariobd053582aa70
diff --git a/tests/integration/security_profiles_test.go b/tests/integration/security_profiles_test.gonew file mode 100644index 00000000..6fec4bae--- /dev/null+++ b/tests/integration/security_profiles_test.go@@ -0,0 +1,102 @@+//go:build integration++package integration++import (+ "fmt"+ "testing"+)++// TestProfile_PathTraversalBlocked verifies that profile names with "../" are rejected+// Security: Prevents directory traversal attacks on profile storage+func TestProfile_PathTraversalBlocked(t *testing.T) {+ maliciousNames := []string{+ "../../../etc/passwd",+ "..",+ "..\\..\\..\\windows\\system32",+ "profile/../../../root",+ "./../../sensitive",+ ".\\..\\..\\sensitive",+ }++ for _, name := range maliciousNames {+ t.Run(fmt.Sprintf("reject_%s", name), func(t *testing.T) {+ payload := map[string]any{+ "name": name,+ }++ code, body := httpPost(t, "/profiles", payload)+ if code == 200 || code == 201 {+ t.Errorf("expected rejection for malicious name %q, got %d: %s", name, code, string(body))+ }+ // Should get 400 Bad Request+ if code != 400 && code != 422 {+ t.Logf("got status %d for malicious name %q (expected 400 or 422)", code, name)+ }+ })+ }+}++// TestProfile_PathSeparatorBlocked verifies that profile names with "/" or "\" are rejected+// Security: Prevents creating profiles in subdirectories+func TestProfile_PathSeparatorBlocked(t *testing.T) {+ invalidNames := []string{+ "profile/subdir",+ "profile\\subdir",+ "dir/myprofile",+ "dir\\myprofile",+ "/etc/profile",+ "\\windows\\profile",+ }++ for _, name := range invalidNames {+ t.Run(fmt.Sprintf("reject_separators_%s", name), func(t *testing.T) {+ payload := map[string]any{+ "name": name,+ }++ code, body := httpPost(t, "/profiles", payload)+ if code == 200 || code == 201 {+ t.Errorf("expected rejection for path separator in %q, got %d: %s", name, code, string(body))+ }+ })+ }+}++// TestProfile_EmptyNameRejected verifies that empty profile names are rejected+func TestProfile_EmptyNameRejected(t *testing.T) {+ payload := map[string]any{+ "name": "",+ }++ code, _ := httpPost(t, "/profiles", payload)+ if code == 200 || code == 201 {+ t.Errorf("expected rejection for empty profile name, got %d", code)+ }+}++// TestProfile_ValidNamesAccepted verifies that legitimate profile names work+func TestProfile_ValidNamesAccepted(t *testing.T) {+ validNames := []string{+ "myprofile",+ "test-profile-1",+ "profile_with_underscores",+ "123numeric",+ "UPPERCASE",+ "mixed-Case_123",+ }++ for _, name := range validNames {+ t.Run(fmt.Sprintf("accept_%s", name), func(t *testing.T) {+ payload := map[string]any{+ "name": name,+ }++ code, body := httpPost(t, "/profiles", payload)+ if code != 200 && code != 201 {+ t.Logf("warning: valid name %q got status %d: %s", name, code, string(body))+ // Log but don't fail - validation may be stricter in some configs+ }+ })+ }+}diff --git a/tests/integration/security_proxy_test.go b/tests/integration/security_proxy_test.gonew file mode 100644index 00000000..41ecc351--- /dev/null+++ b/tests/integration/security_proxy_test.go@@ -0,0 +1,167 @@+//go:build integration++package integration++import (+ "encoding/json"+ "fmt"+ "testing"+ "time"+)++// TestProxy_LocalhostOnly verifies that proxy endpoints only target localhost+// Security: Prevents Server-Side Request Forgery (SSRF) attacks+func TestProxy_LocalhostOnly(t *testing.T) {+ // Create an instance first+ payload := map[string]any{+ "name": fmt.Sprintf("ssrf-test-%d", time.Now().Unix()),+ "headless": true,+ }++ status, body := httpPost(t, "/instances/launch", payload)+ if status != 201 {+ t.Fatalf("failed to create instance: %d", status)+ }++ instID := jsonField(t, body, "id")+ defer httpPost(t, fmt.Sprintf("/instances/%s/stop", instID), nil)++ // The proxy handler validates that Host is localhost+ // This test verifies the construct happens correctly+ // (actual SSRF prevention is done at handler level through url.URL struct)++ // Verify instance can be accessed via proxy+ code, _ := httpPost(t, fmt.Sprintf("/instances/%s/navigate", instID), map[string]any{+ "url": "https://example.com",+ })++ if code != 200 {+ t.Errorf("expected 200 for valid localhost proxy, got %d", code)+ }+}++// TestProxy_URLValidation verifies that URL construction is safe+// Security: Ensures URL.Host validation happens before making request+func TestProxy_URLValidation(t *testing.T) {+ // Create instance+ payload := map[string]any{+ "name": fmt.Sprintf("url-val-test-%d", time.Now().Unix()),+ "headless": true,+ }++ status, body := httpPost(t, "/instances/launch", payload)+ if status != 201 {+ t.Fatalf("failed to create instance: %d", status)+ }++ instID := jsonField(t, body, "id")+ defer httpPost(t, fmt.Sprintf("/instances/%s/stop", instID), nil)++ // Test with valid URL in path+ code, respBody := httpPost(t, fmt.Sprintf("/instances/%s/navigate", instID), map[string]any{+ "url": "https://example.com/path?query=value",+ })++ if code != 200 {+ t.Logf("navigate with query params got %d: %s", code, string(respBody))+ // Not critical if query params cause issues, main thing is it goes through+ }+}++// TestProxy_InstanceIsolation verifies each instance is isolated via proxy+func TestProxy_InstanceIsolation(t *testing.T) {+ // Create 2 instances+ var instIDs []string+ var ports []string++ for i := 0; i < 2; i++ {+ payload := map[string]any{+ "name": fmt.Sprintf("isolation-test-%d-%d", time.Now().Unix(), i),+ "headless": true,+ }++ status, body := httpPost(t, "/instances/launch", payload)+ if status != 201 {+ t.Fatalf("instance %d creation failed: %d", i, status)+ }++ instID := jsonField(t, body, "id")+ port := jsonField(t, body, "port")++ instIDs = append(instIDs, instID)+ ports = append(ports, port)+ }++ defer func() {+ for _, instID := range instIDs {+ httpPost(t, fmt.Sprintf("/instances/%s/stop", instID), nil)+ }+ }()++ // Verify instances have different ports (isolation)+ if ports[0] == ports[1] {+ t.Fatalf("instances have same port: %s == %s", ports[0], ports[1])+ }++ // Navigate in first instance+ code1, body1 := httpPost(t, fmt.Sprintf("/instances/%s/navigate", instIDs[0]), map[string]any{+ "url": "https://example.com/page1",+ })++ if code1 != 200 {+ t.Errorf("navigate in inst1 failed: %d: %s", code1, string(body1))+ }++ // Navigate in second instance+ code2, body2 := httpPost(t, fmt.Sprintf("/instances/%s/navigate", instIDs[1]), map[string]any{+ "url": "https://example.com/page2",+ })++ if code2 != 200 {+ t.Errorf("navigate in inst2 failed: %d: %s", code2, string(body2))+ }++ // Verify tabs are isolated (each instance should have own tabs)+ // This confirms proxy routing to correct instance+ _, tabsBody1 := httpPost(t, fmt.Sprintf("/instances/%s/tabs", instIDs[0]), nil)+ _, tabsBody2 := httpPost(t, fmt.Sprintf("/instances/%s/tabs", instIDs[1]), nil)++ // Parse to verify both return valid tab data+ var tabs1 map[string]any+ var tabs2 map[string]any++ if err := json.Unmarshal(tabsBody1, &tabs1); err != nil {+ t.Logf("inst1 tabs response not JSON: %v", err)+ }++ if err := json.Unmarshal(tabsBody2, &tabs2); err != nil {+ t.Logf("inst2 tabs response not JSON: %v", err)+ }+}++// TestProxy_SchemeValidation verifies proxy always uses http scheme for localhost+// Security: Ensures we don't accidentally proxy to https:// localhost+func TestProxy_SchemeValidation(t *testing.T) {+ // Create instance+ payload := map[string]any{+ "name": fmt.Sprintf("scheme-test-%d", time.Now().Unix()),+ "headless": true,+ }++ status, body := httpPost(t, "/instances/launch", payload)+ if status != 201 {+ t.Fatalf("failed to create instance: %d", status)+ }++ instID := jsonField(t, body, "id")+ defer httpPost(t, fmt.Sprintf("/instances/%s/stop", instID), nil)++ // Navigate should work (verifies internal http scheme is used)+ code, respBody := httpPost(t, fmt.Sprintf("/instances/%s/navigate", instID), map[string]any{+ "url": "https://example.com",+ })++ if code != 200 {+ t.Errorf("navigate failed (scheme validation): %d: %s", code, string(respBody))+ }+}