Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix: add permission check when set workspace

ldstyle8b0a08f4edea9
diff --git a/jiuwenclaw/channels/tui/frontend/src/app-state.ts b/jiuwenclaw/channels/tui/frontend/src/app-state.ts
index d33e021565..8d49a3cd54 100644
--- a/jiuwenclaw/channels/tui/frontend/src/app-state.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/app-state.ts
@@ -48,6 +48,7 @@ import { type ConnectionStatus, WsClient } from "./core/ws-client.js";
import { loadTuiConfig } from "./core/tui-config-store.js";
import {
getTrustedDirs,
+ validateDirPath,
addTrustedDir,
setTrustedDir,
removeTrustedDir,
@@ -413,6 +414,7 @@ export class CliPiAppState {
sessionTitle: snapshot.sessionTitle,
setSessionTitle: this.setSessionTitle,
getTrustedDirs: getTrustedDirs,
+ validateDirPath: validateDirPath,
addTrustedDir: addTrustedDir,
setTrustedDir: setTrustedDir,
removeTrustedDir: removeTrustedDir,
diff --git a/jiuwenclaw/channels/tui/frontend/src/core/commands/builtins/workspace-dir.ts b/jiuwenclaw/channels/tui/frontend/src/core/commands/builtins/workspace-dir.ts
index 946c956e31..0e47948420 100644
--- a/jiuwenclaw/channels/tui/frontend/src/core/commands/builtins/workspace-dir.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/core/commands/builtins/workspace-dir.ts
@@ -119,6 +119,8 @@ export function createWorkspaceCommand(): SlashCommand {
ctx.addItem(addInfo(ctx.sessionId, `Path already set as trusted dir: ${resolvedPath}`, "c"));
} else if (result === "not_found") {
ctx.addItem(addError(ctx.sessionId, `Path does not exist: ${resolvedPath}`));
+ } else if (result === "no_access") {
+ ctx.addItem(addError(ctx.sessionId, `Permission denied: cannot access directory ${resolvedPath}`));
} else {
ctx.addItem(addError(ctx.sessionId, `Path is not a directory: ${resolvedPath}`));
}
@@ -137,8 +139,8 @@ export function createWorkspaceCommand(): SlashCommand {
return;
}
- // Check if path exists first
- const result = ctx.setTrustedDir(directoryPath);
+ // Validate path without modifying state
+ const result = ctx.validateDirPath(directoryPath);
if (result === "not_found") {
ctx.addItem(addError(ctx.sessionId, `Path does not exist: ${directoryPath}`));
return;
@@ -147,6 +149,10 @@ export function createWorkspaceCommand(): SlashCommand {
ctx.addItem(addError(ctx.sessionId, `Path is not a directory: ${directoryPath}`));
return;
}
+ if (result === "no_access") {
+ ctx.addItem(addError(ctx.sessionId, `Permission denied: cannot access directory ${directoryPath}`));
+ return;
+ }
// If trusted dirs already has content, ask for confirmation
const currentDirs = ctx.getTrustedDirs();
diff --git a/jiuwenclaw/channels/tui/frontend/src/core/commands/types.ts b/jiuwenclaw/channels/tui/frontend/src/core/commands/types.ts
index 84b300a797..649f31ad16 100644
--- a/jiuwenclaw/channels/tui/frontend/src/core/commands/types.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/core/commands/types.ts
@@ -62,8 +62,9 @@ export interface CommandContext {
setSessionTitle: (title: string) => void;
// Trusted directories management (global, not session-specific)
getTrustedDirs: () => string[];
- addTrustedDir: (path: string) => "added" | "exists" | "not_found" | "invalid";
- setTrustedDir: (path: string) => "set" | "not_found" | "invalid";
+ validateDirPath: (path: string) => "valid" | "not_found" | "invalid" | "no_access";
+ addTrustedDir: (path: string) => "added" | "exists" | "not_found" | "invalid" | "no_access";
+ setTrustedDir: (path: string) => "set" | "not_found" | "invalid" | "no_access";
removeTrustedDir: (path: string) => boolean;
clearTrustedDirs: () => void;
// Workspace directory (current working directory)
diff --git a/jiuwenclaw/channels/tui/frontend/src/core/tui-trusted-dirs-store.ts b/jiuwenclaw/channels/tui/frontend/src/core/tui-trusted-dirs-store.ts
index 271c7a7eae..10ee4f044b 100644
--- a/jiuwenclaw/channels/tui/frontend/src/core/tui-trusted-dirs-store.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/core/tui-trusted-dirs-store.ts
@@ -1,4 +1,5 @@
-import { existsSync, statSync } from "node:fs";
+import { existsSync, readdirSync, statSync } from "node:fs";
+import { homedir } from "node:os";
import { resolve } from "node:path";
import { loadTuiConfig, saveTuiConfig } from "./tui-config-store.js";
@@ -34,7 +35,14 @@ function normalizePath(path: string): string {
if (!trimmed) {
return "";
}
- const resolved = resolve(trimmed);
+ // Expand ~ to home directory before resolving
+ let expanded = trimmed;
+ if (expanded === "~") {
+ expanded = homedir();
+ } else if (expanded.startsWith("~/")) {
+ expanded = homedir() + expanded.slice(1);
+ }
+ const resolved = resolve(expanded);
// On Windows, normalize case
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
}
@@ -52,9 +60,9 @@ export function getTrustedDirs(): string[] {
/**
* Add a trusted directory.
* @param path - Directory path to add (must be a folder, not a file)
- * @returns "added" if added, "exists" if already trusted, "not_found" if path doesn't exist, "invalid" if invalid path or not a directory
+ * @returns "added" if added, "exists" if already trusted, "not_found" if path doesn't exist, "invalid" if invalid path or not a directory, "no_access" if permission denied
*/
-export function addTrustedDir(path: string): "added" | "exists" | "not_found" | "invalid" {
+export function addTrustedDir(path: string): "added" | "exists" | "not_found" | "invalid" | "no_access" {
ensureLoaded();
const normalized = normalizePath(path);
if (!normalized) {
@@ -71,6 +79,10 @@ export function addTrustedDir(path: string): "added" | "exists" | "not_found" |
} catch {
return "invalid";
}
+ const access = checkDirAccess(normalized);
+ if (access !== "valid") {
+ return access;
+ }
if (_trustedDirs!.includes(normalized)) {
return "exists";
}
@@ -79,12 +91,56 @@ export function addTrustedDir(path: string): "added" | "exists" | "not_found" |
return "added";
}
+/**
+ * Check that a normalized directory path is accessible (readable).
+ * @returns "valid" if accessible, "no_access" if permission denied, "invalid" for other errors
+ */
+function checkDirAccess(normalized: string): "valid" | "no_access" | "invalid" {
+ try {
+ readdirSync(normalized);
+ } catch (err: any) {
+ if (err.code === "EACCES") {
+ return "no_access";
+ }
+ return "invalid";
+ }
+ return "valid";
+}
+
+/**
+ * Validate a directory path without modifying trusted dirs state.
+ * @param path - Directory path to validate
+ * @returns "valid" if accessible directory, "not_found" if path doesn't exist, "invalid" if not a directory, "no_access" if permission denied
+ */
+export function validateDirPath(path: string): "valid" | "not_found" | "invalid" | "no_access" {
+ const normalized = normalizePath(path);
+ if (!normalized) {
+ return "invalid";
+ }
+ if (!existsSync(normalized)) {
+ return "not_found";
+ }
+ try {
+ const stats = statSync(normalized);
+ if (!stats.isDirectory()) {
+ return "invalid";
+ }
+ } catch {
+ return "invalid";
+ }
+ const access = checkDirAccess(normalized);
+ if (access !== "valid") {
+ return access;
+ }
+ return "valid";
+}
+
/**
* Reset trusted dirs and set a single path.
* @param path - Directory path to set as the only trusted dir (must be a folder, not a file)
- * @returns "set" if set successfully, "not_found" if path doesn't exist, "invalid" if invalid path or not a directory
+ * @returns "set" if set successfully, "not_found" if path doesn't exist, "invalid" if invalid path or not a directory, "no_access" if permission denied
*/
-export function setTrustedDir(path: string): "set" | "not_found" | "invalid" {
+export function setTrustedDir(path: string): "set" | "not_found" | "invalid" | "no_access" {
ensureLoaded();
const normalized = normalizePath(path);
if (!normalized) {
@@ -101,6 +157,10 @@ export function setTrustedDir(path: string): "set" | "not_found" | "invalid" {
} catch {
return "invalid";
}
+ const access = checkDirAccess(normalized);
+ if (access !== "valid") {
+ return access;
+ }
_trustedDirs = [normalized];
persist();
return "set";
@@ -153,5 +213,5 @@ export function isTrustedDir(path: string): boolean {
* Get the default workspace path.
*/
export function getDefaultWorkspacePath(): string {
- return resolve("~/.jiuwenclaw/agent/jiuwenclaw_workspace");
+ return resolve(homedir(), ".jiuwenclaw/agent/jiuwenclaw_workspace");
}