upfynai-code 3.0.4 → 3.1.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.
Files changed (246) hide show
  1. package/README.md +66 -91
  2. package/bin/cli.js +191 -0
  3. package/{client/dist/assets/AppContent-CwrTP6TW.js → dist/client/assets/AppContent-BofJquUs.js} +4 -4
  4. package/{client/dist/assets/BrowserPanel-0TLEl-IC.js → dist/client/assets/BrowserPanel-CSvD4jOX.js} +2 -2
  5. package/dist/client/assets/CanvasFullScreen-onRfarpc.js +1 -0
  6. package/dist/client/assets/CanvasWorkspace-DvGKdL-k.js +259 -0
  7. package/dist/client/assets/DashboardPanel-DqAHbXDO.js +1 -0
  8. package/dist/client/assets/FileTree-BE0h-9M9.js +1 -0
  9. package/{client/dist/assets/GitPanel-C_xFM-N2.js → dist/client/assets/GitPanel-DdeJ0bp5.js} +2 -2
  10. package/{client/dist/assets/LoginModal-CImJHRjX.js → dist/client/assets/LoginModal-BP0pCTrH.js} +3 -3
  11. package/dist/client/assets/MermaidBlock-D0rfEhrT.js +2 -0
  12. package/dist/client/assets/Onboarding-B2zQy-_6.js +1 -0
  13. package/dist/client/assets/SetupForm-Be7-WBe-.js +1 -0
  14. package/dist/client/assets/WorkflowsPanel-CusLbVJ6.js +1 -0
  15. package/{client/dist/assets/index-HaY-3pK1.js → dist/client/assets/index-BQy15irW.js} +24 -24
  16. package/dist/client/assets/index-CS0fDqEC.js +1 -0
  17. package/dist/client/assets/index-DYLSCCCp.css +1 -0
  18. package/dist/client/assets/vendor-canvas-QWTduIvM.js +23 -0
  19. package/{client/dist/assets/vendor-icons-GyYE35HP.js → dist/client/assets/vendor-icons-kix3Gb31.js} +1 -1
  20. package/{client/dist/assets/vendor-mermaid-DucWyDEe.js → dist/client/assets/vendor-mermaid-CS3J4_Bz.js} +329 -326
  21. package/dist/client/favicon.png +0 -0
  22. package/dist/client/favicon.svg +15 -0
  23. package/{client/dist → dist/client}/index.html +3 -3
  24. package/{client/dist → dist/client}/manifest.json +12 -12
  25. package/package.json +55 -104
  26. package/scripts/postinstall.js +9 -0
  27. package/scripts/prepublish.js +77 -0
  28. package/src/animation.js +228 -0
  29. package/src/auth.js +142 -0
  30. package/src/config.js +40 -0
  31. package/src/connect.js +416 -0
  32. package/src/launch.js +81 -0
  33. package/src/mcp.js +57 -0
  34. package/src/permissions.js +140 -0
  35. package/src/persistent-shell.js +261 -0
  36. package/src/server.js +54 -0
  37. package/client/dist/assets/CanvasFullScreen-D1GWQsGL.js +0 -1
  38. package/client/dist/assets/CanvasWorkspace-D7ORj358.js +0 -163
  39. package/client/dist/assets/DashboardPanel-BV7ybUDe.js +0 -1
  40. package/client/dist/assets/FileTree-5qfhBqdE.js +0 -1
  41. package/client/dist/assets/MermaidBlock-BFM21cwe.js +0 -2
  42. package/client/dist/assets/Onboarding-B3cteLu2.js +0 -1
  43. package/client/dist/assets/SetupForm-P6dsYgHO.js +0 -1
  44. package/client/dist/assets/WorkflowsPanel-CBoN80kc.js +0 -1
  45. package/client/dist/assets/index-46kkVu2i.css +0 -1
  46. package/client/dist/assets/vendor-canvas-DvHJ_Pn2.js +0 -49
  47. package/client/dist/favicon.png +0 -0
  48. package/client/dist/favicon.svg +0 -5
  49. package/commands/upfynai-connect.md +0 -59
  50. package/commands/upfynai-disconnect.md +0 -31
  51. package/commands/upfynai-doctor.md +0 -99
  52. package/commands/upfynai-export.md +0 -49
  53. package/commands/upfynai-local.md +0 -82
  54. package/commands/upfynai-status.md +0 -75
  55. package/commands/upfynai-stop.md +0 -49
  56. package/commands/upfynai-uninstall.md +0 -58
  57. package/commands/upfynai.md +0 -69
  58. package/scripts/build-client.js +0 -17
  59. package/scripts/fix-node-pty.js +0 -67
  60. package/scripts/install-commands.js +0 -78
  61. package/server/agent-loop.js +0 -242
  62. package/server/auto-compact.js +0 -99
  63. package/server/browser.js +0 -131
  64. package/server/claude-sdk.js +0 -797
  65. package/server/cli-ui.js +0 -798
  66. package/server/cli.js +0 -751
  67. package/server/constants/config.js +0 -31
  68. package/server/cursor-cli.js +0 -270
  69. package/server/database/auth.db +0 -0
  70. package/server/database/db.js +0 -1547
  71. package/server/database/init.sql +0 -70
  72. package/server/index.js +0 -3813
  73. package/server/load-env.js +0 -26
  74. package/server/mcp-server.js +0 -621
  75. package/server/middleware/auth.js +0 -184
  76. package/server/middleware/relayHelpers.js +0 -44
  77. package/server/middleware/sandboxRouter.js +0 -174
  78. package/server/openai-codex.js +0 -403
  79. package/server/openrouter.js +0 -137
  80. package/server/projects.js +0 -1807
  81. package/server/provider-factory.js +0 -174
  82. package/server/relay-client.js +0 -390
  83. package/server/routes/agent.js +0 -1234
  84. package/server/routes/auth.js +0 -559
  85. package/server/routes/browser.js +0 -419
  86. package/server/routes/canvas.js +0 -53
  87. package/server/routes/cli-auth.js +0 -263
  88. package/server/routes/codex.js +0 -396
  89. package/server/routes/commands.js +0 -707
  90. package/server/routes/composio.js +0 -176
  91. package/server/routes/cursor.js +0 -770
  92. package/server/routes/dashboard.js +0 -295
  93. package/server/routes/git.js +0 -1208
  94. package/server/routes/keys.js +0 -34
  95. package/server/routes/mcp-utils.js +0 -48
  96. package/server/routes/mcp.js +0 -661
  97. package/server/routes/payments.js +0 -227
  98. package/server/routes/projects.js +0 -754
  99. package/server/routes/sessions.js +0 -146
  100. package/server/routes/settings.js +0 -261
  101. package/server/routes/taskmaster.js +0 -1928
  102. package/server/routes/user.js +0 -106
  103. package/server/routes/vapi-chat.js +0 -624
  104. package/server/routes/voice.js +0 -235
  105. package/server/routes/webhooks.js +0 -166
  106. package/server/routes/workflows.js +0 -312
  107. package/server/sandbox.js +0 -120
  108. package/server/services/browser-ai.js +0 -154
  109. package/server/services/composio.js +0 -204
  110. package/server/services/sessionRegistry.js +0 -139
  111. package/server/services/whisperService.js +0 -84
  112. package/server/services/workflowScheduler.js +0 -211
  113. package/server/tests/relay-flow.test.js +0 -570
  114. package/server/tests/sessions.test.js +0 -259
  115. package/server/utils/commandParser.js +0 -303
  116. package/server/utils/email.js +0 -66
  117. package/server/utils/gitConfig.js +0 -24
  118. package/server/utils/mcp-detector.js +0 -198
  119. package/server/utils/taskmaster-websocket.js +0 -129
  120. package/shared/integrationCatalog.d.ts +0 -12
  121. package/shared/integrationCatalog.js +0 -172
  122. package/shared/modelConstants.js +0 -96
  123. /package/{shared → dist}/agents/claude.js +0 -0
  124. /package/{shared → dist}/agents/codex.js +0 -0
  125. /package/{shared → dist}/agents/cursor.js +0 -0
  126. /package/{shared → dist}/agents/detect.js +0 -0
  127. /package/{shared → dist}/agents/exec.js +0 -0
  128. /package/{shared → dist}/agents/files.js +0 -0
  129. /package/{shared → dist}/agents/git.js +0 -0
  130. /package/{shared → dist}/agents/gitagent.js +0 -0
  131. /package/{shared → dist}/agents/index.js +0 -0
  132. /package/{shared → dist}/agents/shell.js +0 -0
  133. /package/{shared → dist}/agents/utils.js +0 -0
  134. /package/{client/dist → dist/client}/api-docs.html +0 -0
  135. /package/{client/dist → dist/client}/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
  136. /package/{client/dist → dist/client}/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
  137. /package/{client/dist → dist/client}/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
  138. /package/{client/dist → dist/client}/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
  139. /package/{client/dist → dist/client}/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
  140. /package/{client/dist → dist/client}/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
  141. /package/{client/dist → dist/client}/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
  142. /package/{client/dist → dist/client}/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
  143. /package/{client/dist → dist/client}/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
  144. /package/{client/dist → dist/client}/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
  145. /package/{client/dist → dist/client}/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
  146. /package/{client/dist → dist/client}/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
  147. /package/{client/dist → dist/client}/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
  148. /package/{client/dist → dist/client}/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
  149. /package/{client/dist → dist/client}/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
  150. /package/{client/dist → dist/client}/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
  151. /package/{client/dist → dist/client}/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
  152. /package/{client/dist → dist/client}/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
  153. /package/{client/dist → dist/client}/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
  154. /package/{client/dist → dist/client}/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
  155. /package/{client/dist → dist/client}/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
  156. /package/{client/dist → dist/client}/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
  157. /package/{client/dist → dist/client}/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
  158. /package/{client/dist → dist/client}/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
  159. /package/{client/dist → dist/client}/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
  160. /package/{client/dist → dist/client}/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
  161. /package/{client/dist → dist/client}/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
  162. /package/{client/dist → dist/client}/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
  163. /package/{client/dist → dist/client}/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
  164. /package/{client/dist → dist/client}/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
  165. /package/{client/dist → dist/client}/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
  166. /package/{client/dist → dist/client}/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
  167. /package/{client/dist → dist/client}/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
  168. /package/{client/dist → dist/client}/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
  169. /package/{client/dist → dist/client}/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
  170. /package/{client/dist → dist/client}/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
  171. /package/{client/dist → dist/client}/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
  172. /package/{client/dist → dist/client}/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
  173. /package/{client/dist → dist/client}/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
  174. /package/{client/dist → dist/client}/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
  175. /package/{client/dist → dist/client}/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
  176. /package/{client/dist → dist/client}/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
  177. /package/{client/dist → dist/client}/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
  178. /package/{client/dist → dist/client}/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
  179. /package/{client/dist → dist/client}/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
  180. /package/{client/dist → dist/client}/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
  181. /package/{client/dist → dist/client}/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
  182. /package/{client/dist → dist/client}/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
  183. /package/{client/dist → dist/client}/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
  184. /package/{client/dist → dist/client}/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
  185. /package/{client/dist → dist/client}/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
  186. /package/{client/dist → dist/client}/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
  187. /package/{client/dist → dist/client}/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
  188. /package/{client/dist → dist/client}/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
  189. /package/{client/dist → dist/client}/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
  190. /package/{client/dist → dist/client}/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
  191. /package/{client/dist → dist/client}/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
  192. /package/{client/dist → dist/client}/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
  193. /package/{client/dist → dist/client}/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
  194. /package/{client/dist → dist/client}/assets/MarkdownPreview-CESjI261.js +0 -0
  195. /package/{client/dist → dist/client}/assets/PreviewPanel-CqCa92Tf.js +0 -0
  196. /package/{client/dist → dist/client}/assets/pdf-CE_K4jFx.js +0 -0
  197. /package/{client/dist → dist/client}/assets/vendor-canvas-BZV40eAE.css +0 -0
  198. /package/{client/dist → dist/client}/assets/vendor-codemirror-D2ALgpaX.js +0 -0
  199. /package/{client/dist → dist/client}/assets/vendor-diff-DNQpbhrT.js +0 -0
  200. /package/{client/dist → dist/client}/assets/vendor-i18n-DCFGyhQR.js +0 -0
  201. /package/{client/dist → dist/client}/assets/vendor-markdown-CimbIo6Y.js +0 -0
  202. /package/{client/dist → dist/client}/assets/vendor-react-96lCPsRK.js +0 -0
  203. /package/{client/dist → dist/client}/assets/vendor-syntax-LS_Nt30I.js +0 -0
  204. /package/{client/dist → dist/client}/assets/vendor-xterm-CZq1hqo1.js +0 -0
  205. /package/{client/dist → dist/client}/assets/vendor-xterm-qxJ8_QYu.css +0 -0
  206. /package/{client/dist → dist/client}/clear-cache.html +0 -0
  207. /package/{client/dist → dist/client}/convert-icons.md +0 -0
  208. /package/{client/dist → dist/client}/generate-icons.js +0 -0
  209. /package/{client/dist → dist/client}/icons/claude-ai-icon.svg +0 -0
  210. /package/{client/dist → dist/client}/icons/codex-white.svg +0 -0
  211. /package/{client/dist → dist/client}/icons/codex.svg +0 -0
  212. /package/{client/dist → dist/client}/icons/cursor-white.svg +0 -0
  213. /package/{client/dist → dist/client}/icons/cursor.svg +0 -0
  214. /package/{client/dist → dist/client}/icons/icon-128x128.png +0 -0
  215. /package/{client/dist → dist/client}/icons/icon-128x128.svg +0 -0
  216. /package/{client/dist → dist/client}/icons/icon-144x144.png +0 -0
  217. /package/{client/dist → dist/client}/icons/icon-144x144.svg +0 -0
  218. /package/{client/dist → dist/client}/icons/icon-152x152.png +0 -0
  219. /package/{client/dist → dist/client}/icons/icon-152x152.svg +0 -0
  220. /package/{client/dist → dist/client}/icons/icon-192x192.png +0 -0
  221. /package/{client/dist → dist/client}/icons/icon-192x192.svg +0 -0
  222. /package/{client/dist → dist/client}/icons/icon-384x384.png +0 -0
  223. /package/{client/dist → dist/client}/icons/icon-384x384.svg +0 -0
  224. /package/{client/dist → dist/client}/icons/icon-512x512.png +0 -0
  225. /package/{client/dist → dist/client}/icons/icon-512x512.svg +0 -0
  226. /package/{client/dist → dist/client}/icons/icon-72x72.png +0 -0
  227. /package/{client/dist → dist/client}/icons/icon-72x72.svg +0 -0
  228. /package/{client/dist → dist/client}/icons/icon-96x96.png +0 -0
  229. /package/{client/dist → dist/client}/icons/icon-96x96.svg +0 -0
  230. /package/{client/dist → dist/client}/icons/icon-template.svg +0 -0
  231. /package/{client/dist → dist/client}/logo-128.png +0 -0
  232. /package/{client/dist → dist/client}/logo-256.png +0 -0
  233. /package/{client/dist → dist/client}/logo-32.png +0 -0
  234. /package/{client/dist → dist/client}/logo-512.png +0 -0
  235. /package/{client/dist → dist/client}/logo-64.png +0 -0
  236. /package/{client/dist → dist/client}/logo.svg +0 -0
  237. /package/{client/dist → dist/client}/mcp-docs.html +0 -0
  238. /package/{client/dist → dist/client}/offline.html +0 -0
  239. /package/{client/dist → dist/client}/screenshots/cli-selection.png +0 -0
  240. /package/{client/dist → dist/client}/screenshots/desktop-main.png +0 -0
  241. /package/{client/dist → dist/client}/screenshots/mobile-chat.png +0 -0
  242. /package/{client/dist → dist/client}/screenshots/tools-modal.png +0 -0
  243. /package/{client/dist → dist/client}/sw.js +0 -0
  244. /package/{shared → dist}/gitagent/index.js +0 -0
  245. /package/{shared → dist}/gitagent/parser.js +0 -0
  246. /package/{shared → dist}/gitagent/prompt-builder.js +0 -0
