@@ -41,6 +41,7 @@ fn download_component(
41
41
url : & str ,
42
42
name : & str ,
43
43
version : & str ,
44
+ checksum : Option < & str > ,
44
45
) -> WasmFdwResult < Component > {
45
46
if let Some ( file_path) = url. strip_prefix ( "file://" ) {
46
47
return Ok ( Component :: from_file ( engine, file_path) ?) ;
@@ -87,9 +88,18 @@ fn download_component(
87
88
path. set_extension ( "wasm" ) ;
88
89
89
90
if !path. exists ( ) {
90
- // download component wasm from remote and save it to local cache
91
+ // package checksum must be specified
92
+ let option_checksum = checksum. ok_or ( "pacakge checksum option not specified" . to_owned ( ) ) ?;
93
+
94
+ // download component wasm from remote and check its checksum
91
95
let resp = rt. block_on ( reqwest:: get ( url) ) ?;
92
96
let bytes = rt. block_on ( resp. bytes ( ) ) ?;
97
+ let bytes_checksum = hex:: encode ( Sha256 :: digest ( & bytes) ) ;
98
+ if bytes_checksum != option_checksum {
99
+ return Err ( "pacakge checksum not match" . to_string ( ) . into ( ) ) ;
100
+ }
101
+
102
+ // save the component wasm to local cache
93
103
if let Some ( parent) = path. parent ( ) {
94
104
// create all parent directories if they do not exist
95
105
fs:: create_dir_all ( parent) ?;
@@ -105,7 +115,7 @@ fn download_component(
105
115
}
106
116
107
117
#[ wrappers_fdw(
108
- version = "0.1.1 " ,
118
+ version = "0.1.2 " ,
109
119
author = "Supabase" ,
110
120
website = "https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/wasm_fdw" ,
111
121
error_type = "WasmFdwError"
@@ -126,14 +136,16 @@ impl ForeignDataWrapper<WasmFdwError> for WasmFdw {
126
136
let pkg_url = require_option ( "fdw_package_url" , options) ?;
127
137
let pkg_name = require_option ( "fdw_package_name" , options) ?;
128
138
let pkg_version = require_option ( "fdw_package_version" , options) ?;
139
+ let pkg_checksum = options. get ( "fdw_package_checksum" ) . map ( |t| t. as_str ( ) ) ;
129
140
130
141
let rt = create_async_runtime ( ) ?;
131
142
132
143
let mut config = Config :: new ( ) ;
133
144
config. wasm_component_model ( true ) ;
134
145
let engine = Engine :: new ( & config) ?;
135
146
136
- let component = download_component ( & rt, & engine, pkg_url, pkg_name, pkg_version) ?;
147
+ let component =
148
+ download_component ( & rt, & engine, pkg_url, pkg_name, pkg_version, pkg_checksum) ?;
137
149
138
150
let mut linker = Linker :: new ( & engine) ;
139
151
Wrappers :: add_to_linker ( & mut linker, |host : & mut FdwHost | host) ?;
0 commit comments