diff --git a/pm-stack/aggregator/src/comments.ts b/pm-stack/aggregator/src/comments.ts index 2c947c84bb..ece0c25de2 100644 --- a/pm-stack/aggregator/src/comments.ts +++ b/pm-stack/aggregator/src/comments.ts @@ -41,6 +41,10 @@ export interface CommentsFile { } const GH_REF = /^[\w.-]+\/[\w.-]+#\d+$/; +// Result URLs are rendered as link hrefs on the dashboard (#950): +// only https://github.com/ result links are storable — a stored +// javascript:/data: URL would render as a clickable script link. +const GH_RESULT_URL = /^https:\/\/github\.com\//; const COMMENT_KINDS = new Set(["message", "note", "github"]); function commentRecordProblem(value: unknown): string | null { @@ -352,6 +356,13 @@ export class CommentsStore { throw new Error("github result cannot carry both a url and an error"); } if (result.url) { + // #950: the stored githubUrl is rendered as a link href, so only + // https://github.com/ result URLs are accepted — anything else + // (javascript:, data:, http:, other hosts) is refused with the + // state unchanged (the endpoint maps the throw to a 400). + if (!GH_RESULT_URL.test(result.url)) { + throw new Error("github result url must start with https://github.com/"); + } if (c.githubState === "posted") { // Posted is terminal. A repeat of the same url is an // idempotent no-op; a different url is a conflicting diff --git a/pm-stack/aggregator/test/slice3.test.ts b/pm-stack/aggregator/test/slice3.test.ts index f7f9fc0986..25b40a0577 100644 --- a/pm-stack/aggregator/test/slice3.test.ts +++ b/pm-stack/aggregator/test/slice3.test.ts @@ -470,6 +470,43 @@ describe("dashboard comments", () => { expect(landed?.githubState).toBe("posted"); }); + test("github result urls outside https://github.com/ are refused with state unchanged (#950)", async () => { + const dir3 = mkdtempSync(join(tmpdir(), "agg-comments-url-")); + const store = new CommentsStore(join(dir3, "comments.json")); + const addGh = () => + store.add({ + objectRef: "gh:developing-today/code#950", + kind: "github", + author: agent, + body: "posting this", + githubRef: "developing-today/code#950", + }); + + // Every non-github.com scheme/host is refused on a pending + // comment, with no githubUrl stored and state unchanged. + const pending = await addGh(); + for (const bad of [ + "javascript:alert(document.domain)", + "data:text/html,", + "http://github.com/developing-today/code/issues/950#issuecomment-1", + "https://evil-github.com/developing-today/code#1", + "https://github.com.evil.com/#1", + ]) { + await expect(store.markGithubResult(pending.id, { url: bad })).rejects.toThrow("https://github.com/"); + const row = (await store.all()).find((c) => c.id === pending.id); + expect(row?.githubState).toBe("pending"); + expect(row?.githubUrl).toBeUndefined(); + } + + // The real flow still works: an https://github.com/ result URL + // is recorded and the comment posts. + const ok = await store.markGithubResult(pending.id, { + url: "https://github.com/developing-today/code/issues/950#issuecomment-1", + }); + expect(ok?.githubState).toBe("posted"); + expect(ok?.githubUrl).toBe("https://github.com/developing-today/code/issues/950#issuecomment-1"); + }); + test("a repeat report after a failed persist still lands the result on disk (#1686)", async () => { // Codex P2 on PR 3062: markGithubResult flips the in-memory state // before persist() runs. If that write fails, a repeat report hit @@ -763,6 +800,32 @@ describe("slice-3 routes", () => { expect(discuss).toContain("Post as GitHub comment"); }); + test("github-result endpoint refuses non-github.com urls with a 400 (#950)", async () => { + const post = (body: unknown) => + fetch(`http://127.0.0.1:${port}/api/comments/github-result`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }); + const author = { kind: "drew", id: "drew", label: "Drew" }; + const ref = "gh:developing-today/code#950"; + const created = (await ( + await fetch(`http://127.0.0.1:${port}/api/comments`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + objectRef: ref, + kind: "github", + author, + body: "for the issue", + githubRef: "developing-today/code#950", + }), + }) + ).json()) as { comment: { id: string } }; + const bad = await post({ id: created.comment.id, url: "javascript:alert(document.domain)" }); + expect(bad.status).toBe(400); + }); + test("item page carries the dashboard comments form + kinds explainer", async () => { const board = (await (await fetch(`http://127.0.0.1:${port}/api/board.json`)).json()) as { board: { id: string }[];