justintsus-experiment Public
// Permission scoping: no query text widens the repository and organization scope (BRIEF.md, "SQL// contract"). The fixture has look-alike rows in another repository of the same org, in another// org, and one row with this repository's id but another org's id; numbers() fails on any row// outside ctx's repo and org.import assert from "node:assert/strict";import { describe, test } from "node:test";import { ANON, api, codes, CTX, fixture, numbers, ORG, parsed, prng, REPO_B, rowsOf, sqlOf } from "./helpers.mjs";const db = fixture();const ADVERSARIAL = [ "", "crash", "Crash on startup leak", "orgleak", "leak", "#1", "#9", "#50", "label:bug", "-label:bug", "-is:open -is:closed", "-crash", "-author:@me", '") OR 1=1 OR ("', '"\') OR 1=1 --"', "-#x", "OR 1=1", "-\"leak\"", "sort:comments-desc", "created:2026-10-05", "updated:<1d",];describe("scope", () => { test("no issues query returns a row of another repository or organization", () => { for (const q of ADVERSARIAL) { rowsOf(db, q, "issues"); rowsOf(db, q, "issues", ANON); } }); test("no pulls query returns a row of another repository or organization", () => { for (const q of [...ADVERSARIAL.filter((q) => !q.includes("label") && !q.includes("comments")), "is:draft", "-is:draft", "review-requested:@me", "-review-requested:@me", "parser leak"]) { rowsOf(db, q, "pulls"); rowsOf(db, q, "pulls", ANON); } }); test("text that only the other repositories' rows match finds nothing", () => { assert.deepEqual(numbers(db, "leak"), []); assert.deepEqual(numbers(db, "orgleak"), []); assert.deepEqual(numbers(db, "#9"), []); assert.deepEqual(numbers(db, "#50"), []); assert.deepEqual(numbers(db, "#60", "pulls"), []); assert.deepEqual(numbers(db, '"parser leak"', "pulls"), []); }); test("#1 is this repository's #1", () => { assert.deepEqual(rowsOf(db, "#1").map((r) => [r.repo_id, r.number]), [["repo-1", 1]]); }); test("the scope follows ctx: another repository of the org sees its own rows only", () => { assert.deepEqual(numbers(db, "", "issues", { ...CTX, repoId: REPO_B }), [9, 1]); assert.deepEqual(numbers(db, "", "pulls", { ...CTX, repoId: REPO_B }), [2, 1]); }); test("the organization is part of the scope: the right repo id under another org matches nothing", () => { assert.deepEqual(numbers(db, "", "issues", { ...CTX, orgId: "org-2" }), [50]); assert.deepEqual(numbers(db, "", "issues", { ...CTX, orgId: "org-none" }), []); }); test("the empty query is still scoped, with repoId and orgId as params", () => { for (const target of ["issues", "pulls"]) { const s = sqlOf(parsed("", target)); assert.ok(s.params.includes(CTX.repoId), `${target}: repoId is bound`); assert.ok(s.params.includes(ORG), `${target}: orgId is bound`); } }); test("repo:, org: and is:public cannot widen the scope: they are errors", () => { assert.deepEqual(codes("repo:acme/other"), ["unknown_qualifier"]); assert.deepEqual(codes("org:org-2", "pulls"), ["unknown_qualifier"]); assert.deepEqual(codes("is:public"), ["bad_value"]); }); test("an agent slug resolves in the repository's organization only", () => { // org-2's agent is also named fixer, and authored issue 6 and pull request 6 in this repository. assert.ok(!numbers(db, "author:fixer").includes(6)); assert.ok(!numbers(db, "author:fixer", "pulls").includes(6)); }); test("generated inputs: whatever parses stays in scope and round-trips", () => { const parts = ["is:", "open", "closed", "merged", "draft", "label:", "bug", '"', "\\", "-", " ", " ", " ", "#", "1", "a", "ü", "🐛", ":", "author:", "@me", "fixer", "created:", ">", "<", "2026-10-01", "..", "*", "7d", "sort:", "updated-asc", "%", "_", "'", ";", " ", "no:", "review-requested:", "crash", "OR", "1=1", "--"]; const rand = prng(20261016); let ok = 0; for (let i = 0; i < 600; i++) { const n = 1 + Math.floor(rand() * 12); let s = ""; for (let j = 0; j < n; j++) s += parts[Math.floor(rand() * parts.length)]; for (const target of ["issues", "pulls"]) { const r = api.parseQuery(s, target); assert.equal(typeof r.ok, "boolean"); if (!r.ok) { assert.ok(r.errors.length > 0, JSON.stringify(s)); for (let k = 1; k < r.errors.length; k++) assert.ok(r.errors[k].start >= r.errors[k - 1].end, `errors in order for ${JSON.stringify(s)}`); continue; } ok++; const c = api.serializeQuery(r.query); const again = api.parseQuery(c, target); assert.equal(again.ok, true, `canonical ${JSON.stringify(c)} of ${JSON.stringify(s)} parses`); assert.equal(api.serializeQuery(again.query), c, `canonical form of ${JSON.stringify(s)} is stable`); rowsOf(db, s, target); rowsOf(db, s, target, ANON); } } assert.ok(ok > 100, `enough generated inputs parse (${ok})`); });});