security.adoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. :github-icon: pass:[<svg class="icon"><use href="#github-icon"/></svg>]
  2. :PullPayment: pass:normal[xref:security.adoc#PullPayment[`PullPayment`]]
  3. :ReentrancyGuard: pass:normal[xref:security.adoc#ReentrancyGuard[`ReentrancyGuard`]]
  4. :Pausable: pass:normal[xref:security.adoc#Pausable[`Pausable`]]
  5. :xref-PullPayment-constructor--: xref:security.adoc#PullPayment-constructor--
  6. :xref-PullPayment-withdrawPayments-address-payable-: xref:security.adoc#PullPayment-withdrawPayments-address-payable-
  7. :xref-PullPayment-payments-address-: xref:security.adoc#PullPayment-payments-address-
  8. :xref-PullPayment-_asyncTransfer-address-uint256-: xref:security.adoc#PullPayment-_asyncTransfer-address-uint256-
  9. :ReentrancyGuard: pass:normal[xref:security.adoc#ReentrancyGuard[`ReentrancyGuard`]]
  10. :Escrow: pass:normal[xref:utils.adoc#Escrow[`Escrow`]]
  11. :xref-ReentrancyGuard-nonReentrant--: xref:security.adoc#ReentrancyGuard-nonReentrant--
  12. :xref-ReentrancyGuard-constructor--: xref:security.adoc#ReentrancyGuard-constructor--
  13. :xref-ReentrancyGuard-_reentrancyGuardEntered--: xref:security.adoc#ReentrancyGuard-_reentrancyGuardEntered--
  14. :xref-Pausable-whenNotPaused--: xref:security.adoc#Pausable-whenNotPaused--
  15. :xref-Pausable-whenPaused--: xref:security.adoc#Pausable-whenPaused--
  16. :xref-Pausable-constructor--: xref:security.adoc#Pausable-constructor--
  17. :xref-Pausable-paused--: xref:security.adoc#Pausable-paused--
  18. :xref-Pausable-_requireNotPaused--: xref:security.adoc#Pausable-_requireNotPaused--
  19. :xref-Pausable-_requirePaused--: xref:security.adoc#Pausable-_requirePaused--
  20. :xref-Pausable-_pause--: xref:security.adoc#Pausable-_pause--
  21. :xref-Pausable-_unpause--: xref:security.adoc#Pausable-_unpause--
  22. :xref-Pausable-Paused-address-: xref:security.adoc#Pausable-Paused-address-
  23. :xref-Pausable-Unpaused-address-: xref:security.adoc#Pausable-Unpaused-address-
  24. = Security
  25. [.readme-notice]
  26. NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/security
  27. These contracts aim to cover common security practices.
  28. * {PullPayment}: A pattern that can be used to avoid reentrancy attacks.
  29. * {ReentrancyGuard}: A modifier that can prevent reentrancy during certain functions.
  30. * {Pausable}: A common emergency response mechanism that can pause functionality while a remediation is pending.
  31. TIP: For an overview on reentrancy and the possible mechanisms to prevent it, read our article https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
  32. == Contracts
  33. :constructor: pass:normal[xref:#PullPayment-constructor--[`++constructor++`]]
  34. :withdrawPayments: pass:normal[xref:#PullPayment-withdrawPayments-address-payable-[`++withdrawPayments++`]]
  35. :payments: pass:normal[xref:#PullPayment-payments-address-[`++payments++`]]
  36. :_asyncTransfer: pass:normal[xref:#PullPayment-_asyncTransfer-address-uint256-[`++_asyncTransfer++`]]
  37. [.contract]
  38. [[PullPayment]]
  39. === `++PullPayment++` link:https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.9.3/contracts/security/PullPayment.sol[{github-icon},role=heading-link]
  40. [.hljs-theme-light.nopadding]
  41. ```solidity
  42. import "@openzeppelin/contracts/security/PullPayment.sol";
  43. ```
  44. Simple implementation of a
  45. https://consensys.github.io/smart-contract-best-practices/development-recommendations/general/external-calls/#favor-pull-over-push-for-external-calls[pull-payment]
  46. strategy, where the paying contract doesn't interact directly with the
  47. receiver account, which must withdraw its payments itself.
  48. Pull-payments are often considered the best practice when it comes to sending
  49. Ether, security-wise. It prevents recipients from blocking execution, and
  50. eliminates reentrancy concerns.
  51. TIP: If you would like to learn more about reentrancy and alternative ways
  52. to protect against it, check out our blog post
  53. https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
  54. To use, derive from the `PullPayment` contract, and use {_asyncTransfer}
  55. instead of Solidity's `transfer` function. Payees can query their due
  56. payments with {payments}, and retrieve them with {withdrawPayments}.
  57. [.contract-index]
  58. .Functions
  59. --
  60. * {xref-PullPayment-constructor--}[`++constructor()++`]
  61. * {xref-PullPayment-withdrawPayments-address-payable-}[`++withdrawPayments(payee)++`]
  62. * {xref-PullPayment-payments-address-}[`++payments(dest)++`]
  63. * {xref-PullPayment-_asyncTransfer-address-uint256-}[`++_asyncTransfer(dest, amount)++`]
  64. --
  65. [.contract-item]
  66. [[PullPayment-constructor--]]
  67. ==== `[.contract-item-name]#++constructor++#++()++` [.item-kind]#internal#
  68. [.contract-item]
  69. [[PullPayment-withdrawPayments-address-payable-]]
  70. ==== `[.contract-item-name]#++withdrawPayments++#++(address payable payee)++` [.item-kind]#public#
  71. Withdraw accumulated payments, forwarding all gas to the recipient.
  72. Note that _any_ account can call this function, not just the `payee`.
  73. This means that contracts unaware of the `PullPayment` protocol can still
  74. receive funds this way, by having a separate account call
  75. {withdrawPayments}.
  76. WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
  77. Make sure you trust the recipient, or are either following the
  78. checks-effects-interactions pattern or using {ReentrancyGuard}.
  79. [.contract-item]
  80. [[PullPayment-payments-address-]]
  81. ==== `[.contract-item-name]#++payments++#++(address dest) → uint256++` [.item-kind]#public#
  82. Returns the payments owed to an address.
  83. [.contract-item]
  84. [[PullPayment-_asyncTransfer-address-uint256-]]
  85. ==== `[.contract-item-name]#++_asyncTransfer++#++(address dest, uint256 amount)++` [.item-kind]#internal#
  86. Called by the payer to store the sent amount as credit to be pulled.
  87. Funds sent in this way are stored in an intermediate {Escrow} contract, so
  88. there is no danger of them being spent before withdrawal.
  89. :constructor: pass:normal[xref:#ReentrancyGuard-constructor--[`++constructor++`]]
  90. :nonReentrant: pass:normal[xref:#ReentrancyGuard-nonReentrant--[`++nonReentrant++`]]
  91. :_reentrancyGuardEntered: pass:normal[xref:#ReentrancyGuard-_reentrancyGuardEntered--[`++_reentrancyGuardEntered++`]]
  92. [.contract]
  93. [[ReentrancyGuard]]
  94. === `++ReentrancyGuard++` link:https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.9.3/contracts/security/ReentrancyGuard.sol[{github-icon},role=heading-link]
  95. [.hljs-theme-light.nopadding]
  96. ```solidity
  97. import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
  98. ```
  99. Contract module that helps prevent reentrant calls to a function.
  100. Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
  101. available, which can be applied to functions to make sure there are no nested
  102. (reentrant) calls to them.
  103. Note that because there is a single `nonReentrant` guard, functions marked as
  104. `nonReentrant` may not call one another. This can be worked around by making
  105. those functions `private`, and then adding `external` `nonReentrant` entry
  106. points to them.
  107. TIP: If you would like to learn more about reentrancy and alternative ways
  108. to protect against it, check out our blog post
  109. https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
  110. [.contract-index]
  111. .Modifiers
  112. --
  113. * {xref-ReentrancyGuard-nonReentrant--}[`++nonReentrant()++`]
  114. --
  115. [.contract-index]
  116. .Functions
  117. --
  118. * {xref-ReentrancyGuard-constructor--}[`++constructor()++`]
  119. * {xref-ReentrancyGuard-_reentrancyGuardEntered--}[`++_reentrancyGuardEntered()++`]
  120. --
  121. [.contract-item]
  122. [[ReentrancyGuard-nonReentrant--]]
  123. ==== `[.contract-item-name]#++nonReentrant++#++()++` [.item-kind]#modifier#
  124. Prevents a contract from calling itself, directly or indirectly.
  125. Calling a `nonReentrant` function from another `nonReentrant`
  126. function is not supported. It is possible to prevent this from happening
  127. by making the `nonReentrant` function external, and making it call a
  128. `private` function that does the actual work.
  129. [.contract-item]
  130. [[ReentrancyGuard-constructor--]]
  131. ==== `[.contract-item-name]#++constructor++#++()++` [.item-kind]#internal#
  132. [.contract-item]
  133. [[ReentrancyGuard-_reentrancyGuardEntered--]]
  134. ==== `[.contract-item-name]#++_reentrancyGuardEntered++#++() → bool++` [.item-kind]#internal#
  135. Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
  136. `nonReentrant` function in the call stack.
  137. :Paused: pass:normal[xref:#Pausable-Paused-address-[`++Paused++`]]
  138. :Unpaused: pass:normal[xref:#Pausable-Unpaused-address-[`++Unpaused++`]]
  139. :constructor: pass:normal[xref:#Pausable-constructor--[`++constructor++`]]
  140. :whenNotPaused: pass:normal[xref:#Pausable-whenNotPaused--[`++whenNotPaused++`]]
  141. :whenPaused: pass:normal[xref:#Pausable-whenPaused--[`++whenPaused++`]]
  142. :paused: pass:normal[xref:#Pausable-paused--[`++paused++`]]
  143. :_requireNotPaused: pass:normal[xref:#Pausable-_requireNotPaused--[`++_requireNotPaused++`]]
  144. :_requirePaused: pass:normal[xref:#Pausable-_requirePaused--[`++_requirePaused++`]]
  145. :_pause: pass:normal[xref:#Pausable-_pause--[`++_pause++`]]
  146. :_unpause: pass:normal[xref:#Pausable-_unpause--[`++_unpause++`]]
  147. [.contract]
  148. [[Pausable]]
  149. === `++Pausable++` link:https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.9.3/contracts/security/Pausable.sol[{github-icon},role=heading-link]
  150. [.hljs-theme-light.nopadding]
  151. ```solidity
  152. import "@openzeppelin/contracts/security/Pausable.sol";
  153. ```
  154. Contract module which allows children to implement an emergency stop
  155. mechanism that can be triggered by an authorized account.
  156. This module is used through inheritance. It will make available the
  157. modifiers `whenNotPaused` and `whenPaused`, which can be applied to
  158. the functions of your contract. Note that they will not be pausable by
  159. simply including this module, only once the modifiers are put in place.
  160. [.contract-index]
  161. .Modifiers
  162. --
  163. * {xref-Pausable-whenNotPaused--}[`++whenNotPaused()++`]
  164. * {xref-Pausable-whenPaused--}[`++whenPaused()++`]
  165. --
  166. [.contract-index]
  167. .Functions
  168. --
  169. * {xref-Pausable-constructor--}[`++constructor()++`]
  170. * {xref-Pausable-paused--}[`++paused()++`]
  171. * {xref-Pausable-_requireNotPaused--}[`++_requireNotPaused()++`]
  172. * {xref-Pausable-_requirePaused--}[`++_requirePaused()++`]
  173. * {xref-Pausable-_pause--}[`++_pause()++`]
  174. * {xref-Pausable-_unpause--}[`++_unpause()++`]
  175. --
  176. [.contract-index]
  177. .Events
  178. --
  179. * {xref-Pausable-Paused-address-}[`++Paused(account)++`]
  180. * {xref-Pausable-Unpaused-address-}[`++Unpaused(account)++`]
  181. --
  182. [.contract-item]
  183. [[Pausable-whenNotPaused--]]
  184. ==== `[.contract-item-name]#++whenNotPaused++#++()++` [.item-kind]#modifier#
  185. Modifier to make a function callable only when the contract is not paused.
  186. Requirements:
  187. - The contract must not be paused.
  188. [.contract-item]
  189. [[Pausable-whenPaused--]]
  190. ==== `[.contract-item-name]#++whenPaused++#++()++` [.item-kind]#modifier#
  191. Modifier to make a function callable only when the contract is paused.
  192. Requirements:
  193. - The contract must be paused.
  194. [.contract-item]
  195. [[Pausable-constructor--]]
  196. ==== `[.contract-item-name]#++constructor++#++()++` [.item-kind]#internal#
  197. Initializes the contract in unpaused state.
  198. [.contract-item]
  199. [[Pausable-paused--]]
  200. ==== `[.contract-item-name]#++paused++#++() → bool++` [.item-kind]#public#
  201. Returns true if the contract is paused, and false otherwise.
  202. [.contract-item]
  203. [[Pausable-_requireNotPaused--]]
  204. ==== `[.contract-item-name]#++_requireNotPaused++#++()++` [.item-kind]#internal#
  205. Throws if the contract is paused.
  206. [.contract-item]
  207. [[Pausable-_requirePaused--]]
  208. ==== `[.contract-item-name]#++_requirePaused++#++()++` [.item-kind]#internal#
  209. Throws if the contract is not paused.
  210. [.contract-item]
  211. [[Pausable-_pause--]]
  212. ==== `[.contract-item-name]#++_pause++#++()++` [.item-kind]#internal#
  213. Triggers stopped state.
  214. Requirements:
  215. - The contract must not be paused.
  216. [.contract-item]
  217. [[Pausable-_unpause--]]
  218. ==== `[.contract-item-name]#++_unpause++#++()++` [.item-kind]#internal#
  219. Returns to normal state.
  220. Requirements:
  221. - The contract must be paused.
  222. [.contract-item]
  223. [[Pausable-Paused-address-]]
  224. ==== `[.contract-item-name]#++Paused++#++(address account)++` [.item-kind]#event#
  225. Emitted when the pause is triggered by `account`.
  226. [.contract-item]
  227. [[Pausable-Unpaused-address-]]
  228. ==== `[.contract-item-name]#++Unpaused++#++(address account)++` [.item-kind]#event#
  229. Emitted when the pause is lifted by `account`.