Rust Smart Contracts Security Advanced: Permission Control and Access Management Practices

robot
Abstract generation in progress

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. Visibility of contract methods
  2. Access control of privileged functions

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:

  • pub fn: public function, callable from external sources
  • fn: Internal function, can only be called within the contract
  • pub(crate) fn: restrict calling within crate

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.

View Original
This page may contain third-party content, which is provided for information purposes only (not representations/warranties) and should not be considered as an endorsement of its views by Gate, nor as financial or professional advice. See Disclaimer for details.
  • Reward
  • 7
  • Share
Comment
0/400
LootboxPhobiavip
· 7h ago
I'm stuck, I can't learn smart contracts.
View OriginalReply0
GasOptimizervip
· 7h ago
Visualizing the gas consumption of multi-signature is another major topic.
View OriginalReply0
BridgeJumpervip
· 7h ago
Is the code safe now? It's still being injected.
View OriginalReply0
UncleLiquidationvip
· 7h ago
The security of multi-signature is quite good.
View OriginalReply0
UncleWhalevip
· 7h ago
Security still needs to be taken seriously.
View OriginalReply0
BankruptcyArtistvip
· 7h ago
What is the use of multi-signature? It can still be exploited if there are vulnerabilities.
View OriginalReply0
MEVSandwichvip
· 7h ago
Good job! Multi-signature looks safe~
View OriginalReply0
Trade Crypto Anywhere Anytime
qrCode
Scan to download Gate app
Community
English
  • 简体中文
  • English
  • Tiếng Việt
  • 繁體中文
  • Español
  • Русский
  • Français (Afrique)
  • Português (Portugal)
  • Bahasa Indonesia
  • 日本語
  • بالعربية
  • Українська
  • Português (Brasil)