Public: anyone can read this; the crew can change it.
Sealed reports: an edit that closes one from a public pull is refused like an open; to a stranger the number closes nothing #960
steward/927-sealed-followups → main 1 commit
7 files
changed +174
−27
src/actions/issues.ts +36 −6
| @@ -18,7 +18,13 @@ | ||
| 18 | 18 | * "mirrored" on the others, which is honest rather than silently wrong. |
| 19 | 19 | */ |
| 20 | 20 | |
| 21 | import { canSeeSealed, mayView, setSealed } from "../core/sealed.ts"; | |
| 21 | import { | |
| 22 | canSeeSealed, | |
| 23 | mayView, | |
| 24 | refusedInPublic, | |
| 25 | sealedClosedBy, | |
| 26 | setSealed, | |
| 27 | } from "../core/sealed.ts"; | |
| 22 | 28 | import { requireVerified } from "./verify.ts"; |
| 23 | 29 | import type { App } from "../app.ts"; |
| 24 | 30 | import { crewRole, isCrew } from "../core/agents.ts"; |
| @@ -45,8 +51,8 @@ import { | ||
| 45 | 51 | standsForPerson, |
| 46 | 52 | } from "../core/issues.ts"; |
| 47 | 53 | import { isStructural, pillarsOf } from "../core/pillars.ts"; |
| 48 | import { getPull } from "../core/pulls.ts"; | |
| 49 | import { canRead, canWrite, type Repo } from "../core/repos.ts"; | |
| 54 | import { closesFromBody, getPull } from "../core/pulls.ts"; | |
| 55 | import { canRead, canWrite, getRepoById, type Repo } from "../core/repos.ts"; | |
| 50 | 56 | import { getUserById, type User } from "../core/users.ts"; |
| 51 | 57 | import { |
| 52 | 58 | directorsAmong, |
| @@ -117,7 +123,8 @@ export type EditError = | ||
| 117 | 123 | | TitleError |
| 118 | 124 | | "body_too_long" |
| 119 | 125 | | "nothing_to_change" |
| 120 | | "taken_up"; | |
| 126 | | "taken_up" | |
| 127 | | "sealed_in_public"; | |
| 121 | 128 | |
| 122 | 129 | /** The sentence a refused author reads: someone who directs the crew acted. */ |
| 123 | 130 | export const TAKEN_UP = "a writer has taken this up: comment instead"; |
| @@ -225,7 +232,12 @@ export function editIssue( | ||
| 225 | 232 | actor: User | undefined, |
| 226 | 233 | repo: Repo, |
| 227 | 234 | issue: Issue, |
| 228 | input: { title?: string | undefined; body?: string | undefined }, | |
| 235 | input: { | |
| 236 | title?: string | undefined; | |
| 237 | body?: string | undefined; | |
| 238 | /** A person's deliberate yes to new words that close a sealed report on a public pull. */ | |
| 239 | confirmPublic?: boolean | undefined; | |
| 240 | }, | |
| 229 | 241 | ): Result<Issue, EditError> { |
| 230 | 242 | const user = requireParticipant(app, actor, repo); |
| 231 | 243 | if (!user.ok) return user; |
| @@ -241,7 +253,25 @@ export function editIssue( | ||
| 241 | 253 | (field) => input[field] !== undefined && input[field] !== issue[field], |
| 242 | 254 | ); |
| 243 | 255 | if (changed.length === 0) return ok(issue); |
| 244 | const updated = updateIssueText(app.db, issue.id, input); | |
| 256 | // New words on a pull that close a sealed report are the same accident as | |
| 257 | // opening one that does (#927's review): refused from a public head | |
| 258 | // unless a person says yes. Reports the editor may not see close nothing. | |
| 259 | if (issue.kind === "pull" && input.body !== undefined) { | |
| 260 | const before = new Set(closesFromBody(issue.body)); | |
| 261 | const added = closesFromBody(input.body).filter((n) => !before.has(n)); | |
| 262 | const fixes = sealedClosedBy(app.db, repo, added, user.value); | |
| 263 | const pull = getPull(app.db, repo.id, issue.number); | |
| 264 | const head = | |
| 265 | pull === undefined || pull.headRepoId === repo.id | |
| 266 | ? undefined | |
| 267 | : getRepoById(app.db, pull.headRepoId); | |
| 268 | if (refusedInPublic(fixes, head, repo, user.value, input.confirmPublic)) | |
| 269 | return err("sealed_in_public"); | |
| 270 | } | |
| 271 | const updated = updateIssueText(app.db, issue.id, { | |
| 272 | ...(input.title === undefined ? {} : { title: input.title }), | |
| 273 | ...(input.body === undefined ? {} : { body: input.body }), | |
| 274 | }); | |
| 245 | 275 | if (!updated.ok) return updated; |
| 246 | 276 | app.bus.append({ |
| 247 | 277 | kind: `${issue.kind}.edited`, |
src/actions/pulls.ts +8 −10
| @@ -33,7 +33,7 @@ import { | ||
| 33 | 33 | import { isStructural, pillarsOf } from "../core/pillars.ts"; |
| 34 | 34 | import type { Repo } from "../core/repos.ts"; |
| 35 | 35 | import { canRead, getRepoById } from "../core/repos.ts"; |
| 36 | import { sealedByNumber } from "../core/sealed.ts"; | |
| 36 | import { refusedInPublic, sealedClosedBy } from "../core/sealed.ts"; | |
| 37 | 37 | import { |
| 38 | 38 | addReviewComment, |
| 39 | 39 | anchorContent, |
| @@ -190,15 +190,10 @@ export async function openPull( | ||
| 190 | 190 | // A fix for a sealed report, from a branch anyone can read (this |
| 191 | 191 | // repository, or a public fork), publishes where the hole is. Refused |
| 192 | 192 | // unless a person says yes to that; the private repository is the way. |
| 193 | const fixesSealed = closesFromBody(input.body ?? "").some((n) => | |
| 194 | sealedByNumber(app.db, baseRepo.id, n), | |
| 195 | ); | |
| 196 | const apart = head !== undefined && head.sealedFor === baseRepo.id; | |
| 197 | if ( | |
| 198 | fixesSealed && | |
| 199 | !apart && | |
| 200 | !(input.confirmPublic === true && user.value.kind !== "agent") | |
| 201 | ) | |
| 193 | // Only reports the author may see count: to anyone else the number | |
| 194 | // closes nothing, and is never named as sealed. | |
| 195 | const fixes = sealedClosedBy(app.db, baseRepo, closesFromBody(input.body ?? ""), user.value); | |
| 196 | if (refusedInPublic(fixes, head, baseRepo, user.value, input.confirmPublic)) | |
| 202 | 197 | return err("sealed_in_public"); |
| 203 | 198 | const created = await createPull( |
| 204 | 199 | app.db, |
| @@ -217,6 +212,7 @@ export async function openPull( | ||
| 217 | 212 | } |
| 218 | 213 | : {}), |
| 219 | 214 | ...(input.revertsIssueId ? { revertsIssueId: input.revertsIssueId } : {}), |
| 215 | sealed: fixes.length > 0, | |
| 220 | 216 | }, |
| 221 | 217 | ); |
| 222 | 218 | if (!created.ok) return created; |
| @@ -433,6 +429,8 @@ export async function mergePull( | ||
| 433 | 429 | for (const n of closesFromBody(pull.body)) { |
| 434 | 430 | const issue = getIssue(app.db, repo.id, n); |
| 435 | 431 | if (!issue || issue.kind !== "issue" || issue.state !== "open") continue; |
| 432 | // A public pull never closes a sealed report: only its sealed fix does. | |
| 433 | if (issue.sealed && !pull.sealed) continue; | |
| 436 | 434 | if (isStructural(plan, issue.id)) continue; |
| 437 | 435 | const closed = setIssueStateCore(app.db, issue.id, "closed"); |
| 438 | 436 | if (!closed) continue; |
src/core/pulls.ts +11 −3
| @@ -119,6 +119,12 @@ export interface CreatePullInput { | ||
| 119 | 119 | headDir?: string; |
| 120 | 120 | /** The issue id of the pull this one reverts ("Take it back"). */ |
| 121 | 121 | revertsIssueId?: string; |
| 122 | /** | |
| 123 | * Whether the pull is sealed with the report it fixes, when its caller has | |
| 124 | * decided (`actions/pulls.ts`: only reports the author may see count). | |
| 125 | * Derived from the body when absent. | |
| 126 | */ | |
| 127 | sealed?: boolean; | |
| 122 | 128 | } |
| 123 | 129 | |
| 124 | 130 | /** |
| @@ -197,9 +203,11 @@ export async function createPull( | ||
| 197 | 203 | |
| 198 | 204 | // The fix for a sealed report is sealed with it (`sealed.ts`), and its |
| 199 | 205 | // objects never enter the base before it merges (`pullDir`). |
| 200 | const sealed = closesFromBody(input.body ?? "").some((n) => | |
| 201 | sealedByNumber(db, input.repoId, n), | |
| 202 | ); | |
| 206 | const sealed = | |
| 207 | input.sealed ?? | |
| 208 | closesFromBody(input.body ?? "").some((n) => | |
| 209 | sealedByNumber(db, input.repoId, n), | |
| 210 | ); | |
| 203 | 211 | const keptApart = |
| 204 | 212 | sealed && |
| 205 | 213 | crossRepo && |
src/core/sealed.ts +36
| @@ -103,6 +103,42 @@ export function sealedRowByNumber( | ||
| 103 | 103 | return row ? { sealed: row.sealed === 1, authorId: row.author_id } : undefined; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | /** | |
| 107 | * The sealed reports a pull's words say they close, among those `user` may | |
| 108 | * see. A number `user` may not see is, to them, a number that closes | |
| 109 | * nothing: never named as sealed, never sealing their pull, never closed by | |
| 110 | * it (#927's review: no existence oracle). | |
| 111 | */ | |
| 112 | export function sealedClosedBy( | |
| 113 | db: Database, | |
| 114 | repo: Repo, | |
| 115 | numbers: readonly number[], | |
| 116 | user: User | undefined, | |
| 117 | ): number[] { | |
| 118 | return numbers.filter((n) => { | |
| 119 | const row = sealedRowByNumber(db, repo.id, n); | |
| 120 | return row?.sealed === true && mayView(db, repo, { sealed: true, authorId: row.authorId }, user); | |
| 121 | }); | |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * Whether words that close these sealed reports may stand on a pull whose | |
| 126 | * head is `head` (undefined: the base itself): yes from the base's sealed | |
| 127 | * private repository; from anywhere public only on a person's deliberate | |
| 128 | * yes, never an agent's. The one rule for opening a pull and editing one. | |
| 129 | */ | |
| 130 | export function refusedInPublic( | |
| 131 | fixes: readonly number[], | |
| 132 | head: Repo | undefined, | |
| 133 | base: Repo, | |
| 134 | user: User, | |
| 135 | confirmPublic: boolean | undefined, | |
| 136 | ): boolean { | |
| 137 | if (fixes.length === 0) return false; | |
| 138 | if (head !== undefined && head.sealedFor === base.id) return false; | |
| 139 | return !(confirmPublic === true && user.kind !== "agent"); | |
| 140 | } | |
| 141 | ||
| 106 | 142 | /** Whether issue `number` in `repoId` is sealed. */ |
| 107 | 143 | export function sealedByNumber( |
| 108 | 144 | db: Database, |
src/http/api.ts +10 −2
| @@ -590,6 +590,12 @@ function sourceFailure(error: SourceError): Response { | ||
| 590 | 590 | * worth retrying against. |
| 591 | 591 | */ |
| 592 | 592 | function originAware(error: string, status = 422): Response { |
| 593 | if (error === "sealed_in_public") | |
| 594 | return apiError( | |
| 595 | "sealed_in_public", | |
| 596 | 422, | |
| 597 | "these words close a sealed report on a pull whose branch is public: open the fix from your private repository (POST …/issues/<n>/private), or send confirmPublic: true", | |
| 598 | ); | |
| 593 | 599 | if (error === "invalid_reason") |
| 594 | 600 | return apiError( |
| 595 | 601 | "invalid_reason", |
| @@ -629,8 +635,10 @@ function originAware(error: string, status = 422): Response { | ||
| 629 | 635 | */ |
| 630 | 636 | function textFrom( |
| 631 | 637 | input: Record<string, unknown>, |
| 632 | ): { title?: string; body?: string } | undefined { | |
| 633 | const out: { title?: string; body?: string } = {}; | |
| 638 | ): { title?: string; body?: string; confirmPublic?: boolean } | undefined { | |
| 639 | const out: { title?: string; body?: string; confirmPublic?: boolean } = {}; | |
| 640 | // A person's deliberate yes to new words that close a sealed report on a public pull. | |
| 641 | if (input.confirmPublic === true) out.confirmPublic = true; | |
| 634 | 642 | for (const key of ["title", "body"] as const) { |
| 635 | 643 | const value = input[key]; |
| 636 | 644 | if (value === undefined) continue; |
src/web/pages/thread.ts +19 −3
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | * is what makes a long thread skimmable. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { mayView } from "../../core/sealed.ts"; | |
| 11 | import { canSeeSealed, mayView } from "../../core/sealed.ts"; | |
| 12 | 12 | import { listCrew } from "../../actions/crew.ts"; |
| 13 | 13 | import { |
| 14 | 14 | confirmIssue, |
| @@ -231,7 +231,7 @@ export interface ConversationOptions { | ||
| 231 | 231 | * the body as they stand, one form, one Save. Folded away until asked for, |
| 232 | 232 | * since most visits read and few rewrite. |
| 233 | 233 | */ |
| 234 | function editForm(issue: Issue, action: string): Html { | |
| 234 | function editForm(issue: Issue, action: string, offerPublish: boolean): Html { | |
| 235 | 235 | return html`<details class="tl-edit" id="edit"> |
| 236 | 236 | <summary class="muted small">${icon("pencil")} Edit</summary> |
| 237 | 237 | <form class="card comment-form mt" method="post" action="${action}"> |
| @@ -254,6 +254,13 @@ function editForm(issue: Issue, action: string): Html { | ||
| 254 | 254 | maxlength="${MAX_BODY}" |
| 255 | 255 | > |
| 256 | 256 | ${issue.body}</textarea> |
| 257 | ${offerPublish | |
| 258 | ? html`<label class="small mt"> | |
| 259 | <input type="checkbox" name="confirm_public" value="1" /> | |
| 260 | If these words close a sealed report: publish anyway — this pull's | |
| 261 | branch is public, so its fix shows where the hole is. | |
| 262 | </label>` | |
| 263 | : null} | |
| 257 | 264 | <div class="form-actions"> |
| 258 | 265 | <span class="muted small">Markdown, as in a comment.</span> |
| 259 | 266 | <span class="spacer"></span> |
| @@ -306,7 +313,13 @@ export function conversation( | ||
| 306 | 313 | return { |
| 307 | 314 | node: html`<div class="timeline" id="conversation"> |
| 308 | 315 | ${opening}${when(options.editAction, (action) => |
| 309 | editForm(issue, action), | |
| 316 | editForm( | |
| 317 | issue, | |
| 318 | action, | |
| 319 | // Only on a pull, and only for whoever may see sealed reports | |
| 320 | // here: to anyone else the offer would itself say one exists. | |
| 321 | issue.kind === "pull" && canSeeSealed(ctx.app.db, repo, ctx.auth.user), | |
| 322 | ), | |
| 310 | 323 | )}${entries.map((e) => e.node)} |
| 311 | 324 | </div>`, |
| 312 | 325 | lastCommentId: comments.length > 0 ? comments[comments.length - 1]!.id : "", |
| @@ -770,6 +783,7 @@ export async function wordsSubmit( | ||
| 770 | 783 | const edited = editIssue(ctx.app, ctx.auth.user, found.repo, found.issue, { |
| 771 | 784 | title: field(form, "title"), |
| 772 | 785 | body: field(form, "body"), |
| 786 | confirmPublic: field(form, "confirm_public") === "1", | |
| 773 | 787 | }); |
| 774 | 788 | if (!edited.ok) { |
| 775 | 789 | if (edited.error === "not_found") return notFoundPage(ctx); |
| @@ -779,6 +793,8 @@ export async function wordsSubmit( | ||
| 779 | 793 | ? "Only the author or someone with write access can edit this." |
| 780 | 794 | : edited.error === "taken_up" |
| 781 | 795 | ? "A writer has taken this up: comment instead." |
| 796 | : edited.error === "sealed_in_public" | |
| 797 | ? "These words close a sealed security report, and this pull's branch is public. Open the fix from your private repository (the report's “Work on this privately”), or tick “publish anyway” and save again." | |
| 782 | 798 | : edited.error === "empty_title" |
| 783 | 799 | ? "A title is needed." |
| 784 | 800 | : edited.error === "title_too_long" |
test/sealed-private-work.test.ts +54 −3
| @@ -12,14 +12,16 @@ import { tmpdir } from "node:os"; | ||
| 12 | 12 | import { join } from "node:path"; |
| 13 | 13 | import { createApp, type App } from "../src/app.ts"; |
| 14 | 14 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; |
| 15 | import { openIssue } from "../src/actions/issues.ts"; | |
| 16 | import { openPull } from "../src/actions/pulls.ts"; | |
| 15 | import { editIssue, openIssue } from "../src/actions/issues.ts"; | |
| 16 | import { mergePull, openPull } from "../src/actions/pulls.ts"; | |
| 17 | 17 | import { |
| 18 | 18 | commitToBranch, |
| 19 | 19 | createRepo, |
| 20 | forkRepo, | |
| 20 | 21 | privateRepoForSealed, |
| 21 | 22 | } from "../src/actions/repos.ts"; |
| 22 | import type { Issue } from "../src/core/issues.ts"; | |
| 23 | import { getIssue, type Issue } from "../src/core/issues.ts"; | |
| 24 | import { getPull } from "../src/core/pulls.ts"; | |
| 23 | 25 | import { getRepoById, type Repo } from "../src/core/repos.ts"; |
| 24 | 26 | import { createToken } from "../src/core/tokens.ts"; |
| 25 | 27 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; |
| @@ -263,4 +265,53 @@ describe("sealed work stays private", () => { | ||
| 263 | 265 | const yes = await post([["confirm_public", "1"]]); |
| 264 | 266 | expect(yes.status).toBe(303); |
| 265 | 267 | }); |
| 268 | test("to someone who may not see the report, its number closes nothing: no refusal, no seal, no close", async () => { | |
| 269 | const fork = await forkRepo(app, bob, repo); | |
| 270 | if (!fork.ok) throw new Error(String(fork.error)); | |
| 271 | await commitToBranch(app, bob, fork.value, { | |
| 272 | branch: "bob/guess", | |
| 273 | base: "main", | |
| 274 | message: "A guess", | |
| 275 | files: [{ path: "guess.ts", content: "export const guess = 1;\n" }], | |
| 276 | }); | |
| 277 | const opened = await openPull(app, bob, repo, { | |
| 278 | title: "A guess", | |
| 279 | body: "Closes #1", | |
| 280 | baseBranch: "main", | |
| 281 | headBranch: "bob/guess", | |
| 282 | headRepo: fork.value, | |
| 283 | }); | |
| 284 | expect(opened.ok).toBe(true); | |
| 285 | if (!opened.ok) return; | |
| 286 | expect(opened.value.sealed).toBe(false); | |
| 287 | // Landed by a writer, it still does not close the sealed report. | |
| 288 | const merged = await mergePull(app, alice, repo, getPull(app.db, repo.id, opened.value.number)!, "squash"); | |
| 289 | expect(merged.ok).toBe(true); | |
| 290 | expect(getIssue(app.db, repo.id, 1)!.state).toBe("open"); | |
| 291 | }); | |
| 292 | ||
| 293 | test("new words on a public pull that close a sealed report are refused unless a person says yes", async () => { | |
| 294 | await commitToBranch(app, alice, repo, { | |
| 295 | branch: "fix/later", | |
| 296 | base: "main", | |
| 297 | message: "Later", | |
| 298 | files: [{ path: "later.ts", content: "export const later = 1;\n" }], | |
| 299 | }); | |
| 300 | const plain = await openPull(app, alice, repo, { title: "Later", body: "", baseBranch: "main", headBranch: "fix/later" }); | |
| 301 | if (!plain.ok) throw new Error(String(plain.error)); | |
| 302 | const issue = getIssue(app.db, repo.id, plain.value.number)!; | |
| 303 | const refused = editIssue(app, alice, repo, issue, { body: "Closes #1" }); | |
| 304 | expect(refused.ok).toBe(false); | |
| 305 | if (!refused.ok) expect(refused.error).toBe("sealed_in_public"); | |
| 306 | const api = await fetch(`${servers.url}/api/v1/repos/alice/vault/pulls/${plain.value.number}`, { | |
| 307 | method: "PATCH", | |
| 308 | headers: { authorization: `Bearer ${aliceRw}`, "content-type": "application/json" }, | |
| 309 | body: JSON.stringify({ body: "Closes #1" }), | |
| 310 | }); | |
| 311 | expect(api.status).toBe(422); | |
| 312 | expect(((await api.json()) as { error: string }).error).toBe("sealed_in_public"); | |
| 313 | const agent = getUserByName(app.db, "vaultbot")!; | |
| 314 | expect(editIssue(app, agent, repo, issue, { body: "Closes #1", confirmPublic: true }).ok).toBe(false); | |
| 315 | expect(editIssue(app, alice, repo, issue, { body: "Closes #1", confirmPublic: true }).ok).toBe(true); | |
| 316 | }); | |
| 266 | 317 | }); |
new above
offline