Public: anyone can read this; the crew can change it.
Tell from any forge page: the palette's "Tell the crew" opens a composer in place, and the tell carries the page it was told from #1028
scout/forge:scout/639-tell-from-any-forge-page-the-palette-s-t → main 1 commit fork
10 files
changed +357
−19
src/actions/tell.ts +24 −1
| @@ -39,11 +39,32 @@ export type TellError = | ||
| 39 | 39 | | "body_too_long" |
| 40 | 40 | | MirrorError; |
| 41 | 41 | |
| 42 | /** | |
| 43 | * The page a tell was told from, kept only when it is a path on this forge: | |
| 44 | * one leading slash, no scheme or host, no backslash, space or control | |
| 45 | * character, short. Anything else is dropped, never repaired — the line it | |
| 46 | * would have made is the crew's to trust. | |
| 47 | */ | |
| 48 | export function toldFromPath(raw: unknown): string | undefined { | |
| 49 | if (typeof raw !== "string") return undefined; | |
| 50 | if (raw.length === 0 || raw.length > 300) return undefined; | |
| 51 | if (!/^\/(?![/\\])[^\s\\\u0000-\u001f\u007f]*$/.test(raw)) return undefined; | |
| 52 | try { | |
| 53 | const u = new URL(raw, "http://forge.invalid"); | |
| 54 | if (u.origin !== "http://forge.invalid") return undefined; | |
| 55 | } catch { | |
| 56 | return undefined; | |
| 57 | } | |
| 58 | return raw; | |
| 59 | } | |
| 60 | ||
| 42 | 61 | export async function tell( |
| 43 | 62 | app: App, |
| 44 | 63 | actor: User | undefined, |
| 45 | 64 | repo: Repo, |
| 46 | 65 | said: string, |
| 66 | /** The forge page the person was on; see `toldFromPath`. */ | |
| 67 | from?: unknown, | |
| 47 | 68 | ): Promise<Result<Told, TellError>> { |
| 48 | 69 | // A tell is a run: only someone who may direct the crew here says one, |
| 49 | 70 | // and a refused tell opens nothing (`crew/direct.ts`). |
| @@ -58,9 +79,11 @@ export async function tell( | ||
| 58 | 79 | const firstLine = text.split(/\n/)[0]!.trim(); |
| 59 | 80 | const title = |
| 60 | 81 | firstLine.length <= 80 ? firstLine : `${firstLine.slice(0, 77).trimEnd()}…`; |
| 82 | const path = toldFromPath(from); | |
| 83 | const body = path === undefined ? text : `${text}\n\nTold from: ${path}`; | |
| 61 | 84 | const opened = openIssue(app, actor, repo, { |
| 62 | 85 | title, |
| 63 | body: text === firstLine ? "" : text, | |
| 86 | body: body === firstLine ? "" : body, | |
| 64 | 87 | }); |
| 65 | 88 | if (!opened.ok) return opened; |
| 66 | 89 | const crew = listCrewRoles(app.db, repo.id); |
src/http/mcp.ts +11 −2
| @@ -272,14 +272,23 @@ const TOOLS: readonly Tool[] = [ | ||
| 272 | 272 | "Tell an app's crew what should change. One sentence or several; the first line becomes the title. A builder is picked and starts on the crew's next tick.", |
| 273 | 273 | inputSchema: { |
| 274 | 274 | type: "object", |
| 275 | properties: { repo: REPO_ARG, text: { type: "string", maxLength: 8000 } }, | |
| 275 | properties: { | |
| 276 | repo: REPO_ARG, | |
| 277 | text: { type: "string", maxLength: 8000 }, | |
| 278 | from: { | |
| 279 | type: "string", | |
| 280 | maxLength: 300, | |
| 281 | description: | |
| 282 | "Optional forge page this was told from, a path like /owner/name/pulls/3/files. Kept as a 'Told from:' line; anything but a path on this forge is dropped.", | |
| 283 | }, | |
| 284 | }, | |
| 276 | 285 | required: ["repo", "text"], |
| 277 | 286 | }, |
| 278 | 287 | writes: true, |
| 279 | 288 | async run(ctx, user, args) { |
| 280 | 289 | const repo = repoArg(ctx, user, args); |
| 281 | 290 | if (isAnswer(repo)) return repo; |
| 282 | const told = await tell(ctx.app, user, repo, str(args["text"])); | |
| 291 | const told = await tell(ctx.app, user, repo, str(args["text"]), args["from"]); | |
| 283 | 292 | if (!told.ok) |
| 284 | 293 | return bad( |
| 285 | 294 | told.error, |
src/web/chrome.ts +34 −3
| @@ -34,6 +34,7 @@ import { | ||
| 34 | 34 | whereRow, |
| 35 | 35 | type RepoScope, |
| 36 | 36 | } from "./scope.ts"; |
| 37 | import { toldFromPath } from "../actions/tell.ts"; | |
| 37 | 38 | import { changesHref, tellsHref } from "./studio/nouns.ts"; |
| 38 | 39 | import { badge, btn, empty, tabs, type TabItem } from "./ui.ts"; |
| 39 | 40 | |
| @@ -348,6 +349,37 @@ export function scopeHeader( | ||
| 348 | 349 | |
| 349 | 350 | export { hostOf }; |
| 350 | 351 | |
| 352 | /** The page being read as a `from` query value, or "" when it is not a path | |
| 353 | * a tell may name (`toldFromPath`). */ | |
| 354 | function fromQuery(ctx: Ctx): string { | |
| 355 | const path = toldFromPath(ctx.url.pathname); | |
| 356 | return path === undefined ? "" : encodeURIComponent(path); | |
| 357 | } | |
| 358 | ||
| 359 | /** | |
| 360 | * "Tell the crew" for the palette, for whoever may direct it here | |
| 361 | * (`mayDirectCrew`; a reader gets no entry). With a script it opens a | |
| 362 | * composer in the dialog that posts to `tell` and carries `from`; without | |
| 363 | * one it is the link `h`. | |
| 364 | */ | |
| 365 | function tellEntry( | |
| 366 | ctx: Ctx, | |
| 367 | repo: Repo, | |
| 368 | name: string, | |
| 369 | h: string, | |
| 370 | ): PaletteItem[] { | |
| 371 | if (!mayDirectCrew(ctx.app.db, repo, ctx.auth.user)) return []; | |
| 372 | const from = toldFromPath(ctx.url.pathname); | |
| 373 | return [ | |
| 374 | { | |
| 375 | t: `${name} — Tell the crew`, | |
| 376 | h, | |
| 377 | c: "This repo", | |
| 378 | tell: { to: `${repoBase(repo)}/tell`, ...(from ? { from } : {}) }, | |
| 379 | }, | |
| 380 | ]; | |
| 381 | } | |
| 382 | ||
| 351 | 383 | /** This repo's own rows for the command palette — the studio's nouns for a |
| 352 | 384 | * studio-mode viewer of an app, so the palette never sends someone to a |
| 353 | 385 | * list under a name the address bar would not use. Commits is dev's own |
| @@ -369,9 +401,7 @@ export function repoPalette(ctx: Ctx, repo: Repo): PaletteItem[] { | ||
| 369 | 401 | // Telling the crew is for who may direct it (`mayDirectCrew`: a |
| 370 | 402 | // writer, or anyone signed in where the owner let outsiders in), so |
| 371 | 403 | // nobody else is offered a door that leads nowhere. |
| 372 | ...(mayDirectCrew(ctx.app.db, repo, ctx.auth.user) | |
| 373 | ? [{ t: `${name} — Tell the crew`, h: `${base}#tell`, c: "This repo" }] | |
| 374 | : []), | |
| 404 | ...tellEntry(ctx, repo, name, `${base}?from=${fromQuery(ctx)}#tell`), | |
| 375 | 405 | ...(mayReadRuns(ctx.app, ctx.auth.user, repo) |
| 376 | 406 | ? [{ t: `${name} — Runs`, h: `${base}/runs`, c: "This repo" }] |
| 377 | 407 | : []), |
| @@ -385,6 +415,7 @@ export function repoPalette(ctx: Ctx, repo: Repo): PaletteItem[] { | ||
| 385 | 415 | { t: `${name} — Pull requests`, h: `${base}/pulls`, c: "This repo" }, |
| 386 | 416 | { t: `${name} — Commits`, h: `${base}/commits`, c: "This repo" }, |
| 387 | 417 | { t: `${name} — New issue`, h: `${base}/issues/new`, c: "This repo" }, |
| 418 | ...tellEntry(ctx, repo, name, `${base}/issues/new`), | |
| 388 | 419 | ...(mayReadRuns(ctx.app, ctx.auth.user, repo) |
| 389 | 420 | ? [{ t: `${name} — Runs`, h: `${base}/runs`, c: "This repo" }] |
| 390 | 421 | : []), |
src/web/client.ts +78 −2
| @@ -1580,7 +1580,8 @@ export const shellJs = /* js */ ` | ||
| 1580 | 1580 | var input = dlg.querySelector('.palette-input'); |
| 1581 | 1581 | var list = dlg.querySelector('.palette-list'); |
| 1582 | 1582 | var items = (loadIndex().items || []).map(function (i) { |
| 1583 | return { t: String(i.t), h: String(i.h), g: i.c ? String(i.c) : 'Go to' }; | |
| 1583 | var tl = i.tell && typeof i.tell.to === 'string' && i.tell.to.charAt(0) === '/' ? i.tell : null; | |
| 1584 | return { t: String(i.t), h: String(i.h), g: i.c ? String(i.c) : 'Go to', p: tl }; | |
| 1584 | 1585 | }); |
| 1585 | 1586 | var recent = readRecent().map(function (r) { return { t: r.t, h: r.h, g: 'Recent', d: r.g }; }); |
| 1586 | 1587 | var recentHrefs = recent.map(function (r) { return r.h; }); |
| @@ -1702,6 +1703,78 @@ export const shellJs = /* js */ ` | ||
| 1702 | 1703 | }); |
| 1703 | 1704 | }, 120); |
| 1704 | 1705 | } |
| 1706 | // "Tell the crew" opens a composer where the list was: the same box, the | |
| 1707 | // same height, so nothing moves. It posts to the repository's own tell | |
| 1708 | // action (the same one the app page's form uses) with the page the | |
| 1709 | // palette was opened on; the receipt is one line in a line's own room. | |
| 1710 | function compose(r) { | |
| 1711 | var tl = r.p; | |
| 1712 | var form = doc.createElement('form'); | |
| 1713 | form.className = 'palette-tell'; | |
| 1714 | form.setAttribute('method', 'post'); | |
| 1715 | form.setAttribute('action', tl.to); | |
| 1716 | var box = doc.createElement('textarea'); | |
| 1717 | box.className = 'input'; | |
| 1718 | box.name = 'text'; | |
| 1719 | box.required = true; | |
| 1720 | box.maxLength = 8000; | |
| 1721 | box.placeholder = 'What should change? One thing at a time.'; | |
| 1722 | box.setAttribute('aria-label', r.t); | |
| 1723 | var bar = doc.createElement('div'); | |
| 1724 | bar.className = 'palette-tell-bar'; | |
| 1725 | var note = doc.createElement('span'); | |
| 1726 | note.className = 'palette-tell-note muted small'; | |
| 1727 | note.setAttribute('role', 'status'); | |
| 1728 | var send = doc.createElement('button'); | |
| 1729 | send.className = 'btn'; | |
| 1730 | send.setAttribute('data-variant', 'primary'); | |
| 1731 | send.type = 'submit'; | |
| 1732 | send.textContent = 'Send'; | |
| 1733 | bar.appendChild(note); bar.appendChild(send); | |
| 1734 | form.appendChild(box); form.appendChild(bar); | |
| 1735 | input.hidden = true; | |
| 1736 | list.textContent = ''; | |
| 1737 | list.appendChild(form); | |
| 1738 | dlg.onkeydown = function (e) { | |
| 1739 | if (e.key === 'Escape' || ((e.metaKey || e.ctrlKey) && (e.key === 'k' || e.key === 'K'))) { | |
| 1740 | e.preventDefault(); e.stopPropagation(); close(); return; | |
| 1741 | } | |
| 1742 | if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) { e.preventDefault(); form.requestSubmit ? form.requestSubmit() : send.click(); } | |
| 1743 | e.stopPropagation(); | |
| 1744 | }; | |
| 1745 | dlg.onpointerdown = null; | |
| 1746 | list.onclick = null; list.onpointermove = null; | |
| 1747 | form.onsubmit = function (e) { | |
| 1748 | if (typeof fetch === 'undefined') return; | |
| 1749 | e.preventDefault(); | |
| 1750 | var said = box.value.trim(); | |
| 1751 | if (said === '') return; | |
| 1752 | send.disabled = true; | |
| 1753 | note.textContent = 'Sending…'; | |
| 1754 | var body = new URLSearchParams(); | |
| 1755 | body.set('text', said); | |
| 1756 | if (tl.from) body.set('from', tl.from); | |
| 1757 | fetch(tl.to, { method: 'POST', headers: { accept: 'application/json' }, credentials: 'same-origin', body: body }) | |
| 1758 | .then(function (res) { return res.json().catch(function () { return { ok: false }; }); }) | |
| 1759 | .then(function (out) { | |
| 1760 | note.textContent = ''; | |
| 1761 | if (out && out.ok) { | |
| 1762 | box.value = ''; | |
| 1763 | note.appendChild(doc.createTextNode((out.flash || 'Told.') + ' ')); | |
| 1764 | if (typeof out.page === 'string' && out.page.charAt(0) === '/') { | |
| 1765 | var a = doc.createElement('a'); | |
| 1766 | a.href = out.page; | |
| 1767 | a.textContent = 'See the tell'; | |
| 1768 | note.appendChild(a); | |
| 1769 | } | |
| 1770 | } else { | |
| 1771 | note.textContent = (out && out.error) || 'That could not be told.'; | |
| 1772 | } | |
| 1773 | }, function () { note.textContent = 'Could not reach the forge. Try again.'; }) | |
| 1774 | .then(function () { send.disabled = false; }); | |
| 1775 | }; | |
| 1776 | box.focus(); | |
| 1777 | } | |
| 1705 | 1778 | function onKey(e) { |
| 1706 | 1779 | var k = e.key; |
| 1707 | 1780 | if (k === 'Escape' || ((e.metaKey || e.ctrlKey) && (k === 'k' || k === 'K'))) { |
| @@ -1728,6 +1801,7 @@ export const shellJs = /* js */ ` | ||
| 1728 | 1801 | dlg.onkeydown = null; dlg.onpointerdown = null; |
| 1729 | 1802 | list.onclick = null; list.onpointermove = null; |
| 1730 | 1803 | input.oninput = null; |
| 1804 | input.hidden = false; | |
| 1731 | 1805 | window.clearTimeout(remoteTimer); |
| 1732 | 1806 | if (remote) { remote.abort(); remote = null; } |
| 1733 | 1807 | closePalette = null; |
| @@ -1747,7 +1821,9 @@ export const shellJs = /* js */ ` | ||
| 1747 | 1821 | list.onclick = function (e) { |
| 1748 | 1822 | var a = e.target && e.target.closest ? e.target.closest('.palette-item') : null; |
| 1749 | 1823 | var i = a ? nodes.indexOf(a) : -1; |
| 1750 | if (i !== -1) remember(shown[i]); | |
| 1824 | if (i === -1) return; | |
| 1825 | if (shown[i].p && !(e.metaKey || e.ctrlKey || e.shiftKey)) { e.preventDefault(); compose(shown[i]); return; } | |
| 1826 | remember(shown[i]); | |
| 1751 | 1827 | }; |
| 1752 | 1828 | input.focus(); |
| 1753 | 1829 | } |
src/web/layout.ts +3
| @@ -43,6 +43,9 @@ export interface PaletteItem { | ||
| 43 | 43 | t: string; |
| 44 | 44 | h: string; |
| 45 | 45 | c?: string; |
| 46 | /** Opens a composer in the palette instead of following `h`: where the | |
| 47 | * tell posts, and the page it is told from. */ | |
| 48 | tell?: { to: string; from?: string }; | |
| 46 | 49 | } |
| 47 | 50 | |
| 48 | 51 | export interface PageOptions { |
src/web/studio/actions.ts +22 −10
| @@ -22,7 +22,7 @@ import type { Ctx } from "../../http/context.ts"; | ||
| 22 | 22 | import { loadRepo, notFoundPage, repoBase, signInFirst } from "../chrome.ts"; |
| 23 | 23 | import { field, readForm, type Form } from "../form.ts"; |
| 24 | 24 | import { flashOrJson, flashTo } from "../layout.ts"; |
| 25 | import { changeHref } from "./nouns.ts"; | |
| 25 | import { changeHref, tellHref } from "./nouns.ts"; | |
| 26 | 26 | |
| 27 | 27 | /** A merge refusal in a person's words. */ |
| 28 | 28 | function mergeWords(error: unknown): string { |
| @@ -48,13 +48,13 @@ async function tellFrom( | ||
| 48 | 48 | ): Promise<Response> { |
| 49 | 49 | const text = field(form, "text").trim(); |
| 50 | 50 | if (text === "") |
| 51 | return flashTo(ctx, back, { | |
| 51 | return flashOrJson(ctx, back, { | |
| 52 | 52 | tone: "danger", |
| 53 | 53 | message: "Say something first.", |
| 54 | 54 | }); |
| 55 | const told = await tell(ctx.app, ctx.auth.user, repo, text); | |
| 55 | const told = await tell(ctx.app, ctx.auth.user, repo, text, field(form, "from")); | |
| 56 | 56 | if (!told.ok) |
| 57 | return flashTo(ctx, back, { | |
| 57 | return flashOrJson(ctx, back, { | |
| 58 | 58 | tone: "danger", |
| 59 | 59 | message: |
| 60 | 60 | told.error === "forbidden" |
| @@ -63,15 +63,27 @@ async function tellFrom( | ||
| 63 | 63 | ? "Verify your email first." |
| 64 | 64 | : "That could not be told.", |
| 65 | 65 | }); |
| 66 | // Where the tell lives, for a script that keeps the page in place. | |
| 67 | const page = tellHref(repoBase(repo), told.value.issue.number); | |
| 66 | 68 | const { routed } = told.value; |
| 67 | 69 | if (routed === undefined) |
| 68 | return flashTo(ctx, back, { | |
| 69 | message: `Told. Nobody is on the crew to take it yet — add someone under Settings → Crew.`, | |
| 70 | }); | |
| 70 | return flashOrJson( | |
| 71 | ctx, | |
| 72 | back, | |
| 73 | { | |
| 74 | message: `Told. Nobody is on the crew to take it yet — add someone under Settings → Crew.`, | |
| 75 | }, | |
| 76 | { page, issue: told.value.issue.number }, | |
| 77 | ); | |
| 71 | 78 | const builder = routed.builder; |
| 72 | return flashTo(ctx, back, { | |
| 73 | message: `${builder.user.username} is on it — ${routed.size} ${routed.kind}${routed.by === "rule" ? "" : `, ${routed.by} decided in ${routed.ms} ms`}. Watch it work below.`, | |
| 74 | }); | |
| 79 | return flashOrJson( | |
| 80 | ctx, | |
| 81 | back, | |
| 82 | { | |
| 83 | message: `${builder.user.username} is on it — ${routed.size} ${routed.kind}${routed.by === "rule" ? "" : `, ${routed.by} decided in ${routed.ms} ms`}. Watch it work below.`, | |
| 84 | }, | |
| 85 | { page, issue: told.value.issue.number }, | |
| 86 | ); | |
| 75 | 87 | } |
| 76 | 88 | |
| 77 | 89 | /** POST /:owner/:name/tell { text } — a sentence becomes work. */ |
src/web/studio/app.ts +6
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | * for a viewer who can write here; a stranger sees the app. |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | import { toldFromPath } from "../../actions/tell.ts"; | |
| 12 | 13 | import { sealedScope } from "../../core/sealed.ts"; |
| 13 | 14 | import { egressSetting } from "../../core/capacity.ts"; |
| 14 | 15 | import { egressNote } from "./egress.ts"; |
| @@ -271,6 +272,8 @@ export async function studioApp( | ||
| 271 | 272 | // ---- the box ---- |
| 272 | 273 | // Telling the crew, and who takes it, is the owner's own front door — a |
| 273 | 274 | // stranger sees the app, not who is behind it. |
| 275 | // The palette's no-script door says which page it came from (?from=). | |
| 276 | const toldFrom = toldFromPath(ctx.url.searchParams.get("from")); | |
| 274 | 277 | const tell = !ownerFacts |
| 275 | 278 | ? null |
| 276 | 279 | : html`<form |
| @@ -278,6 +281,9 @@ export async function studioApp( | ||
| 278 | 281 | method="post" |
| 279 | 282 | action="${base}/tell" |
| 280 | 283 | > |
| 284 | ${toldFrom === undefined | |
| 285 | ? null | |
| 286 | : html`<input type="hidden" name="from" value="${toldFrom}" />`} | |
| 281 | 287 | <label class="sr-only" for="tell">Tell the crew</label> |
| 282 | 288 | <textarea |
| 283 | 289 | class="input" |
src/web/style/components.ts +5
| @@ -551,6 +551,11 @@ a.row:hover .row-title { color: var(--accent); } | ||
| 551 | 551 | /* One highlight. The pointer moves it (the script listens) rather than |
| 552 | 552 | painting a second one under the cursor; the finder keeps plain hover. */ |
| 553 | 553 | .palette-item[aria-selected="true"], .finder .palette-item:hover { background: var(--muted); } |
| 554 | /* The composer takes the list's own box: same height, so nothing shifts. */ | |
| 555 | .palette-tell { display: flex; flex-direction: column; gap: 8px; height: 100%; padding: 8px; } | |
| 556 | .palette-tell textarea { flex: 1; min-height: 0; resize: none; } | |
| 557 | .palette-tell-bar { display: flex; align-items: center; gap: 10px; min-height: 36px; } | |
| 558 | .palette-tell-note { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } | |
| 554 | 559 | .palette-foot { |
| 555 | 560 | display: flex; align-items: center; gap: 10px; padding: 8px 14px; |
| 556 | 561 | border-top: 1px solid var(--border); font-size: 12px; color: var(--faint); |
test/studio-words.test.ts +3 −1
| @@ -813,7 +813,9 @@ describe("dev's vocabulary never reaches a studio page", () => { | ||
| 813 | 813 | expect(rows.get("alice/vocab — Changes")).toBe(`${repoPath}/changes`); |
| 814 | 814 | // The studio's first verb — telling the crew — reachable from the |
| 815 | 815 | // palette, landing on the app page where the tell box lives. |
| 816 | expect(rows.get("alice/vocab — Tell the crew")).toBe(`${repoPath}#tell`); | |
| 816 | expect(rows.get("alice/vocab — Tell the crew")).toBe( | |
| 817 | `${repoPath}?from=${encodeURIComponent(repoPath)}#tell`, | |
| 818 | ); | |
| 817 | 819 | }); |
| 818 | 820 | |
| 819 | 821 | test("an old ?mode=dev address on any of these routes is the same page without it (#603)", async () => { |
test/tell-from.test.ts added +171
| @@ -0,0 +1,171 @@ | ||
| 1 | /** | |
| 2 | * Tell from any forge page: the palette's "Tell the crew" carries the page it | |
| 3 | * was told from, the tell ends with one "Told from:" line, and only a path on | |
| 4 | * this forge is kept. A reader is offered no entry. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { toldFromPath } from "../src/actions/tell.ts"; | |
| 12 | import { createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { listIssues } from "../src/core/issues.ts"; | |
| 14 | import type { Repo } from "../src/core/repos.ts"; | |
| 15 | import { createToken } from "../src/core/tokens.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 18 | ||
| 19 | describe("tell from any forge page", () => { | |
| 20 | let dataDir: string; | |
| 21 | let app: App; | |
| 22 | let servers: Servers; | |
| 23 | let alice: User; | |
| 24 | let repo: Repo; | |
| 25 | let aliceAuth: Record<string, string>; | |
| 26 | let bobAuth: Record<string, string>; | |
| 27 | let nextId = 1; | |
| 28 | ||
| 29 | const index = async (auth: Record<string, string>, path: string) => { | |
| 30 | const res = await fetch(`${servers.url}${path}`, { headers: auth }); | |
| 31 | expect(res.status).toBe(200); | |
| 32 | const block = (await res.text()).match( | |
| 33 | /<script type="application\/json" id="palette-index">([\s\S]*?)<\/script>/, | |
| 34 | )?.[1]; | |
| 35 | return JSON.parse(block!) as { | |
| 36 | items: { t: string; h: string; tell?: { to: string; from?: string } }[]; | |
| 37 | }; | |
| 38 | }; | |
| 39 | const post = (from: string | undefined, text = "make it purple") => | |
| 40 | fetch(`${servers.url}/alice/site/tell`, { | |
| 41 | method: "POST", | |
| 42 | headers: { ...aliceAuth, accept: "application/json" }, | |
| 43 | body: new URLSearchParams({ text, ...(from === undefined ? {} : { from }) }), | |
| 44 | }); | |
| 45 | ||
| 46 | beforeAll(async () => { | |
| 47 | dataDir = await mkdtemp(join(tmpdir(), "forge-tellfrom-")); | |
| 48 | app = createApp({ dataDir, port: 0 }); | |
| 49 | servers = await serve(app); | |
| 50 | const a = await createUser(app.db, { | |
| 51 | trusted: true, | |
| 52 | username: "alice", | |
| 53 | email: "[email protected]", | |
| 54 | password: "a-good-passphrase", | |
| 55 | adminIfFirst: true, | |
| 56 | }); | |
| 57 | const b = await createUser(app.db, { | |
| 58 | trusted: true, | |
| 59 | username: "bob", | |
| 60 | email: "[email protected]", | |
| 61 | password: "a-good-passphrase", | |
| 62 | }); | |
| 63 | if (!a.ok || !b.ok) throw new Error("users"); | |
| 64 | alice = a.value; | |
| 65 | const bob = b.value; | |
| 66 | const made = await createRepo(app, alice, { | |
| 67 | name: "site", | |
| 68 | visibility: "public", | |
| 69 | description: "", | |
| 70 | }); | |
| 71 | if (!made.ok) throw new Error(String(made.error)); | |
| 72 | repo = made.value; | |
| 73 | aliceAuth = { | |
| 74 | authorization: `Bearer ${createToken(app.db, alice.id, "t", "rw").token}`, | |
| 75 | }; | |
| 76 | bobAuth = { | |
| 77 | authorization: `Bearer ${createToken(app.db, bob.id, "t", "rw").token}`, | |
| 78 | }; | |
| 79 | }); | |
| 80 | afterAll(async () => { | |
| 81 | await servers.stop(); | |
| 82 | await rm(dataDir, { recursive: true, force: true }); | |
| 83 | }); | |
| 84 | ||
| 85 | test("only a path on this forge is kept", () => { | |
| 86 | expect(toldFromPath("/alice/site/pulls/3/files")).toBe("/alice/site/pulls/3/files"); | |
| 87 | expect(toldFromPath("/alice/site/blob/main/a.ts#L3")).toBe("/alice/site/blob/main/a.ts#L3"); | |
| 88 | for (const bad of [ | |
| 89 | "https://evil.test/x", | |
| 90 | "//evil.test/x", | |
| 91 | "/\\evil.test", | |
| 92 | "javascript:alert(1)", | |
| 93 | "alice/site", | |
| 94 | "/a b", | |
| 95 | "/a\nTold from: /x", | |
| 96 | "", | |
| 97 | "/" + "a".repeat(400), | |
| 98 | 42, | |
| 99 | undefined, | |
| 100 | ]) | |
| 101 | expect(toldFromPath(bad)).toBeUndefined(); | |
| 102 | }); | |
| 103 | ||
| 104 | test("the palette entry opens a composer from the page it was opened on", async () => { | |
| 105 | const { items } = await index(aliceAuth, "/alice/site/issues"); | |
| 106 | const row = items.find((i) => i.t === "alice/site — Tell the crew"); | |
| 107 | expect(row?.tell).toEqual({ to: "/alice/site/tell", from: "/alice/site/issues" }); | |
| 108 | // Without a script it is a link to a form that knows the page. | |
| 109 | expect(row!.h).toContain("/alice/site/issues/new"); | |
| 110 | }); | |
| 111 | ||
| 112 | test("a posted tell ends with the page it was told from, and answers with a link", async () => { | |
| 113 | const res = await post("/alice/site/pulls/3/files"); | |
| 114 | const out = (await res.json()) as { ok: boolean; page: string }; | |
| 115 | expect(out.ok).toBe(true); | |
| 116 | expect(out.page).toMatch(/^\/alice\/site\/(issues|tells)\/\d+$/); | |
| 117 | const issue = listIssues(app.db, repo.id, { state: "open" }).at(-1)!; | |
| 118 | expect(issue.title).toBe("make it purple"); | |
| 119 | expect(issue.body).toBe("make it purple\n\nTold from: /alice/site/pulls/3/files"); | |
| 120 | }); | |
| 121 | ||
| 122 | test("a cross-origin from is dropped, the tell still goes", async () => { | |
| 123 | await post("https://evil.test/phish", "second thought"); | |
| 124 | const issue = listIssues(app.db, repo.id, { state: "open" }).find( | |
| 125 | (i) => i.title === "second thought", | |
| 126 | )!; | |
| 127 | expect(issue.body).toBe(""); | |
| 128 | }); | |
| 129 | ||
| 130 | test("a cross-origin post is refused", async () => { | |
| 131 | const res = await fetch(`${servers.url}/alice/site/tell`, { | |
| 132 | method: "POST", | |
| 133 | headers: { ...aliceAuth, origin: "https://evil.test" }, | |
| 134 | body: new URLSearchParams({ text: "nope" }), | |
| 135 | }); | |
| 136 | expect(res.status).toBe(403); | |
| 137 | }); | |
| 138 | ||
| 139 | test("the MCP tell takes the same from", async () => { | |
| 140 | const call = async (args: unknown) => | |
| 141 | ( | |
| 142 | await fetch(`${servers.url}/mcp`, { | |
| 143 | method: "POST", | |
| 144 | headers: { | |
| 145 | ...aliceAuth, | |
| 146 | "content-type": "application/json", | |
| 147 | accept: "application/json, text/event-stream", | |
| 148 | }, | |
| 149 | body: JSON.stringify({ | |
| 150 | jsonrpc: "2.0", | |
| 151 | id: nextId++, | |
| 152 | method: "tools/call", | |
| 153 | params: { name: "tell", arguments: args }, | |
| 154 | }), | |
| 155 | }) | |
| 156 | ).json() as Promise<any>; | |
| 157 | await call({ repo: "alice/site", text: "from an agent", from: "/alice/site/runs/1" }); | |
| 158 | await call({ repo: "alice/site", text: "from a stranger", from: "//evil.test" }); | |
| 159 | const all = listIssues(app.db, repo.id, { state: "open" }); | |
| 160 | expect(all.find((i) => i.title === "from an agent")!.body).toBe( | |
| 161 | "from an agent\n\nTold from: /alice/site/runs/1", | |
| 162 | ); | |
| 163 | expect(all.find((i) => i.title === "from a stranger")!.body).toBe(""); | |
| 164 | }); | |
| 165 | ||
| 166 | test("a reader who cannot tell here is offered no entry", async () => { | |
| 167 | const { items } = await index(bobAuth, "/alice/site"); | |
| 168 | expect(items.some((i) => i.t.endsWith("Tell the crew"))).toBe(false); | |
| 169 | expect(items.some((i) => i.tell !== undefined)).toBe(false); | |
| 170 | }); | |
| 171 | }); |
new above
offline