Skip to content

Commit f0ee162

Browse files
committed
Add tests for reentrancy in _rentStorage function
1 parent d414c3b commit f0ee162

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/IdGateway.sol

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,26 @@ contract IdGateway is IIdGateway, Guardians, Signatures, EIP712, Nonces {
168168
//////////////////////////////////////////////////////////////*/
169169

170170
function _rentStorage(
171-
uint256 fid,
172-
uint256 extraUnits,
173-
uint256 payment,
174-
address payer
171+
uint256 fid,
172+
uint256 extraUnits,
173+
uint256 payment,
174+
address payer
175175
) internal returns (uint256 overpayment) {
176-
overpayment = storageRegistry.rent{value: payment}(fid, 1 + extraUnits);
177-
178-
if (overpayment > 0) {
179-
payer.sendNative(overpayment);
180-
}
176+
// Calculate the overpayment before making any external calls
177+
uint256 amountToRent = 1 + extraUnits;
178+
overpayment = payment - storageRegistry.price(amountToRent);
179+
180+
// Make the external call to rent storage
181+
storageRegistry.rent{value: payment}(fid, amountToRent);
182+
183+
// Return the overpayment after the external call
184+
if (overpayment > 0) {
185+
(bool success, ) = payer.call{value: overpayment}("");
186+
require(success, "Transfer failed");
187+
}
181188
}
182189

190+
183191
receive() external payable {
184192
if (msg.sender != address(storageRegistry)) revert Unauthorized();
185193
}

0 commit comments

Comments
 (0)