Skip to content

Commit

Permalink
feat: store list of whitelisted wasms in a file, instead of hardcoding (
Browse files Browse the repository at this point in the history
#185)

* read from file

* generate const array of &str with `build.rs` script

* use `include!` instead of build script
  • Loading branch information
Marcin Nowak-Liebiediew authored Aug 3, 2023
1 parent 00aebe1 commit 2c02e85
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 10 additions & 7 deletions service/wasm-utils/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use candid::{CandidType, Deserialize};
use serde_bytes::ByteBuf;

use ic_wasm::*;
use serde_bytes::ByteBuf;
use sha2::Digest;

#[derive(CandidType, Deserialize)]
Expand All @@ -12,14 +11,11 @@ struct Config {
backend_canister_id: Option<candid::Principal>,
}

const WHITELISTED_WASMS: [&str; 1] = [
"88d1e5795d29debc1ff56fa0696dcb3adfa67f82fe2739d1aa644263838174b9", // dfx 0.15.0 frontend canister
];

#[ic_cdk::query]
fn is_whitelisted(wasm: ByteBuf) -> ByteBuf {
let wasm_hash = hex::encode(sha2::Sha256::digest(&wasm));
if WHITELISTED_WASMS.contains(&wasm_hash.as_str()) {
let white_list = include!("whitelisted_wasms.txt");
if white_list.contains(&wasm_hash.as_str()) {
wasm
} else {
ic_cdk::trap("Wasm is not whitelisted")
Expand All @@ -41,3 +37,10 @@ fn transform(wasm: ByteBuf, config: Config) -> ByteBuf {
let wasm = m.emit_wasm();
ByteBuf::from(wasm)
}

#[test]
fn test_parsing_whitelisted_wasms_txt() {
let white_list = include!("whitelisted_wasms.txt");
let hash = "88d1e5795d29debc1ff56fa0696dcb3adfa67f82fe2739d1aa644263838174b9";
assert!(white_list.contains(&hash));
}
3 changes: 3 additions & 0 deletions service/wasm-utils/whitelisted_wasms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"88d1e5795d29debc1ff56fa0696dcb3adfa67f82fe2739d1aa644263838174b9", // dfx 0.15.0 frontend canister
]

0 comments on commit 2c02e85

Please sign in to comment.