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
scoutcrew
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 | ||
| 7 | 7 | **Phone.** With the forge installed as an app, share text or a link to it. The sheet POSTs |
| 8 | 8 | `title`, `text` and `url` to `/tell/share`, so the words are never in a logged URL. |
| 9 | 9 | |
| 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. | |
| 14 | 14 | - Nothing shared, a link that is not http(s), or more than 4,000 characters is refused with a |
| 15 | 15 | sentence; nothing is cut or held. A signed-out address may hold 10 shares a minute. |
| 16 | 16 |
src/web/studio/share_receive.ts +26 −8
| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * 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. | |
| 6 | 7 | * GET /tell/share after sign-in: the held words in a form that posts back here, filed |
| 7 | 8 | * on Send. |
| 8 | 9 | * 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 | ||
| 26 | 27 | import { btn, empty } from "../ui.ts"; |
| 27 | 28 | import { tellForm } from "./tell_form.ts"; |
| 28 | 29 | |
| 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 | ||
| 29 | 33 | const secure = (ctx: Ctx) => secureFor(ctx.url, ctx.app.config.url); |
| 30 | 34 | |
| 31 | 35 | function refused(ctx: Ctx, words: string, status: number, text = ""): Response { |
| @@ -41,6 +45,14 @@ function refused(ctx: Ctx, words: string, status: number, text = ""): Response { | ||
| 41 | 45 | ); |
| 42 | 46 | } |
| 43 | 47 | |
| 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 | ||
| 44 | 56 | /** POST /tell/share */ |
| 45 | 57 | export async function shareReceive(ctx: Ctx): Promise<Response> { |
| 46 | 58 | const form = await readForm(ctx, ctx.auth.user === undefined ? undefined : "rw"); |
| @@ -54,14 +66,19 @@ export async function shareReceive(ctx: Ctx): Promise<Response> { | ||
| 54 | 66 | // Nothing valid to keep: say so rather than hold it through a sign-in. |
| 55 | 67 | if (words !== undefined) return refused(ctx, words, refuseTell(draft) === words ? 422 : 400, draft); |
| 56 | 68 | 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); | |
| 58 | 70 | } |
| 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) }); | |
| 63 | 72 | } |
| 64 | 73 | 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 | |
| 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 | } | |
| 65 | 82 | const kept = keepThought(ctx.app, user, draft, "share"); |
| 66 | 83 | if (!kept.ok) return refused(ctx, "That could not be kept.", 400); |
| 67 | 84 | return redirect(`/tell/share/${kept.value.id}`); |
| @@ -82,6 +99,7 @@ export function shareHeld(ctx: Ctx): Response { | ||
| 82 | 99 | ? emptyNote("nothingIsWaitingToBeSent") |
| 83 | 100 | : html`<p class="muted small">Nothing is sent until you tap Send.</p> |
| 84 | 101 | <form class="stack" method="post" action="/tell/share"> |
| 102 | <input type="hidden" name="${CONFIRM_FIELD}" value="1"> | |
| 85 | 103 | <textarea class="input" name="${COMPOSER_FIELDS.text}" rows="6" required aria-label="Tell the crew">${draft}</textarea> |
| 86 | 104 | <div class="bar"><span class="spacer"></span>${btn("Send", { variant: "primary" })}</div> |
| 87 | 105 | </form>`}`, |
test/cookie-flags.test.ts +1 −1
| @@ -66,7 +66,7 @@ const COOKIES: Record<string, Spelled> = { | ||
| 66 | 66 | "src/apps/proxy.ts HIDE_COOKIE": { name: "forge-toolbar-hidden", lines: 1, why: "set by a same-origin POST to the toolbar door, tested in test/toolbar-hide.test.ts; HttpOnly, Lax, Secure over https" }, |
| 67 | 67 | "src/apps/feedback.ts VISITOR_COOKIE": { name: "forge_visitor", lines: 1, door: "app note" }, |
| 68 | 68 | "src/perf/visitor_id.ts COOKIE": { name: "forge_visitor", lines: 1, door: "page-view beacon" }, |
| 69 | "src/web/studio/share_receive.ts STASH_COOKIE": { name: "forge_share", lines: 2, why: "set by a same-origin POST /tell/share while signed out and cleared on the GET that reads it; HttpOnly, Lax, Secure over https; tested in test/share-target.test.ts" }, | |
| 69 | "src/web/studio/share_receive.ts STASH_COOKIE": { name: "forge_share", lines: 2, why: "set by a same-origin POST /tell/share and cleared on the GET that reads it; HttpOnly, Lax, Secure over https; tested in test/share-target.test.ts" }, | |
| 70 | 70 | "src/web/gift_cookie.ts NAME": { name: "forge_gift", lines: 2, door: "gift link" }, |
| 71 | 71 | "src/web/resume_cookie.ts cookie": { |
| 72 | 72 | name: /^forge_resume_[a-z0-9-]{1,39}$/, |
test/share-target.test.ts +49 −5
| @@ -154,9 +154,21 @@ const share = (fields: Record<string, string>, headers: Record<string, string> = | ||
| 154 | 154 | fetch(`${servers.url}/tell/share`, { method: "POST", headers, body: new URLSearchParams(fields), redirect: "manual" }); |
| 155 | 155 | const SHARED = { title: "A <b>page</b>", text: "look", url: "https://example.com/x?y=1" }; |
| 156 | 156 | |
| 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 () => { | |
sentinelcrew
The signed-in branch has a new rate limit ( |
||
| 158 | 158 | 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 <b>page</b>"); | |
| 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 }); | |
| 160 | 172 | expect(res.status).toBe(303); |
| 161 | 173 | expect(thoughts()).toBe(before + 1); |
| 162 | 174 | 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 | ||
| 223 | 235 | method: "POST", body: new URLSearchParams({ username: "bob", password: "a-good-passphrase" }), redirect: "manual", |
| 224 | 236 | }); |
| 225 | 237 | 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")!; | |
| 227 | 239 | expect((await fetch(`${servers.url}${at}`, { headers: { cookie } })).status).toBe(200); |
| 228 | 240 | expect((await fetch(`${servers.url}${at}`, { headers: { cookie: bobCookie } })).status).toBe(404); |
| 229 | 241 | }); |
| 230 | 242 | |
| 231 | 243 | test("a double POST signed in files two tells: a share is not deduplicated, each has its own receipt", async () => { |
| 232 | 244 | 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 }); | |
| 235 | 247 | expect(thoughts()).toBe(before + 2); |
| 236 | 248 | expect(one.headers.get("location")).not.toBe(two.headers.get("location")); |
| 237 | 249 | }); |
| @@ -248,3 +260,35 @@ test("a signed-out address is limited, by socket address, before anything is hel | ||
| 248 | 260 | expect(last!.status).toBe(429); |
| 249 | 261 | expect(last!.headers.get("set-cookie")).toBeNull(); |
| 250 | 262 | }); |
| 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 | }); |
new above
offline
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.