Download, verify, and install the browser engine
Each host build pins one engine version in src-tauri/browser-engine.json. The new engine manager downloads that ZIP over HTTPS, makes sure that its size and SHA-256 match the pin, extracts it safely to the local app data folder, and marks the installation last. It can also install a local ZIP, cancel, report progress, and remove the engine. The host refuses an engine that sends another protocol with its ready event.
This commit is contained in:
+26
-29
@@ -143,32 +143,15 @@ fn migrate_profiles(legacy: &Path, current: &Path, rename: impl Fn(&Path, &Path)
|
||||
let _ = fs::remove_dir_all(legacy);
|
||||
Ok(())
|
||||
}
|
||||
fn executable(app: &AppHandle) -> Result<PathBuf> {
|
||||
let name = if cfg!(windows) {
|
||||
"RuneStone-Browser.exe"
|
||||
} else if cfg!(target_os = "macos") {
|
||||
"RuneStone-Browser.app/Contents/MacOS/RuneStone-Browser"
|
||||
} else {
|
||||
"RuneStone-Browser"
|
||||
};
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let development = Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("browser-engine")
|
||||
.join(name);
|
||||
if development.is_file() {
|
||||
return Ok(development);
|
||||
}
|
||||
/// The engine must speak the protocol of this host. An engine from another release can differ.
|
||||
fn ready(value: &Value) -> Result<()> {
|
||||
if value["event"] != "ready" {
|
||||
return Err("The browser runtime did not start.".into());
|
||||
}
|
||||
let packaged = app
|
||||
.path()
|
||||
.resource_dir()?
|
||||
.join("browser-engine")
|
||||
.join(name);
|
||||
if packaged.is_file() {
|
||||
return Ok(packaged);
|
||||
if value["protocol"].as_u64() != Some(crate::browser_engine::PROTOCOL) {
|
||||
return Err("This browser engine does not match this RuneStone version. Install the browser engine again.".into());
|
||||
}
|
||||
Err("The browser runtime is missing. Build RuneStone with its browser runtime.".into())
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn authenticate_connection(
|
||||
@@ -206,7 +189,7 @@ impl Worker {
|
||||
uuid::Uuid::new_v4().simple(),
|
||||
uuid::Uuid::new_v4().simple()
|
||||
);
|
||||
let mut command = Command::new(executable(app)?);
|
||||
let mut command = Command::new(crate::browser_engine::executable(app)?);
|
||||
command
|
||||
.arg(format!("--rs-connect={port}"))
|
||||
.arg(format!("--rs-browser-root={}", directory.display()))
|
||||
@@ -300,12 +283,15 @@ impl Worker {
|
||||
sequence: 0,
|
||||
healthy: true,
|
||||
};
|
||||
match worker.replies.recv_timeout(Duration::from_secs(30)) {
|
||||
Ok(value) if value["event"] == "ready" => Ok(worker),
|
||||
_ => {
|
||||
let started = worker.replies.recv_timeout(Duration::from_secs(30))
|
||||
.map_err(|_| Error::from("The browser runtime did not start."))
|
||||
.and_then(|value| ready(&value));
|
||||
match started {
|
||||
Ok(()) => Ok(worker),
|
||||
Err(error) => {
|
||||
let _ = worker.child.kill();
|
||||
let _ = worker.child.wait();
|
||||
Err("The browser runtime did not start.".into())
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -455,6 +441,10 @@ impl BrowserState {
|
||||
}
|
||||
pub fn close_all(&self) {
|
||||
if let Ok(mut sessions) = self.sessions.lock() { sessions.clear(); }
|
||||
self.stop_engine();
|
||||
}
|
||||
/// Stop the engine and keep the window sessions, so that open windows can start a new engine later.
|
||||
pub fn stop_engine(&self) {
|
||||
if let Ok(mut guard) = self.inner.lock() {
|
||||
if let Some(mut worker) = guard.take() {
|
||||
let _ = worker.stop();
|
||||
@@ -887,6 +877,13 @@ mod tests {
|
||||
fs::remove_dir_all(&base).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn the_engine_must_send_the_protocol_of_this_host() {
|
||||
assert!(ready(&json!({ "event": "ready", "protocol": crate::browser_engine::PROTOCOL, "version": "0.1.8" })).is_ok());
|
||||
assert!(ready(&json!({ "event": "ready" })).unwrap_err().to_string().contains("does not match"));
|
||||
assert!(ready(&json!({ "event": "ready", "protocol": crate::browser_engine::PROTOCOL + 1 })).is_err());
|
||||
assert!(ready(&json!({ "event": "state" })).unwrap_err().to_string().contains("did not start"));
|
||||
}
|
||||
#[test]
|
||||
fn profile_names_cannot_escape_storage() {
|
||||
for value in ["../other", "a/b", "a\\b", "C:", "", "."] {
|
||||
assert!(!profile_id(value));
|
||||
|
||||
Reference in New Issue
Block a user