Skip to content

Commit 5e76cb4

Browse files
RoniEsteinclaude
authored andcommitted
fix(web): prevent textarea DOM leak in copy fallback
Move removeChild into a finally block so the hidden textarea is always cleaned up, even if select() or execCommand() throws. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 58bb2c0 commit 5e76cb4

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -498,22 +498,27 @@ function MarkdownCodeBlock({
498498
};
499499

500500
if (typeof navigator !== "undefined" && navigator.clipboard != null) {
501-
void navigator.clipboard.writeText(code).then(onSuccess).catch(() => undefined);
501+
void navigator.clipboard
502+
.writeText(code)
503+
.then(onSuccess)
504+
.catch(() => undefined);
502505
return;
503506
}
504507

508+
// Fallback for non-secure contexts (e.g. HTTP over Tailscale)
509+
const textarea = document.createElement("textarea");
510+
textarea.value = code;
511+
textarea.style.position = "fixed";
512+
textarea.style.opacity = "0";
513+
document.body.appendChild(textarea);
505514
try {
506-
const textarea = document.createElement("textarea");
507-
textarea.value = code;
508-
textarea.style.position = "fixed";
509-
textarea.style.opacity = "0";
510-
document.body.appendChild(textarea);
511515
textarea.select();
512516
const ok = document.execCommand("copy");
513-
document.body.removeChild(textarea);
514517
if (ok) onSuccess();
515518
} catch {
516519
// Ignore clipboard failures in unsupported browser contexts.
520+
} finally {
521+
document.body.removeChild(textarea);
517522
}
518523
}, [code]);
519524

0 commit comments

Comments
 (0)