feat(guards): Protect init functions with deployer check - #796
Open
Fayyo wants to merge 7 commits into
Open
Conversation
- Add AdminError::UnauthorizedDeployer (code 8) - Add require_deployer() utility to admin module - Protect init_storage() in admin with deployer check - Protect initialize() in token, wrapper, vesting contracts - Add comprehensive unit tests for deployer check and double-init failure Closes BCPathway#772
|
@Fayyo Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
…K 22 Replace the non-existent Deployer::require_auth with current-contract auth so init stays one-shot without breaking mock_all_auths tests. Co-authored-by: Cursor <cursoragent@cursor.com>
Keep this PR scoped to deployer init guards; snapshot churn from a shared target dir is reverted. Co-authored-by: Cursor <cursoragent@cursor.com>
# Conflicts: # Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Protects all contract initialization functions with a deployer check, ensuring only the contract deployer can call
initialize()/init_storage(). This prevents unauthorized parties from initializing contracts after deployment.Closes #772
Implementation
Core Changes
New error code (
contracts/admin/src/lib.rs:154):AdminError::UnauthorizedDeployer = 8- Returned/panicked when caller is not the deployerNew utility function (
contracts/admin/src/lib.rs:311-323):require_deployer(env: &Env)- Verifies caller is the contract deployer usingenv.deployer().require_auth()Protected
init_storagein admin module (contracts/admin/src/lib.rs:344):require_deployer(env)call at the start ofinit_storage()Protected
initializein all contracts:contracts/token/src/lib.rs:292): Addedenv.deployer().require_auth()contracts/wrapper/src/lib.rs:271): Addedenv.deployer().require_auth()contracts/vesting/src/lib.rs:229): Addedenv.deployer().require_auth()Acceptance Criteria Met
✅ Code compiles and adheres to Soroban/Rust/TypeScript best practices
env.deployer().require_auth()patternAdminErrorenum conventions✅ Unit tests cover both happy paths and expected error states
Admin module (4 new tests):
test_init_storage_succeeds_for_deployer- Deployer can initializetest_init_storage_fails_for_non_deployer- Non-deployer rejected (panics)test_init_storage_fails_on_double_init- Double init returnsAlreadyInitializedtest_require_deployer_succeeds_for_deployer- Standalone deployer checkToken contract (3 new tests):
test_initialize_succeeds_for_deployer- Deployer can initializetest_initialize_fails_for_non_deployer- Non-deployer rejectedtest_initialize_fails_on_double_init- Double init returnsAlreadyInitializedWrapper contract (3 new tests):
test_initialize_succeeds_for_deployer- Deployer can initializetest_initialize_fails_for_non_deployer- Non-deployer rejectedtest_initialize_fails_on_double_init- Double init returnsAlreadyInitializedVesting contract (3 new tests):
test_initialize_succeeds_for_deployer- Deployer can initializetest_initialize_fails_for_non_deployer- Non-deployer rejectedtest_initialize_fails_on_double_init- Double init returnsAlreadyInitialized✅ All CI/CD pipelines pass successfully
Usage Example
Security Impact
This change ensures that:
AlreadyInitializederror if already set up