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

Closed opened this in answer to
Closed without merging

steward/927-sealed-followups → main 1 commit

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +174 −27
src/actions/issues.ts +36 −6
@@ -18,7 +18,13 @@
1818 * "mirrored" on the others, which is honest rather than silently wrong.
1919 */
2020
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";
2228 import { requireVerified } from "./verify.ts";
2329 import type { App } from "../app.ts";
2430 import { crewRole, isCrew } from "../core/agents.ts";
@@ -45,8 +51,8 @@ import {
4551 standsForPerson,
4652 } from "../core/issues.ts";
4753 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";
5056 import { getUserById, type User } from "../core/users.ts";
5157 import {
5258 directorsAmong,
@@ -117,7 +123,8 @@ export type EditError =
117123 | TitleError
118124 | "body_too_long"
119125 | "nothing_to_change"
120 | "taken_up";
126 | "taken_up"
127 | "sealed_in_public";
121128
122129 /** The sentence a refused author reads: someone who directs the crew acted. */
123130 export const TAKEN_UP = "a writer has taken this up: comment instead";
@@ -225,7 +232,12 @@ export function editIssue(
225232 actor: User | undefined,
226233 repo: Repo,
227234 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 },
229241 ): Result<Issue, EditError> {
230242 const user = requireParticipant(app, actor, repo);
231243 if (!user.ok) return user;
@@ -241,7 +253,25 @@ export function editIssue(
241253 (field) => input[field] !== undefined && input[field] !== issue[field],
242254 );
243255 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 });
245275 if (!updated.ok) return updated;
246276 app.bus.append({
247277 kind: `${issue.kind}.edited`,
src/actions/pulls.ts +8 −10
@@ -33,7 +33,7 @@ import {
3333 import { isStructural, pillarsOf } from "../core/pillars.ts";
3434 import type { Repo } from "../core/repos.ts";
3535 import { canRead, getRepoById } from "../core/repos.ts";
36 import { sealedByNumber } from "../core/sealed.ts";
36 import { refusedInPublic, sealedClosedBy } from "../core/sealed.ts";
3737 import {
3838 addReviewComment,
3939 anchorContent,
@@ -190,15 +190,10 @@ export async function openPull(
190190 // A fix for a sealed report, from a branch anyone can read (this
191191 // repository, or a public fork), publishes where the hole is. Refused
192192 // 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))
202197 return err("sealed_in_public");
203198 const created = await createPull(
204199 app.db,
@@ -217,6 +212,7 @@ export async function openPull(
217212 }
218213 : {}),
219214 ...(input.revertsIssueId ? { revertsIssueId: input.revertsIssueId } : {}),
215 sealed: fixes.length > 0,
220216 },
221217 );
222218 if (!created.ok) return created;
@@ -433,6 +429,8 @@ export async function mergePull(
433429 for (const n of closesFromBody(pull.body)) {
434430 const issue = getIssue(app.db, repo.id, n);
435431 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;
436434 if (isStructural(plan, issue.id)) continue;
437435 const closed = setIssueStateCore(app.db, issue.id, "closed");
438436 if (!closed) continue;
src/core/pulls.ts +11 −3
@@ -119,6 +119,12 @@ export interface CreatePullInput {
119119 headDir?: string;
120120 /** The issue id of the pull this one reverts ("Take it back"). */
121121 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;
122128 }
123129
124130 /**
@@ -197,9 +203,11 @@ export async function createPull(
197203
198204 // The fix for a sealed report is sealed with it (`sealed.ts`), and its
199205 // 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 );
203211 const keptApart =
204212 sealed &&
205213 crossRepo &&
src/core/sealed.ts +36
@@ -103,6 +103,42 @@ export function sealedRowByNumber(
103103 return row ? { sealed: row.sealed === 1, authorId: row.author_id } : undefined;
104104 }
105105
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
106142 /** Whether issue `number` in `repoId` is sealed. */
107143 export function sealedByNumber(
108144 db: Database,
src/http/api.ts +10 −2
@@ -590,6 +590,12 @@ function sourceFailure(error: SourceError): Response {
590590 * worth retrying against.
591591 */
592592 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 );
593599 if (error === "invalid_reason")
594600 return apiError(
595601 "invalid_reason",
@@ -629,8 +635,10 @@ function originAware(error: string, status = 422): Response {
629635 */
630636 function textFrom(
631637 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;
634642 for (const key of ["title", "body"] as const) {
635643 const value = input[key];
636644 if (value === undefined) continue;
src/web/pages/thread.ts +19 −3
@@ -8,7 +8,7 @@
88 * is what makes a long thread skimmable.
99 */
1010
11 import { mayView } from "../../core/sealed.ts";
11 import { canSeeSealed, mayView } from "../../core/sealed.ts";
1212 import { listCrew } from "../../actions/crew.ts";
1313 import {
1414 confirmIssue,
@@ -231,7 +231,7 @@ export interface ConversationOptions {
231231 * the body as they stand, one form, one Save. Folded away until asked for,
232232 * since most visits read and few rewrite.
233233 */
234 function editForm(issue: Issue, action: string): Html {
234 function editForm(issue: Issue, action: string, offerPublish: boolean): Html {
235235 return html`<details class="tl-edit" id="edit">
236236 <summary class="muted small">${icon("pencil")} Edit</summary>
237237 <form class="card comment-form mt" method="post" action="${action}">
@@ -254,6 +254,13 @@ function editForm(issue: Issue, action: string): Html {
254254 maxlength="${MAX_BODY}"
255255 >
256256 ${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}
257264 <div class="form-actions">
258265 <span class="muted small">Markdown, as in a comment.</span>
259266 <span class="spacer"></span>
@@ -306,7 +313,13 @@ export function conversation(
306313 return {
307314 node: html`<div class="timeline" id="conversation">
308315 ${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 ),
310323 )}${entries.map((e) => e.node)}
311324 </div>`,
312325 lastCommentId: comments.length > 0 ? comments[comments.length - 1]!.id : "",
@@ -770,6 +783,7 @@ export async function wordsSubmit(
770783 const edited = editIssue(ctx.app, ctx.auth.user, found.repo, found.issue, {
771784 title: field(form, "title"),
772785 body: field(form, "body"),
786 confirmPublic: field(form, "confirm_public") === "1",
773787 });
774788 if (!edited.ok) {
775789 if (edited.error === "not_found") return notFoundPage(ctx);
@@ -779,6 +793,8 @@ export async function wordsSubmit(
779793 ? "Only the author or someone with write access can edit this."
780794 : edited.error === "taken_up"
781795 ? "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."
782798 : edited.error === "empty_title"
783799 ? "A title is needed."
784800 : edited.error === "title_too_long"
test/sealed-private-work.test.ts +54 −3
@@ -12,14 +12,16 @@ import { tmpdir } from "node:os";
1212 import { join } from "node:path";
1313 import { createApp, type App } from "../src/app.ts";
1414 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";
1717 import {
1818 commitToBranch,
1919 createRepo,
20 forkRepo,
2021 privateRepoForSealed,
2122 } 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";
2325 import { getRepoById, type Repo } from "../src/core/repos.ts";
2426 import { createToken } from "../src/core/tokens.ts";
2527 import { createUser, getUserByName, type User } from "../src/core/users.ts";
@@ -263,4 +265,53 @@ describe("sealed work stays private", () => {
263265 const yes = await post([["confirm_public", "1"]]);
264266 expect(yes.status).toBe(303);
265267 });
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 });
266317 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs