8000 ✨ Add opt-in storage layout compatibility guard for UUPS by codermaybe · Pull Request #1500 · Vectorized/solady · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@codermaybe
Copy link

Description

Adds an optional storage layout compatibility check mechanism for UUPS upgrades to prevent storage collision vulnerabilities.

Changes

  • Added StorageLayoutMismatch() error to UUPSUpgradeable.sol
  • Added _checkStorageLayout(address newImplementation) virtual hook (empty by default for backwards compatibility)
  • Provided reference implementation in MockUUPSImplementation.sol using version hash comparison
  • Added test cases for both compatible and incompatible layout scenarios

Usage Pattern

Implementations can override _checkStorageLayout to enforce storage compatibility:

bytes32 private constant _STORAGE_LAYOUT_VERSION = keccak256("MyContract.v1");

function STORAGE_LAYOUT_VERSION() external pure returns (bytes32) {
    return _STORAGE_LAYOUT_VERSION;
}

function _checkStorageLayout(address newImpl) internal view override {
    // Check version hash matches - see MockUUPSImplementation.sol for full example
}

Design Decisions

  • Opt-in mechanism: Default empty implementation preserves existing behavior
  • Non-breaking: Zero impact on contracts that don't override the hook
  • Gas-optimized: Uses assembly for efficient staticcall and error handling
  • Flexible: Implementations can choose their own validation strategy

Addresses the storage layout safety concern raised in the original UUPS test demo.

Closes #1489

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UUPS upgrade: support checking slot conflicts

1 participant

0