Files
PaperClipAI/ui/src/lib/clipboard.test.ts
T
DottaandPaperclip 1fa36be353 fix(ui): use HTTP-safe clipboard copy everywhere (#10875)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Operators often open self-hosted Paperclip over plain HTTP on a LAN
or private network.
> - Browser Clipboard API writes are not reliable in that insecure
context.
> - Paperclip already has one shared helper with a legacy copy fallback,
but many current copy actions bypass it.
> - This pull request routes every core UI copy action and the
first-party workspace-diff plugin through the shared helper.
> - The benefit is consistent copy behavior on HTTPS, localhost, and
plain-HTTP private deployments.

## Linked Issues or Issue Description

Refs #3529.

This change supersedes the stale prior attempt in #3531. Current master
has more copy surfaces and a first-party plugin UI bridge that the prior
branch does not cover.

## What Changed

- Replaced direct Clipboard API writes and duplicate fallback
implementations across the current core UI with `copyTextToClipboard`.
- Added an HTTP-safe clipboard function to the plugin UI SDK and wired
the host bridge to the same implementation.
- Migrated the first-party workspace-diff plugin to the plugin SDK
clipboard function.
- Added unit coverage for native rejection fallback and plugin host
delegation.
- Added a source-level regression test that rejects new direct clipboard
writes outside the shared implementation.
- Documented the plugin UI clipboard function.

## Verification

- `NODE_ENV=test pnpm exec vitest run ...` for 14 affected suites: 164
tests passed.
- `pnpm exec vitest run tests/ui-clipboard.test.ts` in
`packages/plugins/sdk`: 1 test passed.
- `NODE_ENV=test pnpm -r typecheck`: passed for 31 workspace projects.
- `NODE_ENV=test pnpm test:run`: passed.
- `NODE_ENV=production pnpm build`: passed.
- `pnpm check:token-gates`: passed with all gates clean.

## Risks

Low risk. Secure contexts still use the modern Clipboard API. Plain HTTP
and rejected modern writes use the existing `execCommand("copy")`
fallback. That API is deprecated, but it is the compatibility path
required for insecure contexts. The change has no schema, API, or visual
design effect.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

OpenAI Codex, `gpt-5.6-sol`. The runtime did not expose a context-window
size. Reasoning, tool use, repository editing, test execution, and
GitHub CLI access were enabled.

## 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
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-08-05 10:45:08 -05:00

85 lines
3.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { copyTextToClipboard } from "./clipboard";
function installDocumentStub(execCommand: () => boolean) {
const textarea = {
value: "",
style: {} as Record<string, string>,
setAttribute: vi.fn(),
focus: vi.fn(),
select: vi.fn(),
setSelectionRange: vi.fn(),
};
const doc = {
createElement: vi.fn(() => textarea),
body: { appendChild: vi.fn(), removeChild: vi.fn() },
activeElement: null,
getSelection: vi.fn(() => null),
execCommand: vi.fn(execCommand),
};
vi.stubGlobal("document", doc);
return { doc, textarea };
}
afterEach(() => {
vi.unstubAllGlobals();
});
describe("copyTextToClipboard", () => {
it("uses the async Clipboard API in a secure context", async () => {
const writeText = vi.fn(async () => {});
vi.stubGlobal("window", { isSecureContext: true });
vi.stubGlobal("navigator", { clipboard: { writeText } });
const { doc } = installDocumentStub(() => true);
await copyTextToClipboard("ssh agent@host");
expect(writeText).toHaveBeenCalledWith("ssh agent@host");
expect(doc.execCommand).not.toHaveBeenCalled();
});
it("falls back to execCommand on a non-secure context even when clipboard exists", async () => {
// Over plain HTTP the Clipboard API can be present but silently no-op, so we
// must not trust it and should write via the execCommand path instead.
const writeText = vi.fn(async () => {});
vi.stubGlobal("window", { isSecureContext: false });
vi.stubGlobal("navigator", { clipboard: { writeText } });
const { doc, textarea } = installDocumentStub(() => true);
await copyTextToClipboard("ssh agent@host");
expect(writeText).not.toHaveBeenCalled();
expect(textarea.value).toBe("ssh agent@host");
expect(textarea.focus).toHaveBeenCalledWith({ preventScroll: true });
expect(textarea.select).toHaveBeenCalled();
expect(textarea.setSelectionRange).toHaveBeenCalledWith(0, "ssh agent@host".length);
expect(doc.execCommand).toHaveBeenCalledWith("copy");
// Regression guard: Safari silently no-ops execCommand copy from a
// `readonly` or `opacity: 0` textarea, so the fallback must use neither.
expect(textarea.setAttribute).not.toHaveBeenCalledWith("readonly", expect.anything());
expect(textarea.style.opacity).toBeUndefined();
});
it("falls back when the secure-context Clipboard API rejects the write", async () => {
const writeText = vi.fn(async () => {
throw new Error("permission denied");
});
vi.stubGlobal("window", { isSecureContext: true });
vi.stubGlobal("navigator", { clipboard: { writeText } });
const { doc } = installDocumentStub(() => true);
await copyTextToClipboard("retry through fallback");
expect(writeText).toHaveBeenCalledWith("retry through fallback");
expect(doc.execCommand).toHaveBeenCalledWith("copy");
});
it("throws when the execCommand fallback reports failure", async () => {
vi.stubGlobal("window", { isSecureContext: false });
vi.stubGlobal("navigator", {});
installDocumentStub(() => false);
await expect(copyTextToClipboard("x")).rejects.toThrow();
});
});