@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,341 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import type { CommandRunner } from './runner';
3
+ import {
4
+ parseLsof,
5
+ parseNetstatUnix,
6
+ parseNetstatWindows,
7
+ parseProcFdInodes,
8
+ parseProcNet,
9
+ parseSs,
10
+ scanSockets
11
+ } from './scan';
12
+
13
+ /**
14
+ * Fixtures are shaped like real probe output but describe an invented machine,
15
+ * so nothing about a contributor's own box ends up in the repository.
16
+ */
17
+
18
+ describe('parseLsof', () => {
19
+ // Field blocks as `lsof -FpcLfPntT` emits them: a process block, then one
20
+ // block per socket, with process fields carrying forward.
21
+ const output = [
22
+ 'p1200',
23
+ 'cnginx',
24
+ 'Lwww',
25
+ 'f6',
26
+ 'tIPv4',
27
+ 'PTCP',
28
+ 'n*:8080',
29
+ 'TST=LISTEN',
30
+ 'TQR=0',
31
+ 'f7',
32
+ 'tIPv6',
33
+ 'PTCP',
34
+ 'n[::1]:8080',
35
+ 'TST=LISTEN',
36
+ 'p1310',
37
+ 'cnode',
38
+ 'Ldeploy',
39
+ 'f22',
40
+ 'tIPv4',
41
+ 'PTCP',
42
+ 'n127.0.0.1:5173',
43
+ 'TST=LISTEN',
44
+ 'f23',
45
+ 'tIPv4',
46
+ 'PTCP',
47
+ 'n127.0.0.1:5173->127.0.0.1:54120',
48
+ 'TST=ESTABLISHED',
49
+ 'f24',
50
+ 'tIPv4',
51
+ 'PUDP',
52
+ 'n*:5353',
53
+ ''
54
+ ].join('\n');
55
+
56
+ const sockets = parseLsof(output);
57
+
58
+ test('reads every socket across process blocks', () => {
59
+ expect(sockets).toHaveLength(5);
60
+ });
61
+
62
+ test('carries process identity forward across a process’s sockets', () => {
63
+ expect(sockets[0]).toMatchObject({ pid: 1200, processName: 'nginx', user: 'www' });
64
+ expect(sockets[1]).toMatchObject({ pid: 1200, port: 8080, ipVersion: 'v6', address: '::1' });
65
+ expect(sockets[2]).toMatchObject({ pid: 1310, processName: 'node', user: 'deploy' });
66
+ });
67
+
68
+ test('splits an established socket into local and peer', () => {
69
+ expect(sockets[3]).toMatchObject({
70
+ state: 'established',
71
+ port: 5173,
72
+ peerAddress: '127.0.0.1',
73
+ peerPort: 54120
74
+ });
75
+ });
76
+
77
+ test('treats a bound UDP socket with no peer as listening', () => {
78
+ expect(sockets[4]).toMatchObject({ protocol: 'udp', port: 5353, state: 'listen', address: '*' });
79
+ });
80
+ });
81
+
82
+ describe('parseSs', () => {
83
+ const output = [
84
+ 'Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process',
85
+ 'tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=800,fd=3))',
86
+ 'tcp LISTEN 0 511 [::]:443 [::]:* users:(("caddy",pid=910,fd=8),("caddy",pid=911,fd=8))',
87
+ 'tcp ESTAB 0 0 10.0.0.5:22 10.0.0.9:51234 users:(("sshd",pid=1422,fd=4))',
88
+ 'udp UNCONN 0 0 0.0.0.0:68 0.0.0.0:*',
89
+ ''
90
+ ].join('\n');
91
+
92
+ const sockets = parseSs(output);
93
+
94
+ test('skips the header and keeps every socket row', () => {
95
+ expect(sockets).toHaveLength(4);
96
+ });
97
+
98
+ test('pulls the owning pid out of the users:(…) field', () => {
99
+ expect(sockets[0]).toMatchObject({ port: 22, state: 'listen', pid: 800, processName: 'sshd' });
100
+ expect(sockets[1]).toMatchObject({ port: 443, pid: 910, processName: 'caddy' });
101
+ });
102
+
103
+ test('normalises a wildcard bind and reads the IP family from the address', () => {
104
+ expect(sockets[0].address).toBe('*');
105
+ expect(sockets[1]).toMatchObject({ address: '*', ipVersion: 'v4' });
106
+ });
107
+
108
+ test('records the peer of an established socket', () => {
109
+ expect(sockets[2]).toMatchObject({ state: 'established', peerAddress: '10.0.0.9', peerPort: 51234 });
110
+ });
111
+
112
+ test('leaves the owner null when the kernel withheld it', () => {
113
+ expect(sockets[3]).toMatchObject({ protocol: 'udp', port: 68, pid: null, state: 'listen' });
114
+ });
115
+ });
116
+
117
+ describe('parseNetstatUnix', () => {
118
+ const output = [
119
+ 'Active Internet connections (servers and established)',
120
+ 'Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name',
121
+ 'tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 800/sshd',
122
+ 'tcp6 0 0 :::443 :::* LISTEN -',
123
+ 'tcp 0 0 10.0.0.5:22 10.0.0.9:5123 ESTABLISHED 1422/sshd: deploy',
124
+ 'udp 0 0 0.0.0.0:68 0.0.0.0:* 640/dhclient',
125
+ ''
126
+ ].join('\n');
127
+
128
+ const sockets = parseNetstatUnix(output);
129
+
130
+ test('keeps only socket rows', () => {
131
+ expect(sockets).toHaveLength(4);
132
+ });
133
+
134
+ test('reads the family from the proto column rather than guessing', () => {
135
+ // A wildcard IPv6 bind prints as `:::443`, which looks like no address at
136
+ // all once normalised — only `tcp6` says which family it is.
137
+ expect(sockets[1]).toMatchObject({ port: 443, ipVersion: 'v6', address: '*' });
138
+ });
139
+
140
+ test('treats a withheld owner as unknown rather than pid 0', () => {
141
+ expect(sockets[1].pid).toBeNull();
142
+ });
143
+
144
+ test('takes the owner from the right column for UDP, which has no state', () => {
145
+ expect(sockets[3]).toMatchObject({ protocol: 'udp', port: 68, pid: 640, processName: 'dhclient' });
146
+ });
147
+
148
+ test('splits pid from program name', () => {
149
+ expect(sockets[0]).toMatchObject({ pid: 800, processName: 'sshd' });
150
+ });
151
+ });
152
+
153
+ describe('parseNetstatWindows', () => {
154
+ const output = [
155
+ 'Active Connections',
156
+ '',
157
+ ' Proto Local Address Foreign Address State PID',
158
+ ' TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1044',
159
+ ' TCP [::]:445 [::]:0 LISTENING 4',
160
+ ' TCP 10.0.0.5:52200 93.184.216.34:443 ESTABLISHED 7320',
161
+ ' UDP 0.0.0.0:5353 *:* 3180',
162
+ ''
163
+ ].join('\n');
164
+
165
+ const sockets = parseNetstatWindows(output);
166
+
167
+ test('keeps only socket rows', () => {
168
+ expect(sockets).toHaveLength(4);
169
+ });
170
+
171
+ test('reads the pid from the state-shifted column per protocol', () => {
172
+ expect(sockets[0]).toMatchObject({ port: 135, state: 'listen', pid: 1044 });
173
+ // UDP rows have no state column, so the pid sits one place to the left.
174
+ expect(sockets[3]).toMatchObject({ protocol: 'udp', port: 5353, pid: 3180, state: 'listen' });
175
+ });
176
+
177
+ test('unwraps a bracketed IPv6 address', () => {
178
+ expect(sockets[1]).toMatchObject({ address: '*', port: 445, ipVersion: 'v4' });
179
+ });
180
+
181
+ test('records the peer of an established socket', () => {
182
+ expect(sockets[2]).toMatchObject({
183
+ state: 'established',
184
+ peerAddress: '93.184.216.34',
185
+ peerPort: 443
186
+ });
187
+ });
188
+
189
+ test('never names a process, leaving that to the process table', () => {
190
+ expect(sockets.every((socket) => socket.processName === null)).toBe(true);
191
+ });
192
+ });
193
+
194
+ describe('parseProcNet', () => {
195
+ // What `grep '' /proc/net/tcp /proc/net/tcp6 …` prints: each line prefixed
196
+ // with the file it came from, which is what names the protocol and family.
197
+ const output = [
198
+ '/proc/net/tcp: sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode',
199
+ '/proc/net/tcp: 0: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 14200 1 0000 100 0 0 10 0',
200
+ '/proc/net/tcp: 1: 0100007F:1F90 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 25100 1 0000 100 0 0 10 0',
201
+ '/proc/net/tcp: 2: 0500000A:0016 0900000A:C822 01 00000000:00000000 00:00000000 00000000 0 0 25999 1 0000 20 0 0 10 -1',
202
+ '/proc/net/tcp6: 0: 00000000000000000000000000000000:01BB 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 14555 1 0000 100 0 0 10 0',
203
+ '/proc/net/tcp6: 1: 0000000000000000FFFF00000100007F:2383 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 30100 1 0000 100 0 0 10 0',
204
+ '/proc/net/udp: 2680: 00000000:0044 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 15001 2 0000 0',
205
+ ''
206
+ ].join('\n');
207
+
208
+ const rows = parseProcNet(output);
209
+
210
+ test('skips the header and reads every socket', () => {
211
+ expect(rows).toHaveLength(6);
212
+ });
213
+
214
+ test('decodes little-endian hex addresses and ports', () => {
215
+ expect(rows[1].socket).toMatchObject({ address: '127.0.0.1', port: 8080, state: 'listen' });
216
+ });
217
+
218
+ test('renders an all-zero IPv6 bind as a wildcard, not a stray colon', () => {
219
+ expect(rows[3].socket).toMatchObject({ address: '*', port: 443, ipVersion: 'v6' });
220
+ });
221
+
222
+ test('renders a v4-mapped IPv6 address as the address people recognise', () => {
223
+ expect(rows[4].socket).toMatchObject({ address: '127.0.0.1', port: 9091, ipVersion: 'v6' });
224
+ });
225
+
226
+ test('reads the peer of an established socket', () => {
227
+ expect(rows[2].socket).toMatchObject({
228
+ state: 'established',
229
+ address: '10.0.0.5',
230
+ peerAddress: '10.0.0.9',
231
+ peerPort: 51234
232
+ });
233
+ });
234
+
235
+ test('treats a bound UDP socket as listening whatever its state column says', () => {
236
+ expect(rows[5].socket).toMatchObject({ protocol: 'udp', port: 68, state: 'listen' });
237
+ });
238
+
239
+ test('carries the inode, which is the only link back to a process', () => {
240
+ expect(rows.map((row) => row.inode)).toEqual([14200, 25100, 25999, 14555, 30100, 15001]);
241
+ });
242
+ });
243
+
244
+ describe('parseProcFdInodes', () => {
245
+ const output = [
246
+ '/proc/1234/fd/:',
247
+ 'lrwx------ 1 deploy deploy 64 Aug 25 09:00 3 -> socket:[25100]',
248
+ 'lrwx------ 1 deploy deploy 64 Aug 25 09:00 4 -> /dev/null',
249
+ '',
250
+ '/proc/1300/fd/:',
251
+ 'lrwx------ 1 deploy deploy 64 Aug 25 09:00 7 -> socket:[30100]',
252
+ 'lrwx------ 1 deploy deploy 64 Aug 25 09:00 8 -> socket:[25100]',
253
+ ''
254
+ ].join('\n');
255
+
256
+ const owners = parseProcFdInodes(output);
257
+
258
+ test('maps each socket inode to the process holding it', () => {
259
+ expect(owners.get(25100)).toBe(1234);
260
+ expect(owners.get(30100)).toBe(1300);
261
+ });
262
+
263
+ test('credits an inherited socket to the parent, not the forked child', () => {
264
+ // 1300 also holds inode 25100, but 1234 saw it first and is the ancestor.
265
+ expect(owners.get(25100)).toBe(1234);
266
+ });
267
+
268
+ test('ignores descriptors that are not sockets', () => {
269
+ expect(owners.size).toBe(2);
270
+ });
271
+ });
272
+
273
+ describe('probe selection on a host with no port tools', () => {
274
+ /** A host where `ss`, `netstat` and `lsof` are all absent. */
275
+ function bareHost(): { runner: CommandRunner; calls: string[] } {
276
+ const calls: string[] = [];
277
+ const runner: CommandRunner = {
278
+ label: 'bare host',
279
+ async run(argv: string[]) {
280
+ const name = argv.find((part) => !part.includes('=') && part !== 'env') ?? argv[0];
281
+ calls.push(name);
282
+ if (name === 'grep') {
283
+ return {
284
+ stdout:
285
+ '/proc/net/tcp: 0: 0100007F:1F90 00000000:0000 0A 0:0 00:0 0 1000 0 25100 1\n',
286
+ stderr: '',
287
+ code: 0
288
+ };
289
+ }
290
+ if (name === 'sh') return { stdout: '', stderr: '', code: 0 };
291
+ return { stdout: '', stderr: 'command not found', code: 127 };
292
+ }
293
+ };
294
+ return { runner, calls };
295
+ }
296
+
297
+ test('falls through to the kernel’s own tables and still reports the port', async () => {
298
+ const { runner } = bareHost();
299
+ const scan = await scanSockets(runner, 'linux');
300
+
301
+ expect(scan.probe).toBe('/proc/net');
302
+ expect(scan.sockets).toHaveLength(1);
303
+ expect(scan.sockets[0]).toMatchObject({ port: 8080, state: 'listen' });
304
+ });
305
+
306
+ test('remembers what worked instead of re-walking the chain every scan', async () => {
307
+ // This is what exhausted an SSH connection: four failing commands per
308
+ // tick, each one a channel, on a host that will never have those tools.
309
+ const { runner, calls } = bareHost();
310
+ const memo = {};
311
+
312
+ await scanSockets(runner, 'linux', memo);
313
+ const firstPass = calls.length;
314
+ calls.length = 0;
315
+
316
+ await scanSockets(runner, 'linux', memo);
317
+
318
+ expect(firstPass).toBeGreaterThan(3);
319
+ expect(calls).not.toContain('ss');
320
+ expect(calls).not.toContain('netstat');
321
+ expect(calls).not.toContain('lsof');
322
+ });
323
+
324
+ test('re-derives the probe if the remembered one stops answering', async () => {
325
+ const { runner } = bareHost();
326
+ const memo = {};
327
+ await scanSockets(runner, 'linux', memo);
328
+
329
+ // The host changes under us — every probe now fails.
330
+ const dead: CommandRunner = {
331
+ label: 'bare host',
332
+ async run() {
333
+ return { stdout: '', stderr: 'command not found', code: 127 };
334
+ }
335
+ };
336
+
337
+ // Re-derivation finds nothing either, and says so rather than reporting
338
+ // an empty table as though the host had no open ports.
339
+ await expect(scanSockets(dead, 'linux', memo)).rejects.toThrow(/Could not read the port table/);
340
+ });
341
+ });