Add AccessControl base for role-based authorization#1771
Open
Jim8y wants to merge 6 commits into
Open
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master-n3 #1771 +/- ##
=============================================
+ Coverage 82.95% 82.99% +0.03%
=============================================
Files 306 307 +1
Lines 27371 27491 +120
Branches 3826 3848 +22
=============================================
+ Hits 22705 22815 +110
- Misses 3497 3507 +10
Partials 1169 1169 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ajara87
reviewed
Jun 26, 2026
7ef4fb3 to
a025745
Compare
ajara87
previously approved these changes
Jun 27, 2026
Wi1l-B0t
reviewed
Jun 29, 2026
Contributor
Author
|
Updated. CI is green and all review threads are resolved. |
shargon
reviewed
Jun 30, 2026
Wi1l-B0t
previously approved these changes
Jul 1, 2026
shargon
reviewed
Jul 1, 2026
| /// Asserts that <paramref name="account"/> holds <paramref name="role"/> and witnessed the | ||
| /// current invocation. This is the authorization core; it aborts otherwise. | ||
| /// </summary> | ||
| protected static void CheckRole(ByteString role, UInt160 account) |
Contributor
Author
There was a problem hiding this comment.
Addressed. Roles now use integer identifiers directly, with storage keys derived from the integer value.
The framework only shipped single-owner Ownable, so every contract that needs more than one privileged account (a minter, a pauser, a fee setter) hand-rolls its own unaudited authorization. This adds AccessControl, the OpenZeppelin AccessControl analog adapted to Neo's static-method model. A role is a 32-byte id; accounts are granted roles, and each role is administered by another role. DEFAULT_ADMIN_ROLE (32 zero bytes) is the root role and is its own admin; the account it is granted to at deployment can administer every other role. Security properties baked in: - Explicit-actor authorization: callers pass the acting account and the contract verifies it both holds the required role AND witnessed the call (Runtime.CheckWitness), honoring the signer's witness scope. This avoids the confused-deputy hole of deriving the actor from Transaction.Sender and lets a contract/multisig hold a role. - Fixed 32-byte roles (validated on every entry point) make the membership key tag+role+account structurally injective, so no two distinct (role, account) pairs can ever alias to one storage cell. - The last DEFAULT_ADMIN_ROLE holder can never be removed (guarded on both revoke and renounce), so the contract can never be bricked un-administrable from a single call; root control is handed over by granting to a successor first. - One-time initialization flag prevents a deploy-time re-init from adding a co-admin. - Grant/revoke/admin-change are idempotent and emit events only on an actual state change; a maintained per-role member count backs the last-admin guard. API: DEFAULT_ADMIN_ROLE/HasRole/GetRoleAdmin/GetRoleMemberCount ([Safe] reads), GrantRole/RevokeRole/RenounceRole, the OnlyRole(role, account) guard for authors' own methods, and protected GrantRoleInternal/RevokeRoleInternal/ SetRoleAdminInternal cores (not exported to the ABI). Storage uses reserved prefix 0xFB. No [OnlyRole] modifier attribute is shipped, because a modifier cannot read the decorated method's runtime arguments; the OnlyRole guard is the runtime-flexible substitute. Adds AccessControlTest (24 cases) covering bootstrap and replay protection, the confused-deputy attack, grant/revoke/renounce by the wrong party, idempotency, the last-admin brick on both revoke and renounce, safe admin handoff, the admin-role hierarchy, malformed-role faults, and the manifest safe-flag and ABI-exclusion surface.
dfd8d0e to
8eb0d57
Compare
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.
Summary
The framework only shipped single-owner
Ownable, so every contract that needs more than one privileged account (a minter, a pauser, a fee setter) hand-rolls its own unaudited authorization. This addsAccessControl, the OpenZeppelinAccessControlanalog adapted to Neo's static-method model — the single most-used safety primitive in the OZ library.A role is a 32-byte id; accounts are granted roles, and each role is administered by another role.
DEFAULT_ADMIN_ROLE(32 zero bytes) is the root role and is its own admin; the account it is granted to at deployment can administer every other role.Security properties
Runtime.CheckWitness), honoring the signer's witness scope. This avoids the confused-deputy hole of deriving the actor fromTransaction.Sender, and lets a contract/multisig hold a role.tag ++ role ++ accountis structurally injective — no two distinct(role, account)pairs can collide on one storage cell.DEFAULT_ADMIN_ROLEholder can never be removed (guarded on both revoke and renounce), so the contract can never be left un-administrable from one call; root control is handed over by granting to a successor first.API
DEFAULT_ADMIN_ROLE/HasRole/GetRoleAdmin/GetRoleMemberCount([Safe]reads);GrantRole(role, admin, account)/RevokeRole(role, admin, account)/RenounceRole(role, account); theOnlyRole(role, account)guard authors call at the top of their own role-gated methods; and protectedGrantRoleInternal/RevokeRoleInternal/SetRoleAdminInternalcores that are not exported to the ABI. Storage uses reserved prefix0xFB.No
[OnlyRole]modifier attribute is shipped: a NeoModifierAttribute.Enter()cannot read the decorated method's runtime arguments, so it could only gate a compile-time-constant role against the wrong actor model. TheOnlyRoleguard is the runtime-flexible substitute.Testing
AccessControlTest(24 cases): bootstrap and replay protection, the confused-deputy attack (admin without a witness for this call), grant/revoke/renounce by the wrong party, idempotency, the last-admin brick on both revoke and renounce, safe admin handoff, the admin-role hierarchy (re-pointing a role's admin), malformed-role faults, and the manifest safe-flag + ABI-exclusion surface (protected cores absent from the ABI). Full framework suite green (277).The design and this implementation were each put through an independent multi-design + adversarial security-audit pass (privilege escalation, bricking, key aliasing, witness bypass, count desync) before submission.