Skip to content

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

fix: rewrite SafePath with IsLocal + inline sanitizers for all handlers

Data5bca581cd95f
diff --git a/internal/handlers/download.go b/internal/handlers/download.go
index c95b160b..5b0ab597 100644
--- a/internal/handlers/download.go
+++ b/internal/handlers/download.go
@@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
+ "strings"
"time"
"github.com/chromedp/cdproto/network"
@@ -124,7 +125,13 @@ func (h *Handlers) HandleDownload(w http.ResponseWriter, r *http.Request) {
web.Error(w, 400, fmt.Errorf("invalid path: %w", pathErr))
return
}
- filePath = safe
+ absBase, _ := filepath.Abs(h.Config.StateDir)
+ absPath, pathErr := filepath.Abs(safe)
+ if pathErr != nil || !strings.HasPrefix(absPath, absBase+string(filepath.Separator)) {
+ web.Error(w, 400, fmt.Errorf("invalid output path"))
+ return
+ }
+ filePath = absPath
if err := os.MkdirAll(filepath.Dir(filePath), 0750); err != nil {
web.Error(w, 500, fmt.Errorf("failed to create directory: %w", err))
return
diff --git a/internal/handlers/pdf.go b/internal/handlers/pdf.go
index af3775a7..f6489352 100644
--- a/internal/handlers/pdf.go
+++ b/internal/handlers/pdf.go
@@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"strconv"
+ "strings"
"time"
"github.com/chromedp/cdproto/page"
@@ -174,7 +175,13 @@ func (h *Handlers) HandlePDF(w http.ResponseWriter, r *http.Request) {
web.Error(w, 400, fmt.Errorf("invalid path: %w", err))
return
}
- savePath = safe
+ absBase, _ := filepath.Abs(h.Config.StateDir)
+ absPath, err := filepath.Abs(safe)
+ if err != nil || !strings.HasPrefix(absPath, absBase+string(filepath.Separator)) {
+ web.Error(w, 400, fmt.Errorf("invalid output path"))
+ return
+ }
+ savePath = absPath
if err := os.MkdirAll(filepath.Dir(savePath), 0750); err != nil {
web.Error(w, 500, fmt.Errorf("create dir: %w", err))
return
diff --git a/internal/handlers/snapshot.go b/internal/handlers/snapshot.go
index 14bfd7d1..60a57ec3 100644
--- a/internal/handlers/snapshot.go
+++ b/internal/handlers/snapshot.go
@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strconv"
+ "strings"
"time"
"github.com/chromedp/chromedp"
@@ -281,7 +282,13 @@ func (h *Handlers) HandleSnapshot(w http.ResponseWriter, r *http.Request) {
web.Error(w, 400, fmt.Errorf("invalid path: %w", err))
return
}
- filePath = safe
+ absBase, _ := filepath.Abs(h.Config.StateDir)
+ absPath, err := filepath.Abs(safe)
+ if err != nil || !strings.HasPrefix(absPath, absBase+string(filepath.Separator)) {
+ web.Error(w, 400, fmt.Errorf("invalid output path"))
+ return
+ }
+ filePath = absPath
if err := os.MkdirAll(filepath.Dir(filePath), 0750); err != nil {
web.Error(w, 500, fmt.Errorf("create output dir: %w", err))
return
diff --git a/internal/handlers/upload.go b/internal/handlers/upload.go
index 2eb09330..3ab1a1eb 100644
--- a/internal/handlers/upload.go
+++ b/internal/handlers/upload.go
@@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"os"
+ "path/filepath"
"strings"
"github.com/chromedp/cdproto/cdp"
@@ -55,17 +56,24 @@ func (h *Handlers) HandleUpload(w http.ResponseWriter, r *http.Request) {
}
// Validate local paths stay within the allowed StateDir.
+ absBase, _ := filepath.Abs(h.Config.StateDir)
for i, p := range req.Paths {
safe, err := web.SafePath(h.Config.StateDir, p)
if err != nil {
web.Error(w, 400, fmt.Errorf("invalid path: %w", err))
return
}
- if _, err := os.Stat(safe); err != nil {
- web.Error(w, 400, fmt.Errorf("file not found: %s", safe))
+ // Inline sanitizer: CodeQL recognizes filepath.Abs + strings.HasPrefix.
+ absPath, err := filepath.Abs(safe)
+ if err != nil || !strings.HasPrefix(absPath, absBase+string(filepath.Separator)) {
+ web.Error(w, 400, fmt.Errorf("path %q escapes allowed directory", p))
return
}
- req.Paths[i] = safe
+ if _, err := os.Stat(absPath); err != nil {
+ web.Error(w, 400, fmt.Errorf("file not found: %s", absPath))
+ return
+ }
+ req.Paths[i] = absPath
}
// Decode base64 files to temp dir.
diff --git a/internal/web/safepath.go b/internal/web/safepath.go
index 14002c22..630ba1b5 100644
--- a/internal/web/safepath.go
+++ b/internal/web/safepath.go
@@ -6,53 +6,44 @@ import (
"strings"
)
-// SafePath validates that the resolved path stays within the allowed base directory.
-// Returns the cleaned absolute path or an error if traversal is detected.
+// SafePath resolves a user-provided path against a base directory and ensures
+// the result stays within that directory. Returns the cleaned absolute path.
+//
+// The implementation uses the filepath.Abs + strings.HasPrefix pattern
+// recommended by CodeQL (go/path-injection) and OWASP path-traversal guidance.
func SafePath(base, userPath string) (string, error) {
absBase, err := filepath.Abs(base)
if err != nil {
return "", fmt.Errorf("invalid base path: %w", err)
}
- // Detect absolute paths. On Windows filepath.IsAbs("/etc/passwd") returns
- // false because it lacks a drive letter, but the leading separator makes it
- // root-relative (resolves to <current-drive>:\etc\passwd). We must treat
- // any path that starts with a separator as absolute to prevent it from
- // being silently joined into the base directory.
- isAbs := filepath.IsAbs(userPath) ||
- strings.HasPrefix(userPath, "/") ||
- strings.HasPrefix(userPath, string(filepath.Separator))
-
- var resolved string
- if isAbs {
- // Resolve via filepath.Abs so root-relative paths like "/etc/passwd"
- // get a drive letter on Windows (e.g. "C:\etc\passwd"), making the
- // prefix check below work correctly on every platform.
- resolved, err = filepath.Abs(filepath.Clean(userPath))
- if err != nil {
- return "", fmt.Errorf("invalid user path: %w", err)
- }
- } else {
- resolved = filepath.Clean(filepath.Join(absBase, userPath))
+ // Empty or "." means the base directory itself.
+ if userPath == "" || userPath == "." {
+ return absBase, nil
}
- // Ensure resolved path is within or equal to base.
- resolved, err = filepath.Abs(resolved)
- if err != nil {
- return "", fmt.Errorf("invalid resolved path: %w", err)
+ // Reject absolute paths outright — user input must be relative to base.
+ if filepath.IsAbs(userPath) || strings.HasPrefix(userPath, "/") || strings.HasPrefix(userPath, string(filepath.Separator)) {
+ return "", fmt.Errorf("absolute paths not allowed: %q", userPath)
}
- // Ensure resolved path is within or equal to base
- if !strings.HasPrefix(resolved, absBase+string(filepath.Separator)) && resolved != absBase {
- return "", fmt.Errorf("path %q escapes base directory %q", userPath, absBase)
+ // Go 1.20+: reject paths with "..", device names (NUL, CON on Windows),
+ // and other non-local components.
+ if !filepath.IsLocal(userPath) {
+ return "", fmt.Errorf("path %q contains invalid components", userPath)
+ }
+
+ // Join, clean, resolve to absolute.
+ joined := filepath.Join(absBase, userPath)
+ absPath, err := filepath.Abs(joined)
+ if err != nil {
+ return "", fmt.Errorf("invalid resolved path: %w", err)
}
- // Double-check via filepath.Rel — this also serves as an explicit sanitizer
- // recognized by static analysis tools (e.g. CodeQL go/path-injection).
- rel, err := filepath.Rel(absBase, resolved)
- if err != nil || strings.HasPrefix(rel, "..") {
+ // Final containment check — the resolved path must be under absBase.
+ if !strings.HasPrefix(absPath, absBase+string(filepath.Separator)) && absPath != absBase {
return "", fmt.Errorf("path %q escapes base directory %q", userPath, absBase)
}
- return resolved, nil
+ return absPath, nil
}
diff --git a/internal/web/safepath_test.go b/internal/web/safepath_test.go
index bfdbe096..d0c34eaa 100644
--- a/internal/web/safepath_test.go
+++ b/internal/web/safepath_test.go
@@ -18,11 +18,11 @@ func TestSafePath(t *testing.T) {
wantErr bool
}{
{"valid relative", "/tmp/state", "pdfs/out.pdf", false},
- {"valid absolute inside", "/tmp/state", "/tmp/state/pdfs/out.pdf", false},
+ {"absolute inside rejected", "/tmp/state", "/tmp/state/pdfs/out.pdf", true},
{"traversal dotdot", "/tmp/state", "../etc/passwd", true},
{"traversal absolute", "/tmp/state", "/etc/passwd", true},
{"traversal hidden", "/tmp/state", "pdfs/../../etc/passwd", true},
- {"base itself", "/tmp/state", "/tmp/state", false},
+ {"absolute base rejected", "/tmp/state", "/tmp/state", true},
}
for _, tt := range tests {
@@ -55,23 +55,17 @@ func TestSafePath_TempDir(t *testing.T) {
}
})
- t.Run("absolute inside base", func(t *testing.T) {
- got, err := SafePath(base, child)
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- if got != child {
- t.Errorf("got %q, want %q", got, child)
+ t.Run("absolute inside base rejected", func(t *testing.T) {
+ _, err := SafePath(base, child)
+ if err == nil {
+ t.Error("expected error for absolute path, got nil")
}
})
- t.Run("base itself allowed", func(t *testing.T) {
- got, err := SafePath(base, base)
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- if got != base {
- t.Errorf("got %q, want %q", got, base)
+ t.Run("absolute base itself rejected", func(t *testing.T) {
+ _, err := SafePath(base, base)
+ if err == nil {
+ t.Error("expected error for absolute path, got nil")
}
})
@@ -236,7 +230,7 @@ func TestSafePath_RelativeNormalizesInside(t *testing.T) {
func TestSafePath_ReturnValueIsAbsolute(t *testing.T) {
base := t.TempDir()
- paths := []string{".", "", "sub/file.txt", base}
+ paths := []string{".", "", "sub/file.txt"}
for _, p := range paths {
t.Run("path="+p, func(t *testing.T) {
got, err := SafePath(base, p)