|
|
@@ -0,0 +1,169 @@ |
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
import { keepThought } from "../../actions/thought.ts"; |
| 13 |
import { expireCookie, setCookie } from "../../auth/cookies.ts"; |
| 14 |
import type { Ctx } from "../../http/context.ts"; |
| 15 |
import { cookieValue } from "../../http/context.ts"; |
| 16 |
import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| 17 |
import { secureFor } from "../../http/cookie_secure.ts"; |
| 18 |
import { bucketOf, clientAddress } from "../../http/peer.ts"; |
| 19 |
import { RateLimiter } from "../../core/ratelimit.ts"; |
| 20 |
import { notFoundPage, signInHref } from "../chrome.ts"; |
| 21 |
import { field, readForm } from "../form.ts"; |
| 22 |
import { html } from "../html.ts"; |
| 23 |
import { page, redirect } from "../layout.ts"; |
| 24 |
import { COMPOSER_FIELDS, SHARE_PARAMS, refuseShare, refuseTell, sharedDraft } from "../share_target.ts"; |
| 25 |
import { btn, empty } from "../ui.ts"; |
| 26 |
import { tellForm } from "./tell_form.ts"; |
| 27 |
|
| 28 |
const secure = (ctx: Ctx) => secureFor(ctx.url, ctx.app.config.url); |
| 29 |
|
| 30 |
function refused(ctx: Ctx, words: string, status: number, text = ""): Response { |
| 31 |
return page( |
| 32 |
{ |
| 33 |
ctx, |
| 34 |
title: "Share not sent", |
| 35 |
body: status === 422 |
| 36 |
? html`<h1>Tell not sent</h1>${tellForm({ text, refusal: words, hidden: html`` })}` |
|
|
|
| 37 |
: html`<h1>That share was not kept</h1>${empty({ icon: "lock", title: words, action: btn("Back to the forge", { href: "/", variant: "primary" }) })}`, |
| 38 |
}, |
| 39 |
status, |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
export async function shareReceive(ctx: Ctx): Promise<Response> { |
| 45 |
const form = await readForm(ctx, ctx.auth.user === undefined ? undefined : "rw"); |
| 46 |
if (form instanceof Response) return form; |
| 47 |
const params = new URLSearchParams(); |
| 48 |
for (const key of [...Object.values(SHARE_PARAMS), COMPOSER_FIELDS.text]) params.set(key, field(form, key)); |
| 49 |
const draft = sharedDraft(params); |
| 50 |
const words = refuseShare(params); |
| 51 |
const user = ctx.auth.user; |
| 52 |
if (user === undefined) { |
| 53 |
|
| 54 |
if (words !== undefined) return refused(ctx, words, refuseTell(draft) === words ? 422 : 400, draft); |
| 55 |
if (!mayHold(ctx.app, clientAddress(ctx.request))) { |
| 56 |
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); |
| 57 |
} |
| 58 |
const key = stashShare(ctx.app, draft); |
| 59 |
const cookie = setCookie(STASH_COOKIE, key, { maxAge: STASH_SECONDS, secure: secure(ctx) }); |
| 60 |
const next = signInHref(ctx, "/tell/share"); |
| 61 |
return redirect(next, { cookie }); |
| 62 |
} |
| 63 |
if (words !== undefined) return refused(ctx, words, refuseTell(draft) === words ? 422 : 400, draft); |
| 64 |
const kept = keepThought(ctx.app, user, draft, "share"); |
| 65 |
if (!kept.ok) return refused(ctx, "That could not be kept.", 400); |
| 66 |
return redirect(`/tell/share/${kept.value.id}`); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
export function shareHeld(ctx: Ctx): Response { |
| 71 |
notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 72 |
const rendering = performance.now(); |
| 73 |
if (ctx.auth.user === undefined) return redirect(signInHref(ctx, "/tell/share")); |
| 74 |
const draft = takeShare(ctx.app, cookieValue(ctx.request, STASH_COOKIE)); |
| 75 |
const clear = expireCookie(STASH_COOKIE, { secure: secure(ctx) }); |
| 76 |
const response = page({ |
| 77 |
ctx, |
| 78 |
title: "Shared words · held", |
| 79 |
body: html`<h1>Tell the crew</h1> |
| 80 |
${draft === undefined |
| 81 |
? html`<p class="muted">Nothing is waiting to be sent.</p>` |
| 82 |
: html`<p class="muted small">Nothing is sent until you tap Send.</p> |
| 83 |
<form class="stack" method="post" action="/tell/share"> |
| 84 |
<textarea class="input" name="${COMPOSER_FIELDS.text}" rows="6" required aria-label="Tell the crew">${draft}</textarea> |
| 85 |
<div class="bar"><span class="spacer"></span>${btn("Send", { variant: "primary" })}</div> |
| 86 |
</form>`}`, |
| 87 |
}); |
| 88 |
response.headers.append("set-cookie", clear); |
| 89 |
notePhase(ctx.request, "render", performance.now() - rendering); |
| 90 |
return response; |
| 91 |
} |
| 92 |
|
| 93 |
/** GET /tell/share/:id */ |
| 94 |
export function shareReceipt(ctx: Ctx): Response { |
| 95 |
notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 96 |
const user = ctx.auth.user; |
| 97 |
if (user === undefined) return redirect(signInHref(ctx)); |
| 98 |
const row = ctx.app.db |
| 99 |
.query<{ text: string; received_at: string }, [string, string]>( |
| 100 |
"SELECT text, received_at FROM thought WHERE id = ?1 AND author = ?2 AND source = 'share'", |
| 101 |
) |
| 102 |
.get(ctx.params.id ?? "", user.id); |
| 103 |
if (row === null) return notFoundPage(ctx); |
| 104 |
return page({ |
| 105 |
ctx, |
|
|
|
| 106 |
title: "Tell sent", |
|
|
|
| 107 |
body: html`<h1>Sent to the crew</h1> |
| 108 |
<dl class="status-rows" data-receipt> |
| 109 |
<div class="status-row"><dt>Received</dt><dd>${row.received_at}</dd></div> |
| 110 |
<div class="status-row"><dt>Text</dt><dd class="kept-words">${row.text}</dd></div> |
| 111 |
</dl> |
| 112 |
<p>${btn("Back to the feed", { href: "/feed", variant: "primary" })}</p>`, |
| 113 |
}); |
| 114 |
} |
| 115 |
|
| 116 |
// Held words: a share that arrived signed out, kept on the server while its sender signs in. They |
| 117 |
// never ride in a URL (logs, history, Referer keep those); the browser holds only a random |
| 118 |
// HttpOnly cookie naming them. In memory on purpose: a few minutes of hand-off. |
| 119 |
type Held = Map<string, { draft: string; at: number }>; |
| 120 |
/** Per forge, not per process: two forges in one process never see each other's shares. */ |
| 121 |
const HELD = new WeakMap<object, Held>(); |
| 122 |
const heldShares = (app: object): Held => { |
| 123 |
let m = HELD.get(app); |
| 124 |
if (m === undefined) HELD.set(app, (m = new Map())); |
| 125 |
return m; |
| 126 |
}; |
| 127 |
const HELD_FOR_MS = 15 * 60 * 1000; |
| 128 |
const HELD_KEEP = 200; |
| 129 |
|
| 130 |
/** The cookie that names a held share. */ |
| 131 |
export const STASH_COOKIE = "forge_share"; |
| 132 |
export const STASH_SECONDS = HELD_FOR_MS / 1000; |
| 133 |
const KEY = /^[0-9a-f]{32}$/; |
| 134 |
|
| 135 |
/** Signed-out holds one socket address may start in a minute; past it the forge says to wait. */ |
| 136 |
export const SHARES_PER_MINUTE = 10; |
| 137 |
const LIMITERS = new WeakMap<object, RateLimiter>(); |
| 138 |
|
| 139 |
/** |
| 140 |
* Whether this address may hold another share. `address` is what the listener resolved from the |
| 141 |
* socket (`clientAddress`), never a header a caller wrote; counted per address so one flood cannot |
| 142 |
* evict other people's held shares. |
| 143 |
*/ |
| 144 |
export function mayHold(app: object, address: string): boolean { |
| 145 |
let limiter = LIMITERS.get(app); |
| 146 |
if (limiter === undefined) { |
| 147 |
limiter = new RateLimiter({ perMinute: SHARES_PER_MINUTE, adminPerMinute: SHARES_PER_MINUTE, enabled: true, maxKeys: 1_000 }); |
| 148 |
LIMITERS.set(app, limiter); |
| 149 |
} |
| 150 |
return limiter.take(`share:${bucketOf(address)}`, "ro").allowed; |
| 151 |
} |
| 152 |
|
| 153 |
/** Hold a draft; the key to put in the cookie. */ |
| 154 |
export function stashShare(app: object, draft: string, now = Date.now()): string { |
| 155 |
const held = heldShares(app); |
| 156 |
for (const [k, v] of held) if (now - v.at >= HELD_FOR_MS || held.size >= HELD_KEEP) held.delete(k); |
| 157 |
const key = Array.from(crypto.getRandomValues(new Uint8Array(16)), (b) => b.toString(16).padStart(2, "0")).join(""); |
| 158 |
held.set(key, { draft, at: now }); |
| 159 |
return key; |
| 160 |
} |
| 161 |
|
| 162 |
/** The draft a key names, given up once (a second ask gets nothing), or undefined. */ |
| 163 |
export function takeShare(app: object, key: string | undefined, now = Date.now()): string | undefined { |
| 164 |
if (key === undefined || !KEY.test(key)) return undefined; |
| 165 |
const map = heldShares(app); |
| 166 |
const held = map.get(key); |
| 167 |
map.delete(key); |
| 168 |
return held !== undefined && now - held.at < HELD_FOR_MS ? held.draft : undefined; |
| 169 |
} |
Both doors: this how-to names only the phone and the web receipt. The terminal has no screen for share-filed tells, so what a person can do on the page they cannot do in the terminal. Restore a src/tui/ screen and say here how to open it.