@mostra/conformance 0.6.2 → 0.8.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/dist/cli.js +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/{protocol-RQ9lbehE.js → protocol-Bi4gZzsu.js} +23 -2
- package/dist/{protocol-RQ9lbehE.js.map → protocol-Bi4gZzsu.js.map} +1 -1
- package/dist/{reference-host-D7fe25go.js → reference-host-BAAxMabg.js} +30 -3
- package/dist/reference-host-BAAxMabg.js.map +1 -0
- package/dist/{reference-host-BJ-A_Qd_.d.ts → reference-host-DYKrtRCH.d.ts} +27 -1
- package/dist/{reference-host-BJ-A_Qd_.d.ts.map → reference-host-DYKrtRCH.d.ts.map} +1 -1
- package/dist/reference-host.d.ts +1 -1
- package/dist/reference-host.js +1 -1
- package/dist/{runner-C8SO70qb.js → runner-DYbtXiL9.js} +248 -54
- package/dist/runner-DYbtXiL9.js.map +1 -0
- package/package.json +5 -4
- package/src/harness/protocol.ts +52 -1
- package/src/reference-host.ts +33 -0
- package/src/scenario.ts +7 -0
- package/src/scenarios/ports.ts +199 -4
- package/src/viewer.ts +59 -7
- package/tunnels/cloudflared.mjs +61 -0
- package/tunnels/forward.mjs +128 -0
- package/tunnels/tailscale.mjs +125 -0
- package/dist/reference-host-D7fe25go.js.map +0 -1
- package/dist/runner-C8SO70qb.js.map +0 -1
package/src/reference-host.ts
CHANGED
|
@@ -27,6 +27,8 @@ import {
|
|
|
27
27
|
createDiagnostic,
|
|
28
28
|
type DraftMirror,
|
|
29
29
|
hostMethods,
|
|
30
|
+
type PortAddressingExternalBaseParams,
|
|
31
|
+
type PortAddressingExternalBaseResult,
|
|
30
32
|
type PortIdentityResolveParams,
|
|
31
33
|
type PortSecretsGetParams,
|
|
32
34
|
type PortTrustedControlsActivatedParams,
|
|
@@ -62,6 +64,9 @@ export class ReferenceStore {
|
|
|
62
64
|
readonly #identity: StoreResult["identity"][number][] = [];
|
|
63
65
|
readonly #policies: StoreResult["policies"][number][] = [];
|
|
64
66
|
readonly #secrets: StoreResult["secrets"][number][] = [];
|
|
67
|
+
readonly #addressing: NonNullable<StoreResult["addressing"]>[number][] = [];
|
|
68
|
+
/** No external base until scripted: the loopback ports every other scenario expects. */
|
|
69
|
+
#base: Extract<ScriptParams, { port: "addressing" }>["answer"] = null;
|
|
65
70
|
readonly #faults: ReadonlySet<string>;
|
|
66
71
|
#order = 0;
|
|
67
72
|
/** How the scripted ports answer (feature `faults`). */
|
|
@@ -88,6 +93,7 @@ export class ReferenceStore {
|
|
|
88
93
|
if (params.port === "submissions") this.#deliver = params.answer;
|
|
89
94
|
else if (params.port === "trustedControls") this.#policy = params.answer;
|
|
90
95
|
else if (params.port === "identity") this.#person = params.answer;
|
|
96
|
+
else if (params.port === "addressing") this.#base = params.answer;
|
|
91
97
|
else this.#secretValues = params.answer;
|
|
92
98
|
}
|
|
93
99
|
|
|
@@ -202,6 +208,32 @@ export class ReferenceStore {
|
|
|
202
208
|
});
|
|
203
209
|
},
|
|
204
210
|
},
|
|
211
|
+
addressing: {
|
|
212
|
+
externalBase: (
|
|
213
|
+
request: PortAddressingExternalBaseParams,
|
|
214
|
+
): PortAddressingExternalBaseResult => {
|
|
215
|
+
const answer = this.#base;
|
|
216
|
+
const call = {
|
|
217
|
+
artifactId: request.artifactId,
|
|
218
|
+
listener: request.listener,
|
|
219
|
+
local: request.local,
|
|
220
|
+
};
|
|
221
|
+
if (answer === "fail") {
|
|
222
|
+
this.#addressing.push(call);
|
|
223
|
+
throw new Error("the host's tunnel is unavailable");
|
|
224
|
+
}
|
|
225
|
+
const host = request.listener === "shell" ? answer?.shellHost : answer?.artifactHost;
|
|
226
|
+
const base =
|
|
227
|
+
answer === null
|
|
228
|
+
? null
|
|
229
|
+
: {
|
|
230
|
+
origin: `http://${host}:${new URL(request.local).port}`,
|
|
231
|
+
...(answer.mode ? { mode: answer.mode } : {}),
|
|
232
|
+
};
|
|
233
|
+
this.#addressing.push({ ...call, answer: base });
|
|
234
|
+
return base;
|
|
235
|
+
},
|
|
236
|
+
},
|
|
205
237
|
secrets: {
|
|
206
238
|
get: (request: PortSecretsGetParams) => {
|
|
207
239
|
this.#secrets.push({ artifactId: request.artifactId, name: request.name });
|
|
@@ -220,6 +252,7 @@ export class ReferenceStore {
|
|
|
220
252
|
identity: [...this.#identity],
|
|
221
253
|
policies: [...this.#policies],
|
|
222
254
|
secrets: [...this.#secrets],
|
|
255
|
+
addressing: [...this.#addressing],
|
|
223
256
|
};
|
|
224
257
|
}
|
|
225
258
|
}
|
package/src/scenario.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
writeFileSync,
|
|
15
15
|
} from "node:fs";
|
|
16
16
|
import { dirname, join } from "node:path";
|
|
17
|
+
import { fileURLToPath } from "node:url";
|
|
17
18
|
import {
|
|
18
19
|
createDiagnostic,
|
|
19
20
|
type EventRecord,
|
|
@@ -31,6 +32,12 @@ import type { StartParams, StartResult, StoreResult } from "./harness/protocol.t
|
|
|
31
32
|
import { kitVersion } from "./version.ts";
|
|
32
33
|
import { Viewer } from "./viewer.ts";
|
|
33
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The local stand-ins for `tailscale` and `cloudflared` the share scenarios start instances with
|
|
37
|
+
* (`MOSTRA_TEST_TUNNELS`, D158): shipped in the kit's package beside `src` and `dist`.
|
|
38
|
+
*/
|
|
39
|
+
export const TUNNELS: string = fileURLToPath(new URL("../tunnels/", import.meta.url));
|
|
40
|
+
|
|
34
41
|
/** A scenario: one property of a host integration, with the requirements it covers. */
|
|
35
42
|
export interface Scenario {
|
|
36
43
|
/** A stable ID, `group.name`: baselines name scenarios by it. */
|
package/src/scenarios/ports.ts
CHANGED
|
@@ -1,10 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The host's ports, each with and without registration (HO-01): identity, trusted
|
|
3
|
-
* secrets, and what server code raises for the host (D108, D131, D143,
|
|
2
|
+
* The host's ports, each with and without registration (HO-01): identity, addressing, trusted
|
|
3
|
+
* controls, secrets, and what server code raises for the host (D108, D131, D143, D158, AC-05,
|
|
4
|
+
* H-05, H-14).
|
|
4
5
|
*/
|
|
5
6
|
import { ACTIONS, FORM } from "../fixtures.ts";
|
|
6
|
-
import { check, type Scenario, same } from "../scenario.ts";
|
|
7
|
-
import { OpenError, resultOf } from "../viewer.ts";
|
|
7
|
+
import { check, type Scenario, type ScenarioContext, same, TUNNELS } from "../scenario.ts";
|
|
8
|
+
import { Browser, OpenError, resultOf, send, Viewer } from "../viewer.ts";
|
|
9
|
+
|
|
10
|
+
/** What `share/start` answers, as the scenarios read it. */
|
|
11
|
+
interface Shared {
|
|
12
|
+
readonly shareId: string;
|
|
13
|
+
readonly url: string;
|
|
14
|
+
readonly tokenId: string;
|
|
15
|
+
readonly mode: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The person the stand-in for Tailscale Serve names on every tailnet request (D158 item 2). */
|
|
19
|
+
const TAILNET_PERSON = { ref: "ada@example.com", name: "Ada Lövelace" };
|
|
20
|
+
|
|
21
|
+
/** Starts the host's instance with the stand-ins for `tailscale` and `cloudflared` (D158). */
|
|
22
|
+
function startWithTunnels(context: ScenarioContext): Promise<unknown> {
|
|
23
|
+
return context.start({
|
|
24
|
+
env: {
|
|
25
|
+
MOSTRA_TEST_TUNNELS: TUNNELS,
|
|
26
|
+
MOSTRA_FAKE_TAILNET_USER: `${TAILNET_PERSON.ref}|${TAILNET_PERSON.name}`,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Opens a share of `artifactId` through `via`, answers on it, and returns the submission's viewer
|
|
33
|
+
* with the identity calls the host saw meanwhile.
|
|
34
|
+
*/
|
|
35
|
+
async function answeredThrough(
|
|
36
|
+
context: ScenarioContext,
|
|
37
|
+
artifactId: string,
|
|
38
|
+
via: "tailnet" | "cloudflare",
|
|
39
|
+
): Promise<{
|
|
40
|
+
readonly shared: Shared;
|
|
41
|
+
readonly viewer: { readonly kind: string; readonly ref?: string };
|
|
42
|
+
readonly calls: Awaited<ReturnType<ScenarioContext["store"]>>["identity"];
|
|
43
|
+
}> {
|
|
44
|
+
const before = context.host.serves("identity") ? (await context.store()).identity.length : 0;
|
|
45
|
+
const shared = await context.host.call<Shared>(
|
|
46
|
+
"share/start",
|
|
47
|
+
{ artifact: { artifactId }, via },
|
|
48
|
+
120_000,
|
|
49
|
+
);
|
|
50
|
+
try {
|
|
51
|
+
const viewer = await Viewer.open(shared.url);
|
|
52
|
+
const channel = viewer.ticket.submitChannels.includes("artifact") ? "artifact" : "shell";
|
|
53
|
+
same((await viewer.answer({ name: via }, channel)).status, 200, `the submit through ${via}`);
|
|
54
|
+
const read = await context.results(artifactId);
|
|
55
|
+
const calls = context.host.serves("identity")
|
|
56
|
+
? (await context.store()).identity.slice(before)
|
|
57
|
+
: [];
|
|
58
|
+
return { shared, viewer: read.submission.viewer, calls };
|
|
59
|
+
} finally {
|
|
60
|
+
await context.host.call("share/stop", { shareId: shared.shareId });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
8
63
|
|
|
9
64
|
/** D143: the identity port names the person behind a link minted without a viewer. */
|
|
10
65
|
export const identity: Scenario = {
|
|
@@ -72,6 +127,7 @@ export const identity: Scenario = {
|
|
|
72
127
|
["token", "grace@example.com"],
|
|
73
128
|
"the viewer a link was minted for",
|
|
74
129
|
);
|
|
130
|
+
await sharedIdentity(context);
|
|
75
131
|
if (context.host.supports("faults")) {
|
|
76
132
|
// A host that can't answer refuses the link rather than guess (D143).
|
|
77
133
|
await context.host.script({ port: "identity", answer: "fail" });
|
|
@@ -101,6 +157,144 @@ export const identity: Scenario = {
|
|
|
101
157
|
},
|
|
102
158
|
};
|
|
103
159
|
|
|
160
|
+
/**
|
|
161
|
+
* D143 item 2, D158 item 2: through Tailscale Serve the identity port hears `via: "tailnet"` with
|
|
162
|
+
* Serve's identity headers, and through a public share `via: "token"`; a host's answer names the
|
|
163
|
+
* person, and without one the tailnet person, or the link itself, is the viewer, never the home's
|
|
164
|
+
* local user.
|
|
165
|
+
*/
|
|
166
|
+
async function sharedIdentity(context: ScenarioContext): Promise<void> {
|
|
167
|
+
await context.host.stopInstance();
|
|
168
|
+
await startWithTunnels(context);
|
|
169
|
+
const receipt = await context.published(FORM, { policy: { policy: "epochs" } });
|
|
170
|
+
for (const via of ["tailnet", "cloudflare"] as const) {
|
|
171
|
+
const { shared, viewer, calls } = await answeredThrough(context, receipt.artifactId, via);
|
|
172
|
+
const call = calls.at(-1);
|
|
173
|
+
const expected = via === "tailnet" ? "tailnet" : "token";
|
|
174
|
+
if (call !== undefined) {
|
|
175
|
+
same(call.via, expected, `how a request through ${via} reached the gateway`);
|
|
176
|
+
if (via === "tailnet") {
|
|
177
|
+
check(
|
|
178
|
+
call.headers.includes("tailscale-user-login"),
|
|
179
|
+
"the port didn't receive Tailscale Serve's identity headers",
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
check(!context.host.serves("identity"), `the identity port wasn't asked through ${via}`);
|
|
184
|
+
}
|
|
185
|
+
if (call?.answer) {
|
|
186
|
+
same(
|
|
187
|
+
[viewer.kind, viewer.ref],
|
|
188
|
+
["host", call.answer.ref],
|
|
189
|
+
`the viewer the host named (${via})`,
|
|
190
|
+
);
|
|
191
|
+
} else if (via === "tailnet") {
|
|
192
|
+
same([viewer.kind, viewer.ref], ["tailnet", TAILNET_PERSON.ref], "the tailnet person");
|
|
193
|
+
} else {
|
|
194
|
+
same(
|
|
195
|
+
[viewer.kind, viewer.ref],
|
|
196
|
+
["token", `link:${shared.tokenId}`],
|
|
197
|
+
"the public link's viewer",
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* HO-10, D158 item 6: a host's `addressing` port gives the external base of an artifact's links.
|
|
205
|
+
* The base appears in the links Mostra mints and passes the Host and Origin checks, a frame on
|
|
206
|
+
* another site enters in worker mode, a base the host withdraws stops working, and without the
|
|
207
|
+
* port every link stays on loopback.
|
|
208
|
+
*/
|
|
209
|
+
export const addressing: Scenario = {
|
|
210
|
+
id: "ports.addressing",
|
|
211
|
+
title:
|
|
212
|
+
"With an addressing port, the links Mostra mints use the host's external base, which passes the Host and Origin checks and opens a frame on another site in worker mode; a withdrawn base stops working; without the port, links stay on loopback",
|
|
213
|
+
covers: ["HO-01", "HO-05", "HO-10", "HY-07", "H-14"],
|
|
214
|
+
async run(context) {
|
|
215
|
+
await context.start();
|
|
216
|
+
// Epochs: the loopback link after the withdrawal answers again (a once artifact takes one).
|
|
217
|
+
const receipt = await context.published(FORM, { policy: { policy: "epochs" } });
|
|
218
|
+
const loopback = (link: string): boolean =>
|
|
219
|
+
["127.0.0.1", "[::1]"].includes(new URL(link).hostname);
|
|
220
|
+
if (!context.host.serves("addressing")) {
|
|
221
|
+
check(receipt.url !== undefined && loopback(receipt.url), "the publish receipt's link");
|
|
222
|
+
check(loopback(await context.link(receipt.artifactId)), "a minted link without the port");
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
// A host that can be scripted puts the shell and the artifact on two sites (`localhost` and
|
|
226
|
+
// `127.0.0.1` are as cross-site as two tunnel hostnames); any other host is judged by its own
|
|
227
|
+
// answers.
|
|
228
|
+
if (context.host.supports("faults")) {
|
|
229
|
+
await context.host.script({
|
|
230
|
+
port: "addressing",
|
|
231
|
+
answer: { shellHost: "localhost", artifactHost: "127.0.0.1" },
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
const link = await context.link(receipt.artifactId);
|
|
235
|
+
const calls = (await context.store()).addressing ?? [];
|
|
236
|
+
const asked = calls.filter(
|
|
237
|
+
(call) => call.artifactId === receipt.artifactId && call.listener === "shell",
|
|
238
|
+
);
|
|
239
|
+
const answer = asked.at(-1)?.answer;
|
|
240
|
+
check(asked.length > 0, "the host's addressing port wasn't asked for the link's base");
|
|
241
|
+
if (!answer) {
|
|
242
|
+
check(loopback(link), "a link whose host gave no base");
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
same(new URL(link).origin, answer.origin, "the minted link's base");
|
|
246
|
+
const local = asked.at(-1)?.local as string;
|
|
247
|
+
check(loopback(local), `the loopback origin the host forwards to (${local})`);
|
|
248
|
+
const viewer = await Viewer.open(link);
|
|
249
|
+
const frameBase = (await context.store()).addressing?.findLast(
|
|
250
|
+
(call) => call.artifactId === receipt.artifactId && call.listener === "artifact",
|
|
251
|
+
)?.answer;
|
|
252
|
+
if (frameBase) {
|
|
253
|
+
same(viewer.ticket.artifactOrigin, frameBase.origin, "the frame's base");
|
|
254
|
+
const crossSite = new URL(frameBase.origin).hostname !== new URL(answer.origin).hostname;
|
|
255
|
+
same(
|
|
256
|
+
viewer.ticket.mode ?? "cookie",
|
|
257
|
+
frameBase.mode ?? (crossSite ? "worker" : "cookie"),
|
|
258
|
+
"the frame's session mode",
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
const channel = viewer.ticket.submitChannels.includes("artifact") ? "artifact" : "shell";
|
|
262
|
+
same((await viewer.answer({ name: "Ada" }, channel)).status, 200, "a submit at the base");
|
|
263
|
+
const polled = await viewer.poll();
|
|
264
|
+
same(polled.status, 200, "the page's long poll at the base");
|
|
265
|
+
// The base passes the Host and Origin checks; another Host or Origin is refused.
|
|
266
|
+
const parsed = new URL(link);
|
|
267
|
+
const strangerHost = await send({
|
|
268
|
+
url: `${parsed.origin}/a/${receipt.artifactId}`,
|
|
269
|
+
headers: { host: `evil.example:${parsed.port}`, accept: "text/html" },
|
|
270
|
+
});
|
|
271
|
+
same(strangerHost.status, 421, "another Host at the base");
|
|
272
|
+
const otherOrigin = await new Browser().ask({
|
|
273
|
+
method: "POST",
|
|
274
|
+
url: `${parsed.origin}/a/${receipt.artifactId}/_mostra/session`,
|
|
275
|
+
headers: {
|
|
276
|
+
origin: local,
|
|
277
|
+
"content-type": "application/json",
|
|
278
|
+
"sec-fetch-site": "cross-site",
|
|
279
|
+
},
|
|
280
|
+
body: JSON.stringify({ token: new URLSearchParams(parsed.hash.slice(1)).get("t") }),
|
|
281
|
+
});
|
|
282
|
+
same(otherOrigin.status, 403, "an exchange from another origin");
|
|
283
|
+
if (!context.host.supports("faults")) return;
|
|
284
|
+
// The host withdraws its base: new links are on loopback, and the old one stops working.
|
|
285
|
+
await context.host.script({ port: "addressing", answer: null });
|
|
286
|
+
const after = await context.link(receipt.artifactId);
|
|
287
|
+
check(loopback(after), `a link minted after the base was withdrawn (${after})`);
|
|
288
|
+
const stale = await Viewer.open(link).then(
|
|
289
|
+
() => "opened",
|
|
290
|
+
(error: unknown) => (error instanceof OpenError ? error.status : "unreachable"),
|
|
291
|
+
);
|
|
292
|
+
check(stale !== "opened", "a link at a withdrawn base still opened");
|
|
293
|
+
const reopened = await Viewer.open(after);
|
|
294
|
+
same((await reopened.answer({ name: "Grace" }, channel)).status, 200, "a submit on loopback");
|
|
295
|
+
},
|
|
296
|
+
};
|
|
297
|
+
|
|
104
298
|
/** D108, H-05, SH-02: the host decides the channels; the artifact can't submit where it may not. */
|
|
105
299
|
export const trustedControls: Scenario = {
|
|
106
300
|
id: "ports.trusted-controls",
|
|
@@ -272,6 +466,7 @@ export const serverEvents: Scenario = {
|
|
|
272
466
|
/** The port scenarios. */
|
|
273
467
|
export const PORT_SCENARIOS: readonly Scenario[] = [
|
|
274
468
|
identity,
|
|
469
|
+
addressing,
|
|
275
470
|
trustedControls,
|
|
276
471
|
secrets,
|
|
277
472
|
serverEvents,
|
package/src/viewer.ts
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
* a shell session, a frame ticket, the frame's entry) and speaks the browser protocol the way a
|
|
4
4
|
* page does, with a per-host cookie jar (browsers keep cookies per host, not per port, D98) and the
|
|
5
5
|
* `Origin`, `Sec-Fetch-*` and CSRF headers a browser sends. It also sends what no browser would,
|
|
6
|
-
* for the hygiene scenarios. It never follows redirects.
|
|
6
|
+
* for the hygiene scenarios. It never follows redirects. Where the shell and the artifact are two
|
|
7
|
+
* sites (a host's external bases, a Cloudflare share), it enters the frame as the bootstrap page
|
|
8
|
+
* does and signs the frame's requests as Mostra's service worker does (D70, D158 item 5).
|
|
7
9
|
*/
|
|
8
10
|
import { request as httpRequest } from "node:http";
|
|
9
11
|
import {
|
|
@@ -50,11 +52,15 @@ export function send(ask: Ask): Promise<Answer> {
|
|
|
50
52
|
return new Promise((resolve, reject) => {
|
|
51
53
|
const req = httpRequest(
|
|
52
54
|
{
|
|
53
|
-
|
|
55
|
+
// `localhost` and its subdomains are this machine, as browsers resolve them (WHATWG URL,
|
|
56
|
+
// RFC 6761); Mostra's listeners bind 127.0.0.1.
|
|
57
|
+
host: /(^|\.)localhost$/i.test(url.hostname)
|
|
58
|
+
? "127.0.0.1"
|
|
59
|
+
: url.hostname.replace(/^\[|\]$/g, ""),
|
|
54
60
|
port: url.port,
|
|
55
61
|
method: ask.method ?? "GET",
|
|
56
62
|
path: `${url.pathname}${url.search}`,
|
|
57
|
-
headers: ask.headers,
|
|
63
|
+
headers: { host: url.host, ...ask.headers },
|
|
58
64
|
agent: false,
|
|
59
65
|
},
|
|
60
66
|
(res) => {
|
|
@@ -121,6 +127,18 @@ export function parseLink(link: string): ParsedLink {
|
|
|
121
127
|
/** A browser-like client with a per-host cookie jar. */
|
|
122
128
|
export class Browser {
|
|
123
129
|
readonly #jar = new Map<string, { value: string; path: string }>();
|
|
130
|
+
/** Worker-mode frame sessions by origin: the header Mostra's service worker adds (D70). */
|
|
131
|
+
readonly #frames = new Map<string, string>();
|
|
132
|
+
|
|
133
|
+
/** Signs every later request to `origin` with a worker-mode frame session, as its worker does. */
|
|
134
|
+
enterFrame(origin: string, secret: string): void {
|
|
135
|
+
this.#frames.set(origin, secret);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Whether `origin` has a worker-mode frame session in this browser. */
|
|
139
|
+
workerFrame(origin: string): boolean {
|
|
140
|
+
return this.#frames.has(origin);
|
|
141
|
+
}
|
|
124
142
|
|
|
125
143
|
/** The cookies the jar sends to `url`: every cookie of the host whose path matches. */
|
|
126
144
|
cookiesFor(url: string): string {
|
|
@@ -137,9 +155,16 @@ export class Browser {
|
|
|
137
155
|
/** Sends a request with the jar's cookies and keeps the ones that come back. */
|
|
138
156
|
async ask(ask: Ask): Promise<Answer> {
|
|
139
157
|
const cookie = ask.noCookies ? "" : this.cookiesFor(ask.url);
|
|
158
|
+
const url = new URL(ask.url);
|
|
159
|
+
// The service worker signs every same-origin request but a ticketed one, which bypasses it.
|
|
160
|
+
const frame = url.searchParams.has("ticket") ? undefined : this.#frames.get(url.origin);
|
|
140
161
|
const answer = await send({
|
|
141
162
|
...ask,
|
|
142
|
-
headers: {
|
|
163
|
+
headers: {
|
|
164
|
+
...(cookie ? { cookie } : {}),
|
|
165
|
+
...(frame !== undefined ? { "x-mostra-frame": frame } : {}),
|
|
166
|
+
...ask.headers,
|
|
167
|
+
},
|
|
143
168
|
});
|
|
144
169
|
for (const line of answer.headers["set-cookie"] ?? []) {
|
|
145
170
|
const [pair = "", ...attributes] = line.split(";").map((part) => part.trim());
|
|
@@ -251,6 +276,28 @@ export class Viewer {
|
|
|
251
276
|
});
|
|
252
277
|
if (ticketAnswer.status !== 200) throw new OpenError("ticket", ticketAnswer);
|
|
253
278
|
const ticket = ticketAnswer.json<FrameTicket>();
|
|
279
|
+
if (ticket.mode === "worker") {
|
|
280
|
+
// The bootstrap page (D70): it loads, then exchanges the ticket in its fragment.
|
|
281
|
+
const page = await browser.ask({
|
|
282
|
+
url: ticket.src.replace(/#.*$/, ""),
|
|
283
|
+
headers: {
|
|
284
|
+
accept: "text/html",
|
|
285
|
+
"sec-fetch-mode": "navigate",
|
|
286
|
+
"sec-fetch-dest": "iframe",
|
|
287
|
+
"sec-fetch-site": "cross-site",
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
if (page.status !== 200) throw new OpenError("bootstrap", page);
|
|
291
|
+
const fragment = new URLSearchParams(new URL(ticket.src).hash.slice(1)).get("t") ?? "";
|
|
292
|
+
const entered = await browser.fetchJson(ticket.artifactOrigin, "/_mostra/enter", {
|
|
293
|
+
body: { ticket: fragment },
|
|
294
|
+
});
|
|
295
|
+
if (entered.status !== 200) throw new OpenError("enter", entered);
|
|
296
|
+
browser.enterFrame(ticket.artifactOrigin, entered.json<{ session: string }>().session);
|
|
297
|
+
const session = await browser.fetchJson(ticket.artifactOrigin, "/_mostra/session");
|
|
298
|
+
if (session.status !== 200) throw new OpenError("frame session", session);
|
|
299
|
+
return new Viewer(browser, parsed, ticket, shellCsrf, session.json<{ csrf: string }>().csrf);
|
|
300
|
+
}
|
|
254
301
|
const enter = await browser.ask({
|
|
255
302
|
url: ticket.src,
|
|
256
303
|
headers: {
|
|
@@ -364,9 +411,14 @@ export class Viewer {
|
|
|
364
411
|
}
|
|
365
412
|
}
|
|
366
413
|
|
|
367
|
-
/** The page's long poll: events after `cursor` (D47). */
|
|
368
|
-
poll(cursor?: string): Promise<Answer> {
|
|
369
|
-
|
|
414
|
+
/** The page's long poll: events after `cursor` (D47); in worker mode with its ticket (D70). */
|
|
415
|
+
async poll(cursor?: string): Promise<Answer> {
|
|
416
|
+
let query = `view=${this.viewId}&wait=0${cursor !== undefined ? `&cursor=${cursor}` : ""}`;
|
|
417
|
+
if (this.browser.workerFrame(this.ticket.artifactOrigin)) {
|
|
418
|
+
const ticket = await this.page("ticket", { purpose: "stream" });
|
|
419
|
+
if (ticket.status !== 200) return ticket;
|
|
420
|
+
query += `&ticket=${ticket.json<{ ticket: string }>().ticket}`;
|
|
421
|
+
}
|
|
370
422
|
return this.browser.fetchJson(
|
|
371
423
|
this.ticket.artifactOrigin,
|
|
372
424
|
`/_mostra/v/${REVISION}/events/poll?${query}`,
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* A stand-in for `cloudflared tunnel --url <local>`, for tests (`MOSTRA_TEST_TUNNELS`). Like a
|
|
4
|
+
* Cloudflare quick tunnel (VERIFIED_FACTS, WP-13) it gives every tunnel a site of its own (a
|
|
5
|
+
* random `<name>.localhost`, which browsers and the kit resolve to loopback), keeps the request's
|
|
6
|
+
* `Host`, adds Cloudflare's own headers, and holds every response back until it ends, so an event
|
|
7
|
+
* stream never arrives and pages must long-poll. It logs as `cloudflared` does: the address in a
|
|
8
|
+
* banner, then a registered connection. `MOSTRA_FAKE_CLOUDFLARED=fail` plays a refused tunnel.
|
|
9
|
+
*/
|
|
10
|
+
import { randomBytes } from "node:crypto";
|
|
11
|
+
import { forward, takeMark, untilStopped } from "./forward.mjs";
|
|
12
|
+
|
|
13
|
+
const args = process.argv.slice(2);
|
|
14
|
+
const urlAt = args.indexOf("--url");
|
|
15
|
+
const local = urlAt >= 0 ? args[urlAt + 1] : undefined;
|
|
16
|
+
if (args[0] !== "tunnel" || !local) {
|
|
17
|
+
process.stderr.write(`fake cloudflared: unsupported command ${args.join(" ")}\n`);
|
|
18
|
+
process.exit(2);
|
|
19
|
+
}
|
|
20
|
+
const log = (line) => process.stderr.write(`${new Date().toISOString()} ${line}\n`);
|
|
21
|
+
log("INF Requesting new quick Tunnel on trycloudflare.com...");
|
|
22
|
+
// `fail-one`: of a share's two tunnels, the first to start fails at once and the other comes up
|
|
23
|
+
// a moment later, as when Cloudflare refuses one of two requests.
|
|
24
|
+
const failOne = process.env.MOSTRA_FAKE_CLOUDFLARED === "fail-one";
|
|
25
|
+
const failing = failOne && takeMark("cloudflared-failed");
|
|
26
|
+
if (failOne && !failing) await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
27
|
+
if (process.env.MOSTRA_FAKE_CLOUDFLARED === "fail" || failing) {
|
|
28
|
+
log(
|
|
29
|
+
'ERR failed to request quick Tunnel: Post "https://api.trycloudflare.com/tunnel": 429 Too Many Requests',
|
|
30
|
+
);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
const name = `t${randomBytes(6).toString("hex")}`;
|
|
34
|
+
const port = await forward(local, {
|
|
35
|
+
hold: true,
|
|
36
|
+
rewrite(headers) {
|
|
37
|
+
headers["cf-connecting-ip"] = "203.0.113.7";
|
|
38
|
+
headers["cf-ray"] = `${randomBytes(8).toString("hex")}-FAKE`;
|
|
39
|
+
headers["cf-visitor"] = '{"scheme":"https"}';
|
|
40
|
+
headers["x-forwarded-for"] = "203.0.113.7";
|
|
41
|
+
headers["x-forwarded-proto"] = "https";
|
|
42
|
+
return headers;
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
log(
|
|
46
|
+
"INF +--------------------------------------------------------------------------------------------+",
|
|
47
|
+
);
|
|
48
|
+
log(
|
|
49
|
+
"INF | Your quick Tunnel has been created! Visit it at (it may take some time to be reachable): |",
|
|
50
|
+
);
|
|
51
|
+
log(
|
|
52
|
+
`INF | http://${name}.localhost:${port} |`,
|
|
53
|
+
);
|
|
54
|
+
log(
|
|
55
|
+
"INF +--------------------------------------------------------------------------------------------+",
|
|
56
|
+
);
|
|
57
|
+
log(
|
|
58
|
+
"INF Registered tunnel connection connIndex=0 event=0 ip=198.41.192.7 location=fake protocol=quic",
|
|
59
|
+
);
|
|
60
|
+
await untilStopped();
|
|
61
|
+
process.exit(0);
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The forwarding half of the test tunnels (`MOSTRA_TEST_TUNNELS`): an HTTP listener on loopback
|
|
3
|
+
* that passes every request on to a local origin, keeping its `Host` as the real tools do, and
|
|
4
|
+
* WebSocket upgrades through. The fakes add what each real tool adds on its way: Tailscale's
|
|
5
|
+
* identity headers, Cloudflare's held-back responses. Plain Node.js with no dependencies, so it
|
|
6
|
+
* runs from the kit's installed package as from this repository.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, unlinkSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { createServer, request } from "node:http";
|
|
10
|
+
import { connect } from "node:net";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Starts forwarding to `local` (`http://127.0.0.1:<port>`); resolves with the port it listens on.
|
|
15
|
+
*
|
|
16
|
+
* - `rewrite(headers)` changes the request's headers before they go on.
|
|
17
|
+
* - `hold`: answer only once the whole response arrived, as Cloudflare's quick tunnels do.
|
|
18
|
+
* - `host` and `port`: where it listens (loopback, a port the system picks, by default).
|
|
19
|
+
*/
|
|
20
|
+
export function forward(
|
|
21
|
+
local,
|
|
22
|
+
{ rewrite = (headers) => headers, hold = false, host = "127.0.0.1", port = 0 } = {},
|
|
23
|
+
) {
|
|
24
|
+
const target = new URL(local);
|
|
25
|
+
const server = createServer((incoming, outgoing) => {
|
|
26
|
+
const headers = rewrite({ ...incoming.headers });
|
|
27
|
+
const upstream = request(
|
|
28
|
+
{
|
|
29
|
+
host: target.hostname,
|
|
30
|
+
port: target.port,
|
|
31
|
+
method: incoming.method,
|
|
32
|
+
path: incoming.url,
|
|
33
|
+
headers,
|
|
34
|
+
agent: false,
|
|
35
|
+
},
|
|
36
|
+
(answer) => {
|
|
37
|
+
if (!hold) {
|
|
38
|
+
outgoing.writeHead(answer.statusCode ?? 502, answer.headers);
|
|
39
|
+
answer.pipe(outgoing);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const chunks = [];
|
|
43
|
+
answer.on("data", (chunk) => chunks.push(chunk));
|
|
44
|
+
answer.on("end", () => {
|
|
45
|
+
const body = Buffer.concat(chunks);
|
|
46
|
+
const { "transfer-encoding": _chunked, ...rest } = answer.headers;
|
|
47
|
+
outgoing.writeHead(answer.statusCode ?? 502, { ...rest, "content-length": body.length });
|
|
48
|
+
outgoing.end(body);
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
);
|
|
52
|
+
upstream.on("error", () => {
|
|
53
|
+
if (!outgoing.headersSent) outgoing.writeHead(502);
|
|
54
|
+
outgoing.end();
|
|
55
|
+
});
|
|
56
|
+
incoming.pipe(upstream);
|
|
57
|
+
outgoing.on("close", () => upstream.destroy());
|
|
58
|
+
});
|
|
59
|
+
server.on("upgrade", (incoming, socket, head) => {
|
|
60
|
+
const headers = rewrite({ ...incoming.headers });
|
|
61
|
+
const upstream = connect(Number(target.port), target.hostname, () => {
|
|
62
|
+
const lines = [`${incoming.method} ${incoming.url} HTTP/1.1`];
|
|
63
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
64
|
+
for (const one of Array.isArray(value) ? value : [value]) lines.push(`${name}: ${one}`);
|
|
65
|
+
}
|
|
66
|
+
upstream.write(`${lines.join("\r\n")}\r\n\r\n`);
|
|
67
|
+
if (head.length > 0) upstream.write(head);
|
|
68
|
+
upstream.pipe(socket);
|
|
69
|
+
socket.pipe(upstream);
|
|
70
|
+
});
|
|
71
|
+
upstream.on("error", () => socket.destroy());
|
|
72
|
+
socket.on("error", () => upstream.destroy());
|
|
73
|
+
});
|
|
74
|
+
server.keepAliveTimeout = 305_000;
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
server.once("error", reject);
|
|
77
|
+
server.listen({ host, port }, () => resolve(server.address().port));
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Waits until the process is told to stop, as a foreground tunnel command does. `tag` names this
|
|
83
|
+
* tunnel for a test: `die-<tag>` ends this one only (Tailscale's tunnels are tagged with their port).
|
|
84
|
+
*/
|
|
85
|
+
export function untilStopped(tag) {
|
|
86
|
+
return new Promise((resolve) => {
|
|
87
|
+
// Mostra's process manager ends its children by signal, or by their job on Windows.
|
|
88
|
+
for (const signal of ["SIGTERM", "SIGINT", "SIGBREAK"]) process.once(signal, resolve);
|
|
89
|
+
setInterval(() => {}, 1 << 30);
|
|
90
|
+
// A test ends a running tunnel by creating `die` in `MOSTRA_FAKE_CONTROL`, as a tunnel whose
|
|
91
|
+
// process ended by itself: the fake that notices it first takes it and exits 1.
|
|
92
|
+
const control = process.env.MOSTRA_FAKE_CONTROL;
|
|
93
|
+
if (control) {
|
|
94
|
+
const names = tag === undefined ? ["die"] : ["die", "die-" + tag];
|
|
95
|
+
setInterval(() => {
|
|
96
|
+
for (const name of names) {
|
|
97
|
+
try {
|
|
98
|
+
unlinkSync(join(control, name));
|
|
99
|
+
} catch {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
process.exit(1);
|
|
103
|
+
}
|
|
104
|
+
}, 100);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Takes a one-time mark in `MOSTRA_FAKE_CONTROL`: `true` for the one process that creates
|
|
111
|
+
* `<name>` first, so tests can make one of two tunnels behave otherwise.
|
|
112
|
+
*/
|
|
113
|
+
export function takeMark(name) {
|
|
114
|
+
const control = process.env.MOSTRA_FAKE_CONTROL;
|
|
115
|
+
if (!control) return false;
|
|
116
|
+
try {
|
|
117
|
+
writeFileSync(join(control, name), String(process.pid), { flag: "wx" });
|
|
118
|
+
return true;
|
|
119
|
+
} catch {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Whether `MOSTRA_FAKE_CONTROL` holds `<name>`: a switch a test turns on and off. */
|
|
125
|
+
export function marked(name) {
|
|
126
|
+
const control = process.env.MOSTRA_FAKE_CONTROL;
|
|
127
|
+
return control !== undefined && existsSync(join(control, name));
|
|
128
|
+
}
|