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

The web app manifest declares a share target so a phone's share sheet can tell the crew #16035

Merged opened this

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

Merged

scout/forge:scout/15976-the-web-app-manifest-declares-a-share-ta → main 0 commits fork 2 threads · 2 unresolved

  • Merged. scout/15976-the-web-app-manifest-declares-a-share-ta was merged into main as 305c618.

Review

Sign in to review this change.
4 files changed +80 −18
docs/features/share-receive.md +4 −4
@@ -7,10 +7,10 @@ row: Share to the forge from a phone's share sheet
77 **Phone.** With the forge installed as an app, share text or a link to it. The sheet POSTs
88 `title`, `text` and `url` to `/tell/share`, so the words are never in a logged URL.
99
10 - Signed in: one tell is filed as typed and you land on its receipt (`/tell/share/:id`), which
11 only you can open.
12 - Signed out: the words are held on the server for 15 minutes behind an HttpOnly cookie, you
13 sign in, and `/tell/share` shows them in a form; nothing is filed until you tap Send.
10 - A share never files by itself: the words are held on the server for 15 minutes behind an
11 HttpOnly cookie and `/tell/share` shows them in a form; nothing is filed until you tap Send.
12 Signed out, you sign in first and come back to that form. Send files the tell as typed and
13 lands on its receipt (`/tell/share/:id`), which only you can open.
1414 - Nothing shared, a link that is not http(s), or more than 4,000 characters is refused with a
1515 sentence; nothing is cut or held. A signed-out address may hold 10 shares a minute.
1616
src/web/studio/share_receive.ts +26 −8
@@ -1,8 +1,9 @@
11 /**
22 * The phone share sheet's POST target (manifest `share_target`, share_target.ts).
3 * POST /tell/share signed in: files one tell (a thought kept as typed) and redirects to
4 * its receipt; signed out: holds the words server-side (at most
5 * SHARES_PER_MINUTE per socket address) and sends the person to sign in.
3 * POST /tell/share from the share sheet: holds the words server-side (at most
4 * SHARES_PER_MINUTE per socket address) and shows them prefilled (signed in)
5 * or sends the person to sign in first; files nothing. With `send=1` (the
6 * confirming form) signed in: files one tell and redirects to its receipt.
67 * GET /tell/share after sign-in: the held words in a form that posts back here, filed
78 * on Send.
89 * GET /tell/share/:id the receipt: what was sent, to the person who sent it only.
@@ -26,6 +27,9 @@ import { COMPOSER_FIELDS, SHARE_PARAMS, refuseShare, refuseTell, sharedDraft } f
2627 import { btn, empty } from "../ui.ts";
2728 import { tellForm } from "./tell_form.ts";
2829
30 /** The field only the confirming form sends; a share sheet's post has none, so it files nothing. */
31 const CONFIRM_FIELD = "send";
32
2933 const secure = (ctx: Ctx) => secureFor(ctx.url, ctx.app.config.url);
3034
3135 function refused(ctx: Ctx, words: string, status: number, text = ""): Response {
@@ -41,6 +45,14 @@ function refused(ctx: Ctx, words: string, status: number, text = ""): Response {
4145 );
4246 }
4347
48 /** The 429 page: this address has held SHARES_PER_MINUTE shares this minute. */
49 const tooMany = (ctx: Ctx): Response =>
50 page({ ctx, title: "Share not sent", body: html`<h1>Too many shares</h1>${empty({ icon: "lock", title: "Too many shares from this address; try again in a minute.", action: btn("Back to the forge", { href: "/", variant: "primary" }) })}` }, 429);
51
52 /** Hold the words and give the cookie that names them. */
53 const hold = (ctx: Ctx, draft: string): string =>
54 setCookie(STASH_COOKIE, stashShare(ctx.app, draft), { maxAge: STASH_SECONDS, secure: secure(ctx) });
55
4456 /** POST /tell/share */
4557 export async function shareReceive(ctx: Ctx): Promise<Response> {
4658 const form = await readForm(ctx, ctx.auth.user === undefined ? undefined : "rw");
@@ -54,14 +66,19 @@ export async function shareReceive(ctx: Ctx): Promise<Response> {
5466 // Nothing valid to keep: say so rather than hold it through a sign-in.
5567 if (words !== undefined) return refused(ctx, words, refuseTell(draft) === words ? 422 : 400, draft);
5668 if (!mayHold(ctx.app, clientAddress(ctx.request))) {
57 return page({ ctx, title: "Share not sent", body: html`<h1>Too many shares</h1>${empty({ icon: "lock", title: "Too many shares from this address; try again in a minute.", action: btn("Back to the forge", { href: "/", variant: "primary" }) })}` }, 429);
69 return tooMany(ctx);
5870 }
59 const key = stashShare(ctx.app, draft);
60 const cookie = setCookie(STASH_COOKIE, key, { maxAge: STASH_SECONDS, secure: secure(ctx) });
61 const next = signInHref(ctx, "/tell/share");
62 return redirect(next, { cookie });
71 return redirect(signInHref(ctx, "/tell/share"), { cookie: hold(ctx, draft) });
6372 }
6473 if (words !== undefined) return refused(ctx, words, refuseTell(draft) === words ? 422 : 400, draft);
74 // A share arriving from the share sheet never files: it is held and shown, prefilled, for the
outdated

This 429 page is a copy of the one at line 65. Extract one tooMany(ctx) helper and use it in both places; do not paste the markup twice.

75 // person's own Send (the form below carries CONFIRM_FIELD; the share sheet never does).
76 if (field(form, CONFIRM_FIELD) !== "1") {
77 if (!mayHold(ctx.app, clientAddress(ctx.request))) {
78 return tooMany(ctx);
79 }
80 return redirect("/tell/share", { cookie: hold(ctx, draft) });
81 }
6582 const kept = keepThought(ctx.app, user, draft, "share");
6683 if (!kept.ok) return refused(ctx, "That could not be kept.", 400);
6784 return redirect(`/tell/share/${kept.value.id}`);
@@ -82,6 +99,7 @@ export function shareHeld(ctx: Ctx): Response {
8299 ? emptyNote("nothingIsWaitingToBeSent")
83100 : html`<p class="muted small">Nothing is sent until you tap Send.</p>
84101 <form class="stack" method="post" action="/tell/share">
102 <input type="hidden" name="${CONFIRM_FIELD}" value="1">
85103 <textarea class="input" name="${COMPOSER_FIELDS.text}" rows="6" required aria-label="Tell the crew">${draft}</textarea>
86104 <div class="bar"><span class="spacer"></span>${btn("Send", { variant: "primary" })}</div>
87105 </form>`}`,
test/share-target.test.ts +49 −5
@@ -154,9 +154,21 @@ const share = (fields: Record<string, string>, headers: Record<string, string> =
154154 fetch(`${servers.url}/tell/share`, { method: "POST", headers, body: new URLSearchParams(fields), redirect: "manual" });
155155 const SHARED = { title: "A <b>page</b>", text: "look", url: "https://example.com/x?y=1" };
156156
157 test("POST /tell/share signed in files one tell and redirects to an escaped receipt", async () => {
157 test("POST /tell/share signed in files nothing: it shows the words prefilled, and Send files them", async () => {

The signed-in branch has a new rate limit (mayHold before the redirect) and nothing tests it. Add a test that posts more than SHARES_PER_MINUTE signed-in shares from one address and expects 429 with nothing held. It must fail if that check is removed.

158158 const before = thoughts();
159 const res = await share(SHARED, { cookie, origin: servers.url });
159 const held = await share(SHARED, { cookie, origin: servers.url });
160 expect(held.status).toBe(303);
161 expect(held.headers.get("location")).toBe("/tell/share");
162 expect(thoughts()).toBe(before);
163 const page = await (await fetch(`${servers.url}/tell/share`, { headers: { cookie: `${cookie}; ${held.headers.get("set-cookie")!.split(";")[0]}` } })).text();
164 expect(page).toContain("A &lt;b&gt;page&lt;/b&gt;");
165 expect(page).toContain('name="send" value="1"');
166 expect(thoughts()).toBe(before);
167 });
168
169 test("POST /tell/share with the confirming field files one tell and redirects to an escaped receipt", async () => {
170 const before = thoughts();
171 const res = await share({ ...SHARED, send: "1" }, { cookie, origin: servers.url });
160172 expect(res.status).toBe(303);
161173 expect(thoughts()).toBe(before + 1);
162174 const at = res.headers.get("location")!;
@@ -223,15 +235,15 @@ test("another signed-in person gets 404 on a receipt that is not theirs", async
223235 method: "POST", body: new URLSearchParams({ username: "bob", password: "a-good-passphrase" }), redirect: "manual",
224236 });
225237 const bobCookie = (signed.headers.get("set-cookie") ?? "").split(";")[0]!;
226 const at = (await share(SHARED, { cookie, origin: servers.url })).headers.get("location")!;
238 const at = (await share({ ...SHARED, send: "1" }, { cookie, origin: servers.url })).headers.get("location")!;
227239 expect((await fetch(`${servers.url}${at}`, { headers: { cookie } })).status).toBe(200);
228240 expect((await fetch(`${servers.url}${at}`, { headers: { cookie: bobCookie } })).status).toBe(404);
229241 });
230242
231243 test("a double POST signed in files two tells: a share is not deduplicated, each has its own receipt", async () => {
232244 const before = thoughts();
233 const one = await share(SHARED, { cookie, origin: servers.url });
234 const two = await share(SHARED, { cookie, origin: servers.url });
245 const one = await share({ ...SHARED, send: "1" }, { cookie, origin: servers.url });
246 const two = await share({ ...SHARED, send: "1" }, { cookie, origin: servers.url });
235247 expect(thoughts()).toBe(before + 2);
236248 expect(one.headers.get("location")).not.toBe(two.headers.get("location"));
237249 });
@@ -248,3 +260,35 @@ test("a signed-out address is limited, by socket address, before anything is hel
248260 expect(last!.status).toBe(429);
249261 expect(last!.headers.get("set-cookie")).toBeNull();
250262 });
263
264 test("a signed-in address is limited too: past SHARES_PER_MINUTE the share is 429 and nothing is held", async () => {
265 const { SHARES_PER_MINUTE } = await import("../src/web/studio/share_receive.ts");
266 // A forge of its own, so the limiter starts empty whatever the tests above spent.
267 const dir = await mkdtemp(join(tmpdir(), "forge-share-limit-"));
268 const own = createApp({ dataDir: dir, port: 0, host: "forge.test" });
269 const srv = await serve(own);
270 try {
271 const made = await createUser(own.db, {
272 origin: "seed", trusted: true, username: "carol", email: "[email protected]",
273 password: "a-good-passphrase", adminIfFirst: true,
274 });
275 if (!made.ok) throw new Error(made.error);
276 const signed = await fetch(`${srv.url}/signin`, {
277 method: "POST", body: new URLSearchParams({ username: "carol", password: "a-good-passphrase" }), redirect: "manual",
278 });
279 const mine = (signed.headers.get("set-cookie") ?? "").split(";")[0]!;
280 const post = () => fetch(`${srv.url}/tell/share`, {
281 method: "POST", headers: { cookie: mine, origin: srv.url }, body: new URLSearchParams({ text: "hold me" }), redirect: "manual",
282 });
283 for (let i = 0; i < SHARES_PER_MINUTE; i++) expect((await post()).status).toBe(303);
284 const over = await post();
285 expect(over.status).toBe(429);
286 expect(over.headers.get("set-cookie")).toBeNull();
287 expect(over.headers.get("location")).toBeNull();
288 expect(own.db.query<{ n: number }, []>("SELECT count(*) AS n FROM thought").get()!.n).toBe(0);
289 } finally {
290 srv.stop();
291 own.close();
292 await rm(dir, { recursive: true, force: true });
293 }
294 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs