Skip to content

Commit

Permalink
Invalidate sessionKeys when owner changed
Browse files Browse the repository at this point in the history
  • Loading branch information
eloi010 committed Nov 15, 2023
1 parent be88daa commit a6997d2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
12 changes: 10 additions & 2 deletions contracts/core/BaseOpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ abstract contract BaseOpenfortAccount is
bool masterSessionKey;
bool whitelising;
mapping(address => bool) whitelist;
address registrarAddress;
}

mapping(address => SessionKeyStruct) public sessionKeys;
Expand Down Expand Up @@ -111,6 +112,9 @@ abstract contract BaseOpenfortAccount is
// If not owner and the session key is revoked, return false
if (sessionKey.validUntil == 0) return false;

// If the sessionKey was not registered by the owner, return false
if (sessionKey.registrarAddress != owner()) return false;

// If the signer is a session key that is still valid
// Let's first get the selector of the function that the caller is using
bytes4 funcSelector =
Expand Down Expand Up @@ -189,7 +193,9 @@ abstract contract BaseOpenfortAccount is
) {
return 0xffffffff;
} // Not owner or session key revoked
else {
else if (sessionKey.registrarAddress != owner()) {
return 0xffffffff;
} else {
return MAGICVALUE;
}
}
Expand Down Expand Up @@ -292,7 +298,6 @@ abstract contract BaseOpenfortAccount is
) public {
_requireFromEntryPointOrOwner();

// Not sure why changing this for a custom error increases gas dramatically
require(_whitelist.length < 11, "Whitelist too big");
uint256 i = 0;
for (i; i < _whitelist.length;) {
Expand All @@ -302,6 +307,7 @@ abstract contract BaseOpenfortAccount is
}
}
if (i > 0) {
// If there was some whitelisting, it is not a masterSessionKey
sessionKeys[_key].whitelising = true;
sessionKeys[_key].masterSessionKey = false;
} else {
Expand All @@ -312,6 +318,7 @@ abstract contract BaseOpenfortAccount is
sessionKeys[_key].validAfter = _validAfter;
sessionKeys[_key].validUntil = _validUntil;
sessionKeys[_key].limit = _limit;
sessionKeys[_key].registrarAddress = owner();

emit SessionKeyRegistered(_key);
}
Expand All @@ -325,6 +332,7 @@ abstract contract BaseOpenfortAccount is
if (sessionKeys[_key].validUntil != 0) {
sessionKeys[_key].validUntil = 0;
sessionKeys[_key].masterSessionKey = false;
sessionKeys[_key].registrarAddress = address(0);
emit SessionKeyRevoked(_key);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/foundry/core/managed/ManagedOpenfortAccountTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ contract ManagedOpenfortAccountTest is Test {
// Verify that the registered key is not a MasterKey but has whitelisting
bool isMasterKey;
bool isWhitelisted;
(,,, isMasterKey, isWhitelisted) = ManagedOpenfortAccount(payable(account)).sessionKeys(sessionKey);
(,,, isMasterKey, isWhitelisted,) = ManagedOpenfortAccount(payable(account)).sessionKeys(sessionKey);
assert(!isMasterKey);
assert(isWhitelisted);

Expand Down Expand Up @@ -746,7 +746,7 @@ contract ManagedOpenfortAccountTest is Test {
// Verify that the registered key is not a MasterKey but has whitelisting
bool isMasterKey;
bool isWhitelisted;
(,,, isMasterKey, isWhitelisted) = ManagedOpenfortAccount(payable(account)).sessionKeys(sessionKey);
(,,, isMasterKey, isWhitelisted,) = ManagedOpenfortAccount(payable(account)).sessionKeys(sessionKey);
assert(!isMasterKey);
assert(isWhitelisted);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ contract RecoverableOpenfortAccountTest is Test {
// Verify that the registered key is not a MasterKey but has whitelisting
bool isMasterKey;
bool isWhitelisted;
(,,, isMasterKey, isWhitelisted) = RecoverableOpenfortAccount(payable(account)).sessionKeys(sessionKey);
(,,, isMasterKey, isWhitelisted,) = RecoverableOpenfortAccount(payable(account)).sessionKeys(sessionKey);
assert(!isMasterKey);
assert(isWhitelisted);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ contract UpgradeableOpenfortAccountTest is Test {
// Verify that the registered key is not a MasterKey nor has whitelisting
bool isMasterKey;
bool isWhitelisted;
(,,, isMasterKey, isWhitelisted) = UpgradeableOpenfortAccount(payable(account)).sessionKeys(sessionKey);
(,,, isMasterKey, isWhitelisted,) = UpgradeableOpenfortAccount(payable(account)).sessionKeys(sessionKey);
assert(!isMasterKey);
assert(!isWhitelisted);

Expand Down Expand Up @@ -774,7 +774,7 @@ contract UpgradeableOpenfortAccountTest is Test {
// Verify that the registered key is not a MasterKey but has whitelisting
bool isMasterKey;
bool isWhitelisted;
(,,, isMasterKey, isWhitelisted) = UpgradeableOpenfortAccount(payable(account)).sessionKeys(sessionKey);
(,,, isMasterKey, isWhitelisted,) = UpgradeableOpenfortAccount(payable(account)).sessionKeys(sessionKey);
assert(!isMasterKey);
assert(isWhitelisted);

Expand Down

0 comments on commit a6997d2

Please sign in to comment.