Skip to content

Commit

Permalink
Forbid most uses of msg.sender in favor of _msgSender()
Browse files Browse the repository at this point in the history
  • Loading branch information
duncancmt committed Oct 5, 2023
1 parent 43bd6e8 commit 73b6c5c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/Context.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

abstract contract Context {
function _msgSender() internal view virtual returns (address) {
abstract contract ContextAbstract {
function _msgSender() internal view virtual returns (address);

function _msgData() internal view virtual returns (bytes calldata);

function _isForwarded() internal view virtual returns (bool);
}

abstract contract Context is ContextAbstract {
function _msgSender() internal view virtual override returns (address) {
return msg.sender;
}

function _msgData() internal view virtual returns (bytes calldata) {
function _msgData() internal view virtual override returns (bytes calldata) {
return msg.data;
}

function _isForwarded() internal view virtual returns (bool) {
function _isForwarded() internal view virtual override returns (bool) {
return false;
}
}
6 changes: 3 additions & 3 deletions src/Settler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ contract Settler is Permit2Payment, Basic, OtcOrderSettlement, UniswapV3, Uniswa
bytes memory sig
) = abi.decode(data, (address, uint256, uint256, bytes, ISignatureTransfer.PermitTransferFrom, bytes));

sellTokenForTokenToUniswapV3(path, amountIn, amountOutMin, recipient, msg.sender, permit, sig);
sellTokenForTokenToUniswapV3(path, amountIn, amountOutMin, recipient, _msgSender(), permit, sig);
} else {
_dispatch(0, action, data, msg.sender);
_dispatch(0, action, data, _msgSender());
}
}

for (uint256 i = 1; i < actions.length; i = i.unsafeInc()) {
(bytes4 action, bytes calldata data) = actions.decodeCall(i);
_dispatch(i, action, data, msg.sender);
_dispatch(i, action, data, _msgSender());
}

_checkSlippageAndTransfer(slippage);
Expand Down
8 changes: 4 additions & 4 deletions src/core/OtcOrderSettlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,18 @@ abstract contract OtcOrderSettlement is Permit2PaymentAbstract {
ISignatureTransfer.SignatureTransferDetails memory takerTransferDetails;
Consideration memory consideration;
(takerTransferDetails, consideration.token, consideration.amount) = _permitToTransferDetails(takerPermit, maker);
consideration.counterparty = msg.sender;
consideration.counterparty = _msgSender();

bytes32 witness = _hashConsideration(consideration);
// There is no taker witness (see below)

// Maker pays recipient (optional fee)
_permit2TransferFrom(makerPermit, makerTransferDetails, maker, witness, CONSIDERATION_WITNESS, makerSig);
// Taker pays Maker (optional fee)
// We don't need to include a witness here. Taker is `msg.sender`, so
// We don't need to include a witness here. Taker is `_msgSender()`, so
// `recipient` and the maker's details are already authenticated. We're just
// using PERMIT2 to move tokens, not to provide authentication.
_permit2TransferFrom(takerPermit, takerTransferDetails, msg.sender, takerSig);
_permit2TransferFrom(takerPermit, takerTransferDetails, _msgSender(), takerSig);

emit OtcOrderFilled(
_hashOtcOrder(
Expand All @@ -149,7 +149,7 @@ abstract contract OtcOrderSettlement is Permit2PaymentAbstract {
)
),
maker,
msg.sender,
_msgSender(),
buyToken,
consideration.token,
buyAmount,
Expand Down
3 changes: 2 additions & 1 deletion src/core/Permit2Payment.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

import {ContextAbstract} from "../Context.sol";
import {AllowanceHolderContext} from "../AllowanceHolderContext.sol";
import {AllowanceHolder} from "../AllowanceHolder.sol";

Expand Down Expand Up @@ -40,7 +41,7 @@ library UnsafeArray {
}
}

abstract contract Permit2PaymentAbstract {
abstract contract Permit2PaymentAbstract is ContextAbstract {
string internal constant TOKEN_PERMISSIONS_TYPE = "TokenPermissions(address token,uint256 amount)";

function PERMIT2() internal view virtual returns (ISignatureTransfer);
Expand Down

0 comments on commit 73b6c5c

Please sign in to comment.