Skip to content

Commit 444c21a

Browse files
authored
Prevent panic when copying text outside of a secure context (#5326)
* Closes <#5293> * [x] I have followed the instructions in the PR template
1 parent bbbd936 commit 444c21a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

crates/eframe/src/web/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ fn set_cursor_icon(cursor: egui::CursorIcon) -> Option<()> {
174174
/// Set the clipboard text.
175175
fn set_clipboard_text(s: &str) {
176176
if let Some(window) = web_sys::window() {
177+
if !window.is_secure_context() {
178+
log::error!(
179+
"Clipboard is not available because we are not in a secure context. \
180+
See https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts"
181+
);
182+
return;
183+
}
177184
let promise = window.navigator().clipboard().write_text(s);
178185
let future = wasm_bindgen_futures::JsFuture::from(promise);
179186
let future = async move {

crates/egui/src/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,10 @@ impl Context {
14391439
///
14401440
/// Empty strings are ignored.
14411441
///
1442+
/// Note that in wasm applications, the clipboard is only accessible in secure contexts (e.g.,
1443+
/// HTTPS or localhost). If this method is used outside of a secure context, it will log an
1444+
/// error and do nothing. See <https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts>.
1445+
///
14421446
/// Equivalent to:
14431447
/// ```
14441448
/// # let ctx = egui::Context::default();

0 commit comments

Comments
 (0)