Remediating Immutable Violation
How to enforce immutability of constructor-set variables using the immutable keyword and initialization guards.
Remediating Immutable Violation
Overview
Related Detector: Immutable Violation
Variables that should not change after deployment should use Solidity’s immutable keyword. For proxy patterns where immutable cannot be used, implement initialization guards.
Recommended Fix
Before (Vulnerable)
address public oracle; // Mutable — can be changed after deployment
constructor(address _oracle) {
oracle = _oracle;
}
After (Fixed)
address public immutable oracle; // Cannot be changed after construction
constructor(address _oracle) {
oracle = _oracle;
}
Alternative Mitigations
- Initialization guard for proxies: Use OpenZeppelin’s
Initializablewithinitializermodifier to ensure one-time execution. - Constant values: For values known at compile time, use
constantinstead ofimmutable.
Common Mistakes
- Using
immutablein proxy implementations: Immutable values are stored in the bytecode, not storage. In proxy patterns, the proxy’s bytecode is different from the implementation’s, soimmutablein the implementation does not affect the proxy.