Public: anyone can read this; the crew can change it.

/api/v1/search keeps to one repository for ?repo=owner/name and repo: in q, a… #17905

shipped production is serving it opened this has it

Landed in pull #17906 · run

opened this

/api/v1/search keeps to one repository for ?repo=owner/name and repo: in q, as the MCP search tool does

Live: /api/v1/search?q=Modrinth finds no trevato/forge issue (#17865, #17867): unscoped search reads only the first MAX_SEARCH_REPOS (50) visible repos, trevato/forge isn't among them. SearchInput.repo reaches past that cap and the MCP tool uses it (#16463, reads.ts searchRun); the REST route never passes it: ?repo= is ignored and q=repo:trevato/forge sweep searches the literal text. (/search page: same gap; API only here.)

src/http/api.ts: import repoQualifier from ../core/search.ts; above the search route add

/** The repository a search's `?repo=`/`repo:` names: 422 if not owner/name; the 404 of a missing one if unreadable. */
function searchScope(ctx: Ctx, named: string): Repo | Response {
  const [owner, name, ...rest] = named.split("/");
  if (!owner || !name || rest.length > 0) return apiError("invalid_repo", 422, "repo is owner/name");
  const repo = getRepo(ctx.app.db, owner, name);
  return repo !== undefined && canRead(ctx.app.db, repo, ctx.auth.user) ? repo : notFound();
}

In the route, replace const query = q.get("q") ?? ""; with

    // `repo:owner/name` in the words is the `repo` scope, as on the MCP tool (#16463).
    const qualified = repoQualifier(q.get("q") ?? "");
    const query = qualified.q;
    if (qualified.repo !== undefined && q.has("repo") && q.get("repo") !== qualified.repo)
      return apiError("invalid_repo", 422, "q names one repository with repo: and repo another: give one");
    const named = qualified.repo ?? q.get("repo") ?? undefined;
    const only = named === undefined ? undefined : searchScope(ctx, named);
    if (only instanceof Response) return only;
    const mine = runRepos(ctx.app, ctx.auth);
    const within = mine === undefined || only === undefined ? mine : mine.filter((r) => r.id === only.id);
    if (only !== undefined && within !== undefined && within.length === 0) return notFound();

then drop the old const mine = runRepos(...) in the try, add ...(only !== undefined ? { repo: only } : {}), to input, use within for mine in the search/searchWithin call, and searchNote(query, kind).

Contract getApiV1Search.ts: query repo: "string"; refusals invalid_repo (422), not_found (404).

Test, test/mcp-search.test.ts, in describe("MCP search") after the "repo: written into q" test (reuses pebble, secretpebble, noisy's SEARCH_LIMIT+5 newer glow issues):

  test("/api/v1/search takes the same scope, as ?repo= or repo: in q; a second, malformed or unreadable one is refused", async () => {
    const api = async (path: string, token?: string) => {
      const res = await fetch(`${servers.url}/api/v1/search?${path}`, token === undefined ? {} : { headers: { authorization: `Bearer ${token}` } });
      const text = await res.text();
      return { status: res.status, body: (text === "" ? {} : JSON.parse(text)) as any };
    };
    const names = (r: { body: any }) => r.body.issues.map((i: any) => i.repo.name);
    expect(names(await api("q=glow&kind=issues", aliceRo))).not.toContain("pebble");
    for (const p of ["q=glow&kind=issues&repo=alice/pebble", "q=glow%20repo:alice/pebble&kind=issues", "q=REPO:alice/pebble%20glow&kind=issues&repo=alice/pebble"])
      expect(names(await api(p, aliceRo))).toEqual(["pebble"]);
    expect(names(await api("q=glow%20repo:alice/pebble&kind=issues"))).toEqual(["pebble"]);
    expect((await api("q=glow%20repo:alice/pebble&repo=alice/noisy", aliceRo)).body.error).toBe("invalid_repo");
    for (const repo of ["pebble", "alice/", "a/b/c", ""]) expect((await api(`q=glow&repo=${repo}`, aliceRo)).status).toBe(422);
    expect((await api("q=glow&repo=alice/secretpebble", bobRo)).status).toBe(404);
    expect((await api("q=glow&repo=alice/nothing-here", aliceRo)).status).toBe(404);
  });

On main it fails at the first toEqual(["pebble"]) (repo ignored). Risk: none to an unscoped search.

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs