TON smart contract security best practices
This comprehensive guide covers the most critical security vulnerabilities found in TON smart contracts, based on real-world audits and security research. Understanding these pitfalls is essential for developing secure smart contracts on TON Blockchain.
Many of these vulnerabilities can lead to complete loss of funds. Always conduct thorough security audits before deploying contracts to mainnet.
Critical
Missing impure modifier
Severity: 🔴 Critical
The absence of the impure
modifier allows the compiler to skip function calls if the return value is unused, potentially bypassing critical security checks.
Vulnerable code:
() authorize(sender) inline {
throw_unless(187, equal_slice_bits(sender, addr1) | equal_slice_bits(sender, addr2));
}
Secure implementation:
() authorize(sender) impure inline {
throw_unless(187, equal_slice_bits(sender, addr1) | equal_slice_bits(sender, addr2));
}
Always add the impure
modifier to functions that perform state changes or critical validations.
Incorrect use of modifying/non-modifying methods
Severity: 🔴 Critical
Using .
instead of ~
for modifying methods means the original data structure remains unchanged, leading to logic errors.
Vulnerable code:
(_, slice old_balance_slice, int found?) = accounts.udict_delete_get?(256, sender);
Secure implementation:
(_, int found?) = accounts~udict_delete_get?(256, sender);
if(found?) {
;; accounts dictionary has been modified
}