@@ -1,184 +0,0 @@
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 cookieDomain = process.env.COOKIE_DOMAIN || undefined;
92
- const COOKIE_OPTIONS = {
93
- httpOnly: true,
94
- secure: isSecureEnv,
95
- // 'lax' — Vercel reverse-proxies to Railway (same domain from browser's perspective)
96
- // 'strict' used in local/dev mode where everything is same-origin
97
- sameSite: isSecureEnv ? 'lax' : 'strict',
98
- maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
99
- path: '/',
100
- // Set domain for cross-subdomain cookie sharing (e.g., '.upfyn.com')
101
- ...(isSecureEnv && cookieDomain ? { domain: cookieDomain } : {}),
102
- };
103
-
104
- // Set session cookie on response
105
- const setSessionCookie = (res, token) => {
106
- res.cookie('session', token, COOKIE_OPTIONS);
107
- };
108
-
109
- // Clear session cookie
110
- const clearSessionCookie = (res) => {
111
- res.clearCookie('session', { path: '/' });
112
- };
113
-
114
- // WebSocket authentication (parse cookie from upgrade request headers)
115
- const authenticateWebSocket = async (request) => {
116
- // Platform mode: bypass
117
- if (IS_PLATFORM) {
118
- try {
119
- const user = await userDb.getFirstUser();
120
- return user ? { userId: user.id, username: user.username } : null;
121
- } catch { return null; }
122
- }
123
-
124
- let token = null;
125
-
126
- // 1. Parse cookie from upgrade request
127
- const cookieHeader = request.headers?.cookie || '';
128
- if (cookieHeader) {
129
- const cookies = Object.fromEntries(
130
- cookieHeader.split(';').map(c => {
131
- const [k, ...v] = c.trim().split('=');
132
- return [k, v.join('=')];
133
- })
134
- );
135
- token = cookies.session || null;
136
- }
137
-
138
- // 2. Fallback: query param (legacy)
139
- if (!token) {
140
- try {
141
- const url = new URL(request.url, 'http://localhost');
142
- token = url.searchParams.get('token');
143
- } catch { /* ignore */ }
144
- }
145
-
146
- if (!token) {
147
- return null;
148
- }
149
-
150
- // Relay token (upfyn_ prefix) — validate against DB, not JWT
151
- if (token.startsWith('upfyn_') || token.startsWith('rt_')) {
152
- try {
153
- // Timeout relay token validation to prevent hung WebSocket upgrades when Turso is slow
154
- const tokenData = await Promise.race([
155
- relayTokensDb.validateToken(token),
156
- new Promise((_, reject) => setTimeout(() => reject(new Error('Token validation timeout')), 10000))
157
- ]);
158
- if (tokenData) {
159
- return { userId: Number(tokenData.user_id), username: tokenData.username };
160
- }
161
- } catch (err) {
162
- console.warn('[WS] Relay token validation failed:', err.message);
163
- }
164
- return null;
165
- }
166
-
167
- try {
168
- const decoded = jwt.verify(token, JWT_SECRET);
169
- // Validate against Turso — DB is source of truth
170
- const user = await userDb.getUserById(decoded.userId);
171
- if (!user) return null;
172
- return { userId: user.id, username: user.username };
173
- } catch { return null; }
174
- };
175
-
176
- export {
177
- validateApiKey,
178
- authenticateToken,
179
- generateToken,
180
- authenticateWebSocket,
181
- setSessionCookie,
182
- clearSessionCookie,
183
- JWT_SECRET
184
- };
@@ -1,44 +0,0 @@
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
- }
@@ -1,174 +0,0 @@
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
- }