📢 Gate Square #Creator Campaign Phase 2# is officially live!
Join the ZKWASM event series, share your insights, and win a share of 4,000 $ZKWASM!
As a pioneer in zk-based public chains, ZKWASM is now being prominently promoted on the Gate platform!
Three major campaigns are launching simultaneously: Launchpool subscription, CandyDrop airdrop, and Alpha exclusive trading — don’t miss out!
🎨 Campaign 1: Post on Gate Square and win content rewards
📅 Time: July 25, 22:00 – July 29, 22:00 (UTC+8)
📌 How to participate:
Post original content (at least 100 words) on Gate Square related to
Rust Smart Contracts Security Advanced: Permission Control and Access Management Practices
Rust Smart Contracts Development Diary (7) Contract Security and Permission Control
This article will introduce permission control in Rust smart contracts from two perspectives:
1. Contract Function Visibility
Visibility control of contract functions is crucial for protecting key functionalities. For example, in the security incident of Bancor Network exchange in June 2020, the risk to user assets arose because a critical transfer function was mistakenly set to public.
In Rust smart contracts, there are the following types of function visibility:
Additionally, defining a function in an impl block that is not modified by #[near_bindgen] can also make it an internal function.
For the callback function, it must be set to public but also ensure that it can only be called by the contract itself. This functionality can be achieved using the #[private] macro.
It should be noted that the default visibility in Rust is private, which is different from the default public in some versions of Solidity. The exceptions are that items in pub trait and pub enum are public by default.
2. Access Control of Privileged Functions
In addition to function visibility, a whitelist mechanism needs to be established to control access to privileged functions. Similar to the onlyOwner modifier in Solidity, an Ownable trait can be implemented:
rust pub trait Ownable { fn assert_owner(&self) { assert_eq!(env::predecessor_account_id(), self.get_owner()); } AccountId; fn set_owner(&mut self, owner: AccountId); }
This trait can restrict only the owner from calling certain privileged functions. Based on this principle, more complex whitelists can be set up to achieve fine-grained access control.
3. Other Access Control Methods
Other access control methods such as contract invocation timing control, multi-signature invocation mechanism, and DAO governance can also be considered. These will be detailed in subsequent articles.