AccessManaged.sol 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {IAuthority} from "./IAuthority.sol";
  4. import {AuthorityUtils} from "./AuthorityUtils.sol";
  5. import {IAccessManager} from "./IAccessManager.sol";
  6. import {IAccessManaged} from "./IAccessManaged.sol";
  7. import {Context} from "../../utils/Context.sol";
  8. /**
  9. * @dev This contract module makes available a {restricted} modifier. Functions decorated with this modifier will be
  10. * permissioned according to an "authority": a contract like {AccessManager} that follows the {IAuthority} interface,
  11. * implementing a policy that allows certain callers to access certain functions.
  12. *
  13. * IMPORTANT: The `restricted` modifier should never be used on `internal` functions, judiciously used in `public`
  14. * functions, and ideally only used in `external` functions. See {restricted}.
  15. */
  16. abstract contract AccessManaged is Context, IAccessManaged {
  17. address private _authority;
  18. bool private _consumingSchedule;
  19. /**
  20. * @dev Initializes the contract connected to an initial authority.
  21. */
  22. constructor(address initialAuthority) {
  23. _setAuthority(initialAuthority);
  24. }
  25. /**
  26. * @dev Restricts access to a function as defined by the connected Authority for this contract and the
  27. * caller and selector of the function that entered the contract.
  28. *
  29. * [IMPORTANT]
  30. * ====
  31. * In general, this modifier should only be used on `external` functions. It is okay to use it on `public`
  32. * functions that are used as external entry points and are not called internally. Unless you know what you're
  33. * doing, it should never be used on `internal` functions. Failure to follow these rules can have critical security
  34. * implications! This is because the permissions are determined by the function that entered the contract, i.e. the
  35. * function at the bottom of the call stack, and not the function where the modifier is visible in the source code.
  36. * ====
  37. *
  38. * [NOTE]
  39. * ====
  40. * Selector collisions are mitigated by scoping permissions per contract, but some edge cases must be considered:
  41. *
  42. * * If the https://docs.soliditylang.org/en/v0.8.20/contracts.html#receive-ether-function[`receive()`] function
  43. * is restricted, any other function with a `0x00000000` selector will share permissions with `receive()`.
  44. * * Similarly, if there's no `receive()` function but a `fallback()` instead, the fallback might be called with
  45. * empty `calldata`, sharing the `0x00000000` selector permissions as well.
  46. * * For any other selector, if the restricted function is set on an upgradeable contract, an upgrade may remove
  47. * the restricted function and replace it with a new method whose selector replaces the last one, keeping the
  48. * previous permissions.
  49. * ====
  50. */
  51. modifier restricted() {
  52. _checkCanCall(_msgSender(), _msgData());
  53. _;
  54. }
  55. /**
  56. * @dev Returns the current authority.
  57. */
  58. function authority() public view virtual returns (address) {
  59. return _authority;
  60. }
  61. /**
  62. * @dev Transfers control to a new authority. The caller must be the current authority.
  63. */
  64. function setAuthority(address newAuthority) public virtual {
  65. address caller = _msgSender();
  66. if (caller != authority()) {
  67. revert AccessManagedUnauthorized(caller);
  68. }
  69. if (newAuthority.code.length == 0) {
  70. revert AccessManagedInvalidAuthority(newAuthority);
  71. }
  72. _setAuthority(newAuthority);
  73. }
  74. /**
  75. * @dev Returns true only in the context of a delayed restricted call, at the moment that the scheduled operation is
  76. * being consumed. Prevents denial of service for delayed restricted calls in the case that the contract performs
  77. * attacker controlled calls.
  78. */
  79. function isConsumingScheduledOp() public view returns (bool) {
  80. return _consumingSchedule;
  81. }
  82. /**
  83. * @dev Transfers control to a new authority. Internal function with no access restriction.
  84. */
  85. function _setAuthority(address newAuthority) internal virtual {
  86. _authority = newAuthority;
  87. emit AuthorityUpdated(newAuthority);
  88. }
  89. /**
  90. * @dev Reverts if the caller is not allowed to call the function identified by a selector.
  91. */
  92. function _checkCanCall(address caller, bytes calldata data) internal virtual {
  93. (bool allowed, uint32 delay) = AuthorityUtils.canCallWithDelay(
  94. authority(),
  95. caller,
  96. address(this),
  97. bytes4(data)
  98. );
  99. if (!allowed) {
  100. if (delay > 0) {
  101. _consumingSchedule = true;
  102. IAccessManager(authority()).consumeScheduledOp(caller, data);
  103. _consumingSchedule = false;
  104. } else {
  105. revert AccessManagedUnauthorized(caller);
  106. }
  107. }
  108. }
  109. }