From 8e9a53ea811eeee8d911f0082d329502547e7088 Mon Sep 17 00:00:00 2001 From: Etienne Stalmans Date: Wed, 4 Dec 2024 14:46:04 +0100 Subject: [PATCH 1/2] chore: generic error message for file load Give a generic error message for file load failures to prevent file system enumeration. --- wrappers/src/fdw/wasm_fdw/wasm_fdw.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs b/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs index 5dbc028b..a9004b16 100644 --- a/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs +++ b/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs @@ -147,7 +147,10 @@ impl ForeignDataWrapper for WasmFdw { let engine = Engine::new(&config)?; let component = - download_component(&rt, &engine, pkg_url, pkg_name, pkg_version, pkg_checksum)?; + match download_component(&rt, &engine, pkg_url, pkg_name, pkg_version, pkg_checksum) { + Ok(c) => c, + Err(_) => panic!("wasmtime error: failed to parse WebAssembly module"), + }; let mut linker = Linker::new(&engine); Wrappers::add_to_linker(&mut linker, |host: &mut FdwHost| host)?; From 137f8908fe6b4c2e60ad2a2294b7e213534ddb43 Mon Sep 17 00:00:00 2001 From: Bo Lu Date: Thu, 5 Dec 2024 10:40:51 +1100 Subject: [PATCH 2/2] chore: generalize Wasm component load error message --- wrappers/src/fdw/wasm_fdw/mod.rs | 3 +++ wrappers/src/fdw/wasm_fdw/wasm_fdw.rs | 21 +++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/wrappers/src/fdw/wasm_fdw/mod.rs b/wrappers/src/fdw/wasm_fdw/mod.rs index d6c2ed4e..4e530b19 100644 --- a/wrappers/src/fdw/wasm_fdw/mod.rs +++ b/wrappers/src/fdw/wasm_fdw/mod.rs @@ -14,6 +14,9 @@ use self::bindings::supabase::wrappers::types::FdwError as GuestFdwError; #[derive(Error, Debug)] enum WasmFdwError { + #[error("invalid WebAssembly component")] + InvalidWasmComponent, + #[error("guest fdw error: {0}")] GuestFdw(GuestFdwError), diff --git a/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs b/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs index a9004b16..edc8ee4d 100644 --- a/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs +++ b/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs @@ -33,6 +33,14 @@ fn check_version_requirement(ver_req: &str) -> WasmFdwResult<()> { Ok(()) } +// compiles a new WebAssembly component from a wasm file +fn load_component_from_file( + engine: &Engine, + file_path: impl AsRef, +) -> WasmFdwResult { + Component::from_file(engine, file_path).map_err(|_| WasmFdwError::InvalidWasmComponent) +} + // Download wasm component package from warg registry or custom url. // The url protoal can be 'file://', 'warg(s)://' or 'http(s)://'. fn download_component( @@ -44,7 +52,7 @@ fn download_component( checksum: Option<&str>, ) -> WasmFdwResult { if let Some(file_path) = url.strip_prefix("file://") { - return Ok(Component::from_file(engine, file_path)?); + return load_component_from_file(engine, file_path); } if url.starts_with("warg://") || url.starts_with("wargs://") { @@ -69,7 +77,7 @@ fn download_component( .block_on(client.download(&pkg_name, &ver))? .ok_or(format!("{}@{} not found on {}", name, version, url))?; - return Ok(Component::from_file(engine, pkg.path)?); + return load_component_from_file(engine, pkg.path); } // otherwise, download from custom url if it is not in local cache @@ -107,10 +115,10 @@ fn download_component( fs::write(&path, bytes)?; } - Ok(Component::from_file(engine, &path).inspect_err(|_| { + load_component_from_file(engine, &path).inspect_err(|_| { // remove the cache file if it cannot be loaded as component let _ = fs::remove_file(&path); - })?) + }) } #[wrappers_fdw( @@ -147,10 +155,7 @@ impl ForeignDataWrapper for WasmFdw { let engine = Engine::new(&config)?; let component = - match download_component(&rt, &engine, pkg_url, pkg_name, pkg_version, pkg_checksum) { - Ok(c) => c, - Err(_) => panic!("wasmtime error: failed to parse WebAssembly module"), - }; + download_component(&rt, &engine, pkg_url, pkg_name, pkg_version, pkg_checksum)?; let mut linker = Linker::new(&engine); Wrappers::add_to_linker(&mut linker, |host: &mut FdwHost| host)?;