diff --git a/Cargo.lock b/Cargo.lock index 6efae27195..2950fc8c8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4515,6 +4515,7 @@ dependencies = [ "toml 0.9.12+spec-1.1.0", "tracing", "tracing-subscriber", + "url", "uuid", "windows-sys 0.61.2", "zip", diff --git a/crates/ide-host/Cargo.toml b/crates/ide-host/Cargo.toml index 87fc1b81a4..fb6700729b 100644 --- a/crates/ide-host/Cargo.toml +++ b/crates/ide-host/Cargo.toml @@ -68,6 +68,7 @@ specta.workspace = true specta-typescript.workspace = true tokio = { workspace = true, features = ["io-std"] } tracing.workspace = true +url = "2" tracing-subscriber.workspace = true parking_lot.workspace = true dunce.workspace = true diff --git a/crates/ide-host/src/plugins/ui_points.rs b/crates/ide-host/src/plugins/ui_points.rs index bab56bded6..069b82ca16 100644 --- a/crates/ide-host/src/plugins/ui_points.rs +++ b/crates/ide-host/src/plugins/ui_points.rs @@ -36,6 +36,7 @@ //! priority = "default" # or "option": only through Open With //! layout = "tab" # or "split" / "preview": beside the text editor //! web = "web/index.html" # a web page from the package instead of rendered content +//! frames = ["https://example.com"] # API 1.16: origins the page may embed; each needs `net:` //! //! [[contributes.editorPanels]] //! id = "minimap" @@ -641,6 +642,65 @@ struct ViewDecl { /// A web page of the package (`web/index.html`) instead of content the code renders. #[serde(default, skip_serializing_if = "Option::is_none")] web: Option, + /// Origins the web page may embed in `").unwrap(); + let header = "id = \"p\"\nname = \"P\"\nversion = \"1.0.0\"\nengines.openidea = \"^1.16\"\nwasm = \"p.wasm\"\n\ + [capabilities]\nrequired = [\"editor.read\", \"net:example.com\", \"net:*.games.test\", \"net:localhost\"]\n"; + let check = |point: &str, body: &str, header: &str| { + let manifest = ide_extension::Manifest::parse(&format!("{header}[[contributes.{point}]]\n{body}"))?; + let decl = manifest.declarations(point).into_iter().next().unwrap(); + let pkg = Package::new(manifest, dir.path().to_path_buf()); + match point { + VIEWS => view(&pkg, decl), + CUSTOM_EDITORS => custom_editor(&pkg, decl), + _ => editor_panel(&pkg, decl), + } + }; + let view_with = + |frames: &str| format!("id = \"v\"\ntitle = \"V\"\nweb = \"web/index.html\"\nframes = {frames}\n"); + let ok = check( + VIEWS, + &view_with(r#"["https://EXAMPLE.com/", "https://a.games.test:8443", "http://localhost:5173"]"#), + header, + ) + .unwrap(); + assert_eq!( + ok.json["frames"], + serde_json::json!(["https://example.com", "https://a.games.test:8443", "http://localhost:5173"]) + ); + assert!( + check(VIEWS, "id = \"v\"\ntitle = \"V\"\nweb = \"web/index.html\"\n", header) + .unwrap() + .json + .get("frames") + .is_none() + ); + let editor = "id = \"e\"\ntitle = \"E\"\npatterns = [\"*.x\"]\nweb = \"web/index.html\"\nframes = [\"https://example.com\"]\n"; + assert_eq!( + check(CUSTOM_EDITORS, editor, header).unwrap().json["frames"], + serde_json::json!(["https://example.com"]) + ); + let panel = "id = \"m\"\nweb = \"web/index.html\"\nwidth = 110\nframes = [\"https://example.com\"]\n"; + assert_eq!( + check(EDITOR_PANELS, panel, header).unwrap().json["frames"], + serde_json::json!(["https://example.com"]) + ); + + for (frames, error) in [ + (r#"["https://other.com"]"#, "`net:other.com` capability"), + (r#"["https://games.test"]"#, "`net:games.test` capability"), + (r#"["http://example.com"]"#, "https://, or http:// to localhost"), + (r#"["ftp://example.com"]"#, "https://, or http:// to localhost"), + (r#"["https://example.com/room"]"#, "no path"), + (r#"["https://example.com?x"]"#, "no path"), + (r#"["https://example.com/#x"]"#, "no path"), + (r#"["https://*.games.test"]"#, "one exact host"), + (r#"["https://me@example.com"]"#, "user name"), + (r#"["example.com"]"#, "not a URL"), + (r#"["data:text/html,x"]"#, "https://"), + ] { + let err = check(VIEWS, &view_with(frames), header).unwrap_err(); + assert!(err.contains(error), "{frames} → {err}"); + } + let many = format!("[{}]", vec!["\"https://example.com\""; 17].join(", ")); + assert!(check(VIEWS, &view_with(&many), header).unwrap_err().contains("at most 16")); + let older = header.replace("^1.16", "^1.15"); + assert!(check(VIEWS, &view_with(r#"["https://example.com"]"#), &older).unwrap_err().contains("1.16")); + let code_view = "id = \"v\"\ntitle = \"V\"\nframes = [\"https://example.com\"]\n"; + assert!(check(VIEWS, code_view, header).unwrap_err().contains("for a web page")); + // `net:ask` is not a listed host: the install page would not show the origin. + let ask = header.replace("\"net:example.com\"", "\"net:ask\""); + assert!(check(VIEWS, &view_with(r#"["https://example.com"]"#), &ask).unwrap_err().contains("capability")); + } + fn package_with_catalogs(files: &[(&str, &str)]) -> (tempfile::TempDir, Package) { let dir = tempfile::tempdir().unwrap(); std::fs::create_dir(dir.path().join(LOCALES_DIR)).unwrap(); diff --git a/crates/ide-host/src/plugins/web.rs b/crates/ide-host/src/plugins/web.rs index e1192ab5ef..378b610404 100644 --- a/crates/ide-host/src/plugins/web.rs +++ b/crates/ide-host/src/plugins/web.rs @@ -121,6 +121,10 @@ pub(super) struct WebPtySession { pub struct WebFile { pub bytes: Vec, pub mime: &'static str, + /// The origins a page may embed: `None` when no web page whose directory holds + /// the file declares `frames` (API 1.16), else the declared origins the user has + /// granted (`net:` / `net.http:`), possibly none. + pub frames: Option>, } impl PluginHost { @@ -324,6 +328,32 @@ impl PluginHost { }) } + /// `frames` of the web pages whose directory holds `rel`, kept to those `granted`. + fn web_frames(&self, plugin: &str, rel: &str, granted: &[ide_extension::Capability]) -> Option> { + let declared: Vec = super::ui_points::WEB_POINTS + .iter() + .flat_map(|point| self.ui.items(point, Some(plugin))) + .filter(|item| { + let dir = item.decl.get("web").and_then(|w| w.as_str()).and_then(|e| e.rsplit_once('/')); + dir.is_some_and(|(dir, _)| rel.starts_with(&format!("{dir}/"))) + }) + .filter_map(|item| item.decl.get("frames").and_then(|f| f.as_array()).cloned()) + .flatten() + .filter_map(|f| f.as_str().map(str::to_owned)) + .collect(); + if declared.is_empty() { + return None; + } + let mut allowed = Vec::new(); + for origin in declared { + let ok = ide_plugin::net_capability(&origin).is_ok_and(|cap| granted.iter().any(|g| g.covers(&cap))); + if ok && !allowed.contains(&origin) { + allowed.push(origin); + } + } + Some(allowed) + } + /// The web roots `plugin` declares (entry directories, package-relative, `/`-terminated). fn web_dirs(&self, plugin: &str) -> Vec { super::ui_points::WEB_POINTS @@ -376,7 +406,14 @@ impl PluginHost { return Err(IdeError::not_found(format!("{rel}: not part of a web view"))); } let bytes = plugin.pkg.read_bytes(rel_path).map_err(IdeError::not_found)?; - Ok(WebFile { bytes, mime: mime(rel) }) + let mime = mime(rel); + let frames = if mime.starts_with("text/html") { + let granted = plugin.meta.lock().granted.clone(); + self.web_frames(plugin_id, rel, &granted) + } else { + None + }; + Ok(WebFile { bytes, mime, frames }) } } diff --git a/crates/ide-host/tests/plugins.rs b/crates/ide-host/tests/plugins.rs index 4c289246ea..5692fb4d14 100644 --- a/crates/ide-host/tests/plugins.rs +++ b/crates/ide-host/tests/plugins.rs @@ -3902,6 +3902,68 @@ path = "templates/new.draw" assert!(f.host.web_page("web-demo", "draw").is_err()); } +#[test] +fn web_pages_embed_only_declared_and_granted_frames() { + let f = fixture(Limits::default()); + let tmp = tempfile::tempdir().unwrap(); + let pkg = tmp.path().join("frame-demo"); + for (path, text) in [ + ("hall/index.html", ""), + ("hall/rules.html", "

rules

"), + ("hall/app.js", "console.log(1)"), + ("panel/index.html", "

panel

"), + ] { + std::fs::create_dir_all(pkg.join(path).parent().unwrap()).unwrap(); + std::fs::write(pkg.join(path), text).unwrap(); + } + std::fs::write( + pkg.join("plugin.toml"), + r#"id = "frame-demo" +name = "Frame Demo" +version = "1.0.0" +engines.openidea = "^1.16" + +[capabilities] +optional = ["net:example.com", "net:*.games.test"] + +[[contributes.views]] +id = "hall" +title = "Hall" +web = "hall/index.html" +frames = ["https://example.com/", "https://play.games.test:8443", "https://example.com"] + +[[contributes.views]] +id = "panel" +title = "Panel" +web = "panel/index.html" +"#, + ) + .unwrap(); + let info = f.install(&pkg); + assert_eq!(info.status, PluginStatus::Static, "{:?}", info.error); + assert_eq!( + f.host.ui_items("views").iter().find(|v| v.decl["id"] == "hall").unwrap().decl["frames"], + serde_json::json!(["https://example.com", "https://play.games.test:8443"]) + ); + let page = f.host.web_page("frame-demo", "hall").unwrap(); + let rev = page.path.strip_prefix("frame-demo/").unwrap().split_once('/').unwrap().0.to_owned(); + let frames = |path: &str| f.host.web_file("frame-demo", &format!("{rev}/{path}")).unwrap().frames; + let both = Some(vec!["https://example.com".to_owned(), "https://play.games.test:8443".to_owned()]); + // Every page in the declaring view's directory, not its other files nor another view's pages. + assert_eq!(frames("hall/index.html"), both); + assert_eq!(frames("hall/rules.html"), both); + assert_eq!(frames("hall/app.js"), None); + assert_eq!(frames("panel/index.html"), None); + + // Only what the user granted: declining a host takes it out of the policy. + f.host.set_capability("frame-demo", "net:example.com", false).unwrap(); + assert_eq!(frames("hall/index.html"), Some(vec!["https://play.games.test:8443".to_owned()])); + f.host.set_capability("frame-demo", "net:*.games.test", false).unwrap(); + assert_eq!(frames("hall/index.html"), Some(Vec::new())); + f.host.set_capability("frame-demo", "net:example.com", true).unwrap(); + assert_eq!(frames("hall/index.html"), Some(vec!["https://example.com".to_owned()])); +} + #[test] fn file_icons_without_patterns_require_api_1_8_and_project_assets() { let f = fixture(Limits::default()); diff --git a/docs/en/plugins/guides/views.mdx b/docs/en/plugins/guides/views.mdx index e2f3f24f53..b888192c09 100644 --- a/docs/en/plugins/guides/views.mdx +++ b/docs/en/plugins/guides/views.mdx @@ -270,6 +270,29 @@ corner actions (**Options**, **Hide**) and sets `--openidea-corner-inset-width` } ``` +**Embedded pages** (API 1.16). A web page can embed other sites in `