Skip to content

fix(core): copy works in a frame that does not allow the async Clipboard API - #12035

Merged
josephfarina merged 5 commits into
mainfrom
josephfarina/clipboard-sandbox-fallback
Nov 16, 2026
Merged

josephfarina merged 5 commits into
mainfrom
josephfarina/clipboard-sandbox-fallback

Conversation

@josephfarina

@josephfarina josephfarina commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

User impact

A copy button in a page shown inside a sandboxed frame did nothing useful. Artifact viewers, embeds and previews often show HTML in an <iframe sandbox="allow-scripts"> whose permissions policy does not allow clipboard-write. There the async Clipboard API is refused: CodeBlock's Copy and Timestamp's copy showed the failed-copy state, Markdown's heading-link copy copied nothing, and the browser logged "Permissions policy violation: The Clipboard API has been blocked" for each press.

Problem and solution fit

  1. Why it occurs: useClipboard (and the internal useClipboardCopy the components use) writes only through navigator.clipboard.writeText, which a frame without clipboard-write refuses.
  2. Why it harms the task: the reader presses Copy and gets nothing on the clipboard, and a console error.
  3. Why that matters: a page shared as a file is often read in exactly such a frame, and copying a command or a value is the reason the button is there.

The browser's copy command (document.execCommand('copy')) still runs in such a frame inside a click. The hook keeps the async API first: where the document may use it, it writes through it as before. Where the frame's permissions policy refuses clipboard-write, or the browser refuses the write (NotAllowedError), the hook runs the copy command inside the reader's click or key press, and puts the text in through the copy event the command fires (clipboardData.setData). No element takes focus or the selection, so a control in a hover card or an open modal dialog keeps both. It never runs the copy command outside user activation, and copy() resolves true only when that copy event fired with the text set. Where the policy refuses, the refused API is never called, so the browser logs nothing.

Expected behavior and authority

component:CodeBlock FR2: a copy writes the exact code string and reports success; a failed write must not report success. useClipboard's documented contract: copy() writes the text and resolves true on success.

Current specification: the maintainers decided that Core copies through the browser's copy command when a frame refuses the async Clipboard API. The reader asked for the copy, and the refusal covers only the async API (any code in the frame can run the copy command on a click), so a failing Core copy is not a boundary. The docsite playground's isolation test now pins the real boundary: its preview's async Clipboard API stays refused.

Smallest restoration

packages/core/src/hooks/useClipboard.ts: one writeClipboardText used by the hook: the policy check, the async write, and the copy-command fallback through a one-time copy listener. No new prop, option, export or visual change; the success and failure states are the same. useClipboard.doc.mjs describes the fallback.

Evidence

  • Reproduction before the change: in Chromium, a page in a sandbox="allow-scripts" frame: navigator.clipboard.writeText is refused and logs the permissions-policy violation (every template with a copy button, rendered into such a frame).
  • Result after the change: in a sandboxed frame from another origin, Copy outside a dialog and inside an open modal Dialog puts the exact code on the clipboard (read back from the clipboard), runs the copy command, keeps focus on the control, and logs no violation; in the same frame with allow="clipboard-write", Copy writes through the async API and runs no copy command.
  • Representative unchanged path: where clipboard-write is allowed, the hook calls navigator.clipboard.writeText exactly as before, and a write that fails for any other reason still reports a failure.
  • Regression tests: useClipboard.test.tsx: in a refusing frame the copy command carries the exact text, the refused API is not tried, the copy is announced and reported, no focus moves and no element is added, the page's own copy handlers cannot change the text, nothing runs outside user activation, and the copy fails when the command copies nothing or fires no copy event; after a NotAllowedError the copy command copies, after any other failure it does not. useClipboardFrame.a11y.chromium.spec.ts: both paths in a sandboxed frame, as above.
  • The docsite playground's isolation test asserted that Core's copy fails in its preview; it now asserts that the preview's async Clipboard API is refused (NotAllowedError) and that Core's copy on a click succeeds.

Scope

Components: useClipboard (CodeBlock, Timestamp, Markdown heading links through it); the docsite playground isolation test

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

  • pnpm exec vitest run --project ui packages/core/src/hooks/useClipboard packages/core/src/CodeBlock packages/core/src/Timestamp packages/core/src/Markdown
  • pnpm storybook:build, then pnpm exec playwright test useClipboardFrame CodeBlockCopyClearance (Chromium)
  • tsc --noEmit for Core, Storybook and the docsite; ESLint on the changed files; node scripts/check-changesets.mjs

@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 16, 2026 12:49pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 16, 2026
@astracat-bot

astracat-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 16, 2026, 1:06 PM UTC

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 57fb037

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC
richtext/RichTextView 282.5 kB (+342 B) · 2.3 kB CSS (0) —
CodeBlock 80.5 kB (+333 B) · 12.1 kB CSS (0) —
lab/CommentThread 363.4 kB (+312 B) · 2.4 kB CSS (0) —
4 more components
Component Size LoC
richtext/RichTextEditor 305.9 kB (+307 B) · 2.6 kB CSS (0) —
Markdown 203.0 kB (+306 B) · 21.9 kB CSS (0) —
lab/Comments 370.2 kB (+291 B) · 2.5 kB CSS (0) —
Timestamp 27.0 kB (+1 B) · 11.0 kB CSS (0) —

Package size: @astryxdesign/core 7.61 MB packed (+1.8 kB); no new dependencies.

Not run on this PR: add ci:run-visual for Visual, ci:run-a11y for Accessibility, ci:run-rtl for RTL.


Generated by PR Enrichment workflow | View full report

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested: the fallback copies in a frame that refuses clipboard-write, but inside an open modal it reports a copy that did not happen, and the release note misstates one case.

  • copyThroughSelection mounts its field on document.body (useClipboard.ts:75), so under an open Dialog, Drawer, AlertDialog or scrim BottomSheet the field is inert while execCommand('copy') still returns true, and a CodeBlock in an open Dialog inside a cross-origin frame shows "Copied" with the clipboard unchanged (on main it shows the failure); mount the field in the open modal that holds the control and add a browser test that reads the clipboard.
  • The changeset and description say the Markdown heading-link copy showed a failed copy, but heading links never show a failure (they copied nothing), so reword that sentence.

Outside modals the fix works as described in Chromium: CodeBlock, heading links and the useClipboard story copy in a cross-origin frame, focus stays on the control without a focus ring, and the policy violation is gone.

Claims and files checked

Review mode: full; reason: no-earlier-review

  • Claim: copy buttons fail in a frame that refuses clipboard-write and log a policy violation: verified on main in a cross-origin frame in Chromium.
  • Claim: the hook checks the policy and copies through a selected field where it is refused: verified (useClipboard.ts:38-47, :103-109; Chromium).
  • Claim: the browser logs nothing on that path: verified (no console error on the head).
  • Claim: focus and the selection come back: focus verified (test and Chromium); selection restore read in code (useClipboard.ts:87-92).
  • Claim: copy works inside such a frame: false inside an open modal (first finding).
  • Claim: heading links showed a failed copy: false (second finding).
  • Claim: the normal path is unchanged and 323 existing tests pass: verified (21 files, 323 tests).
  • Claim: the regression test copies through the selection, restores focus and reports a failed copy: verified; two of the three new tests fail on main.
  • Claim: no new prop, option, export or visual change: verified (the API surface comparison has no rows).
  • File .changeset/clipboard-sandboxed-frame.md: second finding.
  • File packages/core/src/hooks/useClipboard.ts: first finding; non-blocking comment notes.
  • File packages/core/src/hooks/useClipboard.test.tsx: clear, but no test covers a control inside an open modal (part of the first finding).
  • File packages/core/src/hooks/useClipboard.doc.mjs: clear.

Automated review (maintainer link)

Comment thread packages/core/src/hooks/useClipboard.ts Resolved
Comment thread .changeset/clipboard-sandboxed-frame.md Resolved
Comment thread packages/core/src/hooks/useClipboard.ts Resolved
Comment on lines +99 to +102
/**
* Write text to the clipboard: through the async Clipboard API where the
* document may use it, else through a selection and the copy command.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: this JSDoc restates the two branches below, which the name and body already show. Consider dropping it.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs an engineering owner's decision, then changes: the modal fix works, but the fallback now copies past a frame's clipboard-write refusal, which the docsite's restricted playground preview relies on.

  • Miss: no current record decides whether useClipboard may copy past a frame's clipboard-write refusal, and the docsite's playground preview (sandbox="allow-scripts") pins the opposite in apps/docsite/e2e/playground-preview-isolation.spec.ts:523, which now fails in docsite-browser, so an engineering owner needs to settle it (recorded by commenting /approve-spec with the full head SHA, which the author may do as an engineering owner) and either the fallback or that test then changes to match.
  • Miss: in a refusing frame, a keyboard copy from Timestamp's hover card shows "Copied", then the card closes about 200 ms later and focus drops to the page, because copyThroughSelection focuses a field outside the card and removes it while it still has focus (packages/core/src/hooks/useClipboard.ts:88-99); writing the text from a one-time copy listener during execCommand('copy') moves no focus and still passes your Chromium spec.
  • The new Copy in a dialog story passes header to Dialog, which has no such prop, so typecheck fails at apps/storybook/stories/CodeBlock.stories.tsx:538 and the dialog renders without its title; compose it with Layout and DialogHeader as the Dialog stories do.

In Chromium, Copy inside an open Dialog in a refusing frame now copies the exact code, and your spec fails if the field goes back to the body. Comment suggestions are inline and non-blocking.

Claims and files checked

Review mode: full; reason: history

  • Claim: on main, copy buttons in a refusing frame fail or copy nothing and log a policy violation: verified in Chromium (CodeBlock and Timestamp show the failure; the console logs the violation).
  • Claim: the hook checks the policy and copies through a selected field where it is refused: verified (useClipboard.ts:38-47, :68-122).
  • Claim: the refused API is not called there, so the browser logs nothing: verified (no console error on the head).
  • Claim: inside an open modal the field goes into the dialog that holds focus, and the copy lands: verified (Chromium spec passes; it fails with the field on the body).
  • Claim: focus and the selection come back: focus comes back, but Timestamp's hover card then closes after a keyboard copy (second finding); selection restore read in code (:100-105).
  • Claim: CodeBlock and Timestamp showed a failed copy and heading links copied nothing before: verified.
  • Claim: no new prop, option, export or visual change: verified (the API surface comparison has no rows).
  • Claim: the existing useClipboard, CodeBlock and Markdown plugin tests pass: verified (594 tests with Timestamp).
  • Claim: the regression tests cover the selection path, focus return, a failed copy and the modal case: verified by reading and running them.
  • Claim: the Core package typechecks: verified by CI; the Storybook project does not (third finding).
  • File .changeset/clipboard-sandboxed-frame.md: clear; its claims hold, subject to the owner decision.
  • File apps/storybook/stories/CodeBlock.stories.tsx: third finding.
  • File packages/core/src/hooks/__tests__/useClipboardFrame.a11y.chromium.spec.ts: clear.
  • File packages/core/src/hooks/useClipboard.doc.mjs: clear.
  • File packages/core/src/hooks/useClipboard.test.tsx: clear; no test covers a copy control inside a surface that closes on focus loss.
  • File packages/core/src/hooks/useClipboard.ts: first and second findings; non-blocking comment notes.

Automated review (maintainer link)

Comment thread packages/core/src/hooks/useClipboard.ts Resolved
Comment thread packages/core/src/hooks/useClipboard.ts Resolved
Comment thread packages/core/src/hooks/useClipboard.ts Resolved
… focus move; the docsite test pins the async API refusal
@josephfarina

Copy link
Copy Markdown
Contributor Author

/approve-spec 957379a

@josephfarina

Copy link
Copy Markdown
Contributor Author

/approve-spec 169a026

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested for one documentation fix. In Chromium, Copy now works in a frame that refuses clipboard-write (outside and inside an open Dialog, and from Timestamp's hover card by keyboard without closing it), the changed docsite isolation test passes, and the decision to copy in such a frame is approved for this head.

  • The public JSDoc on UseClipboardReturn.copy (packages/core/src/hooks/useClipboard.ts:137-139) and the file header (:7-8) still say the fallback copies through a selected hidden field, which the copy-event rewrite removed, and the JSDoc leaves out the fallback after a NotAllowedError; describe the copy command and its copy event there and in the Chromium spec's header (useClipboardFrame.a11y.chromium.spec.ts:9-10), as the inline suggestions show.

The earlier hover-card focus loss and story typecheck error are fixed. CI had not finished on this head when this review ended, and the head with the fix needs its own /approve-spec.

Claims and files checked

Review mode: incremental; reason: none

  • Claim: on main, copy buttons in a frame that refuses clipboard-write fail (CodeBlock, Timestamp) or copy nothing, and the browser logs a policy violation: verified in Chromium.
  • Claim: where the frame may use the async Clipboard API, the hook writes through it as before: verified (useClipboard.ts:92-97; the Chromium spec with clipboard-write allowed counts no copy command).
  • Claim: where the policy refuses, or the write is refused with NotAllowedError, the hook runs the copy command inside the click and sets the text from the copy event: verified (:56-84, :92-103).
  • Claim: no element takes focus or the selection, so a control in a hover card or an open modal dialog keeps both: verified (unit test; Chromium spec in a Dialog; Timestamp hover card by keyboard).
  • Claim: the copy command never runs outside user activation: verified where the browser exposes user activation (:57-62, unit test); elsewhere the browser itself refuses it.
  • Claim: copy() resolves true only when the copy event fired with the text set: verified (:81-83, unit tests).
  • Claim: where the policy refuses, the refused API is never called and nothing is logged: verified (no violation in Chromium).
  • Claim: the decision that Core copies through the copy command in such a frame is taken: verified as an engineering owner's approval for this head; no spec record states it yet.
  • Claim: the docsite isolation test now pins the async API refusal and expects Core's copy to succeed: verified (the test passes against a production docsite build).
  • Claim: no new prop, option, export or visual change: verified (the API surface comparison has no rows).
  • Claim: useClipboard.doc.mjs describes the fallback: verified.
  • Claim: the listed unit and Chromium regression tests cover the refused and allowed frames: verified by reading and running them (all pass).
  • Changeset: copy works in such frames for CodeBlock, Timestamp and Markdown heading links, also in a modal dialog or hover card, without moving focus: verified (CodeBlock and Timestamp in Chromium; heading links use the same hook).
  • Changeset: before, those buttons copied nothing there, CodeBlock and Timestamp showed a failed copy, and a violation was logged per press: verified.
  • Changeset: where the async Clipboard API is allowed, nothing changes: verified.
  • File .changeset/clipboard-sandboxed-frame.md: clear.
  • File apps/docsite/e2e/playground-preview-isolation.spec.ts: clear.
  • File apps/storybook/stories/CodeBlock.stories.tsx: clear.
  • File packages/core/src/hooks/__tests__/useClipboardFrame.a11y.chromium.spec.ts: stale header (the finding).
  • File packages/core/src/hooks/useClipboard.doc.mjs: clear.
  • File packages/core/src/hooks/useClipboard.test.tsx: clear.
  • File packages/core/src/hooks/useClipboard.ts: stale JSDoc and header (the finding); one non-blocking comment note.

Automated review (maintainer link)

Comment thread packages/core/src/hooks/useClipboard.ts Resolved
Comment thread packages/core/src/hooks/useClipboard.ts Resolved
@josephfarina

Copy link
Copy Markdown
Contributor Author

/approve-spec 57fb037

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending CI: CI has not finished on this head yet. Copy now works in a frame that refuses clipboard-write, outside and inside an open Dialog and from Timestamp's hover card by keyboard, the changed docsite isolation test passes against a production build, and the hook's header and copy JSDoc now describe the copy command and its copy event. An engineering owner approved copying in such a frame for this head.

  • Optional: the JSDoc on the private writeClipboardText (packages/core/src/hooks/useClipboard.ts:86-90) could keep only the reason the policy is read before a try.
Claims and files checked

Review mode: incremental; reason: none

  • Changed rules (0): none.
  • Claim: on the base, copy buttons in a frame that refuses clipboard-write fail (CodeBlock and Timestamp show the failure, heading links copy nothing) and the browser logs a policy violation per press: verified in Chromium for CodeBlock and Timestamp; heading links fail silently by design.
  • Claim: the base hook writes only through navigator.clipboard.writeText: verified.
  • Claim: where the frame may use the async Clipboard API, the hook writes through it as before: verified (useClipboard.ts:96-97; the Chromium spec's allowed frame runs no copy command).
  • Claim: where the policy refuses, or the write is refused with NotAllowedError, the hook runs the copy command inside the click and sets the text from the copy event: verified (:56-109, unit tests).
  • Claim: no element takes focus or the selection, so a control in a hover card or an open modal dialog keeps both: verified (unit test; Chromium spec in a Dialog; Timestamp hover card by keyboard).
  • Claim: the copy command never runs outside user activation: verified where the browser exposes user activation (:57-62, unit test).
  • Claim: copy() resolves true only when the copy event fired with the text set: verified (:81-83, unit tests).
  • Claim: where the policy refuses, the refused API is never called and nothing is logged: verified (unit test; no console violation in Chromium).
  • Claim: the decision that Core copies through the copy command in such a frame is taken: verified as an engineering owner's approval for this head; no spec record states it yet.
  • Claim: the docsite isolation test now asserts the async API refusal and a successful Core copy on a click: verified (passes against a production docsite build).
  • Claim: a write that fails for any other reason still reports a failure: verified (:99-101, unit test).
  • Claim: no new prop, option, export, default or visual change: verified (the API surface comparison has no rows).
  • Claim: useClipboard.doc.mjs describes the fallback: verified.
  • Claim: the listed unit and Chromium regression tests cover the refused and allowed frames: verified by reading and running them (all pass).
  • Claim: the listed testing commands pass: verified for the unit suites, the Storybook build with the Chromium spec, and the changeset check; typecheck and lint are green in CI.
  • Changeset: copy works in such frames for CodeBlock, Timestamp and Markdown heading links, also in an open modal dialog or a hover card, without moving focus: verified (CodeBlock and Timestamp in Chromium; heading links use the same hook).
  • Changeset: before, those buttons copied nothing there, CodeBlock and Timestamp showed a failed copy, and a violation was logged per press: verified.
  • Changeset: where the async Clipboard API is allowed, nothing changes: verified.
  • File .changeset/clipboard-sandboxed-frame.md: clear.
  • File apps/docsite/e2e/playground-preview-isolation.spec.ts: clear.
  • File apps/storybook/stories/CodeBlock.stories.tsx: clear.
  • File packages/core/src/hooks/__tests__/useClipboardFrame.a11y.chromium.spec.ts: clear.
  • File packages/core/src/hooks/useClipboard.doc.mjs: clear.
  • File packages/core/src/hooks/useClipboard.test.tsx: clear.
  • File packages/core/src/hooks/useClipboard.ts: one optional comment cleanup.

Automated review (maintainer link)

@josephfarina
josephfarina merged commit 90edc6f into main Nov 16, 2026
93 checks passed
@github-actions
github-actions Bot deleted the josephfarina/clipboard-sandbox-fallback branch November 17, 2026 06:03

This branch was successfully deployed

1 active deployment
Preview — 57fb037f Deployed Nov 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant