From 444c21a43773b03edc4311d1adf940af69a8ec3b Mon Sep 17 00:00:00 2001 From: YgorSouza <43298013+YgorSouza@users.noreply.github.com> Date: Fri, 1 Nov 2024 13:21:59 +0100 Subject: [PATCH] Prevent panic when copying text outside of a secure context (#5326) * Closes * [x] I have followed the instructions in the PR template --- crates/eframe/src/web/mod.rs | 7 +++++++ crates/egui/src/context.rs | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/crates/eframe/src/web/mod.rs b/crates/eframe/src/web/mod.rs index 746b619f..827feb92 100644 --- a/crates/eframe/src/web/mod.rs +++ b/crates/eframe/src/web/mod.rs @@ -174,6 +174,13 @@ fn set_cursor_icon(cursor: egui::CursorIcon) -> Option<()> { /// Set the clipboard text. fn set_clipboard_text(s: &str) { if let Some(window) = web_sys::window() { + if !window.is_secure_context() { + log::error!( + "Clipboard is not available because we are not in a secure context. \ + See https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts" + ); + return; + } let promise = window.navigator().clipboard().write_text(s); let future = wasm_bindgen_futures::JsFuture::from(promise); let future = async move { diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 7e7eff2e..3dedb2c8 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -1439,6 +1439,10 @@ impl Context { /// /// Empty strings are ignored. /// + /// Note that in wasm applications, the clipboard is only accessible in secure contexts (e.g., + /// HTTPS or localhost). If this method is used outside of a secure context, it will log an + /// error and do nothing. See . + /// /// Equivalent to: /// ``` /// # let ctx = egui::Context::default();