@clawchatsai/connector 0.1.22 → 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawchatsai/connector",
3
- "version": "0.1.22",
3
+ "version": "0.1.23",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
@@ -7,6 +7,13 @@ import { parseMultipart } from '../util/multipart.js';
7
7
  const HOME = os.homedir();
8
8
  const ALLOWED_FILE_DIRS = [HOME, '/tmp'];
9
9
 
10
+ // Resolves a `~`-relative or absolute path; returns null unless it is HOME or inside it
11
+ // (a bare prefix check would also admit siblings such as /home/user2).
12
+ function resolveInHome(p) {
13
+ const resolved = path.resolve(p.replace(/^~/, HOME));
14
+ return resolved === HOME || resolved.startsWith(HOME + path.sep) ? resolved : null;
15
+ }
16
+
10
17
  export function handleServeFile(req, res, query, workspaceDir) {
11
18
  const filePath = query.path;
12
19
  if (!filePath) return sendError(res, 400, 'Missing path parameter');
@@ -39,8 +46,8 @@ export function handleWorkspaceList(req, res, query) {
39
46
  const reqPath = query.path || '~/.openclaw/workspace';
40
47
  const depth = parseInt(query.depth || '2', 10);
41
48
  const showHidden = query.hidden === '1' || query.hidden === 'true';
42
- const resolved = path.resolve(reqPath.replace(/^~/, HOME));
43
- if (!resolved.startsWith(HOME)) return sendError(res, 403, 'Access denied');
49
+ const resolved = resolveInHome(reqPath);
50
+ if (!resolved) return sendError(res, 403, 'Access denied');
44
51
  if (!fs.existsSync(resolved)) return sendError(res, 404, 'Path not found');
45
52
 
46
53
  const files = [{ path: resolved + '/', type: 'dir', name: path.basename(resolved), size: 0 }];
@@ -61,11 +68,13 @@ export function handleWorkspaceList(req, res, query) {
61
68
  send(res, 200, { files, cwd: resolved });
62
69
  }
63
70
 
64
- export function handleWorkspaceFileRead(req, res, query) {
71
+ // Reads any file the gateway user can read (file links in chat open any path, as the agent
72
+ // wrote it): `~` is home, relative paths resolve against the agent workspace. Writes and
73
+ // deletes stay limited to HOME.
74
+ export function handleWorkspaceFileRead(req, res, query, workspaceDir) {
65
75
  const filePath = query.path;
66
76
  if (!filePath) return sendError(res, 400, 'Missing path parameter');
67
- const resolved = path.resolve(filePath.replace(/^~/, HOME));
68
- if (!resolved.startsWith(HOME)) return sendError(res, 403, 'Access denied');
77
+ const resolved = path.resolve(workspaceDir, filePath.replace(/^~(?=\/|$)/, HOME));
69
78
  if (!fs.existsSync(resolved) || !fs.statSync(resolved).isFile()) return sendError(res, 404, 'File not found');
70
79
 
71
80
  const stat = fs.statSync(resolved);
@@ -87,8 +96,8 @@ export function handleWorkspaceFileRead(req, res, query) {
87
96
  export async function handleWorkspaceFileWrite(req, res, query) {
88
97
  const filePath = query.path;
89
98
  if (!filePath) return sendError(res, 400, 'Missing path parameter');
90
- const resolved = path.resolve(filePath.replace(/^~/, HOME));
91
- if (!resolved.startsWith(HOME)) return sendError(res, 403, 'Can only write to workspace directory');
99
+ const resolved = resolveInHome(filePath);
100
+ if (!resolved) return sendError(res, 403, 'Can only write to workspace directory');
92
101
  const chunks = [];
93
102
  for await (const chunk of req) chunks.push(chunk);
94
103
  const dir = path.dirname(resolved);
@@ -100,8 +109,8 @@ export async function handleWorkspaceFileWrite(req, res, query) {
100
109
  export function handleWorkspaceFileDelete(req, res, query) {
101
110
  const filePath = query.path;
102
111
  if (!filePath) return sendError(res, 400, 'Missing path parameter');
103
- const resolved = path.resolve(filePath.replace(/^~/, HOME));
104
- if (!resolved.startsWith(HOME)) return sendError(res, 403, 'Access denied');
112
+ const resolved = resolveInHome(filePath);
113
+ if (!resolved) return sendError(res, 403, 'Access denied');
105
114
  if (!fs.existsSync(resolved)) return sendError(res, 404, 'Path not found');
106
115
  try {
107
116
  const stat = fs.statSync(resolved);
@@ -113,8 +122,8 @@ export function handleWorkspaceFileDelete(req, res, query) {
113
122
  export async function handleWorkspaceUpload(req, res, query) {
114
123
  const targetDir = query.path;
115
124
  if (!targetDir) return sendError(res, 400, 'Missing path parameter');
116
- const resolved = path.resolve(targetDir.replace(/^~/, HOME));
117
- if (!resolved.startsWith(HOME)) return sendError(res, 403, 'Access denied');
125
+ const resolved = resolveInHome(targetDir);
126
+ if (!resolved) return sendError(res, 403, 'Access denied');
118
127
  if (!fs.existsSync(resolved) || !fs.statSync(resolved).isDirectory()) return sendError(res, 404, 'Target directory not found');
119
128
  if (!(req.headers['content-type'] || '').includes('multipart/form-data')) return sendError(res, 400, 'Expected multipart/form-data');
120
129
 
@@ -137,3 +146,23 @@ export async function handleWorkspaceUpload(req, res, query) {
137
146
  }
138
147
  send(res, 200, { ok: true, uploaded });
139
148
  }
149
+
150
+ // Creates one empty file or folder named `name` inside the directory `path`.
151
+ export function handleWorkspaceCreate(req, res, query) {
152
+ const { path: parentPath, name, type } = query;
153
+ if (!parentPath || !name) return sendError(res, 400, 'Missing path or name parameter');
154
+ if (type !== 'file' && type !== 'dir') return sendError(res, 400, 'type must be file or dir');
155
+ if (name === '.' || name === '..' || /[/\\\0]/.test(name) || name.trim() !== name) return sendError(res, 400, 'Invalid name');
156
+ const parent = resolveInHome(parentPath);
157
+ if (!parent) return sendError(res, 403, 'Access denied');
158
+ if (!fs.existsSync(parent) || !fs.statSync(parent).isDirectory()) return sendError(res, 404, 'Target directory not found');
159
+ const target = path.join(parent, name);
160
+ try {
161
+ if (type === 'dir') fs.mkdirSync(target);
162
+ else fs.writeFileSync(target, '', { flag: 'wx' });
163
+ } catch (err) {
164
+ if (err.code === 'EEXIST') return sendError(res, 409, `"${name}" already exists`);
165
+ return sendError(res, 500, 'Create failed: ' + err.message);
166
+ }
167
+ send(res, 200, { ok: true, path: target + (type === 'dir' ? '/' : ''), type });
168
+ }
@@ -0,0 +1,83 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { handleWorkspaceFileRead, handleWorkspaceCreate } from './filesystem.js';
7
+
8
+ function read(filePath, workspaceDir) {
9
+ const out = { status: 0, body: '' };
10
+ const res = {
11
+ writeHead(status) { out.status = status; },
12
+ setHeader() {},
13
+ end(body) { out.body = String(body ?? ''); },
14
+ };
15
+ handleWorkspaceFileRead({}, res, { path: filePath }, workspaceDir);
16
+ return out;
17
+ }
18
+
19
+ test('file reads: any absolute path, ~ is home, relative paths resolve against the workspace', () => {
20
+ const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'cc-read-'));
21
+ const workspace = fs.mkdtempSync(path.join(os.tmpdir(), 'cc-ws-'));
22
+ fs.writeFileSync(path.join(outside, 'a.md'), 'outside');
23
+ fs.mkdirSync(path.join(workspace, 'notes'));
24
+ fs.writeFileSync(path.join(workspace, 'notes', 'b.md'), 'relative');
25
+ try {
26
+ assert.deepEqual(read(path.join(outside, 'a.md'), workspace), { status: 200, body: 'outside' });
27
+ assert.deepEqual(read('notes/b.md', workspace), { status: 200, body: 'relative' });
28
+ assert.deepEqual(read('./notes/b.md', workspace), { status: 200, body: 'relative' });
29
+ assert.equal(read('notes/missing.md', workspace).status, 404);
30
+ assert.equal(read('notes', workspace).status, 404);
31
+ // `~` only expands as the home segment; "~x" is an ordinary relative name.
32
+ assert.equal(read('~x/b.md', workspace).status, 404);
33
+ } finally {
34
+ fs.rmSync(outside, { recursive: true, force: true });
35
+ fs.rmSync(workspace, { recursive: true, force: true });
36
+ }
37
+ });
38
+
39
+ test('file reads: ~/ expands to home', () => {
40
+ const dir = fs.mkdtempSync(path.join(os.homedir(), '.cc-read-test-'));
41
+ fs.writeFileSync(path.join(dir, 'c.txt'), 'home');
42
+ try {
43
+ const rel = '~/' + path.relative(os.homedir(), path.join(dir, 'c.txt'));
44
+ assert.deepEqual(read(rel, '/nonexistent-workspace'), { status: 200, body: 'home' });
45
+ } finally {
46
+ fs.rmSync(dir, { recursive: true, force: true });
47
+ }
48
+ });
49
+
50
+ function create(query) {
51
+ const out = { status: 0, body: null };
52
+ const res = {
53
+ writeHead(status) { out.status = status; },
54
+ setHeader() {},
55
+ end(body) { out.body = JSON.parse(String(body ?? 'null')); },
56
+ };
57
+ handleWorkspaceCreate({}, res, query);
58
+ return out;
59
+ }
60
+
61
+ test('create: makes an empty file or folder inside home, rejects bad names and existing entries', () => {
62
+ const dir = fs.mkdtempSync(path.join(os.homedir(), '.cc-create-test-'));
63
+ const tilde = '~/' + path.relative(os.homedir(), dir);
64
+ try {
65
+ assert.equal(create({ path: tilde, name: 'sub', type: 'dir' }).status, 200);
66
+ assert.ok(fs.statSync(path.join(dir, 'sub')).isDirectory());
67
+ const file = create({ path: path.join(dir, 'sub'), name: 'a.md', type: 'file' });
68
+ assert.equal(file.status, 200);
69
+ assert.equal(file.body.path, path.join(dir, 'sub', 'a.md'));
70
+ assert.equal(fs.readFileSync(path.join(dir, 'sub', 'a.md'), 'utf8'), '');
71
+ assert.equal(create({ path: dir, name: 'sub', type: 'dir' }).status, 409);
72
+ assert.equal(create({ path: dir, name: 'sub', type: 'file' }).status, 409);
73
+ for (const name of ['..', '.', 'a/b', 'a\\b', ' x', '']) {
74
+ assert.notEqual(create({ path: dir, name, type: 'dir' }).status, 200, name);
75
+ }
76
+ assert.equal(create({ path: dir, name: 'x', type: 'link' }).status, 400);
77
+ assert.equal(create({ path: path.join(dir, 'missing'), name: 'x', type: 'dir' }).status, 404);
78
+ assert.equal(create({ path: '/tmp', name: 'cc-x', type: 'dir' }).status, 403);
79
+ assert.equal(create({ path: os.homedir() + '-sibling', name: 'x', type: 'dir' }).status, 403);
80
+ } finally {
81
+ fs.rmSync(dir, { recursive: true, force: true });
82
+ }
83
+ });
package/server/index.js CHANGED
@@ -8,7 +8,7 @@ import { Database } from './bootstrap/native.js';
8
8
  import { GATEWAY_WS_URL, AUTH_TOKEN, discoverWorkspaceDir } from './config.js';
9
9
  import { DebugLogger } from './debug.js';
10
10
  import { GatewayClient } from './gateway.js';
11
- import { handleServeFile, handleWorkspaceList, handleWorkspaceFileRead, handleWorkspaceFileWrite, handleWorkspaceFileDelete, handleWorkspaceUpload } from './controllers/filesystem.js';
11
+ import { handleServeFile, handleWorkspaceList, handleWorkspaceFileRead, handleWorkspaceFileWrite, handleWorkspaceFileDelete, handleWorkspaceUpload, handleWorkspaceCreate } from './controllers/filesystem.js';
12
12
  import { handleTranscribe } from './controllers/transcribe.js';
13
13
  import { createGatewayMediaHandler } from './controllers/gateway-media.js';
14
14
  import { handleStatic } from './controllers/static.js';
@@ -153,10 +153,11 @@ export function createApp(config = {}) {
153
153
  if (method === 'GET' && urlPath === '/api/file') return handleServeFile(req, res, query, workspaceDir);
154
154
  if (method === 'GET' && urlPath === '/api/gw-media') return handleGatewayMedia(req, res, query);
155
155
  if (method === 'GET' && urlPath === '/api/workspace') return handleWorkspaceList(req, res, query);
156
- if (method === 'GET' && urlPath === '/api/workspace/file') return handleWorkspaceFileRead(req, res, query);
156
+ if (method === 'GET' && urlPath === '/api/workspace/file') return handleWorkspaceFileRead(req, res, query, workspaceDir);
157
157
  if (method === 'PUT' && urlPath === '/api/workspace/file') return await handleWorkspaceFileWrite(req, res, query);
158
158
  if (method === 'DELETE' && urlPath === '/api/workspace/file') return handleWorkspaceFileDelete(req, res, query);
159
159
  if (method === 'POST' && urlPath === '/api/workspace/upload') return await handleWorkspaceUpload(req, res, query);
160
+ if (method === 'POST' && urlPath === '/api/workspace/create') return handleWorkspaceCreate(req, res, query);
160
161
 
161
162
  // Settings & misc
162
163
  if (method === 'GET' && urlPath === '/api/settings') return handleGetSettings(req, res);