Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(agent): Windows file browser — per-entry paths + drive enumeration

Juan Denis9de7ecf7c178
diff --git a/agent/internal/agent/file_handlers.go b/agent/internal/agent/file_handlers.go
index d719ee6b..0e24213b 100644
--- a/agent/internal/agent/file_handlers.go
+++ b/agent/internal/agent/file_handlers.go
@@ -115,19 +115,43 @@ func (a *Agent) handleFileList(ctx context.Context, params json.RawMessage) (int
if err := json.Unmarshal(params, &p); err != nil {
return nil, fmt.Errorf("invalid params: %w", err)
}
- if p.Path == "" {
- p.Path = "/"
+
+ // The panel speaks forward-slash paths on the wire regardless of the
+ // agent's OS. Convert to the native separator before any filesystem
+ // call so os.ReadDir and the allowlist validation behave on Windows.
+ reqPath := filepath.FromSlash(p.Path)
+
+ // An empty (or bare-root) request has no single filesystem root on
+ // Windows. Enumerate the logical drives as virtual directory entries
+ // so the panel's file browser can pick a drive instead of silently
+ // listing only the agent process's working drive. On non-Windows
+ // enumerateDriveRoots() returns nil and we fall through to "/".
+ if reqPath == "" || reqPath == "/" || reqPath == `\` {
+ if drives := enumerateDriveRoots(); drives != nil {
+ return map[string]interface{}{
+ "path": "/",
+ "files": drives,
+ }, nil
+ }
+ reqPath = "/"
}
- if err := a.validateFileAccess(p.Path); err != nil {
+
+ if err := a.validateFileAccess(reqPath); err != nil {
return nil, err
}
- entries, err := os.ReadDir(p.Path)
+ entries, err := os.ReadDir(reqPath)
if err != nil {
return nil, fmt.Errorf("failed to list directory: %w", err)
}
- var files []map[string]interface{}
+ // Each entry carries an absolute, forward-slash path. The frontend
+ // keys/selects/navigates on entry.path, and the agent is the only
+ // party that knows the host separator, so it emits the canonical
+ // path. (It was previously omitted, which broke remote browse
+ // navigation on every OS — Windows additionally needs the separator
+ // normalized.)
+ files := make([]map[string]interface{}, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
@@ -135,6 +159,7 @@ func (a *Agent) handleFileList(ctx context.Context, params json.RawMessage) (int
}
files = append(files, map[string]interface{}{
"name": entry.Name(),
+ "path": filepath.ToSlash(filepath.Join(reqPath, entry.Name())),
"is_dir": entry.IsDir(),
"size": info.Size(),
"modified": info.ModTime().UnixMilli(),
@@ -142,7 +167,7 @@ func (a *Agent) handleFileList(ctx context.Context, params json.RawMessage) (int
}
return map[string]interface{}{
- "path": p.Path,
+ "path": filepath.ToSlash(reqPath),
"files": files,
}, nil
}
diff --git a/agent/internal/agent/file_handlers_other.go b/agent/internal/agent/file_handlers_other.go
new file mode 100644
index 00000000..475ecee1
--- /dev/null
+++ b/agent/internal/agent/file_handlers_other.go
@@ -0,0 +1,10 @@
+//go:build !windows
+
+package agent
+
+// enumerateDriveRoots is a no-op on non-Windows platforms, which have a
+// single "/" filesystem root and therefore no drive list to enumerate.
+// Returning nil makes handleFileList fall through to reading "/".
+func enumerateDriveRoots() []map[string]interface{} {
+ return nil
+}
diff --git a/agent/internal/agent/file_handlers_test.go b/agent/internal/agent/file_handlers_test.go
new file mode 100644
index 00000000..9ac72be9
--- /dev/null
+++ b/agent/internal/agent/file_handlers_test.go
@@ -0,0 +1,67 @@
+package agent
+
+import (
+ "context"
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/serverkit/agent/internal/config"
+)
+
+// handleFileList must emit an absolute, forward-slash `path` for every
+// entry (and for the directory itself). The frontend keys, selects, and
+// navigates on entry.path; before this was added remote browse was broken
+// on every OS. Cross-platform: on Windows the join also normalizes the
+// backslash separator to forward-slash on the wire.
+func TestHandleFileListEmitsForwardSlashPaths(t *testing.T) {
+ tmp := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(tmp, "sub"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(tmp, "a.txt"), []byte("hi"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ a := &Agent{cfg: &config.Config{
+ Security: config.SecurityConfig{AllowedPaths: []string{tmp}},
+ }}
+ params, _ := json.Marshal(map[string]string{"path": filepath.ToSlash(tmp)})
+
+ res, err := a.handleFileList(context.Background(), params)
+ if err != nil {
+ t.Fatalf("handleFileList error: %v", err)
+ }
+
+ m, ok := res.(map[string]interface{})
+ if !ok {
+ t.Fatalf("unexpected result type %T", res)
+ }
+ if dp, _ := m["path"].(string); strings.ContainsRune(dp, '\\') {
+ t.Errorf("directory path must be forward-slash on the wire, got %q", dp)
+ }
+
+ files, ok := m["files"].([]map[string]interface{})
+ if !ok {
+ t.Fatalf("unexpected files type %T", m["files"])
+ }
+ if len(files) != 2 {
+ t.Fatalf("expected 2 entries, got %d", len(files))
+ }
+
+ for _, f := range files {
+ p, ok := f["path"].(string)
+ if !ok || p == "" {
+ t.Fatalf("entry missing path field: %#v", f)
+ }
+ if strings.ContainsRune(p, '\\') {
+ t.Errorf("entry path must be forward-slash on the wire, got %q", p)
+ }
+ want := filepath.ToSlash(filepath.Join(tmp, f["name"].(string)))
+ if p != want {
+ t.Errorf("entry path = %q, want %q", p, want)
+ }
+ }
+}
diff --git a/agent/internal/agent/file_handlers_windows.go b/agent/internal/agent/file_handlers_windows.go
new file mode 100644
index 00000000..b1d5baa1
--- /dev/null
+++ b/agent/internal/agent/file_handlers_windows.go
@@ -0,0 +1,36 @@
+//go:build windows
+
+package agent
+
+import "golang.org/x/sys/windows"
+
+// enumerateDriveRoots lists the available logical drives (C:/, D:/, …) as
+// virtual directory entries. Windows has no single filesystem root, so a
+// browse request for "" or "/" returns the drive list rather than silently
+// listing only the agent process's working drive. Paths are forward-slash
+// to match the on-the-wire convention the rest of file:list uses; the
+// allowlist still gates whether the panel can actually enter each drive.
+func enumerateDriveRoots() []map[string]interface{} {
+ mask, err := windows.GetLogicalDrives()
+ if err != nil || mask == 0 {
+ return nil
+ }
+ drives := make([]map[string]interface{}, 0)
+ for i := 0; i < 26; i++ {
+ if mask&(1<<uint(i)) == 0 {
+ continue
+ }
+ letter := string(rune('A' + i))
+ drives = append(drives, map[string]interface{}{
+ "name": letter + ":",
+ "path": letter + ":/",
+ "is_dir": true,
+ "size": int64(0),
+ "modified": int64(0),
+ })
+ }
+ if len(drives) == 0 {
+ return nil
+ }
+ return drives
+}
diff --git a/frontend/src/pages/FileManager.jsx b/frontend/src/pages/FileManager.jsx
index 722321ad..ad27a36f 100644
--- a/frontend/src/pages/FileManager.jsx
+++ b/frontend/src/pages/FileManager.jsx
@@ -32,10 +32,18 @@ const REMOTE_SUPPORTED = new Set(['browse', 'read', 'write']);
function deriveParent(path) {
if (!path || path === '/' || path === '') return null;
+ // A Windows drive root ("C:/" or "C:") sits directly under the agent's
+ // drive list, which the agent serves for the "/" path. Remote agents
+ // emit forward-slash paths on the wire, so we only ever see "/" here.
+ if (/^[A-Za-z]:\/?$/.test(path)) return '/';
const trimmed = path.replace(/\/+$/, '');
const idx = trimmed.lastIndexOf('/');
if (idx <= 0) return '/';
- return trimmed.slice(0, idx);
+ const parent = trimmed.slice(0, idx);
+ // Keep a bare drive letter as a drive root ("C:" -> "C:/") so navigating
+ // up doesn't hit Windows' "current directory on C:" semantics.
+ if (/^[A-Za-z]:$/.test(parent)) return parent + '/';
+ return parent;
}
function unwrapAgentData(res) {
@@ -618,6 +626,20 @@ function FileManager() {
// ─── derived ─────────────────────────────────────────
const breadcrumbs = useMemo(() => {
const parts = currentPath.split('/').filter(Boolean);
+ // Windows drive-rooted path ("C:/Users/Juan"): the first segment is
+ // the drive and the root crumb is the agent's drive list. Building
+ // crumbs with a leading "/" (the POSIX branch below) would produce
+ // bogus "/C:" paths that the agent can't resolve.
+ if (/^[A-Za-z]:$/.test(parts[0] || '')) {
+ const crumbs = [{ name: 'Drives', path: '/' }];
+ let acc = parts[0];
+ crumbs.push({ name: parts[0], path: acc + '/' });
+ for (let i = 1; i < parts.length; i++) {
+ acc += '/' + parts[i];
+ crumbs.push({ name: parts[i], path: acc });
+ }
+ return crumbs;
+ }
const crumbs = [{ name: '/', path: '/' }];
let acc = '';
parts.forEach((p) => { acc += '/' + p; crumbs.push({ name: p, path: acc }); });