dsh-remote-go 0.3.0 → 0.3.2
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 +1 -0
- package/dist/client.js +1348 -2095
- package/dist/src/admission.js +3 -28
- package/dist/src/api.js +37 -99
- package/dist/src/atomic-json.js +32 -66
- package/dist/src/bootstrap.js +17 -27
- package/dist/src/connect/attach.js +3 -25
- package/dist/src/connect/bootstrap.js +2 -8
- package/dist/src/connect/failures.js +3 -18
- package/dist/src/connect/guards.js +13 -8
- package/dist/src/connect/launch.js +11 -21
- package/dist/src/connect/links.js +11 -27
- package/dist/src/connect/ownership.js +33 -114
- package/dist/src/connect/pipeline.js +62 -159
- package/dist/src/connect/reattach.js +26 -44
- package/dist/src/connect/registry.js +0 -1
- package/dist/src/connect/repair.js +8 -37
- package/dist/src/connect/restart.js +36 -97
- package/dist/src/connect/stop.js +16 -50
- package/dist/src/connect/transport.js +10 -18
- package/dist/src/connect/verify.js +3 -12
- package/dist/src/connect/watchdog.js +6 -38
- package/dist/src/connect-log.js +16 -41
- package/dist/src/connect-progress.js +13 -45
- package/dist/src/constants.js +18 -43
- package/dist/src/context.js +13 -32
- package/dist/src/diagnostics.js +56 -25
- package/dist/src/download-jobs.js +11 -20
- package/dist/src/errors.js +175 -190
- package/dist/src/events.js +20 -84
- package/dist/src/host-lifecycle.js +69 -177
- package/dist/src/host-registry.js +27 -78
- package/dist/src/host-runtime.js +0 -9
- package/dist/src/host-view.js +6 -29
- package/dist/src/index.js +15 -35
- package/dist/src/kernel-adapter.js +34 -80
- package/dist/src/key-files.js +10 -23
- package/dist/src/known-hosts.js +13 -34
- package/dist/src/layout.js +3 -20
- package/dist/src/local-download.js +94 -163
- package/dist/src/local-ssh.js +12 -19
- package/dist/src/local-version.js +35 -49
- package/dist/src/node/download.js +1 -4
- package/dist/src/node-runtime.js +19 -38
- package/dist/src/probe.js +6 -7
- package/dist/src/prompts.js +56 -128
- package/dist/src/proxy/cookies.js +26 -60
- package/dist/src/proxy/headers.js +30 -125
- package/dist/src/proxy/http.js +33 -41
- package/dist/src/proxy/index.js +20 -34
- package/dist/src/proxy/limits.js +4 -6
- package/dist/src/proxy/routes.js +10 -17
- package/dist/src/proxy/upgrade.js +27 -42
- package/dist/src/redact.js +14 -33
- package/dist/src/remote-files.js +9 -21
- package/dist/src/remote-path.js +21 -62
- package/dist/src/remote-proc.js +35 -103
- package/dist/src/remote-web/registry.js +10 -50
- package/dist/src/remote-web/resolve.js +23 -104
- package/dist/src/remote-web/start.js +27 -64
- package/dist/src/remote-web/stop.js +6 -25
- package/dist/src/request-trust.js +15 -32
- package/dist/src/runtime/dispatch.js +33 -143
- package/dist/src/runtime/effects.js +7 -17
- package/dist/src/runtime/events.js +1 -1
- package/dist/src/runtime/fsm.js +11 -61
- package/dist/src/runtime/rules.js +121 -199
- package/dist/src/secrets.js +15 -38
- package/dist/src/service.js +22 -128
- package/dist/src/settings.js +41 -42
- package/dist/src/ssh-config.js +22 -41
- package/dist/src/ssh-failures.js +23 -74
- package/dist/src/ssh-sftp.js +47 -86
- package/dist/src/ssh.js +99 -179
- package/dist/src/store.js +31 -48
- package/dist/src/tool-shared.js +17 -52
- package/dist/src/tools-exec-policy.js +33 -79
- package/dist/src/tools-exec.js +87 -151
- package/dist/src/tools-report.js +100 -167
- package/dist/src/tools.js +59 -133
- package/dist/src/tunnel.js +9 -27
- package/dist/src/util.js +19 -56
- package/dist/src/version.js +6 -12
- package/dist/src/watchdog-bus.js +0 -8
- package/dist/src/watchdog.js +7 -47
- package/package.json +3 -3
|
@@ -1,76 +1,23 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* The proxy hands the browser a page from another origin without handing it a second origin, so
|
|
6
|
-
* `Host`/`Origin` are rewritten to the remote's own loopback authority (its authority-bound cookie
|
|
7
|
-
* and same-origin fence keep working), while the browser's own local credentials and the
|
|
8
|
-
* traffic-shaping headers of this hop stay here.
|
|
9
|
-
*/
|
|
1
|
+
/** Header rewriting for the same-origin proxy: an allowlist out, a blocklist back, and `Host`/`Origin` rewritten to the
|
|
2
|
+
* remote's own loopback authority, so its authority-bound cookie and same-origin fence keep working while the browser's
|
|
3
|
+
* local credentials and this hop's traffic-shaping headers stay here. */
|
|
10
4
|
import { REMOTE_VIEW_PREFIX } from '../constants.js';
|
|
11
5
|
import { cookieHeader } from './cookies.js';
|
|
6
|
+
/** One set of lowercase header names, written space-separated so a list stays visible at a glance. */
|
|
7
|
+
const headerSet = (names) => new Set(names.split(' '));
|
|
12
8
|
/** Headers that describe one hop and must not be copied to the other connection. */
|
|
13
|
-
const HOP_BY_HOP =
|
|
14
|
-
|
|
15
|
-
'keep-alive',
|
|
16
|
-
'proxy-authenticate',
|
|
17
|
-
'proxy-authorization',
|
|
18
|
-
'te',
|
|
19
|
-
'trailer',
|
|
20
|
-
'transfer-encoding',
|
|
21
|
-
'upgrade',
|
|
22
|
-
]);
|
|
23
|
-
/**
|
|
24
|
-
* The request headers this proxy forwards. This is an allowlist on purpose: the browser's own
|
|
25
|
-
* credentials belong to the *local* origin (`authorization`, `cookie`, `x-dsh-*`) and the
|
|
26
|
-
* traffic-shaping headers belong to this hop (`forwarded`, `x-forwarded-*`, `sec-fetch-*`).
|
|
27
|
-
* A blacklist forwarded them all to a host that the remote controls.
|
|
28
|
-
*/
|
|
29
|
-
const FORWARDED_REQUEST_HEADERS = new Set([
|
|
30
|
-
'accept',
|
|
31
|
-
'accept-encoding',
|
|
32
|
-
'accept-language',
|
|
33
|
-
'cache-control',
|
|
34
|
-
'content-length',
|
|
35
|
-
'content-type',
|
|
36
|
-
'range',
|
|
37
|
-
'user-agent',
|
|
38
|
-
'x-requested-with',
|
|
39
|
-
]);
|
|
9
|
+
const HOP_BY_HOP = headerSet('connection keep-alive proxy-authenticate proxy-authorization te trailer transfer-encoding upgrade');
|
|
10
|
+
const FORWARDED_REQUEST_HEADERS = headerSet('accept accept-encoding accept-language cache-control content-length content-type range user-agent x-requested-with');
|
|
40
11
|
/** Conditional-request headers: `if-match`, `if-none-match`, `if-modified-since`, `if-range`, … */
|
|
41
12
|
const FORWARDED_REQUEST_PREFIX = 'if-';
|
|
42
13
|
/** The fields a WebSocket handshake needs, forwarded only on an upgrade. */
|
|
43
|
-
const UPGRADE_REQUEST_HEADERS =
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
'sec-websocket-protocol',
|
|
49
|
-
'sec-websocket-extensions',
|
|
50
|
-
]);
|
|
51
|
-
/**
|
|
52
|
-
* Response headers a remote page may not set on the local origin. `clear-site-data` would let a
|
|
53
|
-
* compromised remote wipe this GUI's own cookies and storage, `access-control-*` would open the
|
|
54
|
-
* local origin to other sites, and `refresh` / `content-location` / `report-to` / `nel` /
|
|
55
|
-
* `permissions-policy` / `alt-svc` redirect or reshape the browser's view of a server it does
|
|
56
|
-
* not own.
|
|
57
|
-
*/
|
|
58
|
-
const BLOCKED_RESPONSE_HEADERS = new Set([
|
|
59
|
-
'clear-site-data',
|
|
60
|
-
'refresh',
|
|
61
|
-
'content-location',
|
|
62
|
-
'report-to',
|
|
63
|
-
'nel',
|
|
64
|
-
'permissions-policy',
|
|
65
|
-
'alt-svc',
|
|
66
|
-
]);
|
|
14
|
+
const UPGRADE_REQUEST_HEADERS = headerSet('connection upgrade sec-websocket-key sec-websocket-version sec-websocket-protocol sec-websocket-extensions');
|
|
15
|
+
// The request side is an allowlist on purpose: a blocklist forwarded the browser's local credentials (`authorization`,
|
|
16
|
+
// `cookie`, `x-dsh-*`) and this hop's `forwarded`/`sec-fetch-*` headers to a host the remote controls. The response
|
|
17
|
+
// side blocks what would let a remote reshape this origin (`clear-site-data`, `access-control-*`).
|
|
18
|
+
const BLOCKED_RESPONSE_HEADERS = headerSet('clear-site-data refresh content-location report-to nel permissions-policy alt-svc');
|
|
67
19
|
/** Every `access-control-*` response header belongs to the remote's origin, not this one. */
|
|
68
20
|
const BLOCKED_RESPONSE_PREFIX = 'access-control-';
|
|
69
|
-
/**
|
|
70
|
-
* Whether one browser request header may reach the remote.
|
|
71
|
-
* @param name - lowercase header name.
|
|
72
|
-
* @param preserveUpgrade - whether this is a WebSocket handshake.
|
|
73
|
-
*/
|
|
74
21
|
export function forwardableRequestHeader(name, preserveUpgrade = false) {
|
|
75
22
|
if (FORWARDED_REQUEST_HEADERS.has(name))
|
|
76
23
|
return true;
|
|
@@ -78,16 +25,10 @@ export function forwardableRequestHeader(name, preserveUpgrade = false) {
|
|
|
78
25
|
return true;
|
|
79
26
|
return preserveUpgrade === true && UPGRADE_REQUEST_HEADERS.has(name);
|
|
80
27
|
}
|
|
81
|
-
/**
|
|
82
|
-
* Whether one upstream response header may reach the browser.
|
|
83
|
-
* @param name - lowercase header name.
|
|
84
|
-
*/
|
|
85
28
|
export function forwardableResponseHeader(name) {
|
|
86
29
|
if (BLOCKED_RESPONSE_HEADERS.has(name))
|
|
87
30
|
return false;
|
|
88
|
-
|
|
89
|
-
return false;
|
|
90
|
-
return true;
|
|
31
|
+
return !name.startsWith(BLOCKED_RESPONSE_PREFIX);
|
|
91
32
|
}
|
|
92
33
|
/** Absolute origin of the remote's own listener, as the remote sees itself through the tunnel. */
|
|
93
34
|
export function remoteOrigin(target) {
|
|
@@ -100,18 +41,15 @@ export function remoteAuthority(target) {
|
|
|
100
41
|
}
|
|
101
42
|
/** Rewrite an absolute URL from the local origin to the remote's origin, keeping its path. */
|
|
102
43
|
function rewriteSameOrigin(value, localOrigin, target) {
|
|
103
|
-
const local = typeof localOrigin === 'string' ? localOrigin.replace(/\/$/, '')
|
|
44
|
+
const local = (typeof localOrigin === 'string' ? localOrigin : '').replace(/\/$/, '');
|
|
104
45
|
const text = String(value);
|
|
105
46
|
if (local !== '' && (text === local || text.startsWith(`${local}/`))) {
|
|
106
47
|
return remoteOrigin(target) + text.slice(local.length);
|
|
107
48
|
}
|
|
108
49
|
return text;
|
|
109
50
|
}
|
|
110
|
-
/**
|
|
111
|
-
*
|
|
112
|
-
* authority fields are rewritten, and only this host's own cookies are added.
|
|
113
|
-
* @param rawHeaders - node raw header list.
|
|
114
|
-
*/
|
|
51
|
+
/** Request headers for the upstream: allowlisted content headers, rewritten `Host`/`Origin`/`Referer`, this host's
|
|
52
|
+
* cookies — nothing else. */
|
|
115
53
|
export function upstreamHeaders(rawHeaders, input) {
|
|
116
54
|
const out = {};
|
|
117
55
|
const preserveUpgrade = input.preserveUpgrade === true;
|
|
@@ -138,12 +76,8 @@ export function upstreamHeaders(rawHeaders, input) {
|
|
|
138
76
|
out.cookie = cookies;
|
|
139
77
|
return out;
|
|
140
78
|
}
|
|
141
|
-
/**
|
|
142
|
-
*
|
|
143
|
-
* and a hostile-looking directive would otherwise make a hardened remote unusable in a browser.
|
|
144
|
-
* Every other directive stays exactly as the remote sent it.
|
|
145
|
-
* @param value - one Content-Security-Policy value.
|
|
146
|
-
*/
|
|
79
|
+
/** Drop only the `frame-ancestors` CSP directive — the proxy frames that one page, so a hardened remote stays usable;
|
|
80
|
+
* every other directive goes out as sent. */
|
|
147
81
|
export function withoutFrameAncestors(value) {
|
|
148
82
|
return String(value)
|
|
149
83
|
.split(';')
|
|
@@ -152,19 +86,9 @@ export function withoutFrameAncestors(value) {
|
|
|
152
86
|
.filter((directive) => directive !== '')
|
|
153
87
|
.join('; ');
|
|
154
88
|
}
|
|
155
|
-
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
* A redirect that names the remote's own loopback listener is folded back under the view
|
|
159
|
-
* prefix, so a form post or a token exchange stays inside the frame. Everything else that is an
|
|
160
|
-
* absolute URL — another host, another scheme, a protocol-relative `//evil.example`, or a
|
|
161
|
-
* different loopback port — is refused (`null`): the header is dropped, because navigation
|
|
162
|
-
* inside this same-origin frame must never leave the seat the user opened. A relative
|
|
163
|
-
* reference (`.`, `./next`, `/assets/app.js`) is already resolved against that seat, so it is
|
|
164
|
-
* passed through untouched.
|
|
165
|
-
* @param value - one upstream Location header value.
|
|
166
|
-
* @returns the header value to send, or null to drop the header.
|
|
167
|
-
*/
|
|
89
|
+
/** Rewrite one upstream `Location` for the browser: one naming the remote's loopback listener folds back under the view
|
|
90
|
+
* prefix (a form post or token exchange stays in-frame); any other absolute target — host, scheme, `//evil.example`,
|
|
91
|
+
* another loopback port — is refused (`null` drops it), since this frame must never leave the seat the user opened. */
|
|
168
92
|
export function safeLocation(value, input = {}) {
|
|
169
93
|
const text = String(value ?? '')
|
|
170
94
|
.replace(/[\r\n]/g, '')
|
|
@@ -184,10 +108,7 @@ export function safeLocation(value, input = {}) {
|
|
|
184
108
|
// Any scheme (`https:`, `javascript:`, `data:`…) is an absolute target.
|
|
185
109
|
if (/^[a-z][a-z0-9+.-]*:/i.test(text))
|
|
186
110
|
return null;
|
|
187
|
-
// A backslash is a slash
|
|
188
|
-
// `\/evil.example` both resolve to `http://evil.example/`, so a surviving backslash in what
|
|
189
|
-
// looks like a relative reference is an off-origin escape. (`%5c` stays percent-encoded and is
|
|
190
|
-
// not a separator, so it is not caught here.)
|
|
111
|
+
// A backslash is a slash for special schemes (`/\evil.example` resolves to http://evil.example/): off-origin.
|
|
191
112
|
if (text.includes('\\'))
|
|
192
113
|
return null;
|
|
193
114
|
// `//host/…`: a browser reads it as protocol-relative, i.e. off-origin.
|
|
@@ -195,22 +116,15 @@ export function safeLocation(value, input = {}) {
|
|
|
195
116
|
return null;
|
|
196
117
|
return text;
|
|
197
118
|
}
|
|
198
|
-
/**
|
|
199
|
-
*
|
|
200
|
-
* remote's cookies (they go to the jar), any frame denial the remote inherited from another
|
|
201
|
-
* deployment, and browser-policy headers the remote must not set on the local origin. A
|
|
202
|
-
* `Location` that names the remote's own listener is folded back under the view prefix; any
|
|
203
|
-
* other absolute target is dropped so a redirect cannot slip out of the frame.
|
|
204
|
-
* @param rawHeaders - upstream raw header list.
|
|
205
|
-
* @returns flat [name, value, …] pairs.
|
|
206
|
-
*/
|
|
119
|
+
/** Response headers for the browser: drop hop-by-hop fields, this connection's framing, the remote's cookies (to the
|
|
120
|
+
* jar), inherited frame denials and browser-policy headers; `Location` is rewritten as `safeLocation` describes. */
|
|
207
121
|
export function responseHeaders(rawHeaders, input = {}) {
|
|
208
122
|
const out = [];
|
|
209
123
|
const preserveUpgrade = input.preserveUpgrade === true;
|
|
210
124
|
for (let at = 0; at < rawHeaders.length; at += 2) {
|
|
211
125
|
const name = String(rawHeaders[at]).toLowerCase();
|
|
212
|
-
// `Connection`/`Upgrade` are hop-by-hop, except in the response to a WebSocket handshake:
|
|
213
|
-
//
|
|
126
|
+
// `Connection`/`Upgrade` are hop-by-hop, except in the response to a WebSocket handshake: dropping them there
|
|
127
|
+
// would turn a valid 101 into a protocol error.
|
|
214
128
|
if (HOP_BY_HOP.has(name) && !(preserveUpgrade && (name === 'connection' || name === 'upgrade')))
|
|
215
129
|
continue;
|
|
216
130
|
if (name === 'set-cookie' || name === 'x-frame-options')
|
|
@@ -223,25 +137,16 @@ export function responseHeaders(rawHeaders, input = {}) {
|
|
|
223
137
|
}
|
|
224
138
|
if (name === 'location') {
|
|
225
139
|
const location = safeLocation(rawHeaders[at + 1], input);
|
|
226
|
-
if (location
|
|
227
|
-
|
|
228
|
-
out.push(rawHeaders[at], location);
|
|
140
|
+
if (location !== null)
|
|
141
|
+
out.push(rawHeaders[at], location);
|
|
229
142
|
continue;
|
|
230
143
|
}
|
|
231
144
|
out.push(rawHeaders[at], rawHeaders[at + 1]);
|
|
232
145
|
}
|
|
233
146
|
return out;
|
|
234
147
|
}
|
|
235
|
-
/**
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
* The upgrade path forwards a 101 byte-for-byte if left alone, which also forwarded
|
|
239
|
-
* `Set-Cookie` and every browser-policy header the remote chose to attach to the handshake. The
|
|
240
|
-
* same policy as `responseHeaders` applies here; only the status line and the header block are
|
|
241
|
-
* touched, never the frames that follow.
|
|
242
|
-
* @param headText - latin1-decoded head, `HTTP/1.1 101 …\r\nkey: value…`.
|
|
243
|
-
* @returns the filtered head (without the trailing blank line), or '' when unparseable.
|
|
244
|
-
*/
|
|
148
|
+
/** Rewrite one raw upstream response head (status line + headers, without the final blank line). The upgrade path would
|
|
149
|
+
* otherwise forward a 101 byte-for-byte, `Set-Cookie` and browser policy included; frames are never touched. */
|
|
245
150
|
export function filterResponseHead(headText, input = {}) {
|
|
246
151
|
const lines = String(headText ?? '').split('\r\n');
|
|
247
152
|
const statusLine = lines.shift() ?? '';
|
package/dist/src/proxy/http.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* resolution, the upstream forward with its 30 s deadline, and the seven plain-text refusals.
|
|
4
|
-
*/
|
|
1
|
+
/** The HTTP half of the same-origin proxy: admission, the local same-origin fence, view-path resolution, the upstream
|
|
2
|
+
* forward with its 30 s deadline, and the seven plain-text refusals. */
|
|
5
3
|
import http from 'node:http';
|
|
6
4
|
import { trustedRequest } from '../request-trust.js';
|
|
7
5
|
import { storeCookies } from './cookies.js';
|
|
@@ -22,33 +20,30 @@ function plain(res, status, text) {
|
|
|
22
20
|
// swallow-ok: the client may already have closed the response
|
|
23
21
|
}
|
|
24
22
|
}
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
/** The one gate both halves run before anything is proxied: teardown, DSH's own admission (first, because the proxy reaches a connected host's remote session), the local origin and then the same-origin fence. No refusal says whether a seat exists. */
|
|
24
|
+
export function admitSeat(rt, req) {
|
|
25
|
+
if (rt.disposed())
|
|
26
|
+
return { ok: false, status: 503, text: 'proxy disposed', admission: false };
|
|
27
|
+
const rejection = rt.admit(req);
|
|
28
|
+
if (rejection !== undefined) {
|
|
29
|
+
return { ok: false, status: rejection, text: 'request rejected by DSH admission', admission: true };
|
|
30
|
+
}
|
|
31
|
+
const origin = rt.localOrigin();
|
|
32
|
+
if (origin === null)
|
|
33
|
+
return { ok: false, status: 503, text: 'local origin is not ready', admission: false };
|
|
34
|
+
if (!trustedRequest(req, origin)) {
|
|
35
|
+
return { ok: false, status: 403, text: 'cross-site request rejected', admission: false };
|
|
36
|
+
}
|
|
37
|
+
return { ok: true, origin };
|
|
38
|
+
}
|
|
39
|
+
/** Create the HTTP handler of one proxy activation. Every request under the view prefix is stripped of the prefix and
|
|
40
|
+
* forwarded to that host's loopback tunnel; a host that is not currently connected is a 404, and DSH's own admission
|
|
41
|
+
* plus the local same-origin fence decide whether the caller may use the seat at all. */
|
|
32
42
|
export function createHttpHandler(rt) {
|
|
33
43
|
return function handle(req, res) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
// DSH's own admission gate runs before anything this module decides: the proxy reaches a
|
|
39
|
-
// connected host's remote session (its cookie jar), so only an admitted caller may use it.
|
|
40
|
-
const rejection = rt.admit(req);
|
|
41
|
-
if (rejection !== undefined) {
|
|
42
|
-
plain(res, rejection, 'request rejected by DSH admission');
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const origin = rt.localOrigin();
|
|
46
|
-
if (origin === null) {
|
|
47
|
-
plain(res, 503, 'local origin is not ready');
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
if (!trustedRequest(req, origin)) {
|
|
51
|
-
plain(res, 403, 'cross-site request rejected');
|
|
44
|
+
const seat = admitSeat(rt, req);
|
|
45
|
+
if (seat.ok === false) {
|
|
46
|
+
plain(res, seat.status, seat.text);
|
|
52
47
|
return;
|
|
53
48
|
}
|
|
54
49
|
let pathname;
|
|
@@ -76,15 +71,15 @@ export function createHttpHandler(rt) {
|
|
|
76
71
|
const upstreamPath = parsed.path + search;
|
|
77
72
|
const headers = upstreamHeaders(req.rawHeaders, {
|
|
78
73
|
target,
|
|
79
|
-
localOrigin: origin.origin,
|
|
74
|
+
localOrigin: seat.origin.origin,
|
|
80
75
|
path: parsed.path,
|
|
81
76
|
jar,
|
|
82
77
|
});
|
|
83
|
-
// The remote is reached over a tunnel; a listener that accepts and never answers used to
|
|
84
|
-
// hang the local request forever. The deadline covers the wait for response headers only —
|
|
85
|
-
// once the response started, an idle stream (SSE, a large asset) is legitimate.
|
|
86
78
|
let answered = false;
|
|
87
79
|
let failed = false;
|
|
80
|
+
// The remote is reached over a tunnel; a listener that accepts and never answers used to hang the local request
|
|
81
|
+
// forever. The deadline covers the wait for response headers only — once the response started, an idle stream
|
|
82
|
+
// (SSE, a large asset) is legitimate.
|
|
88
83
|
const upstream = http.request({
|
|
89
84
|
host: '127.0.0.1',
|
|
90
85
|
port: target.localPort,
|
|
@@ -110,12 +105,9 @@ export function createHttpHandler(rt) {
|
|
|
110
105
|
res.once('close', () => response.destroy());
|
|
111
106
|
response.pipe(res);
|
|
112
107
|
});
|
|
113
|
-
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* request-driven, so CodeQL treats its error as user-controlled (log injection). The
|
|
117
|
-
* loopback port names the host to an operator, and the browser only ever sees the 502.
|
|
118
|
-
*/
|
|
108
|
+
/** One failed upstream: report it once and answer 502 (or nothing, when the response has already started). Nothing
|
|
109
|
+
* from the failure reaches the log line: the emitter is request-driven, so CodeQL treats its error as
|
|
110
|
+
* user-controlled (log injection). The loopback port names the host to an operator; the browser sees the 502. */
|
|
119
111
|
const upstreamFailed = () => {
|
|
120
112
|
if (failed)
|
|
121
113
|
return;
|
|
@@ -132,8 +124,8 @@ export function createHttpHandler(rt) {
|
|
|
132
124
|
upstreamFailed();
|
|
133
125
|
});
|
|
134
126
|
upstream.on('error', () => {
|
|
135
|
-
// A failure after the response started is still worth reporting (the Host confirms it
|
|
136
|
-
//
|
|
127
|
+
// A failure after the response started is still worth reporting (the Host confirms it with probes); `plain`
|
|
128
|
+
// is a no-op once the headers are out.
|
|
137
129
|
upstreamFailed();
|
|
138
130
|
});
|
|
139
131
|
req.once('aborted', () => upstream.destroy());
|
package/dist/src/proxy/index.js
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
import { REMOTE_VIEW_PREFIX } from '../constants.js';
|
|
2
|
-
import { createJarBook } from './cookies.js';
|
|
3
|
-
import { cookieHeader } from './cookies.js';
|
|
2
|
+
import { createJarBook, cookieHeader } from './cookies.js';
|
|
4
3
|
import { remoteAuthority } from './headers.js';
|
|
5
4
|
import { createHttpHandler } from './http.js';
|
|
6
5
|
import { UPSTREAM_TIMEOUT_MS } from './limits.js';
|
|
7
6
|
import { HOST_ID_PATTERN, upgradePath } from './routes.js';
|
|
8
7
|
import { createUpgradeHandler } from './upgrade.js';
|
|
9
|
-
/**
|
|
10
|
-
*
|
|
11
|
-
* connected host (`{ localPort, remotePort }`) or null. Nothing else in this module knows how a
|
|
12
|
-
* tunnel was made.
|
|
13
|
-
*/
|
|
8
|
+
/** Create the proxy. `resolveHost` is the single pivot: the loopback target for one connected host, or null — nothing
|
|
9
|
+
* else in this module knows how a tunnel was made. */
|
|
14
10
|
export function createRemoteProxy(options) {
|
|
15
11
|
const logger = options.logger ?? console;
|
|
16
12
|
const webServer = options.webServer;
|
|
17
13
|
const getOrigin = options.getOrigin;
|
|
18
14
|
const resolveHost = options.resolveHost;
|
|
19
|
-
// Annotated because the default
|
|
20
|
-
//
|
|
15
|
+
// Annotated because the default must answer like the real gate: a kernel may provide no Connection service at all,
|
|
16
|
+
// and a missing gate admits the request.
|
|
21
17
|
const admit = typeof options.admit === 'function' ? options.admit : () => undefined;
|
|
22
18
|
const onUpstreamError = options.onUpstreamError;
|
|
23
19
|
const upstreamTimeoutMs = Number.isSafeInteger(options.upstreamTimeoutMs) && options.upstreamTimeoutMs > 0
|
|
@@ -27,15 +23,6 @@ export function createRemoteProxy(options) {
|
|
|
27
23
|
const upgrades = new Map();
|
|
28
24
|
const sockets = new Set();
|
|
29
25
|
let disposed = false;
|
|
30
|
-
/** Report one failed upstream without letting the reporter break the proxy. */
|
|
31
|
-
function reportUpstreamFailure(hostId) {
|
|
32
|
-
try {
|
|
33
|
-
onUpstreamError?.(hostId);
|
|
34
|
-
}
|
|
35
|
-
catch {
|
|
36
|
-
// swallow-ok: reporting only feeds the liveness confirmation, it must never fail a request
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
26
|
function localOrigin() {
|
|
40
27
|
try {
|
|
41
28
|
const origin = getOrigin();
|
|
@@ -62,22 +49,25 @@ export function createRemoteProxy(options) {
|
|
|
62
49
|
localOrigin,
|
|
63
50
|
targetOf,
|
|
64
51
|
jarFor: (hostId, target) => jars.jarFor(hostId, target),
|
|
65
|
-
|
|
52
|
+
// Report one failed upstream without letting the reporter break the proxy: it only feeds the liveness
|
|
53
|
+
// confirmation, so a throwing reporter must never fail the request.
|
|
54
|
+
reportUpstreamFailure(hostId) {
|
|
55
|
+
try {
|
|
56
|
+
onUpstreamError?.(hostId);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// swallow-ok: reporting only feeds the liveness confirmation
|
|
60
|
+
}
|
|
61
|
+
},
|
|
66
62
|
upstreamTimeoutMs,
|
|
67
63
|
};
|
|
68
64
|
const handle = createHttpHandler(runtime);
|
|
69
65
|
const handleUpgrade = createUpgradeHandler(runtime, {
|
|
70
|
-
add(socket)
|
|
71
|
-
|
|
72
|
-
},
|
|
73
|
-
delete(socket) {
|
|
74
|
-
sockets.delete(socket);
|
|
75
|
-
},
|
|
66
|
+
add: (socket) => void sockets.add(socket),
|
|
67
|
+
delete: (socket) => void sockets.delete(socket),
|
|
76
68
|
});
|
|
77
|
-
/**
|
|
78
|
-
*
|
|
79
|
-
* that disappears loses its route and its cookies.
|
|
80
|
-
*/
|
|
69
|
+
/** Keep one upgrade route per configured host, called on init and on every state change: a host that disappears
|
|
70
|
+
* loses its route and its cookies. */
|
|
81
71
|
function sync(hostIds) {
|
|
82
72
|
if (disposed)
|
|
83
73
|
return;
|
|
@@ -105,11 +95,7 @@ export function createRemoteProxy(options) {
|
|
|
105
95
|
}
|
|
106
96
|
return {
|
|
107
97
|
prefix: REMOTE_VIEW_PREFIX,
|
|
108
|
-
|
|
109
|
-
* The route registration path. The web server matches a prefix as `pathname === prefix ||
|
|
110
|
-
* pathname.startsWith(prefix + '/')`, so registering the trailing-slash seat prefix verbatim
|
|
111
|
-
* would never match a real view URL.
|
|
112
|
-
*/
|
|
98
|
+
// The web server matches `pathname === prefix || pathname.startsWith(prefix + '/')`, so the trailing slash must go.
|
|
113
99
|
routePath: REMOTE_VIEW_PREFIX.replace(/\/$/, ''),
|
|
114
100
|
handle,
|
|
115
101
|
handleUpgrade,
|
package/dist/src/proxy/limits.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
*/
|
|
5
|
-
/** How long the remote may take to accept a connection and answer; see `createRemoteProxy`. */
|
|
1
|
+
/** The two ceilings one upstream connection is held to: how long the remote may take to answer, and how large a
|
|
2
|
+
* handshake response head may grow before it can no longer be a WebSocket upgrade. */
|
|
3
|
+
/** How long the remote may take to accept a connection and answer. */
|
|
6
4
|
export const UPSTREAM_TIMEOUT_MS = 30_000;
|
|
7
|
-
/** A handshake response head larger than this
|
|
5
|
+
/** A handshake response head larger than this cannot be a WebSocket upgrade; it is dropped. */
|
|
8
6
|
export const MAX_UPSTREAM_HEAD_BYTES = 64 * 1024;
|
package/dist/src/proxy/routes.js
CHANGED
|
@@ -1,37 +1,30 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* upgrade pathname one host's stream arrives on.
|
|
4
|
-
*/
|
|
1
|
+
/** The URL shapes of the same-origin proxy: which view path names which host, and the exact upgrade pathname one host's
|
|
2
|
+
* stream arrives on. */
|
|
5
3
|
import { REMOTE_VIEW_PREFIX } from '../constants.js';
|
|
6
|
-
/**
|
|
4
|
+
/** The remote web UI's own stream route — the only upgrade path a DSH web serves. */
|
|
7
5
|
const REMOTE_STREAM_PATH = '/api/remote.mux';
|
|
8
6
|
/** One host id, as the view prefix and the run registry spell it. */
|
|
9
7
|
export const HOST_ID_PATTERN = /^[A-Za-z0-9_-]{1,64}$/;
|
|
10
|
-
/**
|
|
11
|
-
*
|
|
12
|
-
* Returns null for anything that is not one well-formed view path (encoded slashes included).
|
|
13
|
-
* @param pathname - URL pathname; anything else is refused, so the guard stays.
|
|
14
|
-
*/
|
|
8
|
+
/** Split `/dsh-remote-go/remote/<hostId>/<rest>` into its host and the upstream path. Returns null for anything that is
|
|
9
|
+
* not one well-formed view path, encoded slashes included. */
|
|
15
10
|
export function parseViewPath(pathname) {
|
|
16
11
|
if (typeof pathname !== 'string' || !pathname.startsWith(REMOTE_VIEW_PREFIX))
|
|
17
12
|
return null;
|
|
18
13
|
const rest = pathname.slice(REMOTE_VIEW_PREFIX.length);
|
|
14
|
+
// The seat is always `<prefix><hostId>/`: the bare host id is not a page.
|
|
19
15
|
const slash = rest.indexOf('/');
|
|
20
|
-
|
|
16
|
+
if (slash === -1)
|
|
17
|
+
return null;
|
|
21
18
|
let hostId;
|
|
22
19
|
try {
|
|
23
|
-
hostId = decodeURIComponent(
|
|
20
|
+
hostId = decodeURIComponent(rest.slice(0, slash));
|
|
24
21
|
}
|
|
25
22
|
catch {
|
|
26
23
|
return null;
|
|
27
24
|
}
|
|
28
25
|
if (!HOST_ID_PATTERN.test(hostId))
|
|
29
26
|
return null;
|
|
30
|
-
|
|
31
|
-
if (slash === -1)
|
|
32
|
-
return null;
|
|
33
|
-
const upstream = rest.slice(slash);
|
|
34
|
-
return { hostId, path: upstream === '' ? '/' : upstream };
|
|
27
|
+
return { hostId, path: rest.slice(slash) };
|
|
35
28
|
}
|
|
36
29
|
/** Exact pathname the remote's stream upgrade arrives on. */
|
|
37
30
|
export function upgradePath(hostId) {
|
|
@@ -1,51 +1,39 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* is admitted and fenced like an HTTP request, the upstream's response head is filtered before
|
|
7
|
-
* the browser sees it (a 101 used to be forwarded byte-for-byte, also handing the remote's
|
|
8
|
-
* `Set-Cookie` to the local origin), and both directions stream untouched afterwards.
|
|
9
|
-
*/
|
|
1
|
+
/** The WebSocket half of the same-origin proxy: the one WebSocket route a remote DSH owns (`/api/remote.mux`) gets an
|
|
2
|
+
* exact upgrade route per connected host, because the local web server matches upgrades by exact pathname. The
|
|
3
|
+
* handshake is admitted and fenced like an HTTP request, the upstream head is filtered before the browser sees it (a
|
|
4
|
+
* 101 used to be forwarded byte-for-byte, handing the remote's `Set-Cookie` to the local origin), and both directions
|
|
5
|
+
* then stream untouched. */
|
|
10
6
|
import http from 'node:http';
|
|
11
7
|
import net from 'node:net';
|
|
12
|
-
import { trustedRequest } from '../request-trust.js';
|
|
13
8
|
import { filterResponseHead, remoteOrigin, upstreamHeaders } from './headers.js';
|
|
9
|
+
import { admitSeat } from './http.js';
|
|
14
10
|
import { MAX_UPSTREAM_HEAD_BYTES } from './limits.js';
|
|
15
11
|
import { parseViewPath } from './routes.js';
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* An upgrade owns its socket, so every refusal here either answers the handshake with a status and
|
|
20
|
-
* closes it (an admission rejection) or destroys it; nothing falls back to an HTTP response.
|
|
21
|
-
*/
|
|
12
|
+
/** Create the upgrade handler of one proxy activation. An upgrade owns its socket, so every refusal here either answers
|
|
13
|
+
* the handshake with a status and closes it (an admission rejection) or destroys it; nothing falls back to an HTTP
|
|
14
|
+
* response. */
|
|
22
15
|
export function createUpgradeHandler(rt, track) {
|
|
23
16
|
/** One exact upgrade route per host: the only WebSocket a DSH web serves is its stream mux. */
|
|
24
17
|
return function handleUpgrade(hostId, req, socket, head) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
const seat = admitSeat(rt, req);
|
|
19
|
+
if (seat.ok === false) {
|
|
20
|
+
if (seat.admission === false) {
|
|
21
|
+
socket.destroy();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
31
24
|
// An upgrade owns its socket: answer the handshake with the status, then close it.
|
|
32
25
|
socket.on('error', () => { });
|
|
33
|
-
socket.end(`HTTP/1.1 ${
|
|
26
|
+
socket.end(`HTTP/1.1 ${seat.status} ${http.STATUS_CODES[seat.status] ?? 'Rejected'}\r\n` +
|
|
34
27
|
'connection: close\r\ncontent-length: 0\r\n\r\n');
|
|
35
28
|
return;
|
|
36
29
|
}
|
|
37
|
-
const origin = rt.localOrigin();
|
|
38
|
-
if (origin === null || !trustedRequest(req, origin)) {
|
|
39
|
-
socket.destroy();
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
30
|
const target = rt.targetOf(hostId);
|
|
43
31
|
if (target === null) {
|
|
44
32
|
socket.destroy();
|
|
45
33
|
return;
|
|
46
34
|
}
|
|
47
|
-
// A malformed request target (absolute-form, broken escape) must not throw out of the upgrade
|
|
48
|
-
//
|
|
35
|
+
// A malformed request target (absolute-form, broken escape) must not throw out of the upgrade handler: the HTTP
|
|
36
|
+
// path already degrades to a 400, this one just drops the socket.
|
|
49
37
|
let parsed = null;
|
|
50
38
|
try {
|
|
51
39
|
parsed = parseViewPath(new URL(req.url ?? '/', 'http://127.0.0.1').pathname);
|
|
@@ -57,12 +45,11 @@ export function createUpgradeHandler(rt, track) {
|
|
|
57
45
|
socket.destroy();
|
|
58
46
|
return;
|
|
59
47
|
}
|
|
60
|
-
const jar = rt.jarFor(hostId, target);
|
|
61
48
|
const headers = upstreamHeaders(req.rawHeaders, {
|
|
62
49
|
target,
|
|
63
|
-
localOrigin: origin.origin,
|
|
50
|
+
localOrigin: seat.origin.origin,
|
|
64
51
|
path: parsed.path,
|
|
65
|
-
jar,
|
|
52
|
+
jar: rt.jarFor(hostId, target),
|
|
66
53
|
preserveUpgrade: true,
|
|
67
54
|
});
|
|
68
55
|
let raw = `${req.method} ${parsed.path} HTTP/1.1\r\n`;
|
|
@@ -72,9 +59,9 @@ export function createUpgradeHandler(rt, track) {
|
|
|
72
59
|
}
|
|
73
60
|
raw += '\r\n';
|
|
74
61
|
const upstream = net.connect({ port: target.localPort, host: '127.0.0.1', timeout: rt.upstreamTimeoutMs });
|
|
75
|
-
// An upgrade owns its socket, so the deadline covers the handshake only: an upstream that
|
|
76
|
-
// accepts and never answers would otherwise leave both sockets dangling forever.
|
|
77
62
|
let settled = false;
|
|
63
|
+
// An upgrade owns its socket, so the deadline covers the handshake only: an upstream that accepts and never
|
|
64
|
+
// answers would otherwise leave both sockets dangling forever.
|
|
78
65
|
const handshakeTimer = setTimeout(() => failUpgrade(), rt.upstreamTimeoutMs);
|
|
79
66
|
handshakeTimer.unref?.();
|
|
80
67
|
function failUpgrade() {
|
|
@@ -87,9 +74,9 @@ export function createUpgradeHandler(rt, track) {
|
|
|
87
74
|
rt.reportUpstreamFailure(hostId);
|
|
88
75
|
}
|
|
89
76
|
track.add(socket);
|
|
90
|
-
// The upstream's response head is filtered before the browser sees it: a 101 used to be
|
|
91
|
-
//
|
|
92
|
-
//
|
|
77
|
+
// The upstream's response head is filtered before the browser sees it: a 101 used to be forwarded byte-for-byte,
|
|
78
|
+
// which also handed the remote's `Set-Cookie` and policy headers to the local origin. Only the head is parsed;
|
|
79
|
+
// everything after it streams untouched.
|
|
93
80
|
let upstreamHead = Buffer.alloc(0);
|
|
94
81
|
let headForwarded = false;
|
|
95
82
|
const writeToClient = (chunk) => {
|
|
@@ -133,10 +120,8 @@ export function createUpgradeHandler(rt, track) {
|
|
|
133
120
|
socket.on('drain', () => upstream.resume());
|
|
134
121
|
upstream.on('timeout', () => failUpgrade());
|
|
135
122
|
upstream.on('error', () => failUpgrade());
|
|
136
|
-
/**
|
|
137
|
-
*
|
|
138
|
-
* the head is parsed here, so the end of the upstream must still close the browser side or
|
|
139
|
-
* the page would hold a dead socket forever.
|
|
123
|
+
/** The remote ended the stream. `upstream.pipe(socket)` used to forward that EOF implicitly; the head is parsed
|
|
124
|
+
* here, so the end of the upstream must still close the browser side or the page would hold a dead socket forever.
|
|
140
125
|
*/
|
|
141
126
|
const endClient = () => {
|
|
142
127
|
if (settled)
|