Smart Contract Security Part 2: Access Control, tx.origin, Randomness, Oracles, Front-Running and Upgrade Risks
Quick Reference Table
| Problem | What happens | Prevention |
|---|---|---|
| Missing access control | Anyone can call an admin function such as mint or withdraw | onlyOwner, roles, and test who can call what |
| tx.origin for auth | A malicious contract tricks you into calling it, and tx.origin is still you | Use msg.sender |
| Weak randomness | block.timestamp or block data can be predicted or influenced | Use a verifiable randomness service |
| Oracle manipulation | An attacker moves a thin market price used by your contract | Use time-weighted or multi-source price feeds |
| Front-running (MEV) | Bots see your pending transaction and jump ahead | Slippage limits, commit-reveal, private transaction routes |
| Unchecked external calls | A failed call is ignored and logic continues | Always check the result |
| Denial of service | One bad address or huge loop blocks everyone | Pull payments, avoid unbounded loops |
| Integer problems | Solidity 0.8+ reverts on overflow, but casts and unchecked blocks can still wrap | Avoid unchecked unless proven safe, check casts |
| Insecure upgrades | Admin key or bad proxy setup lets someone replace the code | Multisig, timelock, tested upgrade tools |
Missing Access Control Example
solidity
// BAD: anyone can take the money
function withdrawAll() external {
payable(msg.sender).transfer(address(this).balance);
}
// GOOD
function withdrawAll() external onlyOwner {
payable(owner).transfer(address(this).balance);
}
tx.origin Phishing
solidity
// BAD
function sendTo(address payable to, uint256 amount) external {
require(tx.origin == owner); // can be tricked
to.transfer(amount);
}
// GOOD
function sendTo(address payable to, uint256 amount) external {
require(msg.sender == owner);
to.transfer(amount);
}
Pull Over Push
Instead of sending money to many people in one loop, let each person withdraw their own share. One failing receiver cannot block the others.
Randomness
Everything on a blockchain is public and deterministic, so true randomness is hard. Use a verifiable random function from an oracle network for lotteries and games.
Upgradeable Contracts
- Proxy patterns let you change logic later, but they add complexity and trust
- Users must trust whoever controls the upgrade key
- Use multisig plus timelock and publish the upgrade policy
Practice
- Review your PiggyBank: who can call each function?
- Why is
msg.sendersafer thantx.origin? - Why is
block.timestampa poor random number?
Conclusion
Most losses come from simple mistakes: missing permission checks, trusting weak data, unsafe calls and risky upgrades. Review every function for who can call it and what it trusts.
Quick Summary: table; access control; tx.origin; pull payments; randomness; upgrades.

