tacticremote 1.6.5 → 1.7.0
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/lib/authChallengeManager.js +1 -134
- package/lib/claudeSessionManager.js +1 -0
- package/lib/claudeTransport.js +1 -0
- package/lib/errorCodes.js +1 -127
- package/lib/execSafe.js +1 -218
- package/lib/fileService.js +1 -177
- package/lib/hookInstaller.js +21 -7
- package/lib/outputParser.js +1 -106
- package/lib/pushClient.js +1 -0
- package/lib/rateLimiter.js +1 -111
- package/lib/serverIdentity.js +1 -0
- package/lib/streamJsonParser.js +1 -0
- package/lib/tmuxManager.js +1 -671
- package/lib/uploadService.js +1 -366
- package/lib/validators.js +1 -350
- package/package.json +2 -1
- package/routes/httpHandler.js +3 -1
- package/routes/wsHandler.js +418 -364
- package/server.js +67 -152
- package/shared/server-core/authChallengeManager.js +108 -0
- package/shared/server-core/claudeSessionManager.js +222 -0
- package/shared/server-core/claudeTransport.js +176 -0
- package/shared/server-core/errorCodes.js +91 -0
- package/shared/server-core/execSafe.js +185 -0
- package/shared/server-core/fileService.js +155 -0
- package/shared/server-core/outputParser.js +174 -0
- package/shared/server-core/protocol-manifest.json +59 -0
- package/shared/server-core/protocol-manifest.schema.json +90 -0
- package/shared/server-core/protocolManifest.js +42 -0
- package/shared/server-core/pushClient.js +198 -0
- package/shared/server-core/rateLimiter.js +98 -0
- package/shared/server-core/serverConnectionLifecycle.js +118 -0
- package/shared/server-core/serverHandshake.js +72 -0
- package/shared/server-core/serverIdentity.js +102 -0
- package/shared/server-core/streamJsonParser.js +321 -0
- package/shared/server-core/tmuxManager.js +596 -0
- package/shared/server-core/unixProcessLifecycle.js +96 -0
- package/shared/server-core/unixServerHooks.js +148 -0
- package/shared/server-core/unixWsRuntime.js +412 -0
- package/shared/server-core/uploadService.js +321 -0
- package/shared/server-core/validators.js +274 -0
- package/shared/server-core/windowsServerHooks.js +174 -0
- package/shared/server-core/windowsWsRuntime.js +413 -0
package/lib/validators.js
CHANGED
|
@@ -1,350 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Input validation module
|
|
3
|
-
* Provides strict validation for all user inputs to prevent injection attacks
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
const os = require('os');
|
|
9
|
-
|
|
10
|
-
// Session name: alphanumeric, underscore, hyphen only, max 64 chars
|
|
11
|
-
const SESSION_NAME_RE = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
12
|
-
|
|
13
|
-
// Allowed tmux keys whitelist
|
|
14
|
-
const TMUX_KEY_ALLOWLIST = [
|
|
15
|
-
'Enter', 'Escape', 'Tab', 'BTab', 'S-Tab', 'Space',
|
|
16
|
-
'Up', 'Down', 'Left', 'Right',
|
|
17
|
-
'C-c', 'C-d', 'C-u', 'C-z', 'C-m',
|
|
18
|
-
'C-a', 'C-b', 'C-e', 'C-k', 'C-l', 'C-r', 'C-w',
|
|
19
|
-
'F1', 'F2', 'F3', 'F4', 'F5', 'F6',
|
|
20
|
-
'Home', 'End', 'PPage', 'NPage', 'PageUp', 'PageDown',
|
|
21
|
-
'y', 'n', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
// Sensitive directories that should never be accessed
|
|
25
|
-
const SENSITIVE_DIRS = [
|
|
26
|
-
'.ssh',
|
|
27
|
-
'.gnupg',
|
|
28
|
-
'.aws',
|
|
29
|
-
'.kube',
|
|
30
|
-
'.config/gcloud',
|
|
31
|
-
'Library/Keychains',
|
|
32
|
-
'Library/Cookies',
|
|
33
|
-
'Library/Application Support/1Password'
|
|
34
|
-
];
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Validate session name
|
|
38
|
-
* @param {string} name - Session name to validate
|
|
39
|
-
* @returns {{ valid: boolean, error?: string }}
|
|
40
|
-
*/
|
|
41
|
-
function validateSessionName(name) {
|
|
42
|
-
if (!name || typeof name !== 'string') {
|
|
43
|
-
return { valid: false, error: 'Session name is required' };
|
|
44
|
-
}
|
|
45
|
-
if (!SESSION_NAME_RE.test(name)) {
|
|
46
|
-
return { valid: false, error: 'Session name can only contain letters, numbers, underscore and hyphen (max 64 chars)' };
|
|
47
|
-
}
|
|
48
|
-
return { valid: true };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Validate tmux key
|
|
53
|
-
* @param {string} key - Key to validate
|
|
54
|
-
* @returns {{ valid: boolean, error?: string, sanitized?: string }}
|
|
55
|
-
*/
|
|
56
|
-
function validateTmuxKey(key) {
|
|
57
|
-
if (!key || typeof key !== 'string') {
|
|
58
|
-
return { valid: false, error: 'Key is required' };
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Check whitelist first
|
|
62
|
-
if (TMUX_KEY_ALLOWLIST.includes(key)) {
|
|
63
|
-
return { valid: true, sanitized: key };
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Allow single printable ASCII characters (for typing)
|
|
67
|
-
if (/^[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~ ]$/.test(key)) {
|
|
68
|
-
return { valid: true, sanitized: key };
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return { valid: false, error: `Invalid key: ${key}` };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Validate Claude command
|
|
76
|
-
* @param {string} cmd - Command to validate
|
|
77
|
-
* @returns {{ valid: boolean, error?: string, sanitized?: string }}
|
|
78
|
-
*/
|
|
79
|
-
function validateClaudeCommand(cmd) {
|
|
80
|
-
if (!cmd || typeof cmd !== 'string') {
|
|
81
|
-
return { valid: true, sanitized: 'claude' }; // default
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Only allow claude command with optional flags
|
|
85
|
-
// Pattern: claude followed by optional flags like --dangerously-skip-permissions
|
|
86
|
-
const allowedPattern = /^(claude|codex)(\s+--?[a-zA-Z0-9-]+)*$/;
|
|
87
|
-
const trimmed = cmd.trim();
|
|
88
|
-
|
|
89
|
-
if (!allowedPattern.test(trimmed)) {
|
|
90
|
-
return { valid: false, error: 'Invalid claude command format' };
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return { valid: true, sanitized: trimmed };
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Get the matching sensitive directory entry for a path, or null
|
|
98
|
-
* @param {string} resolvedPath - Resolved absolute path
|
|
99
|
-
* @returns {string|null} The matched SENSITIVE_DIRS entry, or null
|
|
100
|
-
*/
|
|
101
|
-
function getSensitiveDir(resolvedPath) {
|
|
102
|
-
const segments = resolvedPath.split(path.sep).filter(Boolean);
|
|
103
|
-
const lowerSegments = segments.map(s => s.toLowerCase());
|
|
104
|
-
|
|
105
|
-
for (const sensitive of SENSITIVE_DIRS) {
|
|
106
|
-
const sensitiveSegments = sensitive.toLowerCase().split('/');
|
|
107
|
-
|
|
108
|
-
for (let i = 0; i <= lowerSegments.length - sensitiveSegments.length; i++) {
|
|
109
|
-
let match = true;
|
|
110
|
-
for (let j = 0; j < sensitiveSegments.length; j++) {
|
|
111
|
-
if (lowerSegments[i + j] !== sensitiveSegments[j]) {
|
|
112
|
-
match = false;
|
|
113
|
-
break;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (match) {
|
|
117
|
-
return sensitive;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Check if path contains sensitive directories
|
|
126
|
-
* @param {string} resolvedPath - Resolved absolute path
|
|
127
|
-
* @returns {boolean}
|
|
128
|
-
*/
|
|
129
|
-
function containsSensitiveDir(resolvedPath) {
|
|
130
|
-
return getSensitiveDir(resolvedPath) !== null;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Check whether a target path is inside a base path (or equal to it)
|
|
135
|
-
* using normalized relative path checks.
|
|
136
|
-
* @param {string} targetPath
|
|
137
|
-
* @param {string} basePath
|
|
138
|
-
* @returns {boolean}
|
|
139
|
-
*/
|
|
140
|
-
function isPathWithinBase(targetPath, basePath) {
|
|
141
|
-
const relative = path.relative(path.resolve(basePath), path.resolve(targetPath));
|
|
142
|
-
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Validate file system path
|
|
147
|
-
* @param {string} userPath - User-provided path
|
|
148
|
-
* @param {object} options - Validation options
|
|
149
|
-
* @param {boolean} options.allowBrowse - Allow browsing home directory
|
|
150
|
-
* @param {string} options.allowedBasePath - Allowed base path for write operations
|
|
151
|
-
* @param {string} options.currentPath - Current client path for resolving relative input paths
|
|
152
|
-
* @returns {{ valid: boolean, error?: string, path?: string }}
|
|
153
|
-
*/
|
|
154
|
-
function validatePath(userPath, options = {}) {
|
|
155
|
-
const {
|
|
156
|
-
allowBrowse = false,
|
|
157
|
-
allowedBasePath = os.homedir(),
|
|
158
|
-
currentPath = allowedBasePath
|
|
159
|
-
} = options;
|
|
160
|
-
|
|
161
|
-
if (!userPath || typeof userPath !== 'string') {
|
|
162
|
-
return { valid: false, error: 'Path is required' };
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Normalize path: strip leading ~ from absolute paths
|
|
166
|
-
let cleanPath = userPath.trim();
|
|
167
|
-
const homeDir = os.homedir();
|
|
168
|
-
|
|
169
|
-
// Fix malformed "~/Users/yi/..." → "/Users/yi/..." (just remove the ~, keep the /)
|
|
170
|
-
if (cleanPath.startsWith('~') && cleanPath.length > 1 && cleanPath[1] === '/') {
|
|
171
|
-
const afterTilde = cleanPath.substring(2); // strip "~/"
|
|
172
|
-
// If the remainder starts with an absolute-looking segment that matches homedir
|
|
173
|
-
if (('/' + afterTilde).startsWith(homeDir)) {
|
|
174
|
-
cleanPath = '/' + afterTilde;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// Expand ~ or ~/relative to home directory
|
|
179
|
-
const expandedPath = cleanPath.startsWith('~')
|
|
180
|
-
? homeDir + cleanPath.substring(1) // ~/foo → /Users/yi/foo, ~ → /Users/yi
|
|
181
|
-
: cleanPath;
|
|
182
|
-
const resolvedCurrent = path.resolve(currentPath || allowedBasePath || homeDir);
|
|
183
|
-
const resolvedPath = path.isAbsolute(expandedPath)
|
|
184
|
-
? path.resolve(expandedPath)
|
|
185
|
-
: path.resolve(resolvedCurrent, expandedPath);
|
|
186
|
-
const resolvedBase = path.resolve(allowedBasePath);
|
|
187
|
-
let realHome = homeDir;
|
|
188
|
-
let realBase = resolvedBase;
|
|
189
|
-
|
|
190
|
-
try {
|
|
191
|
-
realHome = fs.realpathSync(homeDir);
|
|
192
|
-
} catch {
|
|
193
|
-
// Keep lexical home path if resolution fails
|
|
194
|
-
}
|
|
195
|
-
try {
|
|
196
|
-
realBase = fs.realpathSync(resolvedBase);
|
|
197
|
-
} catch {
|
|
198
|
-
// Keep lexical base path if resolution fails
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// Resolve symlinks to prevent traversal outside allowed paths
|
|
202
|
-
let realPath = resolvedPath;
|
|
203
|
-
try {
|
|
204
|
-
realPath = fs.realpathSync(resolvedPath);
|
|
205
|
-
} catch {
|
|
206
|
-
// Path doesn't exist yet (e.g., createFolder) — resolve parent
|
|
207
|
-
try {
|
|
208
|
-
realPath = path.join(fs.realpathSync(path.dirname(resolvedPath)), path.basename(resolvedPath));
|
|
209
|
-
} catch {
|
|
210
|
-
// Parent doesn't exist either — keep lexical resolution
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// Check for sensitive directories — return metadata for auth challenge flow
|
|
215
|
-
const matchedSensitiveDir = getSensitiveDir(realPath);
|
|
216
|
-
if (matchedSensitiveDir) {
|
|
217
|
-
return {
|
|
218
|
-
valid: false,
|
|
219
|
-
error: 'Access denied: sensitive directory',
|
|
220
|
-
requiresAuth: true,
|
|
221
|
-
sensitiveDir: matchedSensitiveDir
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// For browsing (listDirectory), allow access to home directory and below
|
|
226
|
-
if (allowBrowse) {
|
|
227
|
-
if (!isPathWithinBase(realPath, realHome)) {
|
|
228
|
-
return {
|
|
229
|
-
valid: false,
|
|
230
|
-
error: 'Access denied: cannot browse outside home directory'
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
return { valid: true, path: realPath };
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// For write operations, enforce the allowedBasePath restriction
|
|
237
|
-
if (!isPathWithinBase(realPath, realBase)) {
|
|
238
|
-
return {
|
|
239
|
-
valid: false,
|
|
240
|
-
error: `Access denied: path outside allowed directory (${allowedBasePath})`
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return { valid: true, path: realPath };
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Escape content for tmux send-keys (literal mode)
|
|
249
|
-
* @param {string} content - Content to escape
|
|
250
|
-
* @returns {string}
|
|
251
|
-
*/
|
|
252
|
-
function escapeForTmux(content) {
|
|
253
|
-
if (!content || typeof content !== 'string') {
|
|
254
|
-
return '';
|
|
255
|
-
}
|
|
256
|
-
// For tmux send-keys -l (literal mode), keep text unchanged except
|
|
257
|
-
// for newline normalization. Caller handles splitting and Enter presses.
|
|
258
|
-
return content.replace(/\r\n?/g, '\n');
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Validate number of lines for output capture
|
|
263
|
-
* @param {number} lines - Number of lines
|
|
264
|
-
* @returns {{ valid: boolean, sanitized: number }}
|
|
265
|
-
*/
|
|
266
|
-
function validateLineCount(lines) {
|
|
267
|
-
const num = parseInt(lines, 10);
|
|
268
|
-
if (isNaN(num) || num < 1) {
|
|
269
|
-
return { valid: true, sanitized: 500 }; // default
|
|
270
|
-
}
|
|
271
|
-
// Cap at 10000 lines to prevent memory issues
|
|
272
|
-
return { valid: true, sanitized: Math.min(num, 10000) };
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Validate filename for upload
|
|
277
|
-
* @param {string} name - Filename to validate
|
|
278
|
-
* @returns {{ valid: boolean, error?: string }}
|
|
279
|
-
*/
|
|
280
|
-
function validateFilename(name) {
|
|
281
|
-
if (!name || typeof name !== 'string') {
|
|
282
|
-
return { valid: false, error: 'Filename is required' };
|
|
283
|
-
}
|
|
284
|
-
if (name.length > 255) {
|
|
285
|
-
return { valid: false, error: 'Filename too long (max 255 characters)' };
|
|
286
|
-
}
|
|
287
|
-
if (/\.\./.test(name) || /[/\\]/.test(name)) {
|
|
288
|
-
return { valid: false, error: 'Filename contains invalid characters' };
|
|
289
|
-
}
|
|
290
|
-
if (/[\x00-\x1f\x7f]/.test(name)) {
|
|
291
|
-
return { valid: false, error: 'Filename contains control characters' };
|
|
292
|
-
}
|
|
293
|
-
return { valid: true };
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Validate upload file size
|
|
298
|
-
* @param {number} size - File size in bytes
|
|
299
|
-
* @param {number} maxSize - Maximum allowed size (default 50MB)
|
|
300
|
-
* @returns {{ valid: boolean, error?: string }}
|
|
301
|
-
*/
|
|
302
|
-
function validateUploadSize(size, maxSize = 50 * 1024 * 1024) {
|
|
303
|
-
if (typeof size !== 'number' || size <= 0) {
|
|
304
|
-
return { valid: false, error: 'Invalid file size' };
|
|
305
|
-
}
|
|
306
|
-
if (size > maxSize) {
|
|
307
|
-
return { valid: false, error: `File too large: ${(size / 1024 / 1024).toFixed(1)}MB exceeds ${(maxSize / 1024 / 1024)}MB limit` };
|
|
308
|
-
}
|
|
309
|
-
return { valid: true };
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Validate MIME type against whitelist
|
|
314
|
-
* @param {string} mime - MIME type string
|
|
315
|
-
* @returns {{ valid: boolean, error?: string }}
|
|
316
|
-
*/
|
|
317
|
-
function validateMimeType(mime) {
|
|
318
|
-
if (!mime || typeof mime !== 'string') {
|
|
319
|
-
return { valid: false, error: 'MIME type is required' };
|
|
320
|
-
}
|
|
321
|
-
// Accept broad categories
|
|
322
|
-
const allowedPrefixes = ['image/', 'text/', 'application/pdf', 'application/json',
|
|
323
|
-
'application/xml', 'application/zip', 'application/gzip', 'application/javascript',
|
|
324
|
-
'application/typescript', 'application/x-yaml', 'application/x-tar',
|
|
325
|
-
'application/x-7z-compressed', 'application/octet-stream',
|
|
326
|
-
'application/msword', 'application/vnd.openxmlformats', 'application/vnd.ms-'];
|
|
327
|
-
const isAllowed = allowedPrefixes.some(prefix => mime.startsWith(prefix));
|
|
328
|
-
if (!isAllowed) {
|
|
329
|
-
return { valid: false, error: `Unsupported file type: ${mime}` };
|
|
330
|
-
}
|
|
331
|
-
return { valid: true };
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
module.exports = {
|
|
335
|
-
validateSessionName,
|
|
336
|
-
validateTmuxKey,
|
|
337
|
-
validateClaudeCommand,
|
|
338
|
-
validatePath,
|
|
339
|
-
escapeForTmux,
|
|
340
|
-
validateLineCount,
|
|
341
|
-
getSensitiveDir,
|
|
342
|
-
containsSensitiveDir,
|
|
343
|
-
isPathWithinBase,
|
|
344
|
-
validateFilename,
|
|
345
|
-
validateUploadSize,
|
|
346
|
-
validateMimeType,
|
|
347
|
-
SESSION_NAME_RE,
|
|
348
|
-
TMUX_KEY_ALLOWLIST,
|
|
349
|
-
SENSITIVE_DIRS
|
|
350
|
-
};
|
|
1
|
+
module.exports = require('../shared/server-core/validators');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tacticremote",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Control Claude Code from your iPhone/iPad — server for Tactic Remote",
|
|
5
5
|
"bin": {
|
|
6
6
|
"tacticremote": "./bin/tactic-remote-server.js"
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
"lib/",
|
|
52
52
|
"middleware/",
|
|
53
53
|
"routes/",
|
|
54
|
+
"shared/",
|
|
54
55
|
"server.js",
|
|
55
56
|
"README.md",
|
|
56
57
|
"LICENSE"
|
package/routes/httpHandler.js
CHANGED
|
@@ -66,7 +66,9 @@ function createHttpHandler(server, handleHookEvent) {
|
|
|
66
66
|
try {
|
|
67
67
|
const rawBody = Buffer.concat(bodyChunks).toString('utf8');
|
|
68
68
|
const hookData = JSON.parse(rawBody);
|
|
69
|
-
handleHookEvent(hookData)
|
|
69
|
+
Promise.resolve(handleHookEvent(hookData)).catch(err => {
|
|
70
|
+
console.error('[hook] handler error:', err.message);
|
|
71
|
+
});
|
|
70
72
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
71
73
|
res.end(JSON.stringify({ success: true }));
|
|
72
74
|
} catch (error) {
|