@myrialabs/clopen 0.4.32 → 0.4.34

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 (102) hide show
  1. package/backend/auth/permissions.ts +3 -0
  2. package/backend/auth/rate-limiter.test.ts +115 -0
  3. package/backend/auth/rate-limiter.ts +25 -12
  4. package/backend/chat/stream-manager.ts +4 -1
  5. package/backend/database/migrations/069_create_ssh_client_tables.ts +116 -0
  6. package/backend/database/migrations/index.ts +7 -0
  7. package/backend/database/queries/db-client-connection-queries.ts +7 -2
  8. package/backend/database/queries/file-audit-log-queries.ts +1 -0
  9. package/backend/database/queries/index.ts +3 -0
  10. package/backend/database/queries/ssh-connection-queries.ts +244 -0
  11. package/backend/database/queries/ssh-known-host-queries.ts +67 -0
  12. package/backend/database/queries/ssh-port-forward-queries.ts +113 -0
  13. package/backend/db-client/connection-manager.ts +21 -1
  14. package/backend/engine/adapters/codex/message-converter.ts +18 -12
  15. package/backend/engine/adapters/codex/patch-rollout.test.ts +88 -0
  16. package/backend/engine/adapters/codex/patch-rollout.ts +120 -109
  17. package/backend/engine/adapters/codex/stream.ts +33 -4
  18. package/backend/engine/adapters/copilot/stream.ts +1 -1
  19. package/backend/engine/adapters/cursor/stream.ts +1 -1
  20. package/backend/engine/adapters/qwen/stream.ts +1 -1
  21. package/backend/engine/docs/adapter-contract.md +7 -0
  22. package/backend/files/file-watcher.ts +283 -51
  23. package/backend/files/git-events.test.ts +110 -0
  24. package/backend/git/git-dirs.test.ts +132 -0
  25. package/backend/git/git-dirs.ts +0 -0
  26. package/backend/git/nested-repos.test.ts +108 -5
  27. package/backend/git/nested-repos.ts +103 -18
  28. package/backend/http/audio.ts +5 -32
  29. package/backend/http/bearer-auth.ts +47 -0
  30. package/backend/http/files-download.test.ts +146 -0
  31. package/backend/http/files-download.ts +109 -0
  32. package/backend/http/files-upload.ts +3 -29
  33. package/backend/http/ssh-sftp.ts +133 -0
  34. package/backend/index.ts +25 -2
  35. package/backend/mcp/README.md +34 -6
  36. package/backend/mcp/index.ts +8 -8
  37. package/backend/mcp/internal/config.ts +69 -19
  38. package/backend/mcp/internal/project-context.test.ts +40 -0
  39. package/backend/mcp/internal/project-context.ts +80 -7
  40. package/backend/mcp/internal/remote-server.test.ts +17 -2
  41. package/backend/mcp/internal/remote-server.ts +36 -3
  42. package/backend/mcp/internal/servers/helper.ts +48 -18
  43. package/backend/middleware/security.test.ts +95 -0
  44. package/backend/middleware/security.ts +114 -0
  45. package/backend/ports/attribute.test.ts +231 -0
  46. package/backend/ports/attribute.ts +444 -0
  47. package/backend/ports/kill.ts +203 -0
  48. package/backend/ports/monitor.ts +365 -0
  49. package/backend/ports/processes.test.ts +98 -0
  50. package/backend/ports/processes.ts +374 -0
  51. package/backend/ports/registry.ts +165 -0
  52. package/backend/ports/runner.ts +195 -0
  53. package/backend/ports/scan-host.ts +260 -0
  54. package/backend/ports/scan.test.ts +341 -0
  55. package/backend/ports/scan.ts +687 -0
  56. package/backend/ports/ssh-lineage.ts +318 -0
  57. package/backend/preview/browser/browser-video-capture.ts +51 -4
  58. package/backend/ssh/client-pool.ts +196 -0
  59. package/backend/ssh/connect.ts +375 -0
  60. package/backend/ssh/disk-usage.test.ts +134 -0
  61. package/backend/ssh/disk-usage.ts +214 -0
  62. package/backend/ssh/forwards.ts +340 -0
  63. package/backend/ssh/health.ts +141 -0
  64. package/backend/ssh/known-hosts.ts +96 -0
  65. package/backend/ssh/pty-backend.ts +215 -0
  66. package/backend/ssh/ptykit.ts +66 -0
  67. package/backend/ssh/sftp.test.ts +242 -0
  68. package/backend/ssh/sftp.ts +967 -0
  69. package/backend/ssh/socks5.test.ts +167 -0
  70. package/backend/ssh/socks5.ts +164 -0
  71. package/backend/ssh/tunnel.ts +87 -0
  72. package/backend/ws/auth/device.ts +1 -1
  73. package/backend/ws/auth/login.ts +8 -4
  74. package/backend/ws/db-client/connections.ts +1 -0
  75. package/backend/ws/files/watch.ts +5 -1
  76. package/backend/ws/index.ts +6 -0
  77. package/backend/ws/ports/index.ts +19 -0
  78. package/backend/ws/ports/kill.ts +80 -0
  79. package/backend/ws/ports/list.ts +60 -0
  80. package/backend/ws/ssh/access.ts +31 -0
  81. package/backend/ws/ssh/connections.ts +224 -0
  82. package/backend/ws/ssh/forwards.ts +109 -0
  83. package/backend/ws/ssh/index.ts +18 -0
  84. package/backend/ws/ssh/sftp.ts +259 -0
  85. package/backend/ws/ssh/tunnel.ts +93 -0
  86. package/dist/assets/{index-hu17G6tX.js → index-gGTMFUa8.js} +1 -1
  87. package/dist/assets/{index-DSvGoxp2.js → index-vW2RpdHu.js} +1 -1
  88. package/dist/assets/main-CYCMEbXB.css +1 -0
  89. package/dist/assets/{main-BccclZsC.js → main-ZCojSzvW.js} +506 -477
  90. package/dist/index.html +2 -2
  91. package/package.json +1 -1
  92. package/shared/types/database/schema.ts +49 -0
  93. package/shared/types/db-client/connection.ts +6 -0
  94. package/shared/types/ports/index.ts +5 -0
  95. package/shared/types/ports/port.ts +192 -0
  96. package/shared/types/ssh/connection.ts +80 -0
  97. package/shared/types/ssh/forward.ts +46 -0
  98. package/shared/types/ssh/index.ts +8 -0
  99. package/shared/types/ssh/known-host.ts +19 -0
  100. package/shared/types/ssh/sftp.ts +141 -0
  101. package/shared/utils/logger.ts +2 -0
  102. package/dist/assets/main-Ciuw0wF0.css +0 -1
@@ -0,0 +1,114 @@
1
+ import type { Elysia } from 'elysia';
2
+
3
+ /**
4
+ * Security Headers Middleware
5
+ *
6
+ * Adds the response headers a browser needs to constrain what a Clopen page is
7
+ * allowed to do. Each directive below is set to what the app actually uses —
8
+ * see the notes on the loose ones, which are loose for a reason rather than by
9
+ * oversight.
10
+ */
11
+
12
+ /**
13
+ * `@monaco-editor/loader` fetches the editor at runtime from jsDelivr; nothing
14
+ * calls `loader.config()` to point it at a local copy, so the code editor, diff
15
+ * viewer, conflict resolver and SQL console all load their scripts, stylesheet
16
+ * and codicon font cross-origin. Monaco then boots its language workers from a
17
+ * `blob:` that `importScripts()` the same host, which is why `blob:` appears in
18
+ * `script-src` and `worker-src` too. Bundling Monaco locally would let every
19
+ * one of these entries go.
20
+ */
21
+ const MONACO_CDN = 'https://cdn.jsdelivr.net';
22
+
23
+ const CONTENT_SECURITY_POLICY = [
24
+ "default-src 'self'",
25
+
26
+ // No <base> tag and no cross-origin form target in the app, so these cost
27
+ // nothing and close two common ways an injected tag redirects a page.
28
+ "base-uri 'self'",
29
+ "form-action 'self'",
30
+ "object-src 'none'",
31
+
32
+ // Clopen is never meant to be embedded. Paired with X-Frame-Options below
33
+ // for browsers that predate frame-ancestors.
34
+ "frame-ancestors 'none'",
35
+
36
+ // 'unsafe-inline' and 'unsafe-eval' are what Monaco's AMD loader needs. They
37
+ // blunt the XSS value of script-src considerably — the origin allowlist is
38
+ // what this directive still buys us.
39
+ `script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: ${MONACO_CDN}`,
40
+ `style-src 'self' 'unsafe-inline' ${MONACO_CDN}`,
41
+ `font-src 'self' data: ${MONACO_CDN}`,
42
+
43
+ // Markdown renders whatever image URL the author wrote, including remote
44
+ // ones from agent output and project READMEs. Narrowing this would silently
45
+ // break those, so images stay open and the tighter directives above carry
46
+ // the weight.
47
+ "img-src 'self' data: blob: https: http:",
48
+
49
+ // Media previews, notification sounds and the PDF viewer all read from
50
+ // object URLs built in the browser.
51
+ "media-src 'self' blob:",
52
+ "frame-src 'self' blob:",
53
+ "worker-src 'self' blob:",
54
+ "child-src 'self' blob:",
55
+
56
+ // The WebSocket is always same-origin (`frontend/utils/ws.ts` builds it from
57
+ // window.location), which 'self' covers under CSP3.
58
+ `connect-src 'self' blob: data: ${MONACO_CDN}`,
59
+
60
+ "manifest-src 'self'"
61
+ ].join('; ');
62
+
63
+ /**
64
+ * Browser features are granted to the app itself, not to third parties. The
65
+ * Preview Browser relays a page's device requests to the viewer's own browser
66
+ * (`frontend/services/preview/browser/host-bridge.service.ts`), so camera,
67
+ * microphone, geolocation, clipboard and screen capture have to stay
68
+ * available; everything Clopen never touches is denied outright.
69
+ */
70
+ const PERMISSIONS_POLICY = [
71
+ 'camera=(self)',
72
+ 'microphone=(self)',
73
+ 'geolocation=(self)',
74
+ 'display-capture=(self)',
75
+ 'clipboard-read=(self)',
76
+ 'clipboard-write=(self)',
77
+ 'fullscreen=(self)',
78
+ 'accelerometer=()',
79
+ 'ambient-light-sensor=()',
80
+ 'bluetooth=()',
81
+ 'gyroscope=()',
82
+ 'hid=()',
83
+ 'idle-detection=()',
84
+ 'local-fonts=()',
85
+ 'magnetometer=()',
86
+ 'midi=()',
87
+ 'payment=()',
88
+ 'serial=()',
89
+ 'usb=()',
90
+ 'xr-spatial-tracking=()'
91
+ ].join(', ');
92
+
93
+ export const SECURITY_HEADERS: Record<string, string> = {
94
+ 'Content-Security-Policy': CONTENT_SECURITY_POLICY,
95
+
96
+ // Every asset the static handler serves gets an explicit Content-Type from
97
+ // Bun.file().type, so refusing to sniff can't strand one of them.
98
+ 'X-Content-Type-Options': 'nosniff',
99
+
100
+ // frame-ancestors already covers this on current browsers; kept for older
101
+ // ones that only understand the legacy header.
102
+ 'X-Frame-Options': 'DENY',
103
+
104
+ 'Referrer-Policy': 'strict-origin-when-cross-origin',
105
+ 'Permissions-Policy': PERMISSIONS_POLICY
106
+ };
107
+
108
+ export function securityMiddleware(app: Elysia) {
109
+ return app.onAfterHandle(({ set }) => {
110
+ for (const [name, value] of Object.entries(SECURITY_HEADERS)) {
111
+ set.headers[name] = value;
112
+ }
113
+ });
114
+ }
@@ -0,0 +1,231 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import type { PortProcess, PortSocket } from '$shared/types/ports';
3
+ import { findClopenPids, groupListenerPids, traceAncestry, type SessionPid } from './attribute';
4
+ import { findConnectionSshd, parseEnvironProbe, sameConnection } from './ssh-lineage';
5
+
6
+ function proc(pid: number, parentPid: number | null, command: string): PortProcess {
7
+ return { pid, parentPid, user: 'deploy', command, startedAt: null, cwd: null };
8
+ }
9
+
10
+ function table(...processes: PortProcess[]): Map<number, PortProcess> {
11
+ return new Map(processes.map((process) => [process.pid, process]));
12
+ }
13
+
14
+ describe('groupListenerPids', () => {
15
+ test('collapses workers onto a master that listens alongside them', () => {
16
+ const processes = table(
17
+ proc(100, 1, 'node server.js'),
18
+ proc(101, 100, 'node server.js'),
19
+ proc(102, 100, 'node server.js')
20
+ );
21
+
22
+ const groups = groupListenerPids([100, 101, 102], processes);
23
+
24
+ expect(groups).toHaveLength(1);
25
+ expect(groups[0]).toEqual({ owner: 100, members: [100, 101, 102] });
26
+ });
27
+
28
+ test('collapses workers onto a shared parent that holds no socket itself', () => {
29
+ // nginx on macOS: the master binds nothing, every worker inherits it.
30
+ const processes = table(
31
+ proc(500, 1, 'nginx: master process /usr/sbin/nginx'),
32
+ proc(501, 500, 'nginx: worker process'),
33
+ proc(502, 500, 'nginx: worker process'),
34
+ proc(503, 500, 'nginx: worker process')
35
+ );
36
+
37
+ const groups = groupListenerPids([501, 502, 503], processes);
38
+
39
+ expect(groups).toHaveLength(1);
40
+ expect(groups[0]).toEqual({ owner: 500, members: [501, 502, 503] });
41
+ });
42
+
43
+ test('keeps unrelated programs apart even when they share a parent shell', () => {
44
+ // Two different servers started from one shell, both bound to the same
45
+ // port number on different addresses. Merging them would invent a
46
+ // relationship that does not exist.
47
+ const processes = table(
48
+ proc(700, 1, '-bash'),
49
+ proc(701, 700, 'node vite.js'),
50
+ proc(702, 700, 'python -m http.server')
51
+ );
52
+
53
+ const groups = groupListenerPids([701, 702], processes);
54
+
55
+ expect(groups).toHaveLength(2);
56
+ expect(groups.map((group) => group.owner).sort()).toEqual([701, 702]);
57
+ });
58
+
59
+ test('keeps a lone listener as its own group', () => {
60
+ const processes = table(proc(900, 1, 'redis-server'));
61
+ expect(groupListenerPids([900], processes)).toEqual([{ owner: 900, members: [900] }]);
62
+ });
63
+
64
+ test('does not fold onto init when the parent chain is unknown', () => {
65
+ // Both parents point at pid 1, which is every daemon's parent — folding
66
+ // there would merge the whole machine into one row.
67
+ const processes = table(proc(310, 1, 'postgres'), proc(320, 1, 'mysqld'));
68
+ expect(groupListenerPids([310, 320], processes)).toHaveLength(2);
69
+ });
70
+
71
+ test('survives a parent chain that loops', () => {
72
+ const processes = table(proc(10, 11, 'a'), proc(11, 10, 'b'));
73
+ expect(() => groupListenerPids([10, 11], processes)).not.toThrow();
74
+ });
75
+ });
76
+
77
+ describe('traceAncestry', () => {
78
+ const sessions = new Map<number, SessionPid>([
79
+ [400, { pid: 400, sessionId: 'sess-1', projectId: 'proj-1', projectName: 'shop', cwd: '/srv/shop' }]
80
+ ]);
81
+
82
+ test('finds the terminal session a process descends from', () => {
83
+ const processes = table(
84
+ proc(400, 1, '/bin/zsh'),
85
+ proc(401, 400, 'bun run dev'),
86
+ proc(402, 401, 'node vite.js')
87
+ );
88
+
89
+ const ancestry = traceAncestry(402, processes, sessions);
90
+
91
+ expect(ancestry.session?.sessionId).toBe('sess-1');
92
+ expect(ancestry.chain.map((process) => process.pid)).toEqual([402, 401, 400]);
93
+ });
94
+
95
+ test('recognises the sshd session Clopen itself is using', () => {
96
+ // The remote counterpart of a PtyKit session pid: same tier, same
97
+ // certainty, resolved from the host's end instead of ours.
98
+ const processes = table(
99
+ proc(600, 1, 'sshd: deploy@pts/0'),
100
+ proc(601, 600, '-bash'),
101
+ proc(602, 601, 'node vite.js')
102
+ );
103
+
104
+ const ancestry = traceAncestry(602, processes, new Map(), new Set([600]));
105
+
106
+ expect(ancestry.fromClopen).toBe(true);
107
+ });
108
+
109
+ test('does not claim someone else’s SSH session as Clopen’s', () => {
110
+ const processes = table(
111
+ proc(650, 1, 'sshd: other@pts/3'),
112
+ proc(651, 650, 'node server.js')
113
+ );
114
+
115
+ const ancestry = traceAncestry(651, processes, new Map(), new Set([600]));
116
+
117
+ expect(ancestry.fromClopen).toBe(false);
118
+ expect(ancestry.sshdUser).toBe('other');
119
+ });
120
+
121
+ test('reports the sshd session user when the lineage ends at sshd', () => {
122
+ const processes = table(
123
+ proc(600, 1, 'sshd: deploy@pts/0'),
124
+ proc(601, 600, '-bash'),
125
+ proc(602, 601, 'node server.js')
126
+ );
127
+
128
+ const ancestry = traceAncestry(602, processes, new Map());
129
+
130
+ expect(ancestry.sshdUser).toBe('deploy');
131
+ expect(ancestry.session).toBeNull();
132
+ });
133
+
134
+ test('stops cleanly when an ancestor is missing from the table', () => {
135
+ const processes = table(proc(800, 799, 'node server.js'));
136
+ const ancestry = traceAncestry(800, processes, new Map());
137
+ expect(ancestry.chain.map((process) => process.pid)).toEqual([800]);
138
+ expect(ancestry.session).toBeNull();
139
+ });
140
+
141
+ test('survives a parent chain that loops', () => {
142
+ const processes = table(proc(20, 21, 'a'), proc(21, 20, 'b'));
143
+ expect(() => traceAncestry(20, processes, new Map())).not.toThrow();
144
+ });
145
+ });
146
+
147
+ describe('remote connection identity', () => {
148
+ const ours = '103.0.113.7 51234 10.0.0.5 64000';
149
+
150
+ test('matches a process started on the same SSH connection', () => {
151
+ expect(sameConnection('103.0.113.7 51234 10.0.0.5 64000', ours)).toBe(true);
152
+ });
153
+
154
+ test('rejects another session from the same client machine', () => {
155
+ // Same address, different source port — a separate connection, and the
156
+ // port is what makes a connection unique while it is open.
157
+ expect(sameConnection('103.0.113.7 51999 10.0.0.5 64000', ours)).toBe(false);
158
+ });
159
+
160
+ test('rejects a session from somewhere else entirely', () => {
161
+ expect(sameConnection('198.51.100.4 51234 10.0.0.5 64000', ours)).toBe(false);
162
+ });
163
+
164
+ test('rejects an empty or unset value rather than matching loosely', () => {
165
+ expect(sameConnection('', ours)).toBe(false);
166
+ expect(sameConnection('$SSH_CONNECTION', ours)).toBe(false);
167
+ });
168
+
169
+ test('a process with our connection is claimed through the tier-2 path', () => {
170
+ // The environment is inherited, so the listening process carries it
171
+ // directly — no walk to a session leader is needed, which is what makes
172
+ // this survive a disowned or reparented server.
173
+ const processes = table(proc(42802, 42800, 'node app.js'));
174
+ const ancestry = traceAncestry(42802, processes, new Map(), new Set([42802]));
175
+ expect(ancestry.fromClopen).toBe(true);
176
+ });
177
+ });
178
+
179
+ describe('parseEnvironProbe', () => {
180
+ // One line per pid: the marker, then the matching environment entry if the
181
+ // process had one and we were allowed to read it.
182
+ const output = [
183
+ '@42802 SSH_CONNECTION=103.0.113.7 51234 10.0.0.5 64000',
184
+ '@1201 ',
185
+ '@1310 SSH_CONNECTION=198.51.100.4 40001 10.0.0.5 64000',
186
+ ''
187
+ ].join('\n');
188
+
189
+ const values = parseEnvironProbe(output);
190
+
191
+ test('reads the value for each process that had one', () => {
192
+ expect(values.get(42802)).toBe('103.0.113.7 51234 10.0.0.5 64000');
193
+ expect(values.get(1310)).toBe('198.51.100.4 40001 10.0.0.5 64000');
194
+ });
195
+
196
+ test('records nothing for a process whose environment was unreadable', () => {
197
+ // Unreadable is not the same as "not ours" — it simply has no answer.
198
+ expect(values.has(1201)).toBe(false);
199
+ });
200
+ });
201
+
202
+ describe('findConnectionSshd', () => {
203
+ // The macOS path: that host reports no process environment at all, so the
204
+ // connection has to be identified through the tree instead.
205
+ const processes = table(
206
+ proc(700, 1, '/usr/sbin/sshd -D'),
207
+ proc(800, 700, 'sshd: deploy [priv]'),
208
+ proc(815, 800, 'sshd: deploy@ttys000'),
209
+ proc(820, 800, 'sshd: deploy@notty')
210
+ );
211
+
212
+ test('climbs to the sshd every channel of the connection shares', () => {
213
+ // Resolved from the probe's own channel; the shell channel hangs off the
214
+ // same session leader, which is what makes the whole tree ours.
215
+ expect(findConnectionSshd(820, processes)).toBe(800);
216
+ });
217
+
218
+ test('stops before the listening daemon, which parents everyone’s sessions', () => {
219
+ expect(findConnectionSshd(815, processes)).not.toBe(700);
220
+ });
221
+
222
+ test('returns null when the chain never looked like an SSH session', () => {
223
+ const unlabelled = table(proc(900, 1, '/usr/sbin/sshd'), proc(910, 900, 'bash'));
224
+ expect(findConnectionSshd(910, unlabelled)).toBeNull();
225
+ });
226
+
227
+ test('survives a parent chain that loops', () => {
228
+ const looped = table(proc(30, 31, 'sshd: a@ttys000'), proc(31, 30, 'sshd: a [priv]'));
229
+ expect(() => findConnectionSshd(30, looped)).not.toThrow();
230
+ });
231
+ });