upfynai-code 2.9.0 → 2.9.2
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/README.md +91 -66
- package/client/dist/api-docs.html +838 -0
- package/client/dist/assets/AppContent-BXZDeSIC.js +545 -0
- package/client/dist/assets/CanvasFullScreen-mnpCnLZ9.js +1 -0
- package/client/dist/assets/CanvasWorkspace-4CqmjAVQ.js +163 -0
- package/client/dist/assets/DashboardPanel-zFIFlw56.js +1 -0
- package/client/dist/assets/FileTree-B0c_GaB3.js +1 -0
- package/client/dist/assets/GitPanel-DUP4zVU4.js +2 -0
- package/client/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- package/client/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- package/client/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- package/client/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- package/client/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- package/client/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- package/client/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- package/client/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- package/client/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- package/client/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- package/client/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- package/client/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- package/client/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- package/client/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- package/client/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- package/client/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- package/client/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- package/client/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- package/client/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- package/client/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- package/client/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- package/client/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- package/client/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- package/client/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- package/client/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- package/client/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- package/client/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- package/client/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- package/client/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- package/client/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- package/client/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- package/client/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- package/client/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- package/client/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- package/client/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- package/client/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- package/client/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- package/client/dist/assets/LoginModal-BRycfsyD.js +13 -0
- package/client/dist/assets/MarkdownPreview-DHmk3qzu.js +1 -0
- package/client/dist/assets/MermaidBlock-BuBc_G-F.js +2 -0
- package/client/dist/assets/Onboarding-BcnaZZ0o.js +1 -0
- package/client/dist/assets/PreviewPanel-CqCa92Tf.js +32 -0
- package/client/dist/assets/SetupForm-S0g6u5yT.js +1 -0
- package/client/dist/assets/WorkflowsPanel-CouH9JDO.js +1 -0
- package/client/dist/assets/index-BFuqS0tY.css +1 -0
- package/client/dist/assets/index-CNDcVl2g.js +68 -0
- package/client/dist/assets/pdf-CE_K4jFx.js +12 -0
- package/client/dist/assets/vendor-canvas-BZV40eAE.css +1 -0
- package/client/dist/assets/vendor-canvas-D39yWul6.js +49 -0
- package/client/dist/assets/vendor-codemirror-CbtmxxaB.js +35 -0
- package/client/dist/assets/vendor-diff-DNQpbhrT.js +69 -0
- package/client/dist/assets/vendor-i18n-DCFGyhQR.js +1 -0
- package/client/dist/assets/vendor-icons-BaD0x9SL.js +711 -0
- package/client/dist/assets/vendor-markdown-CimbIo6Y.js +296 -0
- package/client/dist/assets/vendor-mermaid-CH7SGc99.js +2556 -0
- package/client/dist/assets/vendor-react-96lCPsRK.js +67 -0
- package/client/dist/assets/vendor-syntax-DuHI9Ok6.js +16 -0
- package/client/dist/assets/vendor-xterm-CZq1hqo1.js +66 -0
- package/client/dist/assets/vendor-xterm-qxJ8_QYu.css +32 -0
- package/client/dist/clear-cache.html +85 -0
- package/client/dist/convert-icons.md +53 -0
- package/client/dist/favicon.png +0 -0
- package/client/dist/favicon.svg +5 -0
- package/client/dist/generate-icons.js +49 -0
- package/client/dist/icons/claude-ai-icon.svg +1 -0
- package/client/dist/icons/codex-white.svg +3 -0
- package/client/dist/icons/codex.svg +3 -0
- package/client/dist/icons/cursor-white.svg +12 -0
- package/client/dist/icons/cursor.svg +1 -0
- package/client/dist/icons/icon-128x128.png +0 -0
- package/client/dist/icons/icon-128x128.svg +5 -0
- package/client/dist/icons/icon-144x144.png +0 -0
- package/client/dist/icons/icon-144x144.svg +5 -0
- package/client/dist/icons/icon-152x152.png +0 -0
- package/client/dist/icons/icon-152x152.svg +5 -0
- package/client/dist/icons/icon-192x192.png +0 -0
- package/client/dist/icons/icon-192x192.svg +5 -0
- package/client/dist/icons/icon-384x384.png +0 -0
- package/client/dist/icons/icon-384x384.svg +5 -0
- package/client/dist/icons/icon-512x512.png +0 -0
- package/client/dist/icons/icon-512x512.svg +5 -0
- package/client/dist/icons/icon-72x72.png +0 -0
- package/client/dist/icons/icon-72x72.svg +5 -0
- package/client/dist/icons/icon-96x96.png +0 -0
- package/client/dist/icons/icon-96x96.svg +5 -0
- package/client/dist/icons/icon-template.svg +5 -0
- package/client/dist/index.html +119 -0
- package/client/dist/logo-128.png +0 -0
- package/client/dist/logo-256.png +0 -0
- package/client/dist/logo-32.png +0 -0
- package/client/dist/logo-512.png +0 -0
- package/client/dist/logo-64.png +0 -0
- package/client/dist/logo.svg +14 -0
- package/client/dist/manifest.json +61 -0
- package/client/dist/mcp-docs.html +108 -0
- package/client/dist/offline.html +84 -0
- package/client/dist/screenshots/cli-selection.png +0 -0
- package/client/dist/screenshots/desktop-main.png +0 -0
- package/client/dist/screenshots/mobile-chat.png +0 -0
- package/client/dist/screenshots/tools-modal.png +0 -0
- package/client/dist/sw.js +82 -0
- package/commands/upfynai-connect.md +59 -0
- package/commands/upfynai-disconnect.md +31 -0
- package/commands/upfynai-doctor.md +99 -0
- package/commands/upfynai-export.md +49 -0
- package/commands/upfynai-local.md +82 -0
- package/commands/upfynai-status.md +75 -0
- package/commands/upfynai-stop.md +49 -0
- package/commands/upfynai-uninstall.md +58 -0
- package/commands/upfynai.md +69 -0
- package/package.json +143 -82
- package/scripts/build-client.js +17 -0
- package/scripts/fix-node-pty.js +67 -0
- package/scripts/install-commands.js +78 -0
- package/server/agent-loop.js +242 -0
- package/server/auto-compact.js +99 -0
- package/server/claude-sdk.js +797 -0
- package/server/cli-ui.js +785 -0
- package/server/cli.js +596 -0
- package/server/constants/config.js +31 -0
- package/server/cursor-cli.js +270 -0
- package/server/database/auth.db +0 -0
- package/server/database/db.js +1391 -0
- package/server/database/init.sql +70 -0
- package/server/index.js +3799 -0
- package/server/load-env.js +26 -0
- package/server/mcp-server.js +621 -0
- package/server/middleware/auth.js +176 -0
- package/server/middleware/relayHelpers.js +44 -0
- package/server/middleware/sandboxRouter.js +174 -0
- package/server/openai-codex.js +403 -0
- package/server/openrouter.js +137 -0
- package/server/projects.js +1807 -0
- package/server/provider-factory.js +174 -0
- package/server/relay-client.js +379 -0
- package/server/routes/agent.js +1226 -0
- package/server/routes/auth.js +554 -0
- package/server/routes/canvas.js +53 -0
- package/server/routes/cli-auth.js +263 -0
- package/server/routes/codex.js +396 -0
- package/server/routes/commands.js +707 -0
- package/server/routes/composio.js +176 -0
- package/server/routes/cursor.js +770 -0
- package/server/routes/dashboard.js +295 -0
- package/server/routes/git.js +1208 -0
- package/server/routes/keys.js +34 -0
- package/server/routes/mcp-utils.js +48 -0
- package/server/routes/mcp.js +661 -0
- package/server/routes/payments.js +227 -0
- package/server/routes/projects.js +655 -0
- package/server/routes/sessions.js +146 -0
- package/server/routes/settings.js +261 -0
- package/server/routes/taskmaster.js +1928 -0
- package/server/routes/user.js +106 -0
- package/server/routes/vapi-chat.js +624 -0
- package/server/routes/voice.js +235 -0
- package/server/routes/webhooks.js +166 -0
- package/server/routes/workflows.js +312 -0
- package/server/sandbox.js +120 -0
- package/server/services/composio.js +204 -0
- package/server/services/sessionRegistry.js +139 -0
- package/server/services/whisperService.js +84 -0
- package/server/services/workflowScheduler.js +206 -0
- package/server/tests/relay-flow.test.js +570 -0
- package/server/tests/sessions.test.js +259 -0
- package/server/utils/commandParser.js +303 -0
- package/server/utils/email.js +61 -0
- package/server/utils/gitConfig.js +24 -0
- package/server/utils/mcp-detector.js +198 -0
- package/server/utils/taskmaster-websocket.js +129 -0
- package/shared/integrationCatalog.d.ts +12 -0
- package/shared/integrationCatalog.js +172 -0
- package/shared/modelConstants.js +96 -0
- package/bin/cli.js +0 -97
- package/dist/agents/claude.js +0 -229
- package/dist/agents/codex.js +0 -48
- package/dist/agents/cursor.js +0 -48
- package/dist/agents/detect.js +0 -51
- package/dist/agents/exec.js +0 -31
- package/dist/agents/files.js +0 -105
- package/dist/agents/git.js +0 -18
- package/dist/agents/gitagent.js +0 -67
- package/dist/agents/index.js +0 -88
- package/dist/agents/shell.js +0 -38
- package/dist/agents/utils.js +0 -136
- package/scripts/postinstall.js +0 -9
- package/scripts/prepublish.js +0 -58
- package/src/animation.js +0 -228
- package/src/auth.js +0 -122
- package/src/config.js +0 -40
- package/src/connect.js +0 -416
- package/src/launch.js +0 -78
- package/src/mcp.js +0 -57
- package/src/permissions.js +0 -140
- package/src/persistent-shell.js +0 -261
- package/src/server.js +0 -54
- /package/{dist → shared}/gitagent/index.js +0 -0
- /package/{dist → shared}/gitagent/parser.js +0 -0
- /package/{dist → shared}/gitagent/prompt-builder.js +0 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import jwt from 'jsonwebtoken';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import { userDb, relayTokensDb } from '../database/db.js';
|
|
4
|
+
import { IS_PLATFORM } from '../constants/config.js';
|
|
5
|
+
|
|
6
|
+
let JWT_SECRET = process.env.JWT_SECRET?.trim();
|
|
7
|
+
if (!JWT_SECRET) {
|
|
8
|
+
if (IS_PLATFORM) {
|
|
9
|
+
// In local/self-hosted mode, generate a random secret (auth is bypassed anyway)
|
|
10
|
+
JWT_SECRET = crypto.randomBytes(32).toString('hex');
|
|
11
|
+
} else {
|
|
12
|
+
console.error('[SECURITY] JWT_SECRET environment variable is required. Server cannot start without it.');
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Optional static API key middleware
|
|
18
|
+
const validateApiKey = (req, res, next) => {
|
|
19
|
+
if (!process.env.API_KEY) return next();
|
|
20
|
+
const apiKey = req.headers['x-api-key'];
|
|
21
|
+
if (apiKey !== process.env.API_KEY.trim()) {
|
|
22
|
+
return res.status(401).json({ error: 'Invalid API key' });
|
|
23
|
+
}
|
|
24
|
+
next();
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Extract JWT from request: cookie → Bearer header → query param (SSE only)
|
|
28
|
+
const extractToken = (req) => {
|
|
29
|
+
// 1. httpOnly cookie (browser sessions — primary auth method)
|
|
30
|
+
if (req.cookies?.session) return req.cookies.session;
|
|
31
|
+
|
|
32
|
+
// 2. Bearer header (API clients, MCP)
|
|
33
|
+
const authHeader = req.headers['authorization'];
|
|
34
|
+
if (authHeader?.startsWith('Bearer ')) return authHeader.slice(7);
|
|
35
|
+
|
|
36
|
+
// 3. Query param — for GET requests (SSE EventSource + iframe embedding)
|
|
37
|
+
if (req.query?.token && req.method === 'GET') {
|
|
38
|
+
return req.query.token;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return null;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// JWT authentication middleware
|
|
45
|
+
const authenticateToken = async (req, res, next) => {
|
|
46
|
+
// Platform mode: use first database user
|
|
47
|
+
if (IS_PLATFORM) {
|
|
48
|
+
try {
|
|
49
|
+
const user = await userDb.getFirstUser();
|
|
50
|
+
if (!user) return res.status(500).json({ error: 'Platform mode: No user found in database' });
|
|
51
|
+
req.user = user;
|
|
52
|
+
return next();
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error('Platform mode error:', error);
|
|
55
|
+
return res.status(500).json({ error: 'Platform mode: Failed to fetch user' });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const token = extractToken(req);
|
|
60
|
+
if (!token) {
|
|
61
|
+
return res.status(401).json({ error: 'Access denied. No token provided.' });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const decoded = jwt.verify(token, JWT_SECRET);
|
|
66
|
+
const user = await userDb.getUserById(decoded.userId);
|
|
67
|
+
if (!user) return res.status(401).json({ error: 'Invalid token. User not found.' });
|
|
68
|
+
req.user = user;
|
|
69
|
+
// If token came from query param, set session cookie for subsequent requests (iframe auto-auth)
|
|
70
|
+
if (req.query?.token && !req.cookies?.session) {
|
|
71
|
+
res.cookie('session', token, COOKIE_OPTIONS);
|
|
72
|
+
}
|
|
73
|
+
next();
|
|
74
|
+
} catch (error) {
|
|
75
|
+
return res.status(403).json({ error: 'Invalid or expired token' });
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// Generate JWT token (30-day expiration)
|
|
80
|
+
const generateToken = (user) => {
|
|
81
|
+
return jwt.sign(
|
|
82
|
+
{ userId: user.id, username: user.username },
|
|
83
|
+
JWT_SECRET,
|
|
84
|
+
{ expiresIn: '30d' }
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// Cookie config for httpOnly session
|
|
89
|
+
// Works for both self-hosted (same origin) and split deploy (Vercel proxy → Railway)
|
|
90
|
+
const isSecureEnv = process.env.NODE_ENV === 'production' || !!process.env.VERCEL || !!process.env.RAILWAY_ENVIRONMENT;
|
|
91
|
+
const COOKIE_OPTIONS = {
|
|
92
|
+
httpOnly: true,
|
|
93
|
+
secure: isSecureEnv,
|
|
94
|
+
// 'none' required for cross-origin iframe embedding (Vercel frontend → Railway backend)
|
|
95
|
+
// 'strict' used in local/dev mode where everything is same-origin
|
|
96
|
+
sameSite: isSecureEnv ? 'none' : 'strict',
|
|
97
|
+
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
|
|
98
|
+
path: '/',
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// Set session cookie on response
|
|
102
|
+
const setSessionCookie = (res, token) => {
|
|
103
|
+
res.cookie('session', token, COOKIE_OPTIONS);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// Clear session cookie
|
|
107
|
+
const clearSessionCookie = (res) => {
|
|
108
|
+
res.clearCookie('session', { path: '/' });
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// WebSocket authentication (parse cookie from upgrade request headers)
|
|
112
|
+
const authenticateWebSocket = async (request) => {
|
|
113
|
+
// Platform mode: bypass
|
|
114
|
+
if (IS_PLATFORM) {
|
|
115
|
+
try {
|
|
116
|
+
const user = await userDb.getFirstUser();
|
|
117
|
+
return user ? { userId: user.id, username: user.username } : null;
|
|
118
|
+
} catch { return null; }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let token = null;
|
|
122
|
+
|
|
123
|
+
// 1. Parse cookie from upgrade request
|
|
124
|
+
const cookieHeader = request.headers?.cookie || '';
|
|
125
|
+
if (cookieHeader) {
|
|
126
|
+
const cookies = Object.fromEntries(
|
|
127
|
+
cookieHeader.split(';').map(c => {
|
|
128
|
+
const [k, ...v] = c.trim().split('=');
|
|
129
|
+
return [k, v.join('=')];
|
|
130
|
+
})
|
|
131
|
+
);
|
|
132
|
+
token = cookies.session || null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// 2. Fallback: query param (legacy)
|
|
136
|
+
if (!token) {
|
|
137
|
+
try {
|
|
138
|
+
const url = new URL(request.url, 'http://localhost');
|
|
139
|
+
token = url.searchParams.get('token');
|
|
140
|
+
} catch { /* ignore */ }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!token) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Relay token (upfyn_ prefix) — validate against DB, not JWT
|
|
148
|
+
if (token.startsWith('upfyn_') || token.startsWith('rt_')) {
|
|
149
|
+
try {
|
|
150
|
+
const tokenData = await relayTokensDb.validateToken(token);
|
|
151
|
+
if (tokenData) {
|
|
152
|
+
return { userId: Number(tokenData.user_id), username: tokenData.username };
|
|
153
|
+
}
|
|
154
|
+
} catch {
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
const decoded = jwt.verify(token, JWT_SECRET);
|
|
161
|
+
// Validate against Turso — DB is source of truth
|
|
162
|
+
const user = await userDb.getUserById(decoded.userId);
|
|
163
|
+
if (!user) return null;
|
|
164
|
+
return { userId: user.id, username: user.username };
|
|
165
|
+
} catch { return null; }
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
export {
|
|
169
|
+
validateApiKey,
|
|
170
|
+
authenticateToken,
|
|
171
|
+
generateToken,
|
|
172
|
+
authenticateWebSocket,
|
|
173
|
+
setSessionCookie,
|
|
174
|
+
clearSessionCookie,
|
|
175
|
+
JWT_SECRET
|
|
176
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { IS_CLOUD } from '../constants/config.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates middleware that injects relay helper functions onto req.
|
|
5
|
+
* Must be applied AFTER authenticateToken.
|
|
6
|
+
*
|
|
7
|
+
* Security model:
|
|
8
|
+
* - All relay commands go through sendRelayCommand which validates action allowlist
|
|
9
|
+
* - Shell commands are restricted to known-safe prefixes (git, mkdir, rm, wmic)
|
|
10
|
+
* - Dangerous shell patterns (pipes to bash, backtick injection, etc.) are blocked
|
|
11
|
+
* - IP pinning: relay connection records the CLI's IP; only the authenticated user can send commands
|
|
12
|
+
* - The relay token is validated at WebSocket connect time; commands flow only to the token owner's machine
|
|
13
|
+
*
|
|
14
|
+
* Adds to req:
|
|
15
|
+
* - req.isCloud: boolean — true when running on Railway/Vercel/Render
|
|
16
|
+
* - req.hasRelay(): boolean — true when user's machine is connected
|
|
17
|
+
* - req.sendRelay(action, payload, timeout): Promise — send command to user's machine
|
|
18
|
+
* - req.requireRelay(): returns false + sends 503 if cloud mode and no relay connected
|
|
19
|
+
*/
|
|
20
|
+
export function createRelayMiddleware(hasActiveRelay, sendRelayCommand, withRetry) {
|
|
21
|
+
return (req, res, next) => {
|
|
22
|
+
const userId = Number(req.user?.id);
|
|
23
|
+
req.isCloud = IS_CLOUD;
|
|
24
|
+
req.hasRelay = () => hasActiveRelay(userId);
|
|
25
|
+
// Retry transient relay failures (opencode pattern: exponential backoff)
|
|
26
|
+
req.sendRelay = (action, payload, timeout) =>
|
|
27
|
+
withRetry(() => sendRelayCommand(userId, action, payload, null, timeout || 15000), { maxRetries: 2 });
|
|
28
|
+
|
|
29
|
+
// Helper: return 503 if cloud mode and no relay connected
|
|
30
|
+
req.requireRelay = () => {
|
|
31
|
+
if (IS_CLOUD && !hasActiveRelay(userId)) {
|
|
32
|
+
res.status(503).json({
|
|
33
|
+
error: 'Machine not connected',
|
|
34
|
+
message: 'Run "uc connect" on your local machine to use this feature.',
|
|
35
|
+
code: 'RELAY_NOT_CONNECTED'
|
|
36
|
+
});
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
next();
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sandbox Command Router Middleware
|
|
3
|
+
* Intercepts REST API commands when no relay is connected,
|
|
4
|
+
* routing them to the sandbox service instead.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { sandboxClient } from '../sandbox.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Middleware that routes file/git/exec commands to sandbox when no relay.
|
|
11
|
+
* Used on routes that normally proxy to the relay (file ops, git, etc.)
|
|
12
|
+
*
|
|
13
|
+
* Usage: app.use('/api/sandbox-proxy', authenticateToken, sandboxCommandRouter);
|
|
14
|
+
*/
|
|
15
|
+
export function sandboxCommandRouter(req, res, next) {
|
|
16
|
+
// If relay is connected, let the normal relay flow handle it
|
|
17
|
+
if (req.hasRelay && req.hasRelay()) {
|
|
18
|
+
return next();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// No relay — check if sandbox is available
|
|
22
|
+
handleSandboxCommand(req, res).catch(() => {
|
|
23
|
+
res.status(503).json({ error: 'No machine connected and sandbox unavailable' });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function handleSandboxCommand(req, res) {
|
|
28
|
+
const userId = req.user.id;
|
|
29
|
+
const { action, ...params } = req.body;
|
|
30
|
+
|
|
31
|
+
// Verify sandbox is available
|
|
32
|
+
const available = await sandboxClient.isAvailable();
|
|
33
|
+
if (!available) {
|
|
34
|
+
return res.status(503).json({ error: 'No machine connected and sandbox service unreachable' });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Check if user has an active sandbox
|
|
38
|
+
const status = await sandboxClient.getStatus(userId);
|
|
39
|
+
if (!status.exists) {
|
|
40
|
+
// Auto-init sandbox for the user
|
|
41
|
+
await sandboxClient.initSandbox(userId);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Route based on action
|
|
45
|
+
switch (action) {
|
|
46
|
+
case 'file-read': {
|
|
47
|
+
const result = await sandboxClient.readFile(userId, params.filePath);
|
|
48
|
+
return res.json(result);
|
|
49
|
+
}
|
|
50
|
+
case 'file-write': {
|
|
51
|
+
const result = await sandboxClient.writeFile(userId, params.filePath, params.content);
|
|
52
|
+
return res.json(result);
|
|
53
|
+
}
|
|
54
|
+
case 'file-tree': {
|
|
55
|
+
const result = await sandboxClient.getFileTree(userId, params.dirPath, params.depth);
|
|
56
|
+
return res.json(result);
|
|
57
|
+
}
|
|
58
|
+
case 'exec': {
|
|
59
|
+
const result = await sandboxClient.exec(userId, params.command, {
|
|
60
|
+
cwd: params.cwd,
|
|
61
|
+
timeout: params.timeout,
|
|
62
|
+
userKeys: params.userKeys,
|
|
63
|
+
});
|
|
64
|
+
return res.json(result);
|
|
65
|
+
}
|
|
66
|
+
case 'git': {
|
|
67
|
+
const result = await sandboxClient.gitOperation(userId, params.gitCommand, params.cwd);
|
|
68
|
+
return res.json(result);
|
|
69
|
+
}
|
|
70
|
+
default:
|
|
71
|
+
return res.status(400).json({ error: `Unknown sandbox action: ${action}` });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* WebSocket sandbox bridge — used in the WS handler for routing
|
|
77
|
+
* claude-command/shell-command/file-* when no relay is connected.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} userId
|
|
80
|
+
* @param {string} messageType - WS message type (e.g. 'claude-command')
|
|
81
|
+
* @param {object} data - WS message data
|
|
82
|
+
* @param {object} writer - WebSocketWriter for sending responses
|
|
83
|
+
* @returns {boolean} true if handled by sandbox, false if not
|
|
84
|
+
*/
|
|
85
|
+
export async function handleSandboxWebSocketCommand(userId, messageType, data, writer) {
|
|
86
|
+
const available = await sandboxClient.isAvailable();
|
|
87
|
+
if (!available) return false;
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
// Ensure sandbox exists
|
|
91
|
+
const status = await sandboxClient.getStatus(userId);
|
|
92
|
+
if (!status.exists) {
|
|
93
|
+
await sandboxClient.initSandbox(userId);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
switch (messageType) {
|
|
97
|
+
case 'claude-command': {
|
|
98
|
+
// Execute claude CLI command in sandbox
|
|
99
|
+
writer.send({ type: 'session-created', sessionId: data.sessionId || `sandbox-${Date.now()}` });
|
|
100
|
+
writer.send({ type: 'stream', content: '[Sandbox] Executing in cloud sandbox...\n' });
|
|
101
|
+
|
|
102
|
+
const result = await sandboxClient.exec(userId, `claude --print "${data.command}"`, {
|
|
103
|
+
cwd: data.cwd,
|
|
104
|
+
timeout: 120000,
|
|
105
|
+
userKeys: data.userKeys,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
writer.send({
|
|
109
|
+
type: 'stream',
|
|
110
|
+
content: result.stdout || '',
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (result.stderr) {
|
|
114
|
+
writer.send({ type: 'stream', content: `\n${result.stderr}` });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
writer.send({
|
|
118
|
+
type: 'session-complete',
|
|
119
|
+
sessionId: data.sessionId,
|
|
120
|
+
exitCode: result.exitCode,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
case 'shell-command': {
|
|
127
|
+
const result = await sandboxClient.exec(userId, data.command, {
|
|
128
|
+
cwd: data.cwd,
|
|
129
|
+
timeout: data.timeout || 30000,
|
|
130
|
+
});
|
|
131
|
+
writer.send({
|
|
132
|
+
type: 'shell-output',
|
|
133
|
+
stdout: result.stdout,
|
|
134
|
+
stderr: result.stderr,
|
|
135
|
+
exitCode: result.exitCode,
|
|
136
|
+
});
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
case 'file-read': {
|
|
141
|
+
const result = await sandboxClient.readFile(userId, data.filePath);
|
|
142
|
+
writer.send({ type: 'file-content', ...result });
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
case 'file-write': {
|
|
147
|
+
const result = await sandboxClient.writeFile(userId, data.filePath, data.content);
|
|
148
|
+
writer.send({ type: 'file-written', ...result });
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
case 'file-tree': {
|
|
153
|
+
const result = await sandboxClient.getFileTree(userId, data.dirPath, data.depth);
|
|
154
|
+
writer.send({ type: 'file-tree', ...result });
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
case 'git-operation': {
|
|
159
|
+
const result = await sandboxClient.gitOperation(userId, data.gitCommand, data.cwd);
|
|
160
|
+
writer.send({ type: 'git-result', ...result });
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
default:
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
} catch (error) {
|
|
168
|
+
writer.send({
|
|
169
|
+
type: 'error',
|
|
170
|
+
error: `Sandbox error: ${error.message || 'Unknown error'}`,
|
|
171
|
+
});
|
|
172
|
+
return true; // Handled (with error)
|
|
173
|
+
}
|
|
174
|
+
}
|