Public: anyone can read this; the crew can change it.
Issue search never shows a sealed security report to a reader who may not see it #15060
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15041-issue-search-never-shows-a-sealed-securi → main 1 commit fork 1 thread · 1 unresolved
2 files
changed +76
−6
src/core/issues.ts +32 −6
| @@ -1304,7 +1304,11 @@ export interface IssueHit extends Issue { | ||
| 1304 | 1304 | |
| 1305 | 1305 | /** |
| 1306 | 1306 | * Titles matching `query`, across every repo the viewer can see, in one |
| 1307 | * query. Sealed security reports are left out for everyone. | |
| 1307 | * query. Sealed security reports are left out unless the caller names | |
| 1308 | * where the viewer may see them (`sealed.ts`): `sealedOk`, the repos where | |
| 1309 | * `canSeeSealed` holds, and `sealedBy`, the viewer's own reports when | |
| 1310 | * `mayView` would let their author follow them. A caller that names | |
| 1311 | * neither shows nothing sealed. | |
| 1308 | 1312 | * |
| 1309 | 1313 | * The shape this replaces was a loop: take the twenty most recent visible |
| 1310 | 1314 | * repos, ask each of them for issues and again for pulls, forty queries for |
| @@ -1321,7 +1325,14 @@ export function searchIssuesVisibleTo( | ||
| 1321 | 1325 | db: Database, |
| 1322 | 1326 | viewer: { id: string } | undefined, |
| 1323 | 1327 | query: string, |
| 1324 | options: { limit?: number; kind?: IssueKind } = {}, | |
| 1328 | options: { | |
| 1329 | limit?: number; | |
| 1330 | kind?: IssueKind; | |
| 1331 | /** Repos whose sealed issues the viewer may see (`canSeeSealed`). */ | |
| 1332 | sealedOk?: ReadonlySet<string>; | |
| 1333 | /** The viewer's id, when they may follow the sealed reports they filed. */ | |
| 1334 | sealedBy?: string; | |
| 1335 | } = {}, | |
| 1325 | 1336 | ): IssueHit[] { |
| 1326 | 1337 | const text = query.trim(); |
| 1327 | 1338 | if (text === "") return []; |
| @@ -1329,11 +1340,26 @@ export function searchIssuesVisibleTo( | ||
| 1329 | 1340 | viewer?.id ?? "", |
| 1330 | 1341 | `%${likeEscape(text)}%`, |
| 1331 | 1342 | ]; |
| 1332 | // Never a sealed row: who may see one is decided per repo (`sealed.ts`), | |
| 1333 | // and a search across every repo has no such scope. A writer reads them | |
| 1334 | // in the repo's own list. | |
| 1343 | // A sealed row only where the caller said this viewer may see it: who | |
| 1344 | // may is decided per repo (`sealed.ts`), so a search across every repo | |
| 1345 | // is told, never guesses. Signed out, nothing sealed at all. | |
| 1346 | const sealedOr = ["issue.sealed = 0"]; | |
| 1347 | if (viewer !== undefined) { | |
| 1348 | const open = [...(options.sealedOk ?? [])]; | |
| 1349 | if (open.length > 0) { | |
| 1350 | const at = params.length; | |
| 1351 | params.push(...open); | |
| 1352 | sealedOr.push( | |
| 1353 | `issue.repo_id IN (${open.map((_, i) => `?${at + i + 1}`).join(",")})`, | |
| 1354 | ); | |
| 1355 | } | |
| 1356 | if (options.sealedBy !== undefined && options.sealedBy === viewer.id) { | |
| 1357 | params.push(options.sealedBy); | |
| 1358 | sealedOr.push(`issue.author_id = ?${params.length}`); | |
| 1359 | } | |
| 1360 | } | |
| 1335 | 1361 | let where = `${repoVisible(viewer !== undefined)} |
| 1336 | AND issue.sealed = 0 | |
| 1362 | AND (${sealedOr.join(" OR ")}) | |
| 1337 | 1363 | AND issue.title LIKE ?2 ESCAPE '\\' COLLATE NOCASE`; |
| 1338 | 1364 | if (options.kind !== undefined) { |
| 1339 | 1365 | params.push(options.kind); |
test/scale.test.ts +44
| @@ -43,6 +43,7 @@ import { listEvents, listEventsVisibleTo } from "../src/core/events.ts"; | ||
| 43 | 43 | import { getPull, mergedPullNumbers } from "../src/core/pulls.ts"; |
| 44 | 44 | import { pin, pinnedRepoIds, isPinned } from "../src/core/pins.ts"; |
| 45 | 45 | import { getRepoById, getReposByIds, type Repo } from "../src/core/repos.ts"; |
| 46 | import { canSeeSealed, sealedScope } from "../src/core/sealed.ts"; | |
| 46 | 47 | import { getUserById, getUsersByIds, hasAnyUser } from "../src/core/users.ts"; |
| 47 | 48 | import { createToken, verifyToken } from "../src/core/tokens.ts"; |
| 48 | 49 | import { |
| @@ -508,6 +509,49 @@ describe("visibility is enforced in SQL, not after the fact", () => { | ||
| 508 | 509 | expect(searchIssuesVisibleTo(app.db, undefined, "the public one")).toHaveLength(1); |
| 509 | 510 | }); |
| 510 | 511 | |
| 512 | test("search shows a sealed report to whoever `sealed.ts` lets see it, and to no one else", () => { | |
| 513 | const report = createIssue(app.db, { | |
| 514 | repoId: mine.id, | |
| 515 | authorId: bob.id, | |
| 516 | title: "quarantined token leak in the upload path", | |
| 517 | sealed: true, | |
| 518 | }); | |
| 519 | if (!report.ok) throw new Error("seed report"); | |
| 520 | const own = createIssue(app.db, { | |
| 521 | repoId: mine.id, | |
| 522 | authorId: alice.id, | |
| 523 | title: "quarantined report alice filed", | |
| 524 | sealed: true, | |
| 525 | }); | |
| 526 | if (!own.ok) throw new Error("seed own report"); | |
| 527 | // The scope a caller derives from the one rule (`sealedScope`). | |
| 528 | const scope = (user: User) => { | |
| 529 | const s = sealedScope(app.db, mine, user); | |
| 530 | return { | |
| 531 | sealedOk: new Set(s.sealed === true ? [mine.id] : []), | |
| 532 | ...(s.sealedBy === undefined ? {} : { sealedBy: s.sealedBy }), | |
| 533 | }; | |
| 534 | }; | |
| 535 | // Alice reads the public repo but may not write it: only her own report. | |
| 536 | expect(canSeeSealed(app.db, mine, alice)).toBe(false); | |
| 537 | const asAlice = searchIssuesVisibleTo(app.db, alice, "quarantined", scope(alice)); | |
| 538 | expect(asAlice.map((hit) => hit.title)).toEqual(["quarantined report alice filed"]); | |
| 539 | // Bob writes it: he finds both. | |
| 540 | const asBob = searchIssuesVisibleTo(app.db, bob, "quarantined", scope(bob)); | |
| 541 | expect(asBob).toHaveLength(2); | |
| 542 | // Signed out, nothing sealed, whatever the caller passes. | |
| 543 | expect( | |
| 544 | searchIssuesVisibleTo(app.db, undefined, "quarantined", { | |
| 545 | sealedOk: new Set([mine.id]), | |
| 546 | sealedBy: alice.id, | |
| 547 | }), | |
| 548 | ).toHaveLength(0); | |
| 549 | // Someone else's id as `sealedBy` opens nothing. | |
| 550 | expect( | |
| 551 | searchIssuesVisibleTo(app.db, bob, "quarantined report", { sealedBy: alice.id }), | |
| 552 | ).toHaveLength(0); | |
| 553 | }); | |
| 554 | ||
| 511 | 555 | test("an empty query is not a request for everything", () => { |
| 512 | 556 | expect(searchIssuesVisibleTo(app.db, bob, " ")).toHaveLength(0); |
| 513 | 557 | }); |
new above
offline
sealedBy is only honoured when it equals viewer.id, so the option is redundant with the viewer. A boolean makes the mismatch unrepresentable.