Public: anyone can read this; the crew can change it.

The phone share target accepts a link shared alone and tells the crew with the link as context #13565

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/13543-the-phone-share-target-accepts-a-link-sh → main 1 commit fork

  • Merged. mason/13543-the-phone-share-target-accepts-a-link-sh was merged into main as 8c30718.

Review

Sign in to review this change.
1 file changed +86 −0
test/share-target-url.test.ts added +86
@@ -0,0 +1,86 @@
1 /** A link shared alone is one tap to tell the crew (#13543). */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { createUser } from "../src/core/users.ts";
8 import { serve, type Servers } from "../src/http/server.ts";
9 import { TELL_MAX, refuseShare, sharedDraft } from "../src/web/share_target.ts";
10 import { quietLogs } from "./helpers/globals.ts";
11
12 quietLogs();
13
14 let dataDir: string;
15 let app: App;
16 let servers: Servers;
17 let cookie = "";
18
19 beforeAll(async () => {
20 dataDir = await mkdtemp(join(tmpdir(), "forge-share-url-"));
21 app = createApp({ dataDir, port: 0, host: "forge.test" });
22 servers = await serve(app);
23 const made = await createUser(app.db, {
24 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
25 password: "a-good-passphrase", adminIfFirst: true,
26 });
27 if (!made.ok) throw new Error(made.error);
28 const res = await fetch(`${servers.url}/signin`, {
29 method: "POST",
30 body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }),
31 redirect: "manual",
32 });
33 cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!;
34 }, 120_000);
35
36 afterAll(async () => {
37 servers.stop();
38 app.close();
39 await rm(dataDir, { recursive: true, force: true });
40 });
41
42 const thoughts = () => app.db.query<{ n: number }, []>("SELECT count(*) AS n FROM thought").get()!.n;
43 const area = (html: string) => /<textarea[^>]*name="text"[^>]*>([^<]*)<\/textarea>/.exec(html)?.[1];
44
45 test("a share with only a url shows the url in the tell and sends it", async () => {
46 const url = "https://example.com/a?x=1&y=2";
47 const res = await fetch(`${servers.url}/tell?${new URLSearchParams({ url })}`, { headers: { cookie } });
48 expect(res.status).toBe(200);
49 const out = await res.text();
50 expect(area(out)).toBe("https://example.com/a?x=1&amp;y=2");
51 const once = /name="once" value="([0-9a-f]{24})"/.exec(out)?.[1];
52 expect(once).toBeDefined();
53 const before = thoughts();
54 const sent = await fetch(`${servers.url}/tell`, {
55 method: "POST", headers: { cookie, origin: servers.url }, redirect: "manual",
56 body: new URLSearchParams({ text: url, app: "forge", once: once! }),
57 });
58 expect(sent.status).toBeLessThan(400);
59 expect(thoughts()).toBe(before + 1);
60 const row = app.db.query<{ text: string }, []>("SELECT text FROM thought ORDER BY received_at DESC LIMIT 1").get()!;
61 expect(row.text).toContain(url);
62 });
63
64 test("title, text and url are joined once and a repeated part is not said twice", () => {
65 const p = new URLSearchParams({ title: "Nice", text: "look https://e.com/x", url: "https://e.com/x" });
66 expect(sharedDraft(p)).toBe("Nice\nlook https://e.com/x");
67 });
68
69 test("a share over the cap is refused whole, and the composer never holds more than the cap", async () => {
70 const p = new URLSearchParams({ text: "x".repeat(TELL_MAX), url: "https://e.com/" });
71 expect(refuseShare(p)).toContain("too long");
72 const out = await (await fetch(`${servers.url}/?${new URLSearchParams({ text: "y".repeat(6000) })}`, { headers: { cookie } })).text();
73 const held = /<input[^>]*name="text"[^>]*value="([^"]*)"/.exec(out)?.[1] ?? "";
74 expect(held.length).toBe(TELL_MAX);
75 });
76
77 test("a url that is not http(s) is refused; a post from another origin files nothing", async () => {
78 expect(refuseShare(new URLSearchParams({ url: "javascript:alert(1)" }))).toContain("not a web address");
79 const before = thoughts();
80 const res = await fetch(`${servers.url}/tell`, {
81 method: "POST", headers: { cookie, origin: "https://evil.example" }, redirect: "manual",
82 body: new URLSearchParams({ text: "https://example.com/", app: "forge" }),
83 });
84 expect(res.status).toBe(403);
85 expect(thoughts()).toBe(before);
86 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs