Файли
openidea/crates/ide-host/tests/distribution.rs
T
LU XIANGRUIтаwildcatDownstairs 10242d2e58 feat(plugins): support Markdown and UI-locale localization for Overview and What's New (#6776)
Plugin Overview and What's New rendered as plain text and ignored the UI language. plugin.toml gains optional description-file and change-notes-file (API 1.18, Markdown inside the package, UTF-8, at most 256 KiB); a shared loader validates them and localized siblings (description.zh-CN.md) in check, pack, install and index. Descriptions and change notes resolve per locale from locales/<tag>.json and those siblings, with English fallback, for installed, local catalog and marketplace plugins.

Markdown is rendered with lazily loaded marked and an isolated DOMPurify that strips scripts, event handlers, javascript:/file: links, styles and external resources, keeping marked out of the Settings window's boot graph.

Closes #6773


Merged with main: the pending-uninstall placeholder plugin now gets docs from its manifest.

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-29 13:31:13 +08:00

934 рядки
44 KiB
Rust
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
//! Versioned installs with automatic rollback, signed packages from
//! a static marketplace index, and revocation.
use ide_extension::package::{self, Index, IndexEntry, KeyEntry, PluginIcon, Publisher, PublisherKey, Revocation};
use ide_host::plugins::*;
use ide_plugin::{Invocation, Limits};
use parking_lot::Mutex;
use std::path::{Path, PathBuf};
use std::sync::Arc;
type Events = Arc<Mutex<Vec<PluginEvent>>>;
fn repo() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")
}
fn copy_dir(from: &Path, to: &Path) {
std::fs::create_dir_all(to).unwrap();
for entry in std::fs::read_dir(from).unwrap().flatten() {
let (src, dst) = (entry.path(), to.join(entry.file_name()));
if entry.file_name() == "target" {
continue;
}
if src.is_dir() { copy_dir(&src, &dst) } else { std::fs::copy(&src, &dst).map(|_| ()).unwrap() }
}
}
/// A copy of `from` (under `dir/name`) whose manifest is rewritten by `f`.
fn variant(from: &Path, dir: &Path, name: &str, f: impl Fn(&str) -> String) -> PathBuf {
let pkg = dir.join(name);
copy_dir(from, &pkg);
let manifest = pkg.join("plugin.toml");
std::fs::write(&manifest, f(&std::fs::read_to_string(&manifest).unwrap())).unwrap();
pkg
}
struct Fixture {
host: PluginHost,
plugins_dir: PathBuf,
events: Events,
_tmp: tempfile::TempDir,
work: PathBuf,
}
fn fixture() -> Fixture {
let tmp = tempfile::tempdir().unwrap();
let plugins_dir = tmp.path().join("plugins");
let events: Events = Arc::default();
let sink = {
let events = events.clone();
move |_: Option<&str>, e: PluginEvent| events.lock().push(e)
};
let host = PluginHost::new(plugins_dir.clone(), Arc::new(sink), Limits::default()).unwrap();
host.load_all();
let work = tmp.path().join("work");
std::fs::create_dir_all(&work).unwrap();
Fixture { host, plugins_dir, events, work, _tmp: tmp }
}
fn inv() -> Invocation {
Invocation {
command_chain: Vec::new(),
client: Some("main".into()),
workspace_root: None,
editor: None,
selected_paths: Vec::new(),
global_settings: false,
}
}
fn misbehave() -> PathBuf {
repo().join("crates/ide-plugin/tests/fixtures/misbehave")
}
/// P8 acceptance: an update that fails to start rolls back to the version it replaced.
#[test]
fn a_failing_update_rolls_back_automatically() {
let f = fixture();
let v1 = variant(&misbehave(), &f.work, "v1", |t| t.replace("version = \"0.0.2\"", "version = \"1.0.0\""));
f.host.install_from_dir(&v1, &Consent::all()).unwrap();
assert_eq!(f.host.run_command("misbehave", "echo", Some("hi"), inv()).unwrap().as_deref(), Some("hi"));
let broken =
variant(&misbehave(), &f.work, "v2", |t| t.replace("version = \"0.0.2\"", "version = \"2.0.0-broken\""));
let info = f.host.install_from_dir(&broken, &Consent::all()).unwrap();
assert_eq!((info.version.as_str(), info.previous_version.as_deref()), ("2.0.0-broken", Some("1.0.0")));
let root = f.plugins_dir.join("misbehave");
assert!(root.join("2.0.0-broken").is_dir() && root.join("1.0.0").is_dir(), "versions side by side");
// Its first start fails: back to 1.0.0, without the user doing anything.
f.events.lock().clear();
let err = f.host.run_command("misbehave", "echo", Some("x"), inv()).unwrap_err();
assert!(err.message.contains("version 1.0.0 was restored"), "{}", err.message);
let info = f.host.info_of("misbehave").unwrap();
assert_eq!((info.version.as_str(), info.status), ("1.0.0", PluginStatus::Inactive));
assert!(!root.join("2.0.0-broken").exists(), "the failed version is removed");
assert!(
f.events
.lock()
.iter()
.any(|e| matches!(e, PluginEvent::Notification { title, .. } if title.contains("rolled back")))
);
assert_eq!(f.host.run_command("misbehave", "echo", Some("ok"), inv()).unwrap().as_deref(), Some("ok"));
// A working update is confirmed by its first start; the user can still roll it back.
let v3 = variant(&misbehave(), &f.work, "v3", |t| t.replace("version = \"0.0.2\"", "version = \"3.0.0\""));
f.host.install_from_dir(&v3, &Consent::all()).unwrap();
f.host.run_command("misbehave", "echo", None, inv()).unwrap();
assert_eq!(f.host.info_of("misbehave").unwrap().version, "3.0.0");
let info = f.host.roll_back_update("misbehave").unwrap();
assert_eq!((info.version.as_str(), info.previous_version.as_deref()), ("1.0.0", None));
assert!(f.host.roll_back_update("misbehave").is_err(), "nothing older to go back to");
// A restart loads the active version.
let again = PluginHost::new(f.plugins_dir.clone(), Arc::new(|_: Option<&str>, _| {}), Limits::default()).unwrap();
again.load_all();
assert_eq!(again.info_of("misbehave").unwrap().version, "1.0.0");
f.host.uninstall("misbehave").unwrap();
assert!(!root.exists(), "every version goes");
}
#[test]
fn installs_from_before_versioning_keep_working_and_move_on_update() {
let f = fixture();
// The flat layout of earlier releases.
copy_dir(&misbehave(), &f.plugins_dir.join("misbehave"));
let again = PluginHost::new(f.plugins_dir.clone(), Arc::new(|_: Option<&str>, _| {}), Limits::default()).unwrap();
again.load_all();
assert_eq!(again.info_of("misbehave").unwrap().version, "0.0.2");
let v1 = variant(&misbehave(), &f.work, "v1", |t| t.replace("version = \"0.0.2\"", "version = \"1.0.0\""));
let info = again.install_from_dir(&v1, &Consent::all()).unwrap();
assert_eq!((info.version.as_str(), info.previous_version.as_deref()), ("1.0.0", Some("0.0.2")));
assert!(f.plugins_dir.join("misbehave/0.0.2/plugin.toml").is_file());
assert!(f.plugins_dir.join("misbehave/1.0.0/plugin.toml").is_file());
}
// ---- marketplace ------------------------------------------------------------------
/// `hello` as `acme.hello` at `version`, packed and signed with `key`.
fn signed_hello(dir: &Path, version: &str, key: &PublisherKey, host: &PluginHost) -> (PathBuf, IndexEntry) {
signed(dir, "acme", version, key, host, |t| t.to_owned())
}
/// `hello` as `<publisher>.hello` at `version`, its manifest changed by
/// `edit`, packed and signed with `key`.
fn signed(
dir: &Path,
publisher: &str,
version: &str,
key: &PublisherKey,
host: &PluginHost,
edit: impl Fn(&str) -> String,
) -> (PathBuf, IndexEntry) {
let id = format!("{publisher}.hello");
let src =
variant(&repo().join("crates/ide-plugin/tests/fixtures/hello"), dir, &format!("src-{id}-{version}"), |t| {
edit(
&t.replace("id = \"hello\"", &format!("id = \"{id}\""))
.replace("version = \"0.3.0\"", &format!("version = \"{version}\"")),
)
});
let files = host.package_files_of(&src).unwrap();
let digest = package::package_digest(&src, &files).unwrap();
let sig = key.sign(publisher, &id, version, &digest);
let out = dir.join(format!("{id}-{version}.oiplugin"));
package::pack(&src, &files, &out, Some(&sig)).unwrap();
let entry = IndexEntry {
id: id.clone(),
name: "Hello".into(),
version: version.into(),
description: String::new(),
description_locales: Default::default(),
authors: vec![],
publisher: publisher.into(),
download: out.file_name().unwrap().to_string_lossy().into_owned(),
digest,
capabilities: vec![],
engine: "^1.0".into(),
categories: vec!["tools".into()],
icon: None,
homepage: None,
notes: None,
notes_locales: Default::default(),
downloads: None,
featured: false,
suggest: vec![],
};
(out, entry)
}
fn write_index(
dir: &Path,
key: &PublisherKey,
plugins: Vec<IndexEntry>,
revoked: Vec<Revocation>,
key_revoked: bool,
) -> String {
let index = Index {
format: package::INDEX_FORMAT,
publishers: vec![Publisher {
id: "acme".into(),
name: "ACME".into(),
keys: vec![KeyEntry { revoked: key_revoked, ..KeyEntry::new(key.public_base64()) }],
}],
plugins,
revoked,
moved: None,
};
let path = dir.join("index.json");
std::fs::write(&path, serde_json::to_string(&index).unwrap()).unwrap();
path.to_string_lossy().into_owned()
}
/// The download sources setting with `mirror` as the GitHub proxy prefix (Tools › Download sources).
fn download_sources(mirror: &str) -> ide_core::settings::ToolDownloadSettings {
ide_core::settings::ToolDownloadSettings { github_mirror: mirror.into(), ..Default::default() }
}
/// Security audit lead 5: an index entry's id and version name the download file,
/// so an entry whose id or version would place it outside the downloads folder
/// is not offered, and nothing is written for it.
#[test]
fn market_entry_ids_cannot_place_the_download_outside_the_downloads_folder() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (_, hello) = signed_hello(&market, "1.0.0", &key, &f.host);
// Never read: an entry that is not offered is not downloaded.
std::fs::write(market.join("junk.bin"), b"attacker bytes").unwrap();
// The id leaves the folder on every platform (`.downloads` exists by then); the version
// only where `..` is resolved before the path is opened (Windows).
for (id, version) in [("../../escape", "1.0.0"), ("acme.hello", "1.0.0/../../../escape")] {
let entry = IndexEntry { id: id.into(), version: version.into(), download: "junk.bin".into(), ..hello.clone() };
let url = write_index(&market, &key, vec![entry], vec![], false);
f.host.set_market_url(Some(url));
f.host.refresh_market().unwrap();
assert!(f.host.market_plugins().is_empty(), "{id} {version} is offered");
let err = f.host.download_market_plugin(id).unwrap_err();
assert!(err.message.contains("is not in the marketplace"), "{id} {version}: {}", err.message);
}
let downloads = f.plugins_dir.join(".downloads");
let mut escaped = Vec::new();
let mut pending = vec![f._tmp.path().to_path_buf()];
while let Some(dir) = pending.pop() {
for e in std::fs::read_dir(&dir).unwrap().flatten() {
let path = e.path();
if path.is_dir() {
pending.push(path);
} else if path.extension().is_some_and(|x| x == "oiplugin")
&& !path.starts_with(&downloads)
&& !path.starts_with(&market)
{
escaped.push(path);
}
}
}
assert!(escaped.is_empty(), "the download was written outside {}: {escaped:?}", downloads.display());
}
/// First run: nothing cached yet, so listing the marketplace reads its index
/// instead of showing an empty one; later listings use the index read.
#[test]
fn the_first_marketplace_listing_reads_the_index() {
let f = fixture();
assert!(f.host.market_listing(false).unwrap().is_empty(), "no marketplace configured");
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (_, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
let url = write_index(&market, &key, vec![entry], vec![], false);
f.host.set_market_url(Some(url.clone()));
let listed = f.host.market_listing(false).unwrap();
assert_eq!(listed.iter().map(|p| p.entry.id.as_str()).collect::<Vec<_>>(), ["acme.hello"]);
std::fs::remove_file(&url).unwrap();
assert_eq!(f.host.market_listing(false).unwrap().len(), 1, "the index read is kept");
assert!(f.host.market_listing(true).is_err(), "refresh reads the index again");
}
#[test]
fn signed_marketplace_plugins_install_and_revocations_disable_them() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (_, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
let url = write_index(&market, &key, vec![entry.clone()], vec![], false);
assert!(f.host.refresh_market().is_err(), "no marketplace configured yet");
f.host.set_market_url(Some(url.clone()));
let listed = f.host.refresh_market().unwrap();
assert_eq!((listed[0].entry.id.as_str(), listed[0].installed.as_deref()), ("acme.hello", None));
let dir = f.host.download_market_plugin("acme.hello").unwrap();
let preview = f.host.preview_install(&dir).unwrap();
assert_eq!((preview.unsigned, preview.publisher.as_deref()), (false, Some("acme")));
let info = f.host.install_from_dir(&dir, &Consent::all()).unwrap();
assert_eq!(info.publisher.as_deref(), Some("acme"));
assert_eq!(f.host.market_plugins()[0].installed.as_deref(), Some("1.0.0"));
// A changed file breaks the signature: refused.
let tampered = f.host.download_market_plugin("acme.hello").unwrap();
let manifest = std::fs::read_to_string(tampered.join("plugin.toml")).unwrap();
std::fs::write(tampered.join("plugin.toml"), manifest.replace("Sample plugin", "Totally safe plugin")).unwrap();
let err = f.host.preview_install(&tampered).unwrap_err();
assert!(err.message.contains("changed after signing"), "{}", err.message);
// Someone else's key, or another publisher's prefix: not trusted.
let stranger = PublisherKey::from_seed([4; 32]);
let (foreign, _) = signed_hello(&f.work, "1.0.1", &stranger, &f.host);
let unpacked = f.host.unpack_package(&foreign).unwrap();
assert!(f.host.preview_install(&unpacked).unwrap_err().message.contains("not one of"));
// The index revokes the installed version: disabled, the user told.
f.events.lock().clear();
let revoked =
vec![Revocation { id: "acme.hello".into(), versions: vec!["1.0.0".into()], reason: "steals tokens".into() }];
write_index(&market, &key, vec![entry.clone()], revoked, false);
f.host.refresh_market().unwrap();
let info = f.host.info_of("acme.hello").unwrap();
assert_eq!((info.status, info.error.as_deref()), (PluginStatus::Failed, Some("revoked: steals tokens")));
assert!(
f.events
.lock()
.iter()
.any(|e| matches!(e, PluginEvent::Notification { body, .. } if body.contains("steals tokens")))
);
assert!(f.host.market_plugins().is_empty(), "revoked versions are not offered");
assert!(f.host.install_from_dir(&dir, &Consent::all()).is_err(), "nor installable");
f.host.set_enabled("acme.hello", true).unwrap();
assert_eq!(f.host.info_of("acme.hello").unwrap().status, PluginStatus::Failed, "enabling doesn't bring it back");
// Revocations hold across restarts (the index is kept).
let again = PluginHost::new(f.plugins_dir.clone(), Arc::new(|_: Option<&str>, _| {}), Limits::default()).unwrap();
again.load_all();
assert_eq!(again.info_of("acme.hello").unwrap().status, PluginStatus::Failed);
// A revoked signing key disables what it signed.
write_index(&market, &key, vec![entry.clone()], vec![], true);
f.host.refresh_market().unwrap();
let info = f.host.info_of("acme.hello").unwrap();
assert!(info.error.as_deref().unwrap_or("").contains("signing key was revoked"), "{:?}", info.error);
// Lifted: it runs again.
write_index(&market, &key, vec![entry], vec![], false);
f.host.refresh_market().unwrap();
let info = f.host.info_of("acme.hello").unwrap();
assert_eq!((info.status, info.error), (PluginStatus::Inactive, None));
}
#[test]
fn package_files_install_from_disk_unsigned() {
let f = fixture();
let src = variant(&misbehave(), &f.work, "pkg", |t| t.to_owned());
let files = f.host.package_files_of(&src).unwrap();
let out = f.work.join("misbehave-0.0.2.oiplugin");
package::pack(&src, &files, &out, None).unwrap();
let dir = f.host.unpack_package(&out).unwrap();
let preview = f.host.preview_install(&dir).unwrap();
assert_eq!((preview.unsigned, preview.publisher), (true, None));
let info = f.host.install_from_dir(&dir, &Consent::all()).unwrap();
assert_eq!((info.status, info.publisher), (PluginStatus::Inactive, None));
}
#[test]
fn the_marketplace_offers_the_newest_version_this_ide_can_load() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (_, old) = signed_hello(&market, "1.0.0", &key, &f.host);
let (_, new) = signed_hello(&market, "2.0.0", &key, &f.host);
let next_major = ide_extension::ENGINE_VERSION.split('.').next().unwrap().parse::<u64>().unwrap() + 1;
let new = IndexEntry { engine: format!("^{next_major}.0"), ..new };
f.host.set_market_url(Some(write_index(&market, &key, vec![old.clone(), new.clone()], vec![], false)));
let listed = f.host.refresh_market().unwrap();
assert_eq!((listed.len(), listed[0].entry.version.as_str(), listed[0].compatible), (1, "1.0.0", true));
let dir = f.host.download_market_plugin("acme.hello").unwrap();
assert_eq!(f.host.preview_install(&dir).unwrap().version, "1.0.0");
// Only a version for a newer IDE: shown as such, not downloaded.
write_index(&market, &key, vec![new], vec![], false);
let listed = f.host.refresh_market().unwrap();
assert_eq!((listed[0].entry.version.as_str(), listed[0].compatible), ("2.0.0", false));
let err = f.host.download_market_plugin("acme.hello").unwrap_err();
assert!(err.message.contains("update OpenIDEA"), "{}", err.message);
}
#[test]
fn a_moved_marketplace_is_read_where_it_moved_to() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (_, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
write_index(&market, &key, vec![entry], vec![], false);
// The old address points at the new one; downloads resolve against where the index is.
let old = f.work.join("old");
std::fs::create_dir_all(&old).unwrap();
std::fs::write(old.join("index.json"), r#"{"format": 1, "moved": "../market/index.json"}"#).unwrap();
f.host.set_market_url(Some(old.join("index.json").to_string_lossy().into_owned()));
let listed = f.host.refresh_market().unwrap();
assert_eq!(listed[0].entry.id, "acme.hello");
let dir = f.host.download_market_plugin("acme.hello").unwrap();
assert_eq!(f.host.preview_install(&dir).unwrap().version, "1.0.0");
// A loop gives up.
std::fs::write(old.join("index.json"), r#"{"format": 1, "moved": "index.json"}"#).unwrap();
let err = f.host.refresh_market().unwrap_err();
assert!(err.message.contains("moved more than"), "{}", err.message);
}
/// A loopback HTTP server whose answer to every request `respond` writes; the URL of its index.
fn serve_with(respond: impl Fn(&mut std::net::TcpStream) + Send + 'static) -> String {
use std::io::{BufRead, BufReader};
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://127.0.0.1:{}/index.json", listener.local_addr().unwrap().port());
std::thread::spawn(move || {
for mut stream in listener.incoming().flatten() {
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut line = String::new();
while reader.read_line(&mut line).unwrap_or(0) > 0 && line != "\r\n" {
line.clear();
}
respond(&mut stream);
}
});
url
}
/// A loopback HTTP server answering every request with `body`; the URL of its index.
fn serve(body: String) -> String {
use std::io::Write;
serve_with(move |stream| {
let _ = write!(
stream,
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len()
);
})
}
/// A loopback HTTP server that logs every request path it is asked for and answers with
/// `bytes` (a package file); the base URL to put in front of the GitHub one.
fn serve_bytes(bytes: Vec<u8>, paths: Arc<Mutex<Vec<String>>>) -> String {
use std::io::{BufRead, BufReader, Write};
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let base = format!("http://127.0.0.1:{}", listener.local_addr().unwrap().port());
std::thread::spawn(move || {
for mut stream in listener.incoming().flatten() {
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut line = String::new();
if reader.read_line(&mut line).unwrap_or(0) > 0 {
paths.lock().push(line.trim().to_owned());
}
while reader.read_line(&mut line).unwrap_or(0) > 0 && line != "\r\n" {
line.clear();
}
let _ = write!(
stream,
"HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
bytes.len()
);
let _ = stream.write_all(&bytes);
}
});
base
}
/// A configured mirror is tried before the official host, and its bytes must still pass the
/// index's digest and the publisher's signature: a mirror only ever carries the download
/// (Tools › Download sources), never the trust root.
#[test]
fn a_mirror_prefix_carries_plugin_packages_and_only_bytes_that_check() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (package, mut entry) = signed_hello(&market, "1.0.0", &key, &f.host);
// The download is a GitHub one, which is the only kind a prefix may carry.
entry.download = format!(
"https://github.com/acme/openidea-hello/releases/download/v1.0.0/{}",
package.file_name().unwrap().to_string_lossy()
);
let url = write_index(&market, &key, vec![entry.clone()], vec![], false);
f.host.set_market_url(Some(url));
f.host.refresh_market().unwrap();
// The mirror answers with the real package.
let paths: Arc<Mutex<Vec<String>>> = Arc::default();
let base = serve_bytes(std::fs::read(&package).unwrap(), paths.clone());
f.host.set_downloads(download_sources(&base));
let dir = f.host.download_market_plugin("acme.hello").unwrap();
assert_eq!(f.host.preview_install(&dir).unwrap().version, "1.0.0");
let asked = paths.lock().clone();
assert_eq!(asked.len(), 1, "{asked:?}");
assert!(
asked[0].contains(entry.download.trim_start_matches("https://")),
"the official URL is appended to the prefix unchanged: {asked:?}"
);
// No mirror: the official URL is what is asked for, exactly as before.
f.host.set_downloads(download_sources(""));
assert_eq!(
ide_tools::mirror::candidates(&entry.download, &download_sources("")),
vec![ide_tools::mirror::Candidate { url: entry.download.clone(), mirror: None }]
);
// A download that is not on GitHub is not one a prefix may carry: it stays official.
let mut local = entry.clone();
local.download = package.to_string_lossy().into_owned();
assert_eq!(
ide_tools::mirror::candidates(&local.download, &download_sources(&base)),
vec![ide_tools::mirror::Candidate { url: local.download.clone(), mirror: None }]
);
}
/// A mirror serving a tampered package is a source that fails its checks — and the unpacked
/// bytes it served must not be left behind where the install page reads from.
///
/// The index has no hash of the archive itself (only `package_digest` of the unpacked
/// contents), so the bytes have to be unpacked before they can be checked; the test therefore
/// watches the staging folders, not the archive.
#[test]
fn a_tampered_package_from_a_mirror_is_refused_and_leaves_nothing_behind() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (package, mut entry) = signed_hello(&market, "1.0.0", &key, &f.host);
entry.download = format!(
"https://github.com/acme/openidea-hello/releases/download/v1.0.0/{}",
package.file_name().unwrap().to_string_lossy()
);
let url = write_index(&market, &key, vec![entry.clone()], vec![], false);
f.host.set_market_url(Some(url));
f.host.refresh_market().unwrap();
// A package whose manifest was changed after signing: its digest disagrees with the index,
// and it carries no signature at all.
let tampered = {
let dir = f.work.join("tampered-src");
std::fs::create_dir_all(&dir).unwrap();
let unpacked = f.host.unpack_package(&package).unwrap();
copy_dir(&unpacked, &dir.join("pkg"));
let manifest = std::fs::read_to_string(dir.join("pkg/plugin.toml")).unwrap();
std::fs::write(dir.join("pkg/plugin.toml"), manifest.replace("Sample plugin", "Totally safe plugin")).unwrap();
let files = f.host.package_files_of(&dir.join("pkg")).unwrap();
let out = dir.join("tampered.oiplugin");
package::pack(&dir.join("pkg"), &files, &out, None).unwrap();
out
};
let mirror_paths: Arc<Mutex<Vec<String>>> = Arc::default();
let mirror = serve_bytes(std::fs::read(&tampered).unwrap(), mirror_paths.clone());
let downloads = f.plugins_dir.join(".downloads");
// Browsing the package above went through `unpack_package`, which stages a folder of its
// own: clear what that left so the baseline is this attempt's alone.
for dir in staging_dirs(&downloads) {
let _ = std::fs::remove_dir_all(downloads.join(dir));
}
let staging_before = staging_dirs(&downloads);
assert!(staging_before.is_empty(), "{staging_before:?}");
// The mirror answers with the tampered package, and it is the only source configured: the
// download must fail rather than install what the mirror served.
f.host.set_downloads(download_sources(&mirror));
let err = f.host.download_market_plugin("acme.hello").unwrap_err();
assert!(err.message.contains("could not download"), "{}", err.message);
assert!(err.message.contains("does not match the marketplace's entry"), "{}", err.message);
assert_eq!(mirror_paths.lock().len(), 1, "the mirror was asked once");
// Nothing the mirror served stays where the install page reads from, and the staging folder
// of the refused attempt is gone.
assert_eq!(staging_dirs(&downloads), staging_before, "a refused package left its staging folder");
let leftovers: Vec<PathBuf> = std::fs::read_dir(&downloads)
.unwrap()
.flatten()
.map(|e| e.path())
.filter(|p| p.to_string_lossy().contains("acme.hello"))
.collect();
assert!(leftovers.is_empty(), "a refused package left files behind: {leftovers:?}");
// The official host, serving the real package, still installs.
let official_paths: Arc<Mutex<Vec<String>>> = Arc::default();
let official = serve_bytes(std::fs::read(&package).unwrap(), official_paths.clone());
f.host.set_downloads(download_sources(&official));
let dir = f.host.download_market_plugin("acme.hello").unwrap();
assert_eq!(f.host.preview_install(&dir).unwrap().version, "1.0.0");
assert_eq!(official_paths.lock().len(), 1);
}
/// The staging folders under `<plugins>/.downloads`: one per unpacked package, `<stem>-<pid>`.
fn staging_dirs(downloads: &Path) -> Vec<String> {
let Ok(entries) = std::fs::read_dir(downloads) else { return Vec::new() };
let mut names: Vec<String> = entries
.flatten()
.filter(|e| e.file_type().map(|t| t.is_dir()).unwrap_or(false))
.map(|e| e.file_name().to_string_lossy().into_owned())
.collect();
names.sort();
names
}
/// An index is read up to the size an index may have, whatever its server says or sends:
/// the marketplace is read again every day, with nobody asking.
#[test]
fn an_index_larger_than_an_index_may_be_is_not_read_to_its_end() {
use std::io::Write;
use std::sync::atomic::{AtomicUsize, Ordering};
let f = fixture();
// Announced: refused on its headers, before any of it is read.
f.host.set_market_url(Some(serve_with(|stream| {
let _ = stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 1073741824\r\nConnection: close\r\n\r\n{");
})));
let err = f.host.refresh_market().unwrap_err();
assert!(err.message.contains("larger than 16 MiB"), "{}", err.message);
// Not announced: the read stops at the limit, and the server is left with the rest.
const BODY: usize = 96 << 20;
let sent = Arc::new(AtomicUsize::new(0));
f.host.set_market_url(Some(serve_with({
let sent = sent.clone();
move |stream| {
let _ = stream.write_all(b"HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n");
let chunk = [b' '; 64 << 10];
let mut written = 0;
while written < BODY && stream.write_all(&chunk).is_ok() {
written += chunk.len();
}
sent.store(written.max(1), Ordering::SeqCst);
}
})));
let err = f.host.refresh_market().unwrap_err();
assert!(err.message.contains("larger than 16 MiB"), "{}", err.message);
let waiting = std::time::Instant::now();
while sent.load(Ordering::SeqCst) == 0 && waiting.elapsed() < std::time::Duration::from_secs(30) {
std::thread::sleep(std::time::Duration::from_millis(20));
}
let sent = sent.load(Ordering::SeqCst);
assert!(sent > 0 && sent < BODY, "the server sent {sent} of {BODY} bytes");
}
/// An index read over the network names downloads on the network: not a file of this
/// machine, nor a share the machine would sign in to.
#[test]
fn an_index_read_over_the_network_cannot_name_a_local_download() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (package, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
let path = package.to_string_lossy().into_owned();
// A package that installs, so only where the index was read from can refuse it.
for download in [path.clone(), format!("file://{path}")] {
let entry = IndexEntry { download: download.clone(), ..entry.clone() };
let local = write_index(&market, &key, vec![entry], vec![], false);
f.host.set_market_url(Some(local.clone()));
f.host.refresh_market().unwrap();
let dir = f.host.download_market_plugin("acme.hello").unwrap();
assert_eq!(f.host.preview_install(&dir).unwrap().version, "1.0.0", "{download}");
f.host.set_market_url(Some(serve(std::fs::read_to_string(&local).unwrap())));
assert!(f.host.refresh_market().unwrap().is_empty(), "{download} is offered");
let err = f.host.download_market_plugin("acme.hello").unwrap_err();
assert!(err.message.contains("is not in the marketplace"), "{download}: {}", err.message);
}
}
/// Nor when an index on this machine is what sent the IDE to the one on the network.
#[test]
fn an_index_reached_through_a_local_one_cannot_name_a_local_download() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (package, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
let entry = IndexEntry { download: package.to_string_lossy().into_owned(), ..entry };
let served = serve(std::fs::read_to_string(write_index(&market, &key, vec![entry], vec![], false)).unwrap());
let old = f.work.join("old");
std::fs::create_dir_all(&old).unwrap();
let moved = serde_json::json!({ "format": package::INDEX_FORMAT, "moved": served }).to_string();
std::fs::write(old.join("index.json"), moved).unwrap();
f.host.set_market_url(Some(old.join("index.json").to_string_lossy().into_owned()));
assert!(f.host.refresh_market().unwrap().is_empty(), "the local download is offered");
let err = f.host.download_market_plugin("acme.hello").unwrap_err();
assert!(err.message.contains("is not in the marketplace"), "{}", err.message);
}
/// The index kept from the last read does not say where it was read from: what an earlier
/// version kept is judged against the marketplace in use.
#[test]
fn a_kept_index_cannot_name_a_local_download_for_a_marketplace_on_the_network() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (package, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
let entry = IndexEntry { download: package.to_string_lossy().into_owned(), ..entry };
let kept = write_index(&market, &key, vec![entry], vec![], false);
std::fs::copy(kept, f.plugins_dir.join("market-index.json")).unwrap();
let again = PluginHost::new(f.plugins_dir.clone(), Arc::new(|_: Option<&str>, _| {}), Limits::default()).unwrap();
// Never read: the check comes before any request.
again.set_market_url(Some("https://market.invalid/index.json".into()));
assert_eq!(again.market_plugins()[0].entry.id, "acme.hello");
let err = again.download_market_plugin("acme.hello").unwrap_err();
assert!(err.message.contains("is not on the network"), "{}", err.message);
}
/// Nor may it send the IDE to an index on this machine.
#[test]
fn an_index_read_over_the_network_cannot_move_to_a_local_one() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (_, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
let local = write_index(&market, &key, vec![entry], vec![], false);
let moved = serde_json::json!({ "format": package::INDEX_FORMAT, "moved": local }).to_string();
f.host.set_market_url(Some(serve(moved)));
let err = f.host.refresh_market().unwrap_err();
assert!(err.message.contains("is not on the network"), "{}", err.message);
assert!(f.host.market_plugins().is_empty(), "the local index was read");
}
/// A download that turns out not to be a package is not kept.
#[test]
fn a_download_that_is_not_a_package_leaves_nothing_behind() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (_, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
std::fs::write(market.join("junk.bin"), b"not a package").unwrap();
let entry = IndexEntry { download: "junk.bin".into(), ..entry };
f.host.set_market_url(Some(write_index(&market, &key, vec![entry], vec![], false)));
f.host.refresh_market().unwrap();
let err = f.host.download_market_plugin("acme.hello").unwrap_err();
assert!(err.message.contains("not a plugin package"), "{}", err.message);
let left: Vec<_> = std::fs::read_dir(f.plugins_dir.join(".downloads"))
.map(|dir| dir.flatten().map(|e| e.file_name()).collect())
.unwrap_or_default();
assert!(left.is_empty(), "{left:?}");
}
/// `dir/index.json` listing `publishers` and `plugins`; its path.
fn index_of(dir: &Path, publishers: Vec<Publisher>, plugins: Vec<IndexEntry>) -> String {
let index = Index { format: package::INDEX_FORMAT, publishers, plugins, ..Index::default() };
let path = dir.join("index.json");
std::fs::write(&path, serde_json::to_string(&index).unwrap()).unwrap();
path.to_string_lossy().into_owned()
}
fn publisher(id: &str, keys: Vec<KeyEntry>) -> Publisher {
Publisher { id: id.into(), name: id.into(), keys }
}
fn updates_announced(events: &Events) -> Vec<Vec<String>> {
events
.lock()
.iter()
.filter_map(|e| match e {
PluginEvent::UpdatesAvailable { updates } => {
Some(updates.iter().map(|u| format!("{} {}", u.id, u.version)).collect())
}
_ => None,
})
.collect()
}
/// What the install page sends for an update it doesn't need to show: the grants the installed version has.
fn as_granted(preview: &InstallPreview) -> Consent {
let optional = preview.capabilities.iter().filter(|c| c.optional && c.granted).map(|c| c.id.clone()).collect();
Consent { optional, signer_change: false }
}
#[test]
fn updates_keep_their_signer_and_need_no_consent_when_nothing_is_new() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let (k1, k2, k3) =
(PublisherKey::from_seed([1; 32]), PublisherKey::from_seed([2; 32]), PublisherKey::from_seed([3; 32]));
let same = |t: &str| t.to_owned();
let (_, v1) = signed(&market, "acme", "1.0.0", &k1, &f.host, same);
f.host.set_market_url(Some(index_of(
&market,
vec![publisher("acme", vec![KeyEntry::new(k1.public_base64())])],
vec![v1.clone()],
)));
f.host.refresh_market().unwrap();
let dir = f.host.download_market_plugin("acme.hello").unwrap();
let first = f.host.preview_install(&dir).unwrap();
assert!(first.needs_consent && !first.signer_changed, "a first install is confirmed");
f.host.install_from_dir(&dir, &Consent::default()).unwrap();
assert!(updates_announced(&f.events).is_empty());
// 1.1.0, same key and needs: announced once, installed with the grants it has.
let (_, v11) = signed(&market, "acme", "1.1.0", &k1, &f.host, same);
let keys1 = vec![KeyEntry::new(k1.public_base64())];
index_of(&market, vec![publisher("acme", keys1.clone())], vec![v1.clone(), v11.clone()]);
f.host.refresh_market().unwrap();
f.host.refresh_market().unwrap();
assert_eq!(updates_announced(&f.events), [["acme.hello 1.1.0"]], "once per set of versions");
let dir = f.host.download_market_plugin("acme.hello").unwrap();
let preview = f.host.preview_install(&dir).unwrap();
assert_eq!(preview.replaces.as_deref(), Some("1.0.0"));
assert!(!preview.needs_consent && !preview.signer_changed);
assert!(preview.capabilities.iter().all(|c| c.granted), "shows what the installed version was granted");
f.host.install_from_dir(&dir, &as_granted(&preview)).unwrap();
assert!(f.host.market_plugins().iter().all(|p| !p.is_update()));
// 1.2.0 asks for something new: the user is asked.
let (_, v12) = signed(&market, "acme", "1.2.0", &k1, &f.host, |t| {
t.replace(
"required = [\"editor.read\", \"editor.write\"]",
"required = [\"editor.read\", \"editor.write\"]\noptional = [\"net:example.com\"]",
)
});
index_of(&market, vec![publisher("acme", keys1.clone())], vec![v12.clone()]);
f.host.refresh_market().unwrap();
let dir = f.host.download_market_plugin("acme.hello").unwrap();
let preview = f.host.preview_install(&dir).unwrap();
assert!(preview.needs_consent && !preview.signer_changed);
f.host.install_from_dir(&dir, &Consent::default()).unwrap();
// 1.3.0 signed with a key the installed one didn't endorse: another signer, the user's call.
let (_, v13) = signed(&market, "acme", "1.3.0", &k2, &f.host, same);
let keys12 = vec![KeyEntry::new(k1.public_base64()), KeyEntry::new(k2.public_base64())];
index_of(&market, vec![publisher("acme", keys12)], vec![v13]);
f.host.refresh_market().unwrap();
let dir = f.host.download_market_plugin("acme.hello").unwrap();
let preview = f.host.preview_install(&dir).unwrap();
assert!(preview.signer_changed && preview.needs_consent);
let err = f.host.install_from_dir(&dir, &as_granted(&preview)).unwrap_err();
assert!(err.message.contains("another key"), "{}", err.message);
assert_eq!(f.host.info_of("acme.hello").unwrap().version, "1.2.0", "nothing changed");
f.host.install_from_dir(&dir, &Consent { signer_change: true, ..as_granted(&preview) }).unwrap();
// 1.4.0 signed with k3, which k2 endorsed: a rotation the installed signer vouched for.
let (_, v14) = signed(&market, "acme", "1.4.0", &k3, &f.host, same);
let keys = vec![
KeyEntry { endorsement: Some(k2.endorse("acme", &k3.public_base64())), ..KeyEntry::new(k3.public_base64()) },
KeyEntry::new(k2.public_base64()),
];
index_of(&market, vec![publisher("acme", keys)], vec![v14]);
f.host.refresh_market().unwrap();
let dir = f.host.download_market_plugin("acme.hello").unwrap();
let preview = f.host.preview_install(&dir).unwrap();
assert!(!preview.signer_changed && !preview.needs_consent);
f.host.install_from_dir(&dir, &as_granted(&preview)).unwrap();
assert_eq!(f.host.info_of("acme.hello").unwrap().version, "1.4.0");
}
#[test]
fn an_index_in_a_newer_format_still_revokes() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (_, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
f.host.set_market_url(Some(write_index(&market, &key, vec![entry], vec![], false)));
f.host.refresh_market().unwrap();
let dir = f.host.download_market_plugin("acme.hello").unwrap();
f.host.install_from_dir(&dir, &Consent::all()).unwrap();
let newer = format!(
r#"{{"format": {}, "plugins": "a shape this IDE can't read",
"revoked": [{{"id": "acme.hello", "reason": "steals tokens"}}]}}"#,
package::INDEX_FORMAT + 1
);
std::fs::write(market.join("index.json"), newer).unwrap();
let err = f.host.refresh_market().unwrap_err();
assert!(err.message.contains("newer OpenIDEA"), "{}", err.message);
let info = f.host.info_of("acme.hello").unwrap();
assert_eq!((info.status, info.error.as_deref()), (PluginStatus::Failed, Some("revoked: steals tokens")));
assert!(f.host.market_plugins().is_empty());
}
#[test]
fn official_plugins_need_the_key_this_ide_was_built_with() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let (root, intruder) = (PublisherKey::from_seed([1; 32]), PublisherKey::from_seed([6; 32]));
f.host.set_official_keys(vec![root.public_base64()]);
// The index lists the intruder's key for the official publisher.
let (_, forged) = signed(&market, OFFICIAL_PUBLISHER, "1.0.0", &intruder, &f.host, |t| t.to_owned());
let keys = vec![KeyEntry::new(intruder.public_base64())];
f.host.set_market_url(Some(index_of(&market, vec![publisher(OFFICIAL_PUBLISHER, keys.clone())], vec![forged])));
f.host.refresh_market().unwrap();
let err = f.host.download_market_plugin("openidea.hello").unwrap_err();
assert!(err.message.contains("trusted publisher"), "{}", err.message);
// Signed with the built-in key it installs, whatever keys the index lists.
let (_, genuine) = signed(&market, OFFICIAL_PUBLISHER, "1.0.1", &root, &f.host, |t| t.to_owned());
index_of(&market, vec![publisher(OFFICIAL_PUBLISHER, keys)], vec![genuine]);
f.host.refresh_market().unwrap();
let dir = f.host.download_market_plugin("openidea.hello").unwrap();
assert_eq!(f.host.preview_install(&dir).unwrap().publisher.as_deref(), Some(OFFICIAL_PUBLISHER));
}
#[test]
fn marketplace_icons_are_sanitised_or_dropped() {
let f = fixture();
let market = f.work.join("market");
std::fs::create_dir_all(&market).unwrap();
let key = PublisherKey::from_seed([3; 32]);
let (_, entry) = signed_hello(&market, "1.0.0", &key, &f.host);
let icon = |light: &str| Some(PluginIcon { light: light.into(), dark: None });
let good =
IndexEntry { icon: icon(r#"<svg viewBox="0 0 16 16"><rect width="16" height="16"/></svg>"#), ..entry.clone() };
let hostile = IndexEntry { id: "acme.hostile".into(), icon: icon(r#"<svg onload="x()"><rect/></svg>"#), ..entry };
let padded = format!("<svg viewBox=\"0 0 16 16\"><!--{}--><rect/></svg>", " ".repeat(20 << 10));
let padded = IndexEntry { id: "acme.padded".into(), icon: icon(&padded), ..hostile.clone() };
f.host.set_market_url(Some(write_index(&market, &key, vec![good, hostile, padded], vec![], false)));
let listed = f.host.refresh_market().unwrap();
let by_id = |id: &str| listed.iter().find(|p| p.entry.id == id).unwrap_or_else(|| panic!("{id} not listed"));
assert!(by_id("acme.hello").entry.icon.as_ref().unwrap().light.contains("<rect"));
assert!(by_id("acme.hostile").entry.icon.is_none(), "an unsafe icon is dropped, the entry stays");
assert!(by_id("acme.padded").entry.icon.is_none(), "an icon over 16 KiB is dropped before it is sanitised");
}