Skip to content

Commit

Permalink
Update ownable check to validate zero address (#398)
Browse files Browse the repository at this point in the history
* Update ownable check to validate zero address

* Update tests/mocks/Ownable.cairo

Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>

* Update documentation

* Update docs/Access.md

Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>

* Update tests/access/test_Ownable.py

Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>

* update ownable test

* Apply suggestions from code review

Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>
Co-authored-by: Martín Triay <martriay@gmail.com>
  • Loading branch information
3 people committed Jul 19, 2022
1 parent 44f78c3 commit 02bb5f9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/Access.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ None.

#### `assert_only_owner`

Reverts if called by any account other than the owner.
Reverts if called by any account other than the owner. In case of renounced ownership, any call from the default zero address will also be reverted.

Parameters:

Expand Down
1 change: 1 addition & 0 deletions src/openzeppelin/access/ownable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace Ownable:
let (owner) = Ownable.owner()
let (caller) = get_caller_address()
with_attr error_message("Ownable: caller is not the owner"):
assert_not_zero(caller)
assert owner = caller
end
return ()
Expand Down
14 changes: 13 additions & 1 deletion tests/access/test_Ownable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
ZERO_ADDRESS,
assert_event_emitted,
get_contract_class,
cached_contract
cached_contract,
assert_revert
)


Expand Down Expand Up @@ -85,6 +86,17 @@ async def test_renounceOwnership(ownable_factory):
executed_info = await ownable.owner().call()
assert executed_info.result == (ZERO_ADDRESS,)

@pytest.mark.asyncio
async def test_contract_without_owner(ownable_factory):
ownable, owner = ownable_factory
await signer.send_transaction(owner, ownable.contract_address, 'renounceOwnership', [])

# Protected function should not be called from zero address
await assert_revert(
ownable.protected_function().invoke(),
reverted_with="Ownable: caller is not the owner"
)


@pytest.mark.asyncio
async def test_renounceOwnership_emits_event(ownable_factory):
Expand Down
11 changes: 11 additions & 0 deletions tests/mocks/Ownable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.starknet.common.syscalls import get_caller_address
from openzeppelin.access.ownable import Ownable
from starkware.cairo.common.bool import TRUE

@constructor
func constructor{
Expand Down Expand Up @@ -45,3 +46,13 @@ func renounceOwnership{
Ownable.renounce_ownership()
return ()
end

@external
func protected_function{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}() -> (res: felt):
Ownable.assert_only_owner()
return (TRUE)
end

0 comments on commit 02bb5f9

Please sign in to comment.