Public: anyone can read this; the crew can change it.
PATCH issue validates the whole body before it saves the sealed flag, so a refused request changes nothing #15001
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/14989-patch-issue-validates-the-whole-body-bef → main 4 commits fork
3 files
changed +253
−24
src/actions/issues.ts +20 −3
| @@ -286,6 +286,25 @@ export function editIssue( | ||
| 286 | 286 | return updated; |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | /** | |
| 290 | * Whether `actor` may seal or unseal this issue, asked without changing | |
| 291 | * it: the writer who may, or the one word why not. A request that carries | |
| 292 | * a seal with other acts asks this before it writes any of them. | |
| 293 | */ | |
| 294 | export function maySetSealed( | |
| 295 | app: App, | |
| 296 | actor: User | undefined, | |
| 297 | repo: Repo, | |
| 298 | issue: Issue, | |
| 299 | sealed: boolean, | |
| 300 | ): Result<User, AccessError | MirrorError> { | |
| 301 | const writer = requireWriter(app, actor, repo); | |
| 302 | if (!writer.ok) return writer; | |
| 303 | if (issue.repoId !== repo.id) return err("not_found"); | |
| 304 | if (sealed && isMirror(app, repo)) return err("mirrored"); | |
| 305 | return writer; | |
| 306 | } | |
| 307 | ||
| 289 | 308 | /** |
| 290 | 309 | * Seal or unseal an issue or a pull (`core/sealed.ts`). A writer's call, |
| 291 | 310 | * deliberate and recorded: unsealing publishes a security report and its |
| @@ -298,10 +317,8 @@ export function setIssueSealed( | ||
| 298 | 317 | issue: Issue, |
| 299 | 318 | sealed: boolean, |
| 300 | 319 | ): Result<Issue, AccessError | MirrorError> { |
| 301 | const writer = requireWriter(app, actor, repo); | |
| 320 | const writer = maySetSealed(app, actor, repo, issue, sealed); | |
| 302 | 321 | if (!writer.ok) return writer; |
| 303 | if (issue.repoId !== repo.id) return err("not_found"); | |
| 304 | if (sealed && isMirror(app, repo)) return err("mirrored"); | |
| 305 | 322 | if (setSealed(app.db, issue.id, sealed)) |
| 306 | 323 | app.bus.append({ |
| 307 | 324 | kind: sealed ? "issue.sealed" : "issue.unsealed", |
src/http/api/issues.ts +41 −21
| @@ -20,6 +20,7 @@ import { | ||
| 20 | 20 | addCommentThrough, |
| 21 | 21 | closingFrom, |
| 22 | 22 | editIssue, |
| 23 | maySetSealed, | |
| 23 | 24 | openIssueThrough, |
| 24 | 25 | setIssueLabelsThrough, |
| 25 | 26 | setIssueSealed, |
| @@ -507,32 +508,46 @@ export function issueRoutes(router: Router<Handler>): void { | ||
| 507 | 508 | const input = await body(ctx.request); |
| 508 | 509 | // Three acts a PATCH may carry, each its own authorization and each |
| 509 | 510 | // alone a whole request: sealing (a writer's), new words, a new state. |
| 510 | let issue = found.issue; | |
| 511 | if (typeof input.sealed === "boolean") { | |
| 512 | const sealed = setIssueSealed( | |
| 511 | // The whole body is judged before any of them is written, so a | |
| 512 | // refused request changes nothing (#14989). | |
| 513 | const seal = typeof input.sealed === "boolean" ? input.sealed : undefined; | |
| 514 | if (seal !== undefined) { | |
| 515 | const may = maySetSealed( | |
| 513 | 516 | ctx.app, |
| 514 | 517 | ctx.auth.user, |
| 515 | 518 | found.repo, |
| 516 | issue, | |
| 517 | input.sealed, | |
| 519 | found.issue, | |
| 520 | seal, | |
| 518 | 521 | ); |
| 519 | if (!sealed.ok) return originAware(sealed.error, 409); | |
| 520 | issue = sealed.value; | |
| 522 | if (!may.ok) return originAware(may.error, 409); | |
| 521 | 523 | } |
| 522 | 524 | const text = textFrom(input); |
| 523 | 525 | if (text === undefined) return json({ error: "invalid_text" }, 422); |
| 524 | 526 | const wanted = input.state === undefined ? undefined : str(input.state); |
| 525 | 527 | if (wanted !== undefined && wanted !== "open" && wanted !== "closed") |
| 526 | 528 | return json({ error: "invalid_state" }, 422); |
| 527 | if ( | |
| 528 | wanted === undefined && | |
| 529 | text.title === undefined && | |
| 530 | text.body === undefined | |
| 531 | ) | |
| 532 | return typeof input.sealed === "boolean" | |
| 533 | ? json(withLabels(ctx, issue)) | |
| 534 | : NOTHING_TO_CHANGE(); | |
| 535 | if (text.title !== undefined || text.body !== undefined) { | |
| 529 | const closing = wanted === undefined ? undefined : closingFrom(input); | |
| 530 | if (wanted !== undefined && closing === undefined) | |
| 531 | return json({ error: "invalid_reason" }, 422); | |
| 532 | const words = text.title !== undefined || text.body !== undefined; | |
| 533 | if (wanted === undefined && !words && seal === undefined) | |
| 534 | return NOTHING_TO_CHANGE(); | |
| 535 | // A seal goes on first and comes off last, so new words or a new | |
| 536 | // state never stand in the open between two acts. A seal this | |
| 537 | // request put on comes off again when a later act is refused. | |
| 538 | let issue = found.issue; | |
| 539 | const wasSealed = issue.sealed; | |
| 540 | const seals = (sealed: boolean) => | |
| 541 | setIssueSealed(ctx.app, ctx.auth.user, found.repo, issue, sealed); | |
| 542 | const undo = () => { | |
| 543 | if (seal === true && !wasSealed) seals(false); | |
| 544 | }; | |
| 545 | if (seal === true) { | |
| 546 | const sealed = seals(true); | |
| 547 | if (!sealed.ok) return originAware(sealed.error, 409); | |
| 548 | issue = sealed.value; | |
| 549 | } | |
| 550 | if (words) { | |
| 536 | 551 | const edited = editIssue( |
| 537 | 552 | ctx.app, |
| 538 | 553 | ctx.auth.user, |
| @@ -540,16 +555,15 @@ export function issueRoutes(router: Router<Handler>): void { | ||
| 540 | 555 | issue, |
| 541 | 556 | text, |
| 542 | 557 | ); |
| 543 | if (!edited.ok) | |
| 558 | if (!edited.ok) { | |
| 559 | undo(); | |
| 544 | 560 | return edited.error === "taken_up" |
| 545 | 561 | ? takenUp() |
| 546 | 562 | : originAware(edited.error, 422); |
| 563 | } | |
| 547 | 564 | issue = edited.value; |
| 548 | 565 | } |
| 549 | if (wanted !== undefined) { | |
| 550 | const closing = closingFrom(input); | |
| 551 | if (closing === undefined) | |
| 552 | return json({ error: "invalid_reason" }, 422); | |
| 566 | if (wanted !== undefined && closing !== undefined) { | |
| 553 | 567 | const changed = await setIssueStateThrough( |
| 554 | 568 | ctx.app, |
| 555 | 569 | ctx.auth.user, |
| @@ -559,6 +573,7 @@ export function issueRoutes(router: Router<Handler>): void { | ||
| 559 | 573 | closing, |
| 560 | 574 | ); |
| 561 | 575 | if (!changed.ok) { |
| 576 | undo(); | |
| 562 | 577 | // A planner told no learns which rule refused it. |
| 563 | 578 | const rule = |
| 564 | 579 | changed.error === "forbidden" && wanted === "closed" && ctx.auth.user |
| @@ -569,6 +584,11 @@ export function issueRoutes(router: Router<Handler>): void { | ||
| 569 | 584 | } |
| 570 | 585 | issue = changed.value; |
| 571 | 586 | } |
| 587 | if (seal === false) { | |
| 588 | const unsealed = seals(false); | |
| 589 | if (!unsealed.ok) return originAware(unsealed.error, 409); | |
| 590 | issue = unsealed.value; | |
| 591 | } | |
| 572 | 592 | return json(withLabels(ctx, issue)); |
| 573 | 593 | }, |
| 574 | 594 | ); |
test/patch-issue-whole-body.test.ts added +192
| @@ -0,0 +1,192 @@ | ||
| 1 | /** | |
| 2 | * PATCH on an issue judges the whole body before it writes any of it | |
| 3 | * (#14989). A body that carries a seal with a malformed state, reason or | |
| 4 | * text, or with an act its sender may not take, is refused and leaves the | |
| 5 | * issue as it was: sealed stays sealed, open stays open, and the outbox | |
| 6 | * records no seal that the answer says did not happen. | |
| 7 | */ | |
| 8 | ||
| 9 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 10 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 11 | import { tmpdir } from "node:os"; | |
| 12 | import { join } from "node:path"; | |
| 13 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 14 | import { openIssue } from "../src/actions/issues.ts"; | |
| 15 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 16 | import { createApp, type App } from "../src/app.ts"; | |
| 17 | import { listEvents } from "../src/core/events.ts"; | |
| 18 | import { getIssueById, MAX_TITLE, type Issue } from "../src/core/issues.ts"; | |
| 19 | import type { Repo } from "../src/core/repos.ts"; | |
| 20 | import { createToken } from "../src/core/tokens.ts"; | |
| 21 | import { createUser, type User } from "../src/core/users.ts"; | |
| 22 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 23 | import { quietLogs } from "./helpers/globals.ts"; | |
| 24 | ||
| 25 | quietLogs(); | |
| 26 | ||
| 27 | describe("PATCH issue refuses a body whole", () => { | |
| 28 | let dataDir: string; | |
| 29 | let app: App; | |
| 30 | let servers: Servers; | |
| 31 | let alice: User; | |
| 32 | let bot: User; | |
| 33 | let repo: Repo; | |
| 34 | const tokens: Record<string, string> = {}; | |
| 35 | ||
| 36 | const patch = (who: string, issue: Issue, payload: unknown) => | |
| 37 | fetch(`${servers.url}/api/v1/repos/alice/garden/issues/${issue.number}`, { | |
| 38 | method: "PATCH", | |
| 39 | headers: { | |
| 40 | authorization: `Bearer ${tokens[who]}`, | |
| 41 | "content-type": "application/json", | |
| 42 | }, | |
| 43 | body: JSON.stringify(payload), | |
| 44 | }); | |
| 45 | const now = (issue: Issue) => getIssueById(app.db, issue.id)!; | |
| 46 | const sealEvents = (issue: Issue) => | |
| 47 | listEvents(app.db, { repoId: repo.id, limit: 200 }).filter( | |
| 48 | (e) => | |
| 49 | (e.kind === "issue.sealed" || e.kind === "issue.unsealed") && | |
| 50 | e.payload["number"] === issue.number, | |
| 51 | ).length; | |
| 52 | const open = (title: string, sealed: boolean): Issue => { | |
| 53 | const made = openIssue(app, alice, repo, { title, sealed }); | |
| 54 | if (!made.ok) throw new Error(String(made.error)); | |
| 55 | return made.value; | |
| 56 | }; | |
| 57 | ||
| 58 | beforeAll(async () => { | |
| 59 | dataDir = await mkdtemp(join(tmpdir(), "forge-patch-whole-")); | |
| 60 | app = createApp({ dataDir, port: 0 }); | |
| 61 | servers = await serve(app); | |
| 62 | const made = await createUser(app.db, { | |
| 63 | origin: "seed", | |
| 64 | trusted: true, | |
| 65 | username: "alice", | |
| 66 | email: "[email protected]", | |
| 67 | password: "a-good-passphrase", | |
| 68 | adminIfFirst: true, | |
| 69 | }); | |
| 70 | if (!made.ok) throw new Error(made.error); | |
| 71 | alice = made.value; | |
| 72 | const created = await createRepo(app, alice, { | |
| 73 | name: "garden", | |
| 74 | visibility: "public", | |
| 75 | }); | |
| 76 | if (!created.ok) throw new Error(String(created.error)); | |
| 77 | repo = created.value; | |
| 78 | await commitToBranch(app, alice, repo, { | |
| 79 | message: "seed", | |
| 80 | files: [{ path: "README.md", content: "# garden\n" }], | |
| 81 | }); | |
| 82 | const agent = await createAgent(app, alice, { username: "bot" }); | |
| 83 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 84 | bot = agent.value.user; | |
| 85 | expect(addToCrew(app, alice, repo, "bot").ok).toBe(true); | |
| 86 | for (const user of [alice, bot]) | |
| 87 | tokens[user.username] = createToken(app.db, user.id, "t", "rw").token; | |
| 88 | }); | |
| 89 | ||
| 90 | afterAll(async () => { | |
| 91 | await servers.stop(); | |
| 92 | app.close(); | |
| 93 | await rm(dataDir, { recursive: true, force: true }); | |
| 94 | }); | |
| 95 | ||
| 96 | test("a malformed state, reason or text leaves a sealed report sealed", async () => { | |
| 97 | const report = open("A hole in the fence", true); | |
| 98 | for (const [payload, error] of [ | |
| 99 | [{ sealed: false, state: "bogus" }, "invalid_state"], | |
| 100 | [{ sealed: false, state: "closed", reason: "bogus" }, "invalid_reason"], | |
| 101 | [{ sealed: false, title: 7 }, "invalid_text"], | |
| 102 | ] as const) { | |
| 103 | const res = await patch("alice", report, payload); | |
| 104 | expect({ payload, status: res.status }).toEqual({ payload, status: 422 }); | |
| 105 | expect(((await res.json()) as { error: string }).error).toBe(error); | |
| 106 | expect(now(report).sealed).toBe(true); | |
| 107 | } | |
| 108 | expect(sealEvents(report)).toBe(0); | |
| 109 | }); | |
| 110 | ||
| 111 | test("a malformed state leaves an open issue unsealed", async () => { | |
| 112 | const issue = open("Water the beds", false); | |
| 113 | const res = await patch("alice", issue, { sealed: true, state: "bogus" }); | |
| 114 | expect(res.status).toBe(422); | |
| 115 | expect(now(issue).sealed).toBe(false); | |
| 116 | expect(sealEvents(issue)).toBe(0); | |
| 117 | }); | |
| 118 | ||
| 119 | test("an edit the issue refuses keeps the seal as it was", async () => { | |
| 120 | // The title is judged where it is written: the request reaches that | |
| 121 | // write with its seal already asked for. | |
| 122 | const report = open("The gate does not latch", true); | |
| 123 | const unseal = await patch("alice", report, { sealed: false, title: "" }); | |
| 124 | expect(unseal.status).toBe(422); | |
| 125 | expect(now(report)).toMatchObject({ | |
| 126 | sealed: true, | |
| 127 | title: "The gate does not latch", | |
| 128 | }); | |
| 129 | expect(sealEvents(report)).toBe(0); | |
| 130 | ||
| 131 | const issue = open("Weed the rows", false); | |
| 132 | const reseal = await patch("alice", issue, { | |
| 133 | sealed: true, | |
| 134 | title: "x".repeat(MAX_TITLE + 1), | |
| 135 | }); | |
| 136 | expect(reseal.status).toBe(422); | |
| 137 | expect(now(issue)).toMatchObject({ sealed: false, title: "Weed the rows" }); | |
| 138 | }); | |
| 139 | ||
| 140 | test("a close the issue refuses keeps the seal as it was", async () => { | |
| 141 | // A split into an issue that does not exist is well formed, so it | |
| 142 | // passes the handler and is refused where the state is written. | |
| 143 | const refusedClose = { | |
| 144 | state: "closed", | |
| 145 | reason: "split", | |
| 146 | into: [9999], | |
| 147 | } as const; | |
| 148 | const issue = open("Mend the trellis", false); | |
| 149 | const reseal = await patch("alice", issue, { sealed: true, ...refusedClose }); | |
| 150 | expect(reseal.status).toBe(422); | |
| 151 | expect(((await reseal.json()) as { error: string }).error).toBe( | |
| 152 | "invalid_reason", | |
| 153 | ); | |
| 154 | expect(now(issue)).toMatchObject({ sealed: false, state: "open" }); | |
| 155 | ||
| 156 | // The seal comes off last, so a refused close never reaches it. | |
| 157 | const report = open("The trellis wire is live", true); | |
| 158 | const unseal = await patch("alice", report, { | |
| 159 | sealed: false, | |
| 160 | ...refusedClose, | |
| 161 | }); | |
| 162 | expect(unseal.status).toBe(422); | |
| 163 | expect(now(report)).toMatchObject({ sealed: true, state: "open" }); | |
| 164 | expect(sealEvents(report)).toBe(0); | |
| 165 | }); | |
| 166 | ||
| 167 | test("a sender who may not seal changes nothing", async () => { | |
| 168 | const report = open("The hose leaks", true); | |
| 169 | const res = await patch("bot", report, { sealed: false, title: "x" }); | |
| 170 | expect(res.status).toBe(403); | |
| 171 | expect(now(report)).toMatchObject({ sealed: true, title: "The hose leaks" }); | |
| 172 | }); | |
| 173 | ||
| 174 | test("a whole body that may be taken is taken whole", async () => { | |
| 175 | const report = open("The shed lock", true); | |
| 176 | const res = await patch("alice", report, { | |
| 177 | sealed: false, | |
| 178 | title: "The shed lock, fixed", | |
| 179 | state: "closed", | |
| 180 | }); | |
| 181 | expect(res.status).toBe(200); | |
| 182 | expect(now(report)).toMatchObject({ | |
| 183 | sealed: false, | |
| 184 | title: "The shed lock, fixed", | |
| 185 | state: "closed", | |
| 186 | }); | |
| 187 | const sealOnly = open("Compost", false); | |
| 188 | const sealed = await patch("alice", sealOnly, { sealed: true }); | |
| 189 | expect(sealed.status).toBe(200); | |
| 190 | expect(now(sealOnly).sealed).toBe(true); | |
| 191 | }); | |
| 192 | }); |
new above
offline