Skip to content
Business logic Audit finding Intermediate

The Hats contract needs to override the ERC1155.balanceOfBatch function

The Hats contract overrides ERC1155.balanceOf to return zero for inactive hats or ineligible wearers, but it does not override balanceOfBatch, which still returns the raw stored balance. An attacker or integrator could therefore read a non-zero balance for a hat the wearer is not eligible to hold, causing inconsistent results between the two functions.

GimelSec Follow with cccz, ktg and 1 other

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-hats-judging/issues/85

Found by

GimelSec, cccz, ktg, roguereddwarf

Summary

The Hats contract does not override the ERC1155.balanceOfBatch function

Vulnerability Detail

The Hats contract overrides the ERC1155.balanceOf function to return a balance of 0 when the hat is inactive or the wearer is ineligible.

Solidity
    function balanceOf(address _wearer, uint256 _hatId)
        public
        view
        override(ERC1155, IHats)
        returns (uint256 balance)
    {
        Hat storage hat = _hats[_hatId];

        balance = 0;

        if (_isActive(hat, _hatId) && _isEligible(_wearer, hat, _hatId)) {
            balance = super.balanceOf(_wearer, _hatId);
        }
    }

But the Hats contract does not override the ERC1155.balanceOfBatch function, which causes balanceOfBatch to return the actual balance no matter what the circumstances.

Solidity
    function balanceOfBatch(address[] calldata owners, uint256[] calldata ids)
        public
        view
        virtual
        returns (uint256[] memory balances)
    {
        require(owners.length == ids.length, "LENGTH_MISMATCH");

        balances = new uint256[](owners.length);

        // Unchecked because the only math done is incrementing
        // the array index counter which cannot possibly overflow.
        unchecked {
            for (uint256 i = 0; i < owners.length; ++i) {
                balances[i] = _balanceOf[owners[i]][ids[i]];
            }
        }
    }

Impact

This will make balanceOfBatch return a different result than balanceOf, which may cause errors when integrating with other projects

Code Snippet

https://github.com/Hats-Protocol/hats-protocol/blob/main/src/Hats.sol#L1149-L1162 https://github.com/Hats-Protocol/hats-protocol/blob/main/lib/ERC1155/ERC1155.sol#L118-L139

Tool used

Manual Review

Recommendation

Consider overriding the ERC1155.balanceOfBatch function in Hats contract to return 0 when the hat is inactive or the wearer is ineligible.

Discussion

spengrah

https://github.com/Hats-Protocol/hats-protocol/pull/102

zobront

Reverting solves the problem, but I'm curious why revert instead of just replacing:

Solidity
balances[i] = _balanceOf[owners[i]][ids[i]];

with...

Solidity
balances[i] = balanceOf(owners[i], ids[i]);

zobront

Reverting solves the problem, but I'm curious why revert instead of just replacing:

Solidity
balances[i] = _balanceOf[owners[i]][ids[i]];

with...

Solidity
balances[i] = balanceOf(owners[i], ids[i]);

MLON33

zobront added "Fix Approved" label

The original report as published on Sherlock, by GimelSec. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

Likely the contract overrode only the single-holder balance lookup and left the batch lookup reading the raw internal balance mapping directly.

Impact

balanceOfBatch could return a different result from balanceOf, which may cause errors when other projects integrate with the contract.

What the attacker needed

None

The fix

The issue was fixed by overriding balanceOfBatch so it returns zero when the hat is inactive or the wearer is ineligible.

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.

This contract tracks who holds which 'hat' (a kind of token). One function, balanceOf, was changed to report zero when a hat is switched off or the holder is not allowed to have it. A second function, balanceOfBatch, does the same job for many holders at once but was left unchanged, so it still reports the old stored number. That mismatch can confuse other programs that rely on the two functions agreeing.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by GimelSec

View all

18 more disclosed reports, best first.

More from Hats

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.