Token guardian protection doesn’t account for approved operators in approve()
The token guardian protection in the approve() function only checks the caller's guardian status, not the token owner's, so an approved operator can still set approvals for a guardian-enabled owner's tokens. An attacker with operator approval could set approvals on the victim's tokens, potentially leading to unauthorised transfers if the victim does not revoke them.
Submitted by MiloTruck, also found by Limbooo
According to the README, when an address has token guardian enabled, approvals should not work for the tokens owned by that address:
Token Guardian: Protection mechanism for the tokens held by an address, which restricts transfers and approvals when enabled. See LIP-4 for more.
In LensHandles.sol, token guardian is enforced by the _hasTokenGuardianEnabled() check in the approve() function:
function approve(address to, uint256 tokenId) public override(IERC721, ERC721) {
// We allow removing approvals even if the wallet has the token guardian enabled
if (to != address(0) && _hasTokenGuardianEnabled(msg.sender)) {
revert HandlesErrors.GuardianEnabled();
}
super.approve(to, tokenId);
}
However, this check is inadequate as approved operators (addresses approved using setApprovalForAll() by the owner) are also allowed to call approve(). We can see this in Openzeppelin’s ERC-721 implementation:
require(
_msgSender() == owner || isApprovedForAll(owner, _msgSender()),
"ERC721: approve caller is not token owner or approved for all"
);
As such, even if an owner has token guardian enabled, approvals can still be set for his tokens by other approved operators, leaving the owner’s tokens vulnerable. For example:
-
Alice sets Bob as an approved operator using
setApprovalForAll(). -
Alice enables token guardian using
enableTokenGuardian(). -
If Bob wants to set approvals for Alice’s tokens, he can do so by:
- Disabling his own token guardian using
DANGER__disableTokenGuardian(). - Calling
approve()for Alice’s tokens. This will still work, even though Alice has token guardian enabled.
- Disabling his own token guardian using
Note that the approve() function in LensProfiles.sol also has the same vulnerability.
Impact
As token guardian protection in approve() does not account for approved operators, although an owner has token guardian enabled, approved operators will still be able to set approvals for his tokens.
Proof of Concept
The following Foundry test demonstrates the example above:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import 'forge-std/Test.sol';
import '../contracts/namespaces/LensHandles.sol';
contract TokenGuardian_POC is Test {
LensHandles lensHandles;
address ALICE;
address BOB;
uint256 tokenId;
function setUp() public {
// Setup LensHandles contract
lensHandles = new LensHandles(address(this), address(0), 0);
// Setup Alice and Bob addresses
ALICE = makeAddr("Alice");
BOB = makeAddr("Bob");
// Mint "alice.lens" to Alice
tokenId = lensHandles.mintHandle(ALICE, "alice");
}
function testCanApproveWhileTokenGuardianEnabled() public {
// Alice disables tokenGuardian to set Bob as an approved operator
vm.startPrank(ALICE);
lensHandles.DANGER__disableTokenGuardian();
lensHandles.setApprovalForAll(BOB, true);
// Alice re-enables tokenGuardian
lensHandles.enableTokenGuardian();
vm.stopPrank();
// Bob disables tokenGuardian for himself
vm.startPrank(BOB);
lensHandles.DANGER__disableTokenGuardian();
// Alice still has tokenGuardian enabled
assertEq(lensHandles.getTokenGuardianDisablingTimestamp(ALICE), 0);
// However, Bob can still set approvals for Alice's handle
lensHandles.approve(address(0x1337), tokenId);
vm.stopPrank();
assertEq(lensHandles.getApproved(tokenId), address(0x1337));
}
}
Recommended Mitigation
Consider checking if the token’s owner has token guardian enabled as well:
function approve(address to, uint256 tokenId) public override(IERC721, ERC721) {
// We allow removing approvals even if the wallet has the token guardian enabled
- if (to != address(0) && _hasTokenGuardianEnabled(msg.sender)) {
+ if (to != address(0) && (_hasTokenGuardianEnabled(msg.sender) || _hasTokenGuardianEnabled(_ownerOf(tokenId)))) {
revert HandlesErrors.GuardianEnabled();
}
super.approve(to, tokenId);
}
Assessed type
Access Control
donosonaumczuk (Lens) commented via duplicate issue #90:
This is Medium as assets cannot be stolen directly, but it requires a hypothetical attack path with stated assumptions.
Note that the transferFrom function is not compromised itself, thus the victim could revoke the approval of the operator and the approvals from the assets before disabling the token guardian, and be safe.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The approve() function checks only the caller's token guardian status, not the token owner's, allowing approved operators to bypass the owner's guardian protection.
Impact
An approved operator could set approvals on a guardian-enabled owner's tokens, potentially enabling later transfers if the victim does not revoke them; the report notes the transferFrom function itself is not compromised.
What the attacker needed
Victim has set attacker as approved operator; victim has token guardian enabled; attacker disables own token guardian
The fix
The approve() function should also check whether the token's owner has token guardian enabled, not just the caller.
How it was found
- Alice disables her token guardian and sets Bob as an approved operator.
- Alice re-enables her token guardian.
- Bob disables his own token guardian.
- Bob calls approve() for Alice's token, and the approval succeeds despite Alice's guardian being enabled.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Token guardian is a safety feature that should stop anyone from moving or approving your tokens when it is on. But the check only looked at whether the person making the approval had guardian on, not whether the token's owner did. So someone you had already trusted as an operator could still approve your tokens for others, even with your guardian on.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.