Skip to content

Commit

Permalink
chore: generic error message for file load (#377)
Browse files Browse the repository at this point in the history
* chore: generic error message for file load

Give a generic error message for file load failures to prevent file
system enumeration.

* chore: generalize Wasm component load error message

---------

Co-authored-by: Bo Lu <lv.patrick@gmail.com>
  • Loading branch information
staaldraad and burmecia authored Dec 5, 2024
1 parent 552fc37 commit 8a30794
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions wrappers/src/fdw/wasm_fdw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
16 changes: 12 additions & 4 deletions wrappers/src/fdw/wasm_fdw/wasm_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::path::Path>,
) -> WasmFdwResult<Component> {
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(
Expand All @@ -44,7 +52,7 @@ fn download_component(
checksum: Option<&str>,
) -> WasmFdwResult<Component> {
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://") {
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 8a30794

Please sign in to comment.