Loading...
Loading...
Audit an existing Internet Computer Rust canister (ic-cdk) for security flaws and PROVE a confirmed one with a PocketIC reproduction. Covers detecting the canister, a high-recall static first pass, a systematic review against the canister-security pitfalls, and the distinctive step: building the canister to wasm and executing the exploit in PocketIC to turn a suspected flaw into a demonstrated one. Use when reviewing or auditing a canister you did NOT write, or proving a finding before you report it. Complements canister-security (which teaches the secure patterns); this one finds and proves violations of them.
npx skill4agent add dfinity/icskills canister-auditic-cdkcanister-securitytoken_vaultsum(balances) <= total_depositedrustup target add wasm32-unknown-unknowncargo-auditpocket-ic16.1POCKET_IC_BINPocketIc::new()dfinity/pocketicPOCKET_IC_BINCargo.tomlic-cdkcandid#[ic_cdk::update]#[ic_cdk::query].didawaittoken_vaultsum(balances) <= total_deposited#[update]#[ic_cdk::update]guard = ...name = ...asyncis_controller== Principal::anonymous()CallerGuardfetch_root_keyfetchRootKeyshouldFetchRootKeycanister-securityic_envIC_ROOT_KEYsafeGetCanisterEnv()rootKeyHttpAgent.create()let _ = CallerGuard::new(...)_ic_cdk::caller()msg_caller()depositwithdrawclaim_bonusdepositwithdrawCallerGuardrules:
- id: ic-update-missing-caller-auth
message: update method with no visible caller check - candidate for review
languages: [rust]
severity: ERROR
patterns:
# Every update entrypoint: bare or parameterized attribute, sync or async.
- pattern-either:
- pattern: |
#[$ATTR]
fn $F(...) { ... }
- pattern: |
#[$ATTR]
fn $F(...) -> $R { ... }
- pattern: |
#[$ATTR]
async fn $F(...) { ... }
- pattern: |
#[$ATTR]
async fn $F(...) -> $R { ... }
- pattern: |
#[$ATTR(...)]
fn $F(...) { ... }
- pattern: |
#[$ATTR(...)]
fn $F(...) -> $R { ... }
- pattern: |
#[$ATTR(...)]
async fn $F(...) { ... }
- pattern: |
#[$ATTR(...)]
async fn $F(...) -> $R { ... }
- metavariable-regex:
metavariable: $ATTR
regex: ^(ic_cdk::)?update$
# A bare `== Principal::anonymous()` is deliberately NOT a suppressor (a
# comparison that does not reject the caller is not a guard), and neither
# is CallerGuard (a reentrancy lock). Under-reporting is worse than noise.
- pattern-not-regex: (is_controller|require_owner|require_admin)canister-securityawaitguard = "..."require_authenticatedResultrequire_owner();Errtoken_vaultclaim_bonustotal_depositeddeposit== Principal::anonymous()file:linePrincipal::anonymous()token_vault_repro/Cargo.toml[package]
name = "token_vault_repro"
version = "0.1.0"
edition = "2021"
publish = false
# Empty [workspace] keeps this crate out of any enclosing workspace.
[workspace]
[dev-dependencies]
candid = "0.10"
pocket-ic = "16.1"token_vault/src/lib.rs// INTENTIONAL FLAW: credits the caller out of thin air - no anonymous-principal
// rejection, no owner guard, and it never touches total_deposited. Breaks the
// invariant sum(balances) <= total_deposited.
#[ic_cdk::update]
fn claim_bonus(amount: u64) {
let caller = msg_caller();
STATE.with(|s| {
*s.borrow_mut().balances.entry(caller).or_default() += amount;
});
}token_vault_reprotests/repro.rsuse candid::{decode_one, encode_args, encode_one, Principal};
use pocket_ic::PocketIc;
fn install(pic: &PocketIc) -> Principal {
let c = pic.create_canister();
pic.add_cycles(c, 2_000_000_000_000);
// Must be an absolute path: cargo runs integration tests from this crate's root.
let path = std::env::var("CANISTER_WASM").expect("set CANISTER_WASM to the built .wasm");
let wasm = std::fs::read(&path).unwrap_or_else(|e| panic!("read {path}: {e}"));
pic.install_canister(c, wasm, encode_args(()).unwrap(), None);
c
}
fn query_u64(pic: &PocketIc, c: Principal, method: &str, arg: Vec<u8>) -> u64 {
let reply = pic.query_call(c, Principal::anonymous(), method, arg).expect("query rejected");
decode_one(&reply).expect("reply decodes to u64")
}
#[test]
fn anonymous_principal_mints_unbacked_balance() {
let pic = PocketIc::new();
let c = install(&pic);
let anon = Principal::anonymous();
// Precondition: an empty ledger.
assert_eq!(query_u64(&pic, c, "total_deposited", encode_args(()).unwrap()), 0);
// Attack: the anonymous principal credits itself 1_000 out of thin air.
pic.update_call(c, anon, "claim_bonus", encode_one(1_000u64).unwrap())
.expect("claim_bonus is callable by the anonymous principal - that is the bug");
let bal = query_u64(&pic, c, "balance_of", encode_one(anon).unwrap());
let total = query_u64(&pic, c, "total_deposited", encode_args(()).unwrap());
assert_eq!(bal, 1_000, "anonymous principal minted a balance");
assert!(
bal > total,
"INVARIANT BROKEN: sum(balances) {bal} > total_deposited {total} - \
the ledger records a balance no deposit backs"
);
}rustup target add wasm32-unknown-unknown
# Build the audited canister in its own repo (no changes to it).
(cd token_vault && cargo build --target wasm32-unknown-unknown --release)
# Run the repro with an ABSOLUTE wasm path - a relative one resolves against
# token_vault_repro/, not the directory you ran this from.
(cd token_vault_repro && \
CANISTER_WASM="$PWD/../token_vault/target/wasm32-unknown-unknown/release/token_vault.wasm" \
cargo test --test repro -- --nocapture)target/claim_bonusclaim_bonuswithdrawpocket-icupdate_callquery_callResult<Vec<u8>, _>