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