dsh-remote-go 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -3
- package/{client.js → dist/client.js} +7144 -8367
- package/dist/index.js +4 -0
- package/dist/src/admission.js +21 -0
- package/dist/src/api.js +380 -0
- package/dist/src/atomic-json.js +201 -0
- package/dist/src/bootstrap.js +96 -0
- package/dist/src/connect/attach.js +21 -0
- package/dist/src/connect/bootstrap.js +8 -0
- package/dist/src/connect/failures.js +29 -0
- package/dist/src/connect/guards.js +36 -0
- package/dist/src/connect/launch.js +70 -0
- package/dist/src/connect/links.js +54 -0
- package/dist/src/connect/ownership.js +139 -0
- package/dist/src/connect/pipeline.js +376 -0
- package/dist/src/connect/reattach.js +191 -0
- package/dist/src/connect/registry.js +10 -0
- package/dist/src/connect/repair.js +97 -0
- package/dist/src/connect/restart.js +255 -0
- package/dist/src/connect/stop.js +164 -0
- package/dist/src/connect/transport.js +101 -0
- package/dist/src/connect/verify.js +30 -0
- package/dist/src/connect/watchdog.js +44 -0
- package/dist/src/connect-log.js +137 -0
- package/dist/src/connect-progress.js +110 -0
- package/dist/src/constants.js +29 -0
- package/dist/src/context.js +96 -0
- package/dist/src/diagnostics.js +133 -0
- package/dist/src/download-jobs.js +221 -0
- package/dist/src/errors.js +228 -0
- package/dist/src/events.js +326 -0
- package/dist/src/host-lifecycle.js +368 -0
- package/dist/src/host-registry.js +181 -0
- package/dist/src/host-runtime.js +77 -0
- package/dist/src/host-view.js +52 -0
- package/dist/src/index.js +196 -0
- package/dist/src/kernel-adapter.js +388 -0
- package/dist/src/key-files.js +39 -0
- package/dist/src/known-hosts.js +104 -0
- package/dist/src/layout.js +26 -0
- package/dist/src/local-download.js +311 -0
- package/dist/src/local-ssh.js +23 -0
- package/dist/src/local-version.js +87 -0
- package/dist/src/node/download.js +157 -0
- package/dist/src/node-runtime.js +327 -0
- package/dist/src/probe.js +95 -0
- package/dist/src/prompts.js +198 -0
- package/dist/src/proxy/cookies.js +102 -0
- package/dist/src/proxy/headers.js +167 -0
- package/dist/src/proxy/http.js +134 -0
- package/dist/src/proxy/index.js +121 -0
- package/dist/src/proxy/limits.js +6 -0
- package/dist/src/proxy/routes.js +32 -0
- package/dist/src/proxy/upgrade.js +160 -0
- package/dist/src/redact.js +85 -0
- package/dist/src/remote-files.js +60 -0
- package/dist/src/remote-path.js +82 -0
- package/dist/src/remote-proc.js +271 -0
- package/dist/src/remote-web/registry.js +107 -0
- package/dist/src/remote-web/resolve.js +229 -0
- package/dist/src/remote-web/start.js +190 -0
- package/dist/src/remote-web/stop.js +74 -0
- package/dist/src/request-trust.js +70 -0
- package/dist/src/runtime/dispatch.js +200 -0
- package/dist/src/runtime/effects.js +81 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +70 -0
- package/dist/src/runtime/rules.js +480 -0
- package/dist/src/secrets.js +119 -0
- package/dist/src/service.js +300 -0
- package/dist/src/settings.js +143 -0
- package/dist/src/ssh-config.js +257 -0
- package/dist/src/ssh-failures.js +71 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +258 -0
- package/dist/src/ssh.js +395 -0
- package/dist/src/store.js +174 -0
- package/dist/src/tool-shared.js +146 -0
- package/dist/src/tools-exec-policy.js +121 -0
- package/dist/src/tools-exec.js +307 -0
- package/dist/src/tools-report.js +357 -0
- package/dist/src/tools.js +337 -0
- package/dist/src/tunnel.js +111 -0
- package/dist/src/util.js +112 -0
- package/dist/src/version.js +108 -0
- package/dist/src/watchdog-bus.js +21 -0
- package/dist/src/watchdog.js +116 -0
- package/index.js +5 -4
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +37 -19
- package/src/admission.js +0 -43
- package/src/api.js +0 -536
- package/src/atomic-json.js +0 -225
- package/src/bootstrap.js +0 -113
- package/src/connect-log.js +0 -176
- package/src/connect-pipeline.js +0 -1900
- package/src/connect-progress.js +0 -155
- package/src/constants.js +0 -93
- package/src/diagnostics.js +0 -160
- package/src/download-jobs.js +0 -234
- package/src/errors.js +0 -412
- package/src/host-lifecycle.js +0 -604
- package/src/host-registry.js +0 -217
- package/src/host-runtime.js +0 -127
- package/src/host-view.js +0 -75
- package/src/index.js +0 -206
- package/src/key-files.js +0 -54
- package/src/known-hosts.js +0 -133
- package/src/layout.js +0 -53
- package/src/local-download.js +0 -381
- package/src/local-ssh.js +0 -27
- package/src/local-version.js +0 -88
- package/src/node-runtime.js +0 -521
- package/src/probe.js +0 -101
- package/src/prompts.js +0 -229
- package/src/redact.js +0 -68
- package/src/remote-files.js +0 -71
- package/src/remote-path.js +0 -137
- package/src/remote-proc.js +0 -343
- package/src/remote-proxy.js +0 -815
- package/src/remote-web.js +0 -751
- package/src/request-trust.js +0 -89
- package/src/secrets.js +0 -109
- package/src/service.js +0 -459
- package/src/settings.js +0 -163
- package/src/ssh-config.js +0 -271
- package/src/ssh.js +0 -731
- package/src/store.js +0 -193
- package/src/tools.js +0 -386
- package/src/tunnel.js +0 -132
- package/src/util.js +0 -153
- package/src/version.js +0 -103
- package/src/webapp.js +0 -43
- /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
/** The kernel introspection adapter reads DSH's own shapes (loader fiber, client modules, `connection`, `credentials`)
|
|
2
|
+
* as `ok`/`absent`/`changed`: callers fall back on `absent` and degrade safely on `changed` — never "admitted". */
|
|
3
|
+
import { PLUGIN_ID } from './constants.js';
|
|
4
|
+
import { isPlainObject, messageOf } from './util.js';
|
|
5
|
+
/** The contract version of the probe table; additive changes bump it. */
|
|
6
|
+
export const KERNEL_PROBE_VERSION = '2.12';
|
|
7
|
+
/** The `dsh-credentials` segment grammar (`KEY_SEGMENT_PATTERN` in that package). */
|
|
8
|
+
const KEY_SEGMENT = /^[a-z][a-z0-9-]*$/;
|
|
9
|
+
/** The replicated grammar as its diagnostics line; kept beside the regex so the two cannot drift. */
|
|
10
|
+
export const CREDENTIAL_KEY_RULE = String(KEY_SEGMENT);
|
|
11
|
+
/** A key the replicated grammar admits, used to prove a real parser agrees with it. */
|
|
12
|
+
const SAMPLE_KEY = 'dsh-remote-go/ssh-password-h-1';
|
|
13
|
+
/** Forms the replicated grammar rejects; a parser that accepts one of them is the `changed` signal. */
|
|
14
|
+
const REJECTED_KEYS = [
|
|
15
|
+
'dsh-remote-go:ssh-password-h-1',
|
|
16
|
+
'Dsh-remote-go/x',
|
|
17
|
+
'dsh-remote-go/ssh-password-h-1/x',
|
|
18
|
+
'dsh-remote-go/ssh_password_h_1',
|
|
19
|
+
];
|
|
20
|
+
function ok(value, detail) {
|
|
21
|
+
return { state: 'ok', value, detail };
|
|
22
|
+
}
|
|
23
|
+
function absent(detail, reason = 'not-injected') {
|
|
24
|
+
return { state: 'absent', value: null, detail, reason };
|
|
25
|
+
}
|
|
26
|
+
function changed(detail, reason = 'shape') {
|
|
27
|
+
return { state: 'changed', value: null, detail, reason };
|
|
28
|
+
}
|
|
29
|
+
function describeType(value) {
|
|
30
|
+
if (value === null)
|
|
31
|
+
return 'null';
|
|
32
|
+
if (Array.isArray(value))
|
|
33
|
+
return 'an array';
|
|
34
|
+
return `a ${typeof value}`;
|
|
35
|
+
}
|
|
36
|
+
/** Turn a probe that failed for its own service into the same failure for a narrower value. */
|
|
37
|
+
function propagate(failed) {
|
|
38
|
+
return {
|
|
39
|
+
state: failed.state === 'changed' ? 'changed' : 'absent',
|
|
40
|
+
value: null,
|
|
41
|
+
detail: failed.detail,
|
|
42
|
+
reason: failed.reason,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Bind one untyped service member to its owner: a detached class method loses `this`, so calls go through
|
|
46
|
+
* `Reflect.apply` instead of an assertion on the member. */
|
|
47
|
+
function bindMember(member, owner) {
|
|
48
|
+
return (...args) => Reflect.apply(member, owner, args);
|
|
49
|
+
}
|
|
50
|
+
/** One member of an untyped service, narrowed to a callable bound to its owner, or why it is not one. */
|
|
51
|
+
function callableOf(owner, name, label) {
|
|
52
|
+
const member = owner[name];
|
|
53
|
+
if (member === undefined)
|
|
54
|
+
return absent(`${label} has no ${name}()`);
|
|
55
|
+
if (typeof member !== 'function')
|
|
56
|
+
return changed(`${label}.${name} is ${describeType(member)}`);
|
|
57
|
+
return ok(bindMember(member, owner), `${label}.${name} is callable`);
|
|
58
|
+
}
|
|
59
|
+
/** Read one revision tag (a string or a number in DSH's own history) without guessing. */
|
|
60
|
+
function readRev(value) {
|
|
61
|
+
if (value === undefined || value === null)
|
|
62
|
+
return null;
|
|
63
|
+
if (typeof value === 'string' || typeof value === 'number')
|
|
64
|
+
return value;
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
/** `ctx.get(name)` trusting neither side: a non-object context, a missing `get` or a throw is *absence*, never
|
|
68
|
+
* `changed` — "could not read it" says nothing about the shape behind it. */
|
|
69
|
+
function lookupService(ctx, name) {
|
|
70
|
+
if (!isPlainObject(ctx))
|
|
71
|
+
return absent('the plugin context is not an object');
|
|
72
|
+
const get = ctx.get;
|
|
73
|
+
if (typeof get !== 'function')
|
|
74
|
+
return absent('the plugin context has no get()');
|
|
75
|
+
try {
|
|
76
|
+
const service = Reflect.apply(get, ctx, [name]);
|
|
77
|
+
// `!service` matches the pre-adapter gate: a falsy service was "no service" for every consumer.
|
|
78
|
+
if (!service)
|
|
79
|
+
return absent(`${name} is not injected`);
|
|
80
|
+
return ok(service, `resolved ${name}`);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return absent(`${name} lookup failed: ${messageOf(error)}`, 'lookup-failed');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** The record-key builder: the replicated rule, the single implementation (`secrets.ts` imports it). */
|
|
87
|
+
export function credentialRecordKey(scope, id) {
|
|
88
|
+
if (typeof scope !== 'string' || typeof id !== 'string')
|
|
89
|
+
return null;
|
|
90
|
+
if (!KEY_SEGMENT.test(scope) || !KEY_SEGMENT.test(id))
|
|
91
|
+
return null;
|
|
92
|
+
return `${scope}/${id}`;
|
|
93
|
+
}
|
|
94
|
+
/** Summarize one probe for the diagnostics table, containing an unexpected throw as `changed`. */
|
|
95
|
+
function line(probe) {
|
|
96
|
+
try {
|
|
97
|
+
const result = probe();
|
|
98
|
+
return { state: result.state, detail: result.detail };
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
// Diagnostics must never throw; a probe that did is reported, not propagated.
|
|
102
|
+
return { state: 'changed', detail: `probe threw: ${messageOf(error)}` };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Build the adapter for one kernel context: `ctx` is `unknown` on purpose (it comes from the loader and every member
|
|
106
|
+
* is shape-checked first, so a renamed member is reported, not a crash). `probeReport()` runs every probe at once. */
|
|
107
|
+
export function createKernelAdapter(ctx) {
|
|
108
|
+
/** The `clientModules` service: `ctx.get` first, then the property `ctx.clientModules` (its fallback). */
|
|
109
|
+
function clientModules() {
|
|
110
|
+
const looked = lookupService(ctx, 'clientModules');
|
|
111
|
+
// A throwing lookup is not retried through the property: the old code read it only when `get` returned nothing.
|
|
112
|
+
if (looked.reason === 'lookup-failed')
|
|
113
|
+
return propagate(looked);
|
|
114
|
+
if (looked.state === 'ok' && looked.value !== null) {
|
|
115
|
+
return isPlainObject(looked.value)
|
|
116
|
+
? ok(looked.value, 'resolved clientModules')
|
|
117
|
+
: changed(`clientModules is ${describeType(looked.value)}`);
|
|
118
|
+
}
|
|
119
|
+
const direct = isPlainObject(ctx) ? ctx.clientModules : undefined;
|
|
120
|
+
if (isPlainObject(direct))
|
|
121
|
+
return ok(direct, 'resolved clientModules from the context');
|
|
122
|
+
if (direct !== undefined && direct !== null)
|
|
123
|
+
return changed(`clientModules is ${describeType(direct)}`);
|
|
124
|
+
return absent('clientModules is not injected');
|
|
125
|
+
}
|
|
126
|
+
/** The published graph object, or why it could not be read. Validation stops at the object boundary: each probe
|
|
127
|
+
* narrows only the fields *it* reads, so a kernel that grows a field cannot switch a probe off. */
|
|
128
|
+
function readGraph() {
|
|
129
|
+
const service = clientModules();
|
|
130
|
+
if (service.state !== 'ok' || service.value === null)
|
|
131
|
+
return propagate(service);
|
|
132
|
+
const owner = service.value;
|
|
133
|
+
const graph = callableOf(owner, 'graph', 'clientModules');
|
|
134
|
+
if (graph.state !== 'ok' || graph.value === null)
|
|
135
|
+
return propagate(graph);
|
|
136
|
+
let raw;
|
|
137
|
+
try {
|
|
138
|
+
raw = graph.value();
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
return changed(`clientModules.graph() threw: ${messageOf(error)}`, 'call-failed');
|
|
142
|
+
}
|
|
143
|
+
if (raw === undefined || raw === null)
|
|
144
|
+
return absent('clientModules.graph() returned nothing');
|
|
145
|
+
if (!isPlainObject(raw))
|
|
146
|
+
return changed(`clientModules.graph() returned ${describeType(raw)}`);
|
|
147
|
+
return ok(raw, 'read clientModules.graph()');
|
|
148
|
+
}
|
|
149
|
+
/** Resolve the admission member without judging a request (shared by the gate and diagnostics). */
|
|
150
|
+
function resolveAdmission() {
|
|
151
|
+
const looked = lookupService(ctx, 'connection');
|
|
152
|
+
if (looked.state !== 'ok' || looked.value === null)
|
|
153
|
+
return propagate(looked);
|
|
154
|
+
if (!isPlainObject(looked.value))
|
|
155
|
+
return changed(`connection is ${describeType(looked.value)}`);
|
|
156
|
+
const member = callableOf(looked.value, 'requestRejection', 'connection');
|
|
157
|
+
if (member.state !== 'ok' || member.value === null)
|
|
158
|
+
return propagate(member);
|
|
159
|
+
return ok(member.value, 'connection admission is callable');
|
|
160
|
+
}
|
|
161
|
+
const adapter = {
|
|
162
|
+
fiberEntry() {
|
|
163
|
+
if (!isPlainObject(ctx))
|
|
164
|
+
return absent('the plugin context is not an object');
|
|
165
|
+
const fiber = ctx.fiber;
|
|
166
|
+
if (fiber === undefined || fiber === null)
|
|
167
|
+
return absent('fiber is not exposed on the plugin context');
|
|
168
|
+
if (!isPlainObject(fiber))
|
|
169
|
+
return changed(`fiber is ${describeType(fiber)}`);
|
|
170
|
+
const entry = fiber.entry;
|
|
171
|
+
if (entry === undefined || entry === null)
|
|
172
|
+
return absent('fiber.entry is not exposed');
|
|
173
|
+
if (!isPlainObject(entry))
|
|
174
|
+
return changed(`fiber.entry is ${describeType(entry)}`);
|
|
175
|
+
const options = entry.options;
|
|
176
|
+
if (!isPlainObject(options)) {
|
|
177
|
+
return changed(`fiber.entry.options is ${options === undefined ? 'missing' : describeType(options)}`);
|
|
178
|
+
}
|
|
179
|
+
const id = options.id;
|
|
180
|
+
const name = options.name;
|
|
181
|
+
if (id !== undefined && id !== null && typeof id !== 'string') {
|
|
182
|
+
return changed(`fiber.entry.options.id is ${describeType(id)}`);
|
|
183
|
+
}
|
|
184
|
+
if (name !== undefined && name !== null && typeof name !== 'string') {
|
|
185
|
+
return changed(`fiber.entry.options.name is ${describeType(name)}`);
|
|
186
|
+
}
|
|
187
|
+
const value = { id: typeof id === 'string' ? id : null, name: typeof name === 'string' ? name : null };
|
|
188
|
+
return ok(value, `id=${value.id ?? '-'} name=${value.name ?? '-'}`);
|
|
189
|
+
},
|
|
190
|
+
clientModuleGraph() {
|
|
191
|
+
const graph = readGraph();
|
|
192
|
+
if (graph.state !== 'ok' || graph.value === null)
|
|
193
|
+
return propagate(graph);
|
|
194
|
+
const entries = graph.value.entries;
|
|
195
|
+
if (!Array.isArray(entries))
|
|
196
|
+
return changed(`clientModules.graph().entries is ${describeType(entries)}`);
|
|
197
|
+
const rev = readRev(graph.value.rev);
|
|
198
|
+
if (rev === undefined)
|
|
199
|
+
return changed(`clientModules.graph().rev is ${describeType(graph.value.rev)}`);
|
|
200
|
+
const release = graph.value.release;
|
|
201
|
+
if (release !== undefined && release !== null && !isPlainObject(release)) {
|
|
202
|
+
return changed(`clientModules.graph().release is ${describeType(release)}`);
|
|
203
|
+
}
|
|
204
|
+
const releaseVersion = isPlainObject(release) && typeof release.version === 'string' ? release.version : '-';
|
|
205
|
+
return ok({ rev, release: isPlainObject(release) ? release : null, entries: entries.length }, `rev=${String(rev ?? '-')} entries=${entries.length} release=${releaseVersion}`);
|
|
206
|
+
},
|
|
207
|
+
clientModuleRelease() {
|
|
208
|
+
const graph = readGraph();
|
|
209
|
+
if (graph.state !== 'ok' || graph.value === null)
|
|
210
|
+
return propagate(graph);
|
|
211
|
+
const release = graph.value.release;
|
|
212
|
+
if (release === undefined || release === null)
|
|
213
|
+
return absent('the client-module graph has no release record');
|
|
214
|
+
if (!isPlainObject(release))
|
|
215
|
+
return changed(`the client-module release is ${describeType(release)}`);
|
|
216
|
+
const version = release.version;
|
|
217
|
+
if (version === undefined || version === null)
|
|
218
|
+
return absent('the client-module release has no version');
|
|
219
|
+
if (typeof version !== 'string' || version === '') {
|
|
220
|
+
return changed(`the client-module release version is ${describeType(version)}`);
|
|
221
|
+
}
|
|
222
|
+
return ok({ version, release }, `release version ${version}`);
|
|
223
|
+
},
|
|
224
|
+
clientModuleRow(id) {
|
|
225
|
+
const graph = readGraph();
|
|
226
|
+
if (graph.state !== 'ok' || graph.value === null)
|
|
227
|
+
return propagate(graph);
|
|
228
|
+
const entries = graph.value.entries;
|
|
229
|
+
if (!Array.isArray(entries))
|
|
230
|
+
return changed(`clientModules.graph().entries is ${describeType(entries)}`);
|
|
231
|
+
const row = entries.find((item) => isPlainObject(item) && item.id === id);
|
|
232
|
+
if (!isPlainObject(row))
|
|
233
|
+
return absent(`the client-module graph lists no row for ${id}`);
|
|
234
|
+
const rev = readRev(row.rev);
|
|
235
|
+
if (rev === undefined)
|
|
236
|
+
return changed(`the ${id} row has a ${describeType(row.rev)} rev`);
|
|
237
|
+
const url = row.url;
|
|
238
|
+
if (url !== undefined && url !== null && typeof url !== 'string') {
|
|
239
|
+
return changed(`the ${id} row has a ${describeType(url)} url`);
|
|
240
|
+
}
|
|
241
|
+
const inject = row.inject;
|
|
242
|
+
if (inject !== undefined && !Array.isArray(inject)) {
|
|
243
|
+
return changed(`the ${id} row has a ${describeType(inject)} inject list`);
|
|
244
|
+
}
|
|
245
|
+
const injected = Array.isArray(inject) ? inject.filter((item) => typeof item === 'string') : [];
|
|
246
|
+
if (Array.isArray(inject) && injected.length !== inject.length) {
|
|
247
|
+
return changed(`the ${id} row injects a non-string entry`);
|
|
248
|
+
}
|
|
249
|
+
const immediately = row.immediately;
|
|
250
|
+
if (immediately !== undefined && immediately !== null && typeof immediately !== 'boolean') {
|
|
251
|
+
return changed(`the ${id} row has a ${describeType(immediately)} immediately flag`);
|
|
252
|
+
}
|
|
253
|
+
const value = {
|
|
254
|
+
id,
|
|
255
|
+
rev,
|
|
256
|
+
url: typeof url === 'string' ? url : null,
|
|
257
|
+
inject: injected,
|
|
258
|
+
immediately: immediately === true,
|
|
259
|
+
};
|
|
260
|
+
return ok(value, `inject=${value.inject.length} immediately=${value.immediately}`);
|
|
261
|
+
},
|
|
262
|
+
clientPath(id) {
|
|
263
|
+
const service = clientModules();
|
|
264
|
+
if (service.state !== 'ok' || service.value === null)
|
|
265
|
+
return propagate(service);
|
|
266
|
+
const member = callableOf(service.value, 'clientPath', 'clientModules');
|
|
267
|
+
if (member.state !== 'ok' || member.value === null)
|
|
268
|
+
return propagate(member);
|
|
269
|
+
let raw;
|
|
270
|
+
try {
|
|
271
|
+
raw = member.value(id);
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
return changed(`clientModules.clientPath() threw: ${messageOf(error)}`, 'call-failed');
|
|
275
|
+
}
|
|
276
|
+
if (raw === undefined || raw === null)
|
|
277
|
+
return absent(`clientModules.clientPath() knows no bundle for ${id}`);
|
|
278
|
+
if (typeof raw !== 'string' || raw === '')
|
|
279
|
+
return changed(`clientModules.clientPath() returned ${describeType(raw)}`);
|
|
280
|
+
// The path itself is never echoed: it carries the machine's plugin directory.
|
|
281
|
+
return ok(raw, 'resolved');
|
|
282
|
+
},
|
|
283
|
+
admissionCapability() {
|
|
284
|
+
const resolved = resolveAdmission();
|
|
285
|
+
if (resolved.state !== 'ok')
|
|
286
|
+
return propagate(resolved);
|
|
287
|
+
return ok(true, 'connection admission is callable');
|
|
288
|
+
},
|
|
289
|
+
requestRejection(req) {
|
|
290
|
+
const resolved = resolveAdmission();
|
|
291
|
+
if (resolved.state !== 'ok' || resolved.value === null)
|
|
292
|
+
return propagate(resolved);
|
|
293
|
+
let verdict;
|
|
294
|
+
try {
|
|
295
|
+
verdict = resolved.value(req);
|
|
296
|
+
}
|
|
297
|
+
catch (error) {
|
|
298
|
+
// A judgement that throws cannot decide: the gate turns this into 500, never "admitted".
|
|
299
|
+
return changed(`connection.requestRejection threw: ${messageOf(error)}`, 'call-failed');
|
|
300
|
+
}
|
|
301
|
+
if (verdict === undefined || verdict === null)
|
|
302
|
+
return ok(undefined, 'admitted');
|
|
303
|
+
if (typeof verdict === 'number' && Number.isFinite(verdict))
|
|
304
|
+
return ok(verdict, `rejected with ${verdict}`);
|
|
305
|
+
return changed(`connection.requestRejection returned ${describeType(verdict)}`);
|
|
306
|
+
},
|
|
307
|
+
credentialService() {
|
|
308
|
+
const looked = lookupService(ctx, 'credentials');
|
|
309
|
+
if (looked.state !== 'ok' || looked.value === null)
|
|
310
|
+
return propagate(looked);
|
|
311
|
+
if (!isPlainObject(looked.value))
|
|
312
|
+
return changed(`credentials is ${describeType(looked.value)}`);
|
|
313
|
+
const owner = looked.value;
|
|
314
|
+
const readRecord = owner.readRecord;
|
|
315
|
+
const modifyRecord = owner.modifyRecord;
|
|
316
|
+
if (readRecord === undefined && modifyRecord === undefined)
|
|
317
|
+
return absent('credentials exposes no record API');
|
|
318
|
+
if (typeof readRecord !== 'function' || typeof modifyRecord !== 'function') {
|
|
319
|
+
return changed(`credentials.readRecord/modifyRecord are ${describeType(readRecord)}/${describeType(modifyRecord)}`);
|
|
320
|
+
}
|
|
321
|
+
const read = bindMember(readRecord, owner);
|
|
322
|
+
const modify = bindMember(modifyRecord, owner);
|
|
323
|
+
const api = {
|
|
324
|
+
async readRecord(key) {
|
|
325
|
+
// A record of an unexpected shape reads as "nothing stored"; the caller re-narrows the payload.
|
|
326
|
+
const record = await read(key);
|
|
327
|
+
return isPlainObject(record) ? { kind: record.kind, payload: record.payload } : null;
|
|
328
|
+
},
|
|
329
|
+
async modifyRecord(key, update) {
|
|
330
|
+
return await modify(key, update);
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
const deletable = owner.deleteRecord;
|
|
334
|
+
if (typeof deletable === 'function') {
|
|
335
|
+
const remove = bindMember(deletable, owner);
|
|
336
|
+
api.deleteRecord = async (key) => await remove(key);
|
|
337
|
+
}
|
|
338
|
+
return ok(api, 'credentials exposes readRecord + modifyRecord');
|
|
339
|
+
},
|
|
340
|
+
credentialKeyGrammar() {
|
|
341
|
+
const looked = lookupService(ctx, 'credentials');
|
|
342
|
+
if (looked.state !== 'ok' || looked.value === null)
|
|
343
|
+
return propagate(looked);
|
|
344
|
+
if (!isPlainObject(looked.value))
|
|
345
|
+
return changed(`credentials is ${describeType(looked.value)}`);
|
|
346
|
+
const parser = looked.value.parseCredentialKey;
|
|
347
|
+
if (parser === undefined) {
|
|
348
|
+
return ok({ verified: false, rule: CREDENTIAL_KEY_RULE }, 'the kernel exposes no key parser; the replicated grammar is in use');
|
|
349
|
+
}
|
|
350
|
+
if (typeof parser !== 'function')
|
|
351
|
+
return changed(`credentials.parseCredentialKey is ${describeType(parser)}`);
|
|
352
|
+
const parse = bindMember(parser, looked.value);
|
|
353
|
+
try {
|
|
354
|
+
parse(SAMPLE_KEY);
|
|
355
|
+
}
|
|
356
|
+
catch (error) {
|
|
357
|
+
return changed(`credentials.parseCredentialKey rejected a valid key: ${messageOf(error)}`, 'call-failed');
|
|
358
|
+
}
|
|
359
|
+
for (const rejected of REJECTED_KEYS) {
|
|
360
|
+
let refused = false;
|
|
361
|
+
try {
|
|
362
|
+
parse(rejected);
|
|
363
|
+
}
|
|
364
|
+
catch {
|
|
365
|
+
// swallow-ok: the refusal is the expected answer; the flag records that it happened
|
|
366
|
+
refused = true;
|
|
367
|
+
}
|
|
368
|
+
if (!refused) {
|
|
369
|
+
return changed(`credentials.parseCredentialKey accepted "${rejected}", which the grammar rejects`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return ok({ verified: true, rule: CREDENTIAL_KEY_RULE }, 'parseCredentialKey agrees with the replicated grammar');
|
|
373
|
+
},
|
|
374
|
+
probeReport() {
|
|
375
|
+
return {
|
|
376
|
+
fiberEntry: line(() => adapter.fiberEntry()),
|
|
377
|
+
clientModuleGraph: line(() => adapter.clientModuleGraph()),
|
|
378
|
+
clientModuleRelease: line(() => adapter.clientModuleRelease()),
|
|
379
|
+
clientModuleRow: line(() => adapter.clientModuleRow(PLUGIN_ID)),
|
|
380
|
+
clientPath: line(() => adapter.clientPath(PLUGIN_ID)),
|
|
381
|
+
requestRejection: line(() => adapter.admissionCapability()),
|
|
382
|
+
credentialService: line(() => adapter.credentialService()),
|
|
383
|
+
credentialKeyGrammar: line(() => adapter.credentialKeyGrammar()),
|
|
384
|
+
};
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
return adapter;
|
|
388
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Read-only directory browsing for the key-file picker; a path that resolves outside the user home directory (through
|
|
2
|
+
* `..` or a symlink) is refused. */
|
|
3
|
+
import { promises as fs } from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { expandHome } from './util.js';
|
|
6
|
+
const MAX_ENTRIES = 500;
|
|
7
|
+
/** Code of the "not inside the home directory" refusal, so callers can map it to an HTTP 400. */
|
|
8
|
+
export const OUTSIDE_HOME = 'EOUTSIDEHOME';
|
|
9
|
+
export async function listKeyFiles(options) {
|
|
10
|
+
const home = path.resolve(options.home);
|
|
11
|
+
const impl = options.fsImpl ?? fs;
|
|
12
|
+
// Canonical base: the home directory as the filesystem reports it, so `path` and `parent` cannot disagree — Windows
|
|
13
|
+
// hands out an 8.3 short path (`C:\Users\RUNNER~1\…`) for a directory realpath reports in long form.
|
|
14
|
+
const realHome = await impl.realpath(home).catch(() => home);
|
|
15
|
+
const target = path.resolve(expandHome(options.dir, realHome) || path.join(realHome, '.ssh'));
|
|
16
|
+
// Resolve first, then judge: a symlinked directory may lead outside home; a missing path keeps its spelling.
|
|
17
|
+
const realTarget = await impl.realpath(target).catch(() => target);
|
|
18
|
+
if (realTarget !== realHome && !realTarget.startsWith(realHome + path.sep)) {
|
|
19
|
+
throw Object.assign(new Error('key-file path must stay inside the home directory'), { code: OUTSIDE_HOME });
|
|
20
|
+
}
|
|
21
|
+
const dirents = await impl.readdir(realTarget, { withFileTypes: true });
|
|
22
|
+
const entries = [];
|
|
23
|
+
for (const dirent of dirents) {
|
|
24
|
+
const full = path.join(realTarget, dirent.name);
|
|
25
|
+
const isDirectory = dirent.isDirectory();
|
|
26
|
+
// Only regular files and directories are selectable; a socket or device node is never a private key.
|
|
27
|
+
if (!isDirectory && !dirent.isFile())
|
|
28
|
+
continue;
|
|
29
|
+
entries.push({ name: dirent.name, path: full, isDirectory });
|
|
30
|
+
}
|
|
31
|
+
entries.sort((left, right) => {
|
|
32
|
+
if (left.isDirectory !== right.isDirectory)
|
|
33
|
+
return left.isDirectory ? -1 : 1;
|
|
34
|
+
return left.name.localeCompare(right.name);
|
|
35
|
+
});
|
|
36
|
+
const shown = entries.slice(0, MAX_ENTRIES);
|
|
37
|
+
const parent = realTarget === realHome ? realHome : path.dirname(realTarget);
|
|
38
|
+
return { path: realTarget, parent, entries: shown, truncated: entries.length > shown.length };
|
|
39
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/** Read-only `~/.ssh/known_hosts` parsing and OpenSSH SHA256 fingerprints; hashed entries are counted, never guessed
|
|
2
|
+
* at. */
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
export function fingerprintFromBase64(keyBase64) {
|
|
5
|
+
return fingerprintFromBlob(Buffer.from(keyBase64, 'base64'));
|
|
6
|
+
}
|
|
7
|
+
/** OpenSSH's `SHA256:<base64, no padding>` form for one already-decoded key blob. */
|
|
8
|
+
export function fingerprintFromBlob(keyBlob) {
|
|
9
|
+
return `SHA256:${createHash('sha256').update(keyBlob).digest('base64').replace(/=+$/g, '')}`;
|
|
10
|
+
}
|
|
11
|
+
export function parseKnownHosts(text) {
|
|
12
|
+
const entries = [];
|
|
13
|
+
const malformed = [];
|
|
14
|
+
let hashedCount = 0;
|
|
15
|
+
let revoked = 0;
|
|
16
|
+
let certAuthority = 0;
|
|
17
|
+
const lines = String(text ?? '').split(/\r?\n/);
|
|
18
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
19
|
+
const lineNo = index + 1;
|
|
20
|
+
const raw = lines[index].trim();
|
|
21
|
+
if (raw === '' || raw.startsWith('#'))
|
|
22
|
+
continue;
|
|
23
|
+
const parts = raw.split(/\s+/);
|
|
24
|
+
let marker = null;
|
|
25
|
+
if (parts[0].startsWith('@'))
|
|
26
|
+
marker = parts.shift() ?? null;
|
|
27
|
+
if (parts.length < 3) {
|
|
28
|
+
malformed.push({ line: lineNo, text: raw });
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const [hostField, keyType, keyBase64] = parts;
|
|
32
|
+
if (marker === '@revoked') {
|
|
33
|
+
revoked += 1;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (marker === '@cert-authority') {
|
|
37
|
+
certAuthority += 1;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (hostField.startsWith('|1|')) {
|
|
41
|
+
hashedCount += 1;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (!/^[A-Za-z0-9+/=]+$/.test(keyBase64 ?? '')) {
|
|
45
|
+
malformed.push({ line: lineNo, text: raw });
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
entries.push({
|
|
49
|
+
hosts: hostField.split(',').filter(Boolean),
|
|
50
|
+
keyType,
|
|
51
|
+
keyBase64,
|
|
52
|
+
fingerprint: fingerprintFromBase64(keyBase64),
|
|
53
|
+
marker,
|
|
54
|
+
line: lineNo,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return { entries, hashedCount, malformed, revoked, certAuthority };
|
|
58
|
+
}
|
|
59
|
+
/** OpenSSH `known_hosts` host-field matching: `*` matches any run of characters, `?` exactly one, everything else is
|
|
60
|
+
* literal and case is ignored. No regular expression on purpose: a host field is data, and building a `RegExp` from it
|
|
61
|
+
* invites injection and backtracking on remote-controlled text. */
|
|
62
|
+
function wildcardMatch(pattern, value) {
|
|
63
|
+
const wanted = String(pattern).toLowerCase();
|
|
64
|
+
const text = String(value).toLowerCase();
|
|
65
|
+
let patternAt = 0;
|
|
66
|
+
let textAt = 0;
|
|
67
|
+
let starAt = -1;
|
|
68
|
+
let markAt = 0;
|
|
69
|
+
while (textAt < text.length) {
|
|
70
|
+
if (patternAt < wanted.length && (wanted[patternAt] === '?' || wanted[patternAt] === text[textAt])) {
|
|
71
|
+
patternAt += 1;
|
|
72
|
+
textAt += 1;
|
|
73
|
+
}
|
|
74
|
+
else if (patternAt < wanted.length && wanted[patternAt] === '*') {
|
|
75
|
+
starAt = patternAt;
|
|
76
|
+
patternAt += 1;
|
|
77
|
+
markAt = textAt;
|
|
78
|
+
}
|
|
79
|
+
else if (starAt >= 0) {
|
|
80
|
+
patternAt = starAt + 1;
|
|
81
|
+
textAt = markAt + 1;
|
|
82
|
+
markAt = textAt;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
while (patternAt < wanted.length && wanted[patternAt] === '*')
|
|
89
|
+
patternAt += 1;
|
|
90
|
+
return patternAt === wanted.length;
|
|
91
|
+
}
|
|
92
|
+
/** Whether one `known_hosts` host field covers `host` on `port`: OpenSSH writes a plain field only for port 22 and
|
|
93
|
+
* `[host]:port` for every other port, so a plain entry never vouches for a service on 2222. */
|
|
94
|
+
export function knownHostFieldMatches(pattern, host, port) {
|
|
95
|
+
const targetPort = Number(port) || 22;
|
|
96
|
+
return wildcardMatch(pattern, targetPort === 22 ? String(host) : `[${host}]:${targetPort}`);
|
|
97
|
+
}
|
|
98
|
+
export function findKnownHostEntries(parsed, host, port) {
|
|
99
|
+
return parsed.entries.filter((entry) => entry.hosts.some((pattern) => knownHostFieldMatches(pattern, host, port)));
|
|
100
|
+
}
|
|
101
|
+
export function summarizeKnownHosts(parsed) {
|
|
102
|
+
const hosts = parsed.entries.flatMap((entry) => entry.hosts).filter((host) => !host.startsWith('|1|'));
|
|
103
|
+
return { entries: parsed.entries.length, hashed: parsed.hashedCount, hosts: [...new Set(hosts)].sort().slice(0, 200) };
|
|
104
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Remote directory layout: everything the plugin writes lives under `$DSH_HOME/remote-go`, so a host gets only
|
|
2
|
+
* `~/.dsh` plus that one subdirectory. */
|
|
3
|
+
import { REMOTE_DSH_DIR, REMOTE_STATE_DIR } from './constants.js';
|
|
4
|
+
import { errors } from './errors.js';
|
|
5
|
+
export function remoteLayout(home) {
|
|
6
|
+
const base = typeof home === 'string' ? home.trim().replace(/\/+$/, '') : '';
|
|
7
|
+
if (!base.startsWith('/')) {
|
|
8
|
+
throw errors.internal(new Error(`the remote home directory is unknown (${JSON.stringify(home)}); refusing to build a "~" layout`));
|
|
9
|
+
}
|
|
10
|
+
const dshHome = `${base}/${REMOTE_DSH_DIR}`;
|
|
11
|
+
const root = `${dshHome}/${REMOTE_STATE_DIR}`;
|
|
12
|
+
return {
|
|
13
|
+
root,
|
|
14
|
+
stateHome: dshHome,
|
|
15
|
+
dshHome,
|
|
16
|
+
nodeDir: `${root}/node`,
|
|
17
|
+
downloadDir: `${root}/downloads`,
|
|
18
|
+
logsDir: `${root}/logs`,
|
|
19
|
+
logFile: `${root}/logs/web.log`,
|
|
20
|
+
runDir: `${root}/run`,
|
|
21
|
+
runFile: `${root}/run/web.json`,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export function layoutForRuntime(runtime) {
|
|
25
|
+
return remoteLayout(runtime?.facts?.home ?? runtime?.remoteHome ?? null);
|
|
26
|
+
}
|