Public: anyone can read this; the crew can change it.
Tell the crew from a toolbar carries the page it was sent from, proven in a test and visible on the issue #16000
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15975-tell-the-crew-from-a-toolbar-carries-the → main 0 commits fork 1 thread · 1 unresolved
3 files
changed +46
−5
src/actions/tell_context.ts +13 −2
| @@ -18,6 +18,15 @@ export interface TellContext { | ||
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | const SAID_MAX = 2000; |
| 21 | ||
| 22 | /** The most characters one line of a tell's context may carry; longer is trimmed, never refused. */ | |
| 23 | export const CONTEXT_LINE_MAX = 300; | |
| 24 | ||
| 25 | /** A context value cut to `max` characters, never inside a `%XX` escape, so it still reads. */ | |
| 26 | export function trimContext(value: string, max: number = CONTEXT_LINE_MAX): string { | |
| 27 | if (value.length <= max) return value; | |
| 28 | return value.slice(0, max).replace(/%[0-9A-F]?$/i, ""); | |
| 29 | } | |
| 21 | 30 | const RUN = /^[A-Za-z0-9_-]{1,64}$/; |
| 22 | 31 | const COMMIT = /^[0-9a-f]{7,64}$/; |
| 23 | 32 | |
| @@ -157,7 +166,7 @@ export function contextNote(c: TellContext | undefined, opts: { preview?: boolea | ||
| 157 | 166 | const app = opts.app ?? c.app; |
| 158 | 167 | const preview = opts.preview === true ? previewLine(c.pull, c.page, app) : ""; |
| 159 | 168 | const lines = [ |
| 160 | preview !== "" || c.page === undefined ? "" : `- page: ${c.page}`, | |
| 169 | preview !== "" || c.page === undefined ? "" : `- page: ${trimContext(c.page, CONTEXT_LINE_MAX - 8)}`, | |
| 161 | 170 | preview !== "" || c.pull === undefined ? "" : `- pull: #${c.pull}`, |
| 162 | 171 | preview !== "" || app === undefined || !APP_SLUG.test(app) |
| 163 | 172 | ? "" |
| @@ -184,5 +193,7 @@ function fromLine(page: string | undefined, app: string | undefined): string { | ||
| 184 | 193 | /[\\\s\x00-\x1f\x7f<>()\[\]`"'*_~|!#&]/g, |
| 185 | 194 | (ch) => `%${ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`, |
| 186 | 195 | ); |
| 187 | return parseFrom(path) === path ? `From: ${app} ${path}` : ""; | |
| 196 | const head = `From: ${app} `; | |
| 197 | const kept = trimContext(path, CONTEXT_LINE_MAX - head.length); | |
| 198 | return parseFrom(kept) === kept ? `${head}${kept}` : ""; | |
| 188 | 199 | } |
src/actions/tell_from.ts +5 −3
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | * what the server would before it spends a round trip. |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | import { scrubQuery } from "./tell_context.ts"; | |
| 6 | import { CONTEXT_LINE_MAX, scrubQuery, trimContext } from "./tell_context.ts"; | |
| 7 | 7 | |
| 8 | 8 | /** The most characters a tell's words may have; every door refuses more. */ |
| 9 | 9 | export const TELL_TEXT_MAX = 4000; |
| @@ -66,7 +66,9 @@ export function previewLine( | ||
| 66 | 66 | /[\\\s\x00-\x1f\x7f<>()\[\]`"'*_~|!#&]/g, |
| 67 | 67 | (c) => `%${c.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`, |
| 68 | 68 | ); |
| 69 | if (parseFrom(path) !== path) return ""; | |
| 70 | 69 | const named = app !== undefined && APP_SLUG.test(app) ? `${app} ` : ""; |
| 71 | return `From: ${named}${path} (preview of #${pull})`; | |
| 70 | const tail = ` (preview of #${pull})`; | |
| 71 | const kept = trimContext(path, CONTEXT_LINE_MAX - `From: ${named}`.length - tail.length); | |
| 72 | if (parseFrom(kept) !== kept) return ""; | |
| 73 | return `From: ${named}${kept}${tail}`; | |
| 72 | 74 | } |
test/apps-toolbar-tell.test.ts +28
| @@ -175,6 +175,34 @@ describe("tell the crew from the pill", () => { | ||
| 175 | 175 | expect(issue.body).not.toContain("- pull:"); |
| 176 | 176 | }); |
| 177 | 177 | |
| 178 | test("a tell from a preview's toolbar reads back through the API with its app, path and pull on one line", async () => { | |
| 179 | const res = await post( | |
| 180 | { text: "Read me back", page: "https://notes-pull-3.apps.test/deep/page?q=1", pull: "3" }, | |
| 181 | { origin: servers.url }, | |
| 182 | ); | |
| 183 | expect(res.status).toBe(200); | |
| 184 | const n = listIssues(app.db, repoId, {}).find((i) => i.title === "Read me back")!.number; | |
| 185 | const api = await fetch(`${servers.url}/api/v1/repos/alice/notes/issues/${n}`, { headers: auth }); | |
| 186 | expect(api.status).toBe(200); | |
| 187 | const body = ((await api.json()) as { body: string }).body; | |
| 188 | expect(body.split("\n").filter((l) => l.startsWith("From:"))).toEqual(["From: notes /deep/page (preview of #3)"]); | |
| 189 | }); | |
| 190 | ||
| 191 | test("a context longer than 300 characters is trimmed, not refused", async () => { | |
| 192 | const long = "/" + "a".repeat(900); | |
| 193 | const res = await post({ text: "Long context", page: `https://notes-pull-3.apps.test${long}`, pull: "3" }); | |
| 194 | expect(res.status).toBe(200); | |
| 195 | const body = listIssues(app.db, repoId, {}).find((i) => i.title === "Long context")!.body; | |
| 196 | const line = body.split("\n").find((l) => l.startsWith("From:"))!; | |
| 197 | expect(line.length).toBeLessThanOrEqual(300); | |
| 198 | expect(line).toContain("(preview of #3)"); | |
| 199 | for (const l of body.split("\n")) expect(l.length).toBeLessThanOrEqual(300); | |
| 200 | const prod = await post({ text: "Long prod", page: `https://notes.apps.test/${"%41".repeat(200)}` }); | |
| 201 | expect(prod.status).toBe(200); | |
| 202 | for (const l of listIssues(app.db, repoId, {}).find((i) => i.title === "Long prod")!.body.split("\n")) | |
| 203 | expect(l.length).toBeLessThanOrEqual(300); | |
| 204 | }); | |
| 205 | ||
| 178 | 206 | test("a hostile page path is encoded in the filed line, and a hostile pull is refused", async () => { |
| 179 | 207 | const res = await post({ |
| 180 | 208 | text: "Hostile path", |
new above
offline
Nit: move trimContext below the RUN and COMMIT constants so the constants stay together.