Files
PaperClipAI/ui/src/lib/safe-url.test.ts
T
DottaandClaude Opus 5.5 ad163d0e36 fix(ui): open and link only http(s) or same-origin URLs from user and agent content (#22639)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The board UI shows content that agents and people write: comments,
tool results, work products, approval cards and runtime links
> - Some of this content becomes a link, an image or a new window
> - Some of these places did not limit the URL scheme, and the chat
image click opened the URL without `noopener`
> - A URL with a script scheme must never open or navigate from the
board, because it would run with the board origin
> - This pull request adds one URL helper and routes the content-fed
link, image-click and window sinks through it
> - The benefit is that only http(s) or same-origin URLs can open from
user or agent content, and new windows never get an opener

## Linked Issues or Issue Description

**What happened?**
The issue-reference parser accepted a URL with any scheme when its path
contained an issue path. The markdown renderer trusted that match and
kept the URL. The chat image click then gave the URL to `window.open`
with no scheme check and no `noopener`. Several other components had
their own copies of a scheme check, and some links used content URLs
with no check.

**What did you expect?**
The board opens or links only http(s) URLs and same-origin paths from
user or agent content. New windows open with `noopener,noreferrer`.

**Steps to reproduce**
Render markdown with an image whose URL uses a non-http scheme and an
issue path, then click the image. The new unit tests cover this case.

**Impact**
Defense-in-depth hardening of the board UI against unsafe URLs in stored
content.

## What Changed

- Add `ui/src/lib/safe-url.ts` with `safeNavigableUrl` (http(s) or
same-origin), `safeExternalHttpUrl` (absolute http(s) only) and
`openSafeUrl` (opens with `noopener,noreferrer`).
- `parseIssueReferenceHref` in `packages/shared` now accepts only
relative paths and http(s) URLs.
- `MarkdownBody` keeps the issue-reference shortcut only for safe URLs.
The image click handler gets only a safe URL.
- The task chat image click, the work products panel and MCP app links
open through `openSafeUrl`.
- The plugin bridge does a top-level navigation only to an http(s) URL.
- Replace the local scheme checks in the task chat thread, protocol
activity row, transcript adapter, MCP app host, workspace detail pages
and artifact cards with the shared helper.
- Apply the helper to artifact rows, rich artifact source and download
links, approval result links, request-confirmation target links,
attachment links in chat bubbles, provider source links, action test
result links and runtime service links.
- Refresh `tests/runner-e2e/source-sdk-public-inputs.json` for the
changed shared files.

## Verification

- `ui/src/lib/safe-url.test.ts`: one table of hostile inputs (script,
data, file, blob, about and custom schemes, whitespace and
control-character variants, non-string values) runs against each helper.
It also checks that `openSafeUrl` uses `noopener,noreferrer`.
- `ui/src/components/MarkdownBody.interaction.test.tsx`: an image with
an unsafe URL never reaches the click handler. An http image still does.
This test failed before the fix (the unsafe URL reached the handler) and
passes now.
- `packages/shared/src/issue-references.test.ts`: non-http schemes with
an issue path return `null`. These cases failed before the fix and pass
now.
- A headless Chromium check confirmed the risk of the old call: a script
URL opened with `window.open(url, "_blank")` ran with access to the
opener. With `noopener,noreferrer` it did not reach the opener.
- `npx vitest run` on the affected UI folders: 116 files, 2119 tests
pass. `tsc --noEmit` for `ui` has no errors in `src`.
- `pnpm verify` exits 2 because the pinned-input registry changed.
Required CI gives the broad coverage.

## Risks

- Low risk. A link from content with a non-http scheme (for example
`mailto:` in an artifact source link) no longer renders as a link in the
changed components. Markdown links keep the existing `react-markdown`
URL rules.
- Mention chips with their own schemes (agent, project, user, skill,
routine) do not change.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, in Claude Code with
tool use and code execution.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 09:37:06 -06:00

87 lines
3.5 KiB
TypeScript

// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from "vitest";
import { openSafeUrl, safeExternalHttpUrl, safeNavigableUrl } from "./safe-url";
const hostileInputs: ReadonlyArray<readonly [string, unknown]> = [
["javascript scheme", "javascript:alert(1)"],
["javascript scheme with an issue path", "javascript:alert(1)//issues/PAP-1"],
["mixed-case javascript scheme", "JaVaScRiPt:alert(1)"],
["javascript scheme after leading whitespace", " javascript:alert(1)"],
["javascript scheme after a control character", "\u0001javascript:alert(1)"],
["javascript scheme split by a tab", "java\tscript:alert(1)"],
["javascript scheme split by a newline", "java\nscript:alert(1)"],
["vbscript scheme", "vbscript:msgbox(1)"],
["html data URL", "data:text/html,<script>alert(1)</script>"],
["base64 data URL", "data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg=="],
["svg data URL", "data:image/svg+xml,<svg onload=alert(1)>"],
["file URL", "file:///etc/passwd"],
["blob URL", "blob:https://paperclip.ing/1234"],
["filesystem URL", "filesystem:https://paperclip.ing/temporary/x"],
["about URL", "about:blank"],
["custom app scheme", "paperclip-desktop://open"],
["mention scheme", "agent://agent-1"],
["empty string", ""],
["whitespace only", " "],
["null", null],
["undefined", undefined],
["number", 42],
["object", { href: "https://example.com" }],
];
afterEach(() => {
vi.restoreAllMocks();
});
describe("safeNavigableUrl", () => {
it.each(hostileInputs)("rejects %s", (_label, input) => {
expect(safeNavigableUrl(input)).toBeNull();
});
it.each([
["https URL", "https://example.com/a?b=c#d", "https://example.com/a?b=c#d"],
["http URL", "http://localhost:3100/issues/PAP-1", "http://localhost:3100/issues/PAP-1"],
["uppercase https scheme", "HTTPS://example.com/", "HTTPS://example.com/"],
["app-relative path", "/PAP/issues/PAP-1", "/PAP/issues/PAP-1"],
["fragment", "#comment-1", "#comment-1"],
["query", "?tab=files", "?tab=files"],
["path with surrounding whitespace", " /issues/PAP-1 ", "/issues/PAP-1"],
])("keeps %s", (_label, input, expected) => {
expect(safeNavigableUrl(input)).toBe(expected);
});
});
describe("safeExternalHttpUrl", () => {
it.each(hostileInputs)("rejects %s", (_label, input) => {
expect(safeExternalHttpUrl(input)).toBeNull();
});
it("rejects relative paths", () => {
expect(safeExternalHttpUrl("/issues/PAP-1")).toBeNull();
expect(safeExternalHttpUrl("#comment-1")).toBeNull();
});
it("keeps absolute http and https URLs", () => {
expect(safeExternalHttpUrl("https://example.com/x")).toBe("https://example.com/x");
expect(safeExternalHttpUrl("http://example.com/x")).toBe("http://example.com/x");
});
});
describe("openSafeUrl", () => {
it.each(hostileInputs)("does not open %s", (_label, input) => {
const open = vi.spyOn(window, "open").mockReturnValue(null);
expect(openSafeUrl(input)).toBe(false);
expect(open).not.toHaveBeenCalled();
});
it("opens http and same-origin URLs in a new context without an opener or referrer", () => {
const open = vi.spyOn(window, "open").mockReturnValue(null);
expect(openSafeUrl("https://example.com/image.png")).toBe(true);
expect(openSafeUrl("/api/attachments/1/content")).toBe(true);
expect(open.mock.calls).toEqual([
["https://example.com/image.png", "_blank", "noopener,noreferrer"],
["/api/attachments/1/content", "_blank", "noopener,noreferrer"],
]);
});
});