SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Web app: open a design from a same-origin path (#6670)
raykholo66b872a749d0
diff --git a/Cargo.lock b/Cargo.lockindex b8cb2b625b..719d8ad0e5 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4451,6 +4451,7 @@ dependencies = ["egui","js-sys","log",+ "percent-encoding","serde_json","solidcraft-engine","solidcraft-ui-egui",diff --git a/apps/solidcraft-web/Cargo.toml b/apps/solidcraft-web/Cargo.tomlindex d96c49f659..b42a04ce75 100644--- a/apps/solidcraft-web/Cargo.toml+++ b/apps/solidcraft-web/Cargo.toml@@ -23,6 +23,7 @@ solidcraft-ui-egui = { workspace = true }egui = { workspace = true }log = { workspace = true }url = "2"+percent-encoding = "2"[target.'cfg(target_arch = "wasm32")'.dependencies]eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit"] }diff --git a/apps/solidcraft-web/src/linked.rs b/apps/solidcraft-web/src/linked.rsindex 5a24cc46c2..c1c46a8413 100644--- a/apps/solidcraft-web/src/linked.rs+++ b/apps/solidcraft-web/src/linked.rs@@ -1,4 +1,4 @@-//! Bounded HTTPS downloads, handed to the normal design-open command as unsaved copies.+//! Bounded HTTPS or same-origin downloads, handed to the normal design-open command as unsaved copies.use std::{cell::RefCell, rc::Rc};use js_sys::{Reflect, Uint8Array};@@ -87,14 +87,15 @@ impl Download {}async fn fetch(value: &str) -> Reply {- let url = design_url(value)?;+ let window = web_sys::window().ok_or("No browser window")?;+ let page_origin = window.location().origin().map_err(|_| "Cannot read page origin")?;+ let url = design_url(value, &page_origin)?;let name = url.path_segments().and_then(|mut segments| segments.next_back()).filter(|name| !name.is_empty()).unwrap_or("Design.solidcraft");let decoded = js_sys::decode_uri_component(name).ok().and_then(|name| name.as_string()).unwrap_or_else(|| name.to_owned());let name: String = decoded.strip_suffix(".solidcraft").unwrap_or(&decoded).chars().take(255).collect();- let window = web_sys::window().ok_or("No browser window")?;let response = JsFuture::from(window.fetch_with_str(url.as_str())).await.map_err(|_| "Network request failed or the host refused CORS access")?;let response: web_sys::Response = response.dyn_into().map_err(|_| "Invalid download response")?;- design_url(&response.url()).map_err(|_| "Design redirected to a URL that does not use HTTPS")?;+ design_url(&response.url(), &page_origin).map_err(|_| "Design redirected to a URL that does not use HTTPS or the page's own origin")?;if !response.ok() {return Err(format!("HTTP {} {}", response.status(), response.status_text()));}diff --git a/apps/solidcraft-web/src/main.rs b/apps/solidcraft-web/src/main.rsindex 0e0f427e86..b7c76e1bc8 100644--- a/apps/solidcraft-web/src/main.rs+++ b/apps/solidcraft-web/src/main.rs@@ -59,16 +59,44 @@ impl Launch {fn persists(&self) -> bool {!self.embed}++ fn full_app_url(&self, mut url: url::Url) -> String {+ url.set_query(None);+ url.set_fragment(None);+ if let Some(open) = &self.open {+ url.query_pairs_mut().append_pair("open", open);+ }+ url.to_string()+ }}#[cfg(any(target_arch = "wasm32", test))]-fn design_url(value: &str) -> Result<url::Url, String> {+fn design_url(value: &str, page_origin: &str) -> Result<url::Url, String> {if value.len() > 16_384 {return Err("Design URL is too long".into());}- let url = url::Url::parse(value).map_err(|_| "Invalid design URL")?;- if url.scheme() != "https" || url.host_str().is_none() || !url.username().is_empty() || url.password().is_some() {- return Err("Design links must use HTTPS without credentials".into());+ let decoded = percent_encoding::percent_decode_str(value).decode_utf8().map_err(|_| "Invalid design URL encoding")?;+ if decoded.chars().any(|ch| ch.is_control() || ch == '\\') {+ return Err("Design links must not contain backslashes or control characters".into());+ }+ // Check before URL parsing normalizes away dot segments. Decode escaped slashes too.+ let path = value.split(['?', '#']).next().unwrap_or_default();+ let path = percent_encoding::percent_decode_str(path).collect::<Vec<_>>();+ if path.split(|byte| *byte == b'/').any(|segment| segment == b"..") {+ return Err("Design links must not contain parent path segments".into());+ }+ let page = url::Url::parse(page_origin).map_err(|_| "Invalid page origin")?;+ let url = if value.starts_with('/') && !value.starts_with("//") {+ page.join(value).map_err(|_| "Invalid design path")?+ } else {+ url::Url::parse(value).map_err(|_| "Invalid design URL")?+ };+ if (url.scheme() != "https" && url.origin() != page.origin())+ || url.host_str().is_none()+ || !url.username().is_empty()+ || url.password().is_some()+ {+ return Err("Design links must use HTTPS or the page's own origin without credentials".into());}Ok(url)}@@ -782,14 +810,7 @@ mod web {let api = Api::install(sender, cc.egui_ctx.clone()).map_err(|e| format!("SolidCraft browser API: {e:?}"))?;let font_language = app.preferences.language.clone();let linked = crate::linked::Download::start(launch.open.as_deref(), &cc.egui_ctx);- let full_url = window.location().href().ok().and_then(|value| url::Url::parse(&value).ok()).map(|mut url| {- url.set_query(None);- url.set_fragment(None);- if let Some(open) = &launch.open {- url.query_pairs_mut().append_pair("open", open);- }- url.to_string()- });+ let full_url = window.location().href().ok().and_then(|value| url::Url::parse(&value).ok()).map(|url| launch.full_app_url(url));Ok(Box::new(Shell {app,launch,@@ -851,24 +872,53 @@ mod tests {assert!(!Launch::parse("?other=embed&reopen=https://example.org").embed);assert!(Launch::parse("?open=https://example.org/a").persists());assert_eq!(Launch::parse("?open=first&open=second").open.as_deref(), Some("first"));+ let launch = Launch::parse("?embed&open=/dutchman/jobs/abc/part.solidcraft%3Fx%3Da%2520b");+ assert!(launch.embed);+ assert!(!launch.persists());+ let page = url::Url::parse("http://solidcraft.io/?embed#view").expect("synthetic page");+ let full = url::Url::parse(&launch.full_app_url(page)).expect("full app URL");+ let reopened = Launch::parse(full.query().unwrap_or_default());+ assert_eq!(reopened.open, launch.open);+ assert_eq!(reopened.open.as_deref(), Some("/dutchman/jobs/abc/part.solidcraft?x=a%20b"));+ assert!(!reopened.embed);+ assert!(reopened.persists());+ assert!(full.fragment().is_none());}#[test]fn design_links_refuse_hostile_schemes_credentials_and_sizes() {- assert!(design_url("https://example.org/arm.solidcraft?download=1").is_ok());+ assert!(design_url("https://example.org/arm.solidcraft?download=1", "http://solidcraft.io").is_ok());for value in ["http://example.org/a","data:application/json,{}","blob:https://example.org/a","javascript:alert(1)",- "/arm.solidcraft","https://user:[email protected]/a",+ "//evil.example/x",+ "/../x",+ "/%2e%2e/x",+ "/a/%2E./x",+ "/a%2f..%2fx",+ "/a\\x",+ "/a%5cx",+ "/a\nx",+ "/a%00x",+ "/a%7fx",+ "/a%C2%85x",+ "/a%3f/../x",+ "/a/..#view",+ "/a/..?download=1",+ "https://solidcraft.io/a/../x",+ "http://user:[email protected]/x",+ "http://solidcraft.io.evil.example/x",+ "http://solidcraft.io:8080/x",+ "relative.solidcraft","",] {- assert!(design_url(value).is_err(), "{value}");+ assert!(design_url(value, "http://solidcraft.io").is_err(), "{value}");}- assert!(design_url(&format!("https://example.org/{}", "x".repeat(16_384))).is_err());- assert!(design_url("http://redirect.example.org/a").is_err());+ assert!(design_url(&format!("https://example.org/{}", "x".repeat(16_384)), "http://solidcraft.io").is_err());+ assert!(design_url("http://redirect.example.org/a", "http://solidcraft.io").is_err());let cap = solidcraft_engine::io::MAX_DESIGN_BYTES;assert!(check_download_size(0, cap as u64).is_ok());@@ -880,6 +930,19 @@ mod tests {assert!(check_download_size(usize::MAX, 0).is_err());}+ #[test]+ fn design_links_allow_same_origin_paths_and_redirects() {+ for origin in ["http://solidcraft.io", "https://solidcraft.io", "http://solidcraft.io:8080"] {+ assert_eq!(design_url("/a/b.solidcraft", origin).map(|url| url.to_string()), Ok(format!("{origin}/a/b.solidcraft")));+ assert!(design_url(&format!("{origin}/x"), origin).is_ok());+ // Final response URLs follow the same origin and scheme rules as the request.+ assert!(design_url("https://docs.solidcraft.io/x", origin).is_ok());+ assert!(design_url("http://other/x", origin).is_err());+ }+ assert!(design_url("http://solidcraft.io/x", "https://solidcraft.io").is_err());+ assert!(design_url("http://SOLIDCRAFT.io:80/x", "http://solidcraft.io").is_ok());+ }+#[test]fn only_ctrl_tab_is_left_to_the_browser() {// Ctrl+Tab and Ctrl+Shift+Tab (the event's key is "Tab" either way) switch browser tabs.diff --git a/docs/book/src/guide/getting-started.md b/docs/book/src/guide/getting-started.mdindex e0e923a97f..4bb2db13eb 100644--- a/docs/book/src/guide/getting-started.md+++ b/docs/book/src/guide/getting-started.md@@ -137,8 +137,18 @@ Use `?open=<encoded HTTPS URL>` to open a hosted `.solidcraft` design as an unsacopy, including its history, constraints, and joints. Encode the URL with`encodeURIComponent` when it contains query parameters. The host must allow CORSrequests from the app's origin. Downloads, including redirected URLs, must use HTTPS-and fit within the File > Open design size limit. Failed downloads and invalid designs-show an error and leave the empty design.+or stay on the app's own origin, and fit within the File > Open design size limit.+Failed downloads and invalid designs show an error and leave the empty design.++For self-hosting, `?open=/dutchman/jobs/abc/part.solidcraft` resolves against the+page's origin, using its scheme and port, including plain HTTP on a LAN. A full URL+on that same origin is also accepted; other absolute URLs still require HTTPS.+For example, expose a reverse-proxy service in Compose, routing `/` to+`solidcraft:8080` and `/dutchman/` to a converter service. The converter can link to+`/?open=/dutchman/jobs/abc/part.solidcraft` without a separate origin or CORS setup.+Paths must start with one `/`; protocol-relative paths (`//`), backslashes, control+characters, and decoded `..` segments are refused. Redirects must end at HTTPS or+the page's own origin. Embed mode and **Open in SolidCraft** preserve the path.Add `embed` to show the viewport, timeline, browser tree, and joint controls withoutthe ribbon, file menus, or start page: