Файли
openidea/sdk/rust/openidea-plugin/tests/clipboard.rs
T
Bert Verhelstтаbertyhell 69d839e123 feat(plugins): read the clipboard from user-invoked commands (API 1.11) (#4047)
Plugins could write the clipboard but not read it. ui.read-clipboard() (SDK host::read_clipboard()) returns the text and a PNG image of the user's clipboard, behind a new clipboard.read capability. Only calls the user started can read: plugin commands and view actions run by the user, not event handlers, web view messages or commands chained through command-runner. The host never reads the clipboard itself: it asks the invoking window (ClipboardRequested / plugin_answer_clipboard), so remote development reads the client's clipboard, and a remote window asks the user first. Plugin API 1.11.0; components rebuilt.

Closes #3524

Co-authored-by: bertyhell <[email protected]>
2026-11-18 09:20:09 +08:00

36 рядки
1.7 KiB
Rust

use openidea_plugin::{ClipboardContent, Error, host, mock::MockHost};
#[test]
fn reads_text_and_images_behind_their_own_grant() {
let mock = MockHost::install().with_capabilities(&["clipboard"]);
mock.set_clipboard(Some("curl https://example.com"), None);
assert_eq!(host::read_clipboard(), Err(Error::Denied("clipboard.read".into())));
let mock = mock.with_capabilities(&["clipboard.read"]);
let text = host::read_clipboard().unwrap();
assert_eq!(text.text.as_deref(), Some("curl https://example.com"));
assert_eq!(text.image, None);
// Reading doesn't grant writing.
assert_eq!(host::copy_to_clipboard("x"), Err(Error::Denied("clipboard".into())));
let png = [0x89, b'P', b'N', b'G'];
mock.set_clipboard(None, Some(&png));
assert_eq!(host::read_clipboard().unwrap(), ClipboardContent { text: None, image: Some(png.to_vec()) });
mock.set_clipboard(None, None);
assert!(host::read_clipboard().unwrap().is_empty());
}
#[test]
fn unattended_calls_and_nested_commands_are_denied() {
let mock = MockHost::install().with_capabilities(&["clipboard.read", "ide.commands"]);
mock.set_clipboard(Some("secret"), None);
mock.set_unattended(true);
assert!(matches!(host::read_clipboard(), Err(Error::Denied(c)) if c.starts_with("clipboard.read")));
mock.set_unattended(false);
assert_eq!(host::read_clipboard().unwrap().text.as_deref(), Some("secret"));
// Another plugin's command, run through `execute_command`, is not one the user ran.
mock.on_command("plugin.other.read", |_| Ok(host::read_clipboard()?.text));
assert!(matches!(host::execute_command("plugin.other.read", None), Err(Error::Denied(_))));
}