@myrialabs/clopen 0.4.33 → 0.4.35
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/backend/auth/permissions.ts +10 -0
- package/backend/auth/rate-limiter.test.ts +115 -0
- package/backend/auth/rate-limiter.ts +25 -12
- package/backend/containers/actions.ts +316 -0
- package/backend/containers/disk-usage.test.ts +68 -0
- package/backend/containers/disk-usage.ts +170 -0
- package/backend/containers/logs.ts +335 -0
- package/backend/containers/monitor.ts +410 -0
- package/backend/containers/parse.test.ts +599 -0
- package/backend/containers/parse.ts +905 -0
- package/backend/containers/port-index.test.ts +91 -0
- package/backend/containers/port-index.ts +198 -0
- package/backend/containers/prune-job.test.ts +148 -0
- package/backend/containers/prune-job.ts +129 -0
- package/backend/containers/pty-backend.ts +337 -0
- package/backend/containers/pty.ts +114 -0
- package/backend/containers/runtime.ts +218 -0
- package/backend/containers/scan.ts +293 -0
- package/backend/db-client/connection-manager.ts +13 -0
- package/backend/files/file-watcher.test.ts +105 -0
- package/backend/files/file-watcher.ts +45 -1
- package/backend/git/git-service.test.ts +33 -0
- package/backend/git/git-service.ts +7 -3
- package/backend/git/nested-repos.test.ts +292 -1
- package/backend/git/nested-repos.ts +330 -17
- package/backend/host/runner.ts +204 -0
- package/backend/index.ts +20 -2
- package/backend/middleware/security.test.ts +95 -0
- package/backend/middleware/security.ts +114 -0
- package/backend/ports/attribute.test.ts +231 -0
- package/backend/ports/attribute.ts +444 -0
- package/backend/ports/kill.ts +203 -0
- package/backend/ports/monitor.ts +365 -0
- package/backend/ports/processes.test.ts +98 -0
- package/backend/ports/processes.ts +374 -0
- package/backend/ports/registry.ts +165 -0
- package/backend/ports/scan-host.ts +329 -0
- package/backend/ports/scan.test.ts +341 -0
- package/backend/ports/scan.ts +687 -0
- package/backend/ports/ssh-lineage.ts +318 -0
- package/backend/preview/browser/browser-video-capture.ts +84 -31
- package/backend/ssh/forwards.ts +31 -0
- package/backend/ws/auth/device.ts +1 -1
- package/backend/ws/auth/login.ts +8 -4
- package/backend/ws/containers/actions.ts +156 -0
- package/backend/ws/containers/index.ts +43 -0
- package/backend/ws/containers/list.ts +146 -0
- package/backend/ws/containers/logs.ts +43 -0
- package/backend/ws/containers/tunnel.ts +101 -0
- package/backend/ws/git/log.ts +3 -1
- package/backend/ws/git/status.ts +37 -27
- package/backend/ws/index.ts +4 -0
- package/backend/ws/message-rate-limiter.test.ts +45 -0
- package/backend/ws/message-rate-limiter.ts +34 -3
- package/backend/ws/ports/index.ts +19 -0
- package/backend/ws/ports/kill.ts +80 -0
- package/backend/ws/ports/list.ts +60 -0
- package/backend/ws/ssh/connections.ts +8 -0
- package/backend/ws/system/device-info.ts +99 -43
- package/dist/assets/{index-BF4buYmI.js → index-B0iIhSO9.js} +1 -1
- package/dist/assets/{index-BW0DA8NV.js → index-BRJqss6F.js} +1 -1
- package/dist/assets/main-Buko4D0t.css +1 -0
- package/dist/assets/{main-BbwWLJRN.js → main-C_iGUZyL.js} +468 -447
- package/dist/index.html +2 -2
- package/dist/manifest.json +9 -2
- package/package.json +1 -1
- package/shared/types/containers/container.ts +366 -0
- package/shared/types/containers/index.ts +5 -0
- package/shared/types/host/index.ts +18 -0
- package/shared/types/ports/index.ts +5 -0
- package/shared/types/ports/port.ts +230 -0
- package/shared/utils/logger.ts +3 -0
- package/shared/utils/ws-client.ts +67 -12
- package/shared/utils/ws-server.ts +64 -13
- package/dist/assets/main-CCvRMXlP.css +0 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The headers are only worth anything if they survive the way Clopen actually
|
|
3
|
+
* returns responses. The SPA fallback and the static file handler in
|
|
4
|
+
* `backend/index.ts` hand back a raw `Response`, which is a different path
|
|
5
|
+
* through Elysia than a plain object return — so both are exercised here.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, test } from 'bun:test';
|
|
9
|
+
import { Elysia } from 'elysia';
|
|
10
|
+
import { SECURITY_HEADERS, securityMiddleware } from './security';
|
|
11
|
+
|
|
12
|
+
const app = new Elysia()
|
|
13
|
+
.use(securityMiddleware)
|
|
14
|
+
.get('/json', () => ({ ok: true }))
|
|
15
|
+
// Mirrors the production SPA fallback: a raw Response with its own headers.
|
|
16
|
+
.get('/spa', () => new Response('<!doctype html>', {
|
|
17
|
+
headers: { 'Content-Type': 'text/html; charset=utf-8' }
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
async function headersFor(path: string): Promise<Headers> {
|
|
21
|
+
const response = await app.handle(new Request(`http://localhost${path}`));
|
|
22
|
+
return response.headers;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function csp(headers: Headers): string {
|
|
26
|
+
return headers.get('content-security-policy') ?? '';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe('security headers reach the response', () => {
|
|
30
|
+
for (const path of ['/json', '/spa']) {
|
|
31
|
+
test(`every header is set on ${path}`, async () => {
|
|
32
|
+
const headers = await headersFor(path);
|
|
33
|
+
for (const [name, value] of Object.entries(SECURITY_HEADERS)) {
|
|
34
|
+
expect(headers.get(name.toLowerCase())).toBe(value);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test('the raw Response keeps its own Content-Type', async () => {
|
|
40
|
+
const headers = await headersFor('/spa');
|
|
41
|
+
expect(headers.get('content-type')).toBe('text/html; charset=utf-8');
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('CSP covers what the app loads', () => {
|
|
46
|
+
test('Monaco can load its scripts, styles and codicon font from the CDN', async () => {
|
|
47
|
+
const policy = csp(await headersFor('/spa'));
|
|
48
|
+
for (const directive of ['script-src', 'style-src', 'font-src', 'connect-src']) {
|
|
49
|
+
expect(policy).toMatch(new RegExp(`${directive} [^;]*https://cdn\\.jsdelivr\\.net`));
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("Monaco's blob-bootstrapped language workers are allowed", async () => {
|
|
54
|
+
const policy = csp(await headersFor('/spa'));
|
|
55
|
+
expect(policy).toMatch(/script-src [^;]*blob:/);
|
|
56
|
+
expect(policy).toMatch(/worker-src [^;]*blob:/);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('object-URL previews are allowed for media, frames and images', async () => {
|
|
60
|
+
const policy = csp(await headersFor('/spa'));
|
|
61
|
+
// MediaPreview.svelte: <audio>/<video> src, the PDF <iframe>, and images.
|
|
62
|
+
expect(policy).toMatch(/media-src [^;]*blob:/);
|
|
63
|
+
expect(policy).toMatch(/frame-src [^;]*blob:/);
|
|
64
|
+
expect(policy).toMatch(/img-src [^;]*blob:/);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('remote images in rendered markdown are not blocked', async () => {
|
|
68
|
+
const policy = csp(await headersFor('/spa'));
|
|
69
|
+
expect(policy).toMatch(/img-src [^;]*https:/);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('embedding, base rewriting and off-site form posts are shut down', async () => {
|
|
73
|
+
const policy = csp(await headersFor('/spa'));
|
|
74
|
+
expect(policy).toContain("frame-ancestors 'none'");
|
|
75
|
+
expect(policy).toContain("object-src 'none'");
|
|
76
|
+
expect(policy).toContain("base-uri 'self'");
|
|
77
|
+
expect(policy).toContain("form-action 'self'");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('Permissions-Policy matches what the host bridge relays', () => {
|
|
82
|
+
test('device APIs the Preview Browser relays stay available to the app', async () => {
|
|
83
|
+
const policy = (await headersFor('/spa')).get('permissions-policy') ?? '';
|
|
84
|
+
for (const feature of ['camera', 'microphone', 'geolocation', 'display-capture', 'clipboard-read']) {
|
|
85
|
+
expect(policy).toContain(`${feature}=(self)`);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('features Clopen never uses are denied', async () => {
|
|
90
|
+
const policy = (await headersFor('/spa')).get('permissions-policy') ?? '';
|
|
91
|
+
for (const feature of ['payment', 'usb', 'serial', 'bluetooth', 'midi', 'hid']) {
|
|
92
|
+
expect(policy).toContain(`${feature}=()`);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -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
|
+
});
|