@niwa-labs/web 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +15 -4
- package/src/app.ts +298 -0
- package/src/denial.ts +13 -0
- package/src/feed.ts +215 -0
- package/src/index.ts +737 -0
- package/src/login.ts +51 -0
- package/src/source.ts +41 -0
- package/src/trace.ts +85 -0
- package/src/viewer.ts +55 -0
- package/test/answer.test.ts +245 -0
- package/test/app.test.ts +282 -0
- package/test/call-by-grant.test.ts +245 -0
- package/test/denial.test.ts +57 -0
- package/test/feed-snapshot.test.ts +83 -0
- package/test/feed.test.ts +163 -0
- package/test/fixtures/feed-host.ts +86 -0
- package/test/fixtures/serve-web.ts +58 -0
- package/test/fixtures/why-host.ts +44 -0
- package/test/instance-host.test.ts +80 -0
- package/test/web-login.test.ts +171 -0
- package/test/why.test.ts +149 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// The web surface lets in only a browser that has the cookie, and speaks for the owner only when
|
|
2
|
+
// it is configured to.
|
|
3
|
+
//
|
|
4
|
+
// 1. a request without the cookie, or with a cookie the server did not give, is refused with 401
|
|
5
|
+
// 2. the login link gives an HttpOnly, SameSite=Strict cookie, and with it the page works
|
|
6
|
+
// 3. the link works once, and a wrong secret gives nothing
|
|
7
|
+
// 4. with speaksFor the page answers a request of the owner; without it the page is a plain
|
|
8
|
+
// client and the answer is refused
|
|
9
|
+
|
|
10
|
+
import { expect, test } from "bun:test";
|
|
11
|
+
|
|
12
|
+
import { owner, record } from "@niwa-labs/core";
|
|
13
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
14
|
+
import { createNiwa, eventually, tempHome, loginWeb, plugin, watch, webFetch, connectAsOwner, opt, answeredBy, requestAsk, question, requests } from "@niwa-labs/testing";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
|
|
17
|
+
import type { WebServer } from "../src/index.ts";
|
|
18
|
+
import { serveWebPages } from "./fixtures/serve-web.ts";
|
|
19
|
+
|
|
20
|
+
const Start = record("web-login.start", z.object({}), { open: true });
|
|
21
|
+
const Done = record("web-login.done", z.object({ answer: z.string() }));
|
|
22
|
+
const Pick = question("web-login.pick", { kind: "choice", title: "Pick one", options: [opt("yes"), opt("no")] });
|
|
23
|
+
|
|
24
|
+
/** Asks the owner one question and goes on with the answer. */
|
|
25
|
+
function executor() {
|
|
26
|
+
return plugin("web-login-executor", (api) => {
|
|
27
|
+
api.declare(Start, Done);
|
|
28
|
+
api.on(Start, async (_data, step) => {
|
|
29
|
+
const { answer } = answeredBy(await step(requestAsk, { to: owner, ...Pick }));
|
|
30
|
+
await step.emit(Done, { answer: JSON.stringify(answer) });
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function withHost(run: (home: string) => Promise<void>): Promise<void> {
|
|
36
|
+
const home = await tempHome();
|
|
37
|
+
const host = createNiwa({ home: home.path });
|
|
38
|
+
host.use(requests());
|
|
39
|
+
host.use(executor());
|
|
40
|
+
await host.serve();
|
|
41
|
+
try {
|
|
42
|
+
await run(home.path);
|
|
43
|
+
} finally {
|
|
44
|
+
await host.close();
|
|
45
|
+
await home.cleanup();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The records of one type seen so far. */
|
|
50
|
+
function of(seen: StoredRecord[], type: string): StoredRecord[] {
|
|
51
|
+
return seen.filter((item) => item.type === type);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const JSON_HEADERS = { "content-type": "application/json" };
|
|
55
|
+
|
|
56
|
+
test("web login: a request without the cookie is refused", () =>
|
|
57
|
+
withHost(async (home) => {
|
|
58
|
+
const web = await serveWebPages({ home, port: 0, speaksFor: "owner" });
|
|
59
|
+
try {
|
|
60
|
+
for (const path of ["/", "/feed", "/events", "/trace-data/rec_x", "/nothing"]) {
|
|
61
|
+
expect((await fetch(`${web.url}${path}`)).status).toBe(401);
|
|
62
|
+
}
|
|
63
|
+
const post = await fetch(`${web.url}/answer`, {
|
|
64
|
+
method: "POST",
|
|
65
|
+
headers: JSON_HEADERS,
|
|
66
|
+
body: JSON.stringify({ request: "r", answer: { ids: ["yes"] } }),
|
|
67
|
+
});
|
|
68
|
+
expect(post.status).toBe(401);
|
|
69
|
+
|
|
70
|
+
// A cookie the server did not give is no cookie, before and after a real login.
|
|
71
|
+
const forged = { headers: { cookie: "niwa_web=forged" } };
|
|
72
|
+
expect((await fetch(`${web.url}/feed`, forged)).status).toBe(401);
|
|
73
|
+
await loginWeb(web);
|
|
74
|
+
expect((await fetch(`${web.url}/feed`, forged)).status).toBe(401);
|
|
75
|
+
} finally {
|
|
76
|
+
await web.close();
|
|
77
|
+
}
|
|
78
|
+
}));
|
|
79
|
+
|
|
80
|
+
test("web login: the link gives a strict HttpOnly cookie, once, and the page works with it", () =>
|
|
81
|
+
withHost(async (home) => {
|
|
82
|
+
const web = await serveWebPages({ home, port: 0, speaksFor: "owner" });
|
|
83
|
+
try {
|
|
84
|
+
const wrong = await fetch(`${web.url}/login?secret=wrong`, { redirect: "manual" });
|
|
85
|
+
expect(wrong.status).toBe(401);
|
|
86
|
+
expect(wrong.headers.get("set-cookie")).toBeNull();
|
|
87
|
+
expect((await fetch(`${web.url}/login`, { redirect: "manual" })).status).toBe(401);
|
|
88
|
+
|
|
89
|
+
const login = await fetch(web.loginUrl, { redirect: "manual" });
|
|
90
|
+
expect(login.status).toBe(302);
|
|
91
|
+
expect(login.headers.get("location")).toBe("/");
|
|
92
|
+
const cookie = login.headers.get("set-cookie") ?? "";
|
|
93
|
+
expect(cookie).toContain("HttpOnly");
|
|
94
|
+
expect(cookie).toContain("SameSite=Strict");
|
|
95
|
+
expect(cookie).not.toContain(new URL(web.loginUrl).searchParams.get("secret") ?? "unreachable");
|
|
96
|
+
|
|
97
|
+
const withCookie = { headers: { cookie: cookie.split(";")[0] ?? "" } };
|
|
98
|
+
expect((await fetch(`${web.url}/feed`, withCookie)).status).toBe(200);
|
|
99
|
+
expect((await fetch(`${web.url}/`, withCookie)).status).toBe(200);
|
|
100
|
+
|
|
101
|
+
// The secret was spent: the same link gives nothing more.
|
|
102
|
+
expect((await fetch(web.loginUrl, { redirect: "manual" })).status).toBe(401);
|
|
103
|
+
// The cookie stays good.
|
|
104
|
+
expect((await fetch(`${web.url}/feed`, withCookie)).status).toBe(200);
|
|
105
|
+
} finally {
|
|
106
|
+
await web.close();
|
|
107
|
+
}
|
|
108
|
+
}));
|
|
109
|
+
|
|
110
|
+
test("web login: each start has a secret of its own", () =>
|
|
111
|
+
withHost(async (home) => {
|
|
112
|
+
const first = await serveWebPages({ home, port: 0, speaksFor: "owner" });
|
|
113
|
+
const second = await serveWebPages({ home, port: 0, speaksFor: "owner" });
|
|
114
|
+
try {
|
|
115
|
+
const secret = (web: WebServer) => new URL(web.loginUrl).searchParams.get("secret");
|
|
116
|
+
expect(secret(first)).not.toBe(secret(second));
|
|
117
|
+
const crossed = `${second.url}/login?secret=${secret(first) ?? ""}`;
|
|
118
|
+
expect((await fetch(crossed, { redirect: "manual" })).status).toBe(401);
|
|
119
|
+
} finally {
|
|
120
|
+
await first.close();
|
|
121
|
+
await second.close();
|
|
122
|
+
}
|
|
123
|
+
}));
|
|
124
|
+
|
|
125
|
+
/** Run one request of the owner through a page, and tell how the page answered it. */
|
|
126
|
+
async function answerThroughPage(home: string, web: WebServer): Promise<{ status: number; seen: StoredRecord[] }> {
|
|
127
|
+
const writer = await connectAsOwner(home);
|
|
128
|
+
const observer = await watch(home);
|
|
129
|
+
try {
|
|
130
|
+
await loginWeb(web);
|
|
131
|
+
await writer.emit(Start, {});
|
|
132
|
+
await eventually(() => of(observer.seen, "request.created").length === 1, 10_000, "the request was not made");
|
|
133
|
+
const id = (of(observer.seen, "request.created")[0]?.data as { id: string }).id;
|
|
134
|
+
const response = await webFetch(`${web.url}/answer`, {
|
|
135
|
+
method: "POST",
|
|
136
|
+
headers: JSON_HEADERS,
|
|
137
|
+
body: JSON.stringify({ request: id, answer: { ids: ["yes"] } }),
|
|
138
|
+
});
|
|
139
|
+
await Bun.sleep(300);
|
|
140
|
+
return { status: response.status, seen: observer.seen };
|
|
141
|
+
} finally {
|
|
142
|
+
await observer.stop();
|
|
143
|
+
await writer.close();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
test("web login: with speaksFor the page answers for the owner", () =>
|
|
148
|
+
withHost(async (home) => {
|
|
149
|
+
const web = await serveWebPages({ home, port: 0, speaksFor: "owner" });
|
|
150
|
+
try {
|
|
151
|
+
const result = await answerThroughPage(home, web);
|
|
152
|
+
expect(result.status).toBe(200);
|
|
153
|
+
expect(of(result.seen, "request.answered")).toHaveLength(1);
|
|
154
|
+
expect(of(result.seen, "web-login.done")).toHaveLength(1);
|
|
155
|
+
} finally {
|
|
156
|
+
await web.close();
|
|
157
|
+
}
|
|
158
|
+
}));
|
|
159
|
+
|
|
160
|
+
test("web login: without speaksFor the page is a plain client and cannot answer for the owner", () =>
|
|
161
|
+
withHost(async (home) => {
|
|
162
|
+
const web = await serveWebPages({ home, port: 0 });
|
|
163
|
+
try {
|
|
164
|
+
const result = await answerThroughPage(home, web);
|
|
165
|
+
expect(result.status).toBe(403);
|
|
166
|
+
expect(of(result.seen, "request.answered")).toHaveLength(0);
|
|
167
|
+
expect(of(result.seen, "web-login.done")).toHaveLength(0);
|
|
168
|
+
} finally {
|
|
169
|
+
await web.close();
|
|
170
|
+
}
|
|
171
|
+
}));
|
package/test/why.test.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// Why: from any result on the page one can walk to the message that started it.
|
|
2
|
+
//
|
|
3
|
+
// A real host process, a real web surface, and real HTTP requests:
|
|
4
|
+
//
|
|
5
|
+
// 1. the host is started, the web surface connects to it
|
|
6
|
+
// 2. an external input is written; the handler produces a result from it
|
|
7
|
+
// 3. the trace page of the result is opened: it names the record, and links to the chain
|
|
8
|
+
// 4. walking the links up reaches the external input that started it: the record whose cause
|
|
9
|
+
// is nothing
|
|
10
|
+
// 5. the chain is walked over HTTP, exactly as the page walks it
|
|
11
|
+
//
|
|
12
|
+
// The walk is the one the core does: `causes` up and `trace` down.
|
|
13
|
+
|
|
14
|
+
import { expect, test } from "bun:test";
|
|
15
|
+
|
|
16
|
+
import { spawnHost, tempHome, loginWeb, webFetch, connectAsOwner } from "@niwa-labs/testing";
|
|
17
|
+
import type { WebServer } from "../src/index.ts";
|
|
18
|
+
import { serveWebPages } from "./fixtures/serve-web.ts";
|
|
19
|
+
import { Done, Start } from "./fixtures/why-host.ts";
|
|
20
|
+
|
|
21
|
+
const HOST_SCRIPT = new URL("./fixtures/why-host.ts", import.meta.url).pathname;
|
|
22
|
+
|
|
23
|
+
/** The chain of one record, as the server answers it. */
|
|
24
|
+
interface Chain {
|
|
25
|
+
record: string;
|
|
26
|
+
causes: Array<{ id: string; type: string; cause: string | null; root: string; depth: number }>;
|
|
27
|
+
trace: Array<{ id: string; type: string; cause: string | null; root: string; depth: number }>;
|
|
28
|
+
root: string | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The chain of one record, over HTTP. */
|
|
32
|
+
async function chainOf(url: string, id: string): Promise<Chain> {
|
|
33
|
+
const response = await webFetch(`${url}/trace-data/${encodeURIComponent(id)}`);
|
|
34
|
+
expect(response.status).toBe(200);
|
|
35
|
+
return (await response.json()) as Chain;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
test("why: from the trace page of a result one can walk to the external input that started it", async () => {
|
|
39
|
+
const home = await tempHome();
|
|
40
|
+
const host = spawnHost(HOST_SCRIPT, home.path);
|
|
41
|
+
let web: WebServer | undefined;
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
await host.ready();
|
|
45
|
+
web = await serveWebPages({ home: home.path, port: 0, speaksFor: "owner" });
|
|
46
|
+
await loginWeb(web);
|
|
47
|
+
|
|
48
|
+
// The external input: the chain starts here.
|
|
49
|
+
const writer = await connectAsOwner(home.path);
|
|
50
|
+
const input = await writer.emit(Start, { text: "the message" });
|
|
51
|
+
|
|
52
|
+
// The result: what the handler produced from it.
|
|
53
|
+
let resultId = "";
|
|
54
|
+
await waitFor(async () => {
|
|
55
|
+
const chain = await chainOf(web!.url, input.id);
|
|
56
|
+
const done = chain.trace.find((record) => record.type === "why.done");
|
|
57
|
+
if (done === undefined) return false;
|
|
58
|
+
resultId = done.id;
|
|
59
|
+
return true;
|
|
60
|
+
}, "the result reaches the trace");
|
|
61
|
+
|
|
62
|
+
// The trace page of the result: it is HTML, it names the record, and it links to the chain.
|
|
63
|
+
const page = await webFetch(`${web.url}/trace/${encodeURIComponent(resultId)}`);
|
|
64
|
+
expect(page.status).toBe(200);
|
|
65
|
+
const html = await page.text();
|
|
66
|
+
expect(html).toContain("<!doctype html>");
|
|
67
|
+
expect(html).toContain(resultId);
|
|
68
|
+
// The page links to the chain: to the record it depends on, and to its own page.
|
|
69
|
+
expect(html).toContain(`href="/trace/${encodeURIComponent(input.id)}"`);
|
|
70
|
+
expect(html).toContain(`href="/trace/${encodeURIComponent(resultId)}"`);
|
|
71
|
+
// The external input is named on the page, with nothing above it.
|
|
72
|
+
expect(html).toContain("why.start");
|
|
73
|
+
expect(html).toContain("cause none");
|
|
74
|
+
|
|
75
|
+
// Walking up from the result reaches the external input: the record whose cause is nothing.
|
|
76
|
+
const chain = await chainOf(web.url, resultId);
|
|
77
|
+
expect(chain.record).toBe(resultId);
|
|
78
|
+
expect(chain.causes.length).toBeGreaterThan(0);
|
|
79
|
+
|
|
80
|
+
const external = chain.causes.find((record) => record.cause === null);
|
|
81
|
+
expect(external).toBeDefined();
|
|
82
|
+
expect(external?.id).toBe(input.id);
|
|
83
|
+
expect(external?.type).toBe("why.start");
|
|
84
|
+
|
|
85
|
+
// The root of the chain is the external input, and every record of it says so.
|
|
86
|
+
expect(chain.root).toBe(input.id);
|
|
87
|
+
for (const record of [...chain.causes, ...chain.trace]) {
|
|
88
|
+
expect(record.root).toBe(input.id);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// The walk is a walk: the links on the page lead to the next page, and following them reaches
|
|
92
|
+
// the external input.
|
|
93
|
+
const seen = new Set<string>();
|
|
94
|
+
let current = chain.causes[0];
|
|
95
|
+
while (current !== undefined && !seen.has(current.id)) {
|
|
96
|
+
seen.add(current.id);
|
|
97
|
+
expect(current.id).toStartWith("rec_");
|
|
98
|
+
// The page of the record links to the record it depends on: the link is what a reader follows.
|
|
99
|
+
const page = await webFetch(`${web.url}/trace/${encodeURIComponent(current.id)}`);
|
|
100
|
+
const pageHtml = await page.text();
|
|
101
|
+
expect(pageHtml).toContain(`href="/trace/${encodeURIComponent(input.id)}"`);
|
|
102
|
+
const next = await chainOf(web.url, current.id);
|
|
103
|
+
const up = next.causes.find((record) => record.cause === null);
|
|
104
|
+
current = up ?? next.causes[0];
|
|
105
|
+
if (next.causes.length === 0) break;
|
|
106
|
+
}
|
|
107
|
+
expect(seen.has(input.id)).toBe(true);
|
|
108
|
+
|
|
109
|
+
// The trace page of the external input itself: it is the start of its chain.
|
|
110
|
+
const inputChain = await chainOf(web.url, input.id);
|
|
111
|
+
expect(inputChain.causes).toEqual([]);
|
|
112
|
+
expect(inputChain.root).toBe(input.id);
|
|
113
|
+
|
|
114
|
+
// What the record holds is shown as text, never as markup.
|
|
115
|
+
const hostile = await writer.emit(Start, { text: "<script>alert(1)</script>\"><img src=x>" });
|
|
116
|
+
let hostileId = "";
|
|
117
|
+
await waitFor(async () => {
|
|
118
|
+
const done = (await chainOf(web!.url, hostile.id)).trace.find((record) => record.type === "why.done");
|
|
119
|
+
hostileId = done?.id ?? "";
|
|
120
|
+
return done !== undefined;
|
|
121
|
+
}, "the hostile result reaches the trace");
|
|
122
|
+
const hostilePage = await (await webFetch(`${web.url}/trace/${encodeURIComponent(hostileId)}`)).text();
|
|
123
|
+
expect(hostilePage).not.toContain("<script>alert");
|
|
124
|
+
expect(hostilePage).not.toContain("<img");
|
|
125
|
+
expect(hostilePage).toContain("<script>alert(1)</script>");
|
|
126
|
+
|
|
127
|
+
// A record that is not there, and an id that is not one, are refused by name.
|
|
128
|
+
expect((await webFetch(`${web.url}/trace/rec_nothing`)).status).toBe(404);
|
|
129
|
+
expect((await webFetch(`${web.url}/trace-data/rec_nothing`)).status).toBe(404);
|
|
130
|
+
expect((await webFetch(`${web.url}/trace/%E0%A4%A`)).status).toBe(400);
|
|
131
|
+
expect((await webFetch(`${web.url}/trace/${encodeURIComponent(resultId)}`, { method: "POST" })).status).toBe(405);
|
|
132
|
+
|
|
133
|
+
await writer.close();
|
|
134
|
+
} finally {
|
|
135
|
+
await web?.close();
|
|
136
|
+
await host.kill().catch(() => {});
|
|
137
|
+
await home.cleanup();
|
|
138
|
+
}
|
|
139
|
+
}, 30_000);
|
|
140
|
+
|
|
141
|
+
/** Wait until a condition holds; the message says what was waited for. */
|
|
142
|
+
async function waitFor(fn: () => boolean | Promise<boolean>, message: string): Promise<void> {
|
|
143
|
+
const until = Date.now() + 10_000;
|
|
144
|
+
for (;;) {
|
|
145
|
+
if (await fn()) return;
|
|
146
|
+
if (Date.now() > until) throw new Error(message);
|
|
147
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
148
|
+
}
|
|
149
|
+
}
|