Skip to content

Commit 6d7f2e7

Browse files
authored
fix(deps): content of wasm_hash_url can have extra fields than the hash (#3563)
* fix: content of wasm_hash_url can have extra fields than the hash * changelog and doc * no unwrap * test: both forms of wasm_hash_url content
1 parent fb2e4b0 commit 6d7f2e7

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
# UNRELEASED
44

5+
### fix(deps): content of wasm_hash_url can have extra fields than the hash
6+
7+
It is natural to point `wasm_hash_url` to the `<FILE>.sha256` file generated by `shasum` or `sha256sum` which consists of the hash and the file name.
8+
9+
Now when `dfx deps pull`, such content will be accept properly.
10+
511
# 0.16.1
612

713
### feat: query stats support

docs/concepts/pull-dependencies.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ In other cases, the wasm module at `wasm_url` is not the same as the on-chain wa
5757

5858
A URL to get the SHA256 hash of the wasm module located at `wasm_url`.
5959

60+
The content of this URL can be the SHA256 hash only.
61+
62+
It can also be the output of `shasum` or `sha256sum` which contains the hash and the file name.
63+
6064
This field is optional.
6165

6266
Aside from specifying SHA256 hash of the wasm module directly using `wasm_hash`, providers can also specify the hash with this URL. If both are defined, the `wasm_hash_url` field will be ignored.

e2e/tests-dfx/deps.bash

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,11 @@ Failed to download from url: http://example.com/c.wasm."
227227
# A: set dfx:wasm_hash
228228
CUSTOM_HASH_A="$(sha256sum .dfx/local/canisters/a/a.wasm | cut -d " " -f 1)"
229229
jq '.canisters.a.pullable.wasm_hash="'"$CUSTOM_HASH_A"'"' dfx.json | sponge dfx.json
230-
# B: set dfx:wasm_hash_url
231-
echo -n "$(sha256sum .dfx/local/canisters/b/b.wasm.gz | cut -d " " -f 1)" > ../www/b.wasm.gz.sha256
230+
# B: set dfx:wasm_hash_url with output of sha256sum
231+
echo -n "$(sha256sum .dfx/local/canisters/b/b.wasm.gz)" > ../www/b.wasm.gz.sha256
232232
jq '.canisters.b.pullable.wasm_hash_url="'"http://localhost:$E2E_WEB_SERVER_PORT/b.wasm.gz.sha256"'"' dfx.json | sponge dfx.json
233-
# C: set both dfx:wasm_hash and dfx:wasm_hash_url. This should be avoided by providers.
233+
# C: set dfx:wasm_hash_url with the hash only
234234
CUSTOM_HASH_C="$(sha256sum .dfx/local/canisters/c/c.wasm | cut -d " " -f 1)"
235-
jq '.canisters.c.pullable.wasm_hash="'"$CUSTOM_HASH_C"'"' dfx.json | sponge dfx.json
236235
echo -n "$CUSTOM_HASH_C" > ../www/c.wasm.sha256
237236
jq '.canisters.c.pullable.wasm_hash_url="'"http://localhost:$E2E_WEB_SERVER_PORT/c.wasm.sha256"'"' dfx.json | sponge dfx.json
238237

@@ -249,11 +248,21 @@ Failed to download from url: http://example.com/c.wasm."
249248
assert_command dfx deps pull --network local -vvv
250249
assert_contains "Canister $CANISTER_ID_A specified a custom hash:"
251250
assert_contains "Canister $CANISTER_ID_B specified a custom hash via url:"
251+
assert_contains "Canister $CANISTER_ID_C specified a custom hash via url:"
252+
253+
# warning: specified both `wasm_hash` and `wasm_hash_url`. Providers should avoid this.
254+
PULLED_DIR="$DFX_CACHE_ROOT/.cache/dfinity/pulled/"
255+
rm -r "${PULLED_DIR:?}/"
256+
cd ../onchain
257+
jq '.canisters.c.pullable.wasm_hash="'"$CUSTOM_HASH_C"'"' dfx.json | sponge dfx.json
258+
dfx build
259+
dfx canister install c -m reinstall --argument 3 --yes
260+
261+
cd ../app
262+
assert_command dfx deps pull --network local -vvv
252263
assert_contains "WARN: Canister $CANISTER_ID_C specified both \`wasm_hash\` and \`wasm_hash_url\`. \`wasm_hash\` will be used."
253-
assert_contains "Canister $CANISTER_ID_C specified a custom hash:"
254264

255265
# warning: hash mismatch
256-
PULLED_DIR="$DFX_CACHE_ROOT/.cache/dfinity/pulled/"
257266
rm -r "${PULLED_DIR:?}/"
258267
cd ../onchain
259268
cp .dfx/local/canisters/a/a.wasm ../www/a.wasm # now the webserver has the onchain version of canister_a which won't match wasm_hash

src/dfx/src/commands/deps/pull.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,15 @@ async fn get_hash_on_chain(
305305
let wasm_hash_content = download_file(&wasm_hash_url)
306306
.await
307307
.with_context(|| format!("Failed to download wasm_hash from {wasm_hash_url}."))?;
308-
let wasm_hash_encoded = String::from_utf8(wasm_hash_content)
308+
let wasm_hash_str = String::from_utf8(wasm_hash_content)
309309
.with_context(|| format!("Content from {wasm_hash_url} is not valid text."))?;
310-
Ok(hex::decode(&wasm_hash_encoded)
310+
// The content might contain the file name (usually from tools like shasum or sha256sum).
311+
// We only need the hash part.
312+
let wasm_hash_encoded = wasm_hash_str
313+
.split_whitespace()
314+
.next()
315+
.with_context(|| format!("Content from {wasm_hash_url} is empty."))?;
316+
Ok(hex::decode(wasm_hash_encoded)
311317
.with_context(|| format!("Failed to decode {wasm_hash_encoded} as sha256 hash."))?)
312318
} else {
313319
match read_state_tree_canister_module_hash(agent, canister_id).await? {

0 commit comments

Comments
 (0)