Skip to content
DoS Audit finding Intermediate

Hats.uri function can be DOSed by providing large details or imageURI string or cause large gas fees

The Hats.uri function builds a JSON string from the details and imageURI fields, and because those fields have no length limit, an attacker can make them so large that calling the function costs more than a block's gas limit and reverts. The attacker can also make the hat immutable after the attack, and anyone calling the function pays the high gas cost.

roguereddwarf Follow

Originally disclosed on Sherlock

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

Found by

roguereddwarf

Summary

The Hats.uri function returns a JSON representation of a hat's properties.

The issue is that the length of the details and imageURI string can be so big that the function consumes more than the maximum amount of gas. This would cause the transaction to revert.

Also a malicious user can just make the length so big that the transaction becomes very expensive. This would cause unnecessary expenses to anyone calling the function which is basically a loss of funds.

Vulnerability Detail

The attack is only feasible on a low cost chain (so not on the ETH mainnet) because the attacker needs to set the large string which costs a lot of gas.

So let us consider Polygon which has a block gas limit of 30 million gas.

Furthermore I used this site to calculate the gas costs for the Polygon Network: https://www.cryptoneur.xyz/gas-fees-calculator

So in order to DOS the Hats.uri function we must make it consume >30m Gas.

It is not possible to set the details field to such a large string directly within one transaction because it would cost >30m Gas.

So we need to slowly increase the length of the details field using the Hats.changeHatDetails function.

Increasing the length by so much that the Hats.uri function consumes an extra 1m Gas costs at most 15m Gas (I tested this but obviously I cannot paste the code here because it would be ridiculously big).

At most we would have to spend 30 * 15m = 450m Gas to execute the attack.

Using the calculator I linked, this costs ~92 USD at the time of writing this.

This is within reach of an attacker. Also we must consider that gas prices may drop in the future.

Impact

So the attacker can DOS the Hats.uri function or cause anyone calling it to spend up to 30m Gas which is ~6 USD on Polygon.

Also the attacker can make the hat immutable after performing the attack such that the string cannot be changed anymore.

Code Snippet

https://github.com/Hats-Protocol/hats-protocol/blob/fafcfdf046c0369c1f9e077eacd94a328f9d7af0/src/Hats.sol#L1072-L1138

https://github.com/Hats-Protocol/hats-protocol/blob/fafcfdf046c0369c1f9e077eacd94a328f9d7af0/src/Hats.sol#L1221-L1223

Tool used

Manual Review

Recommendation

Introduce a reasonable limit for the length of the details and imageURI field.

Discussion

spengrah

This would also DOS viewHat, which is currently the only way for other contracts to read hat status and several other properties. So I agree that a length limit would be appropriate.

Since it's not possible to set a long enough string in one transaction, I think the length checks only need to exist on the changeHat... functions.

TODO: determine appropriate length limit for imageURI and details, to be checked in changeHatDetails and changeHatImageURI.

spengrah

Tentative target cap is 7000 characters, which costs roughly 8,000,000 gas to write. cc @zobront

spengrah

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

MLON33

zobront added "Fix Approved" label

The original report as published on Sherlock, by roguereddwarf. 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 does not limit the length of the details and imageURI strings, so building the JSON output can exceed the block gas limit.

Impact

The attacker can cause Hats.uri to revert or make callers spend up to 30 million gas, and can make the hat immutable; this is only feasible on a low-cost chain, not Ethereum mainnet.

What the attacker needed

Low-cost chain such as Polygon; ability to call changeHatDetails repeatedly; funds for gas costs

The fix

A length limit for the details and imageURI fields should be enforced in the changeHat functions; a tentative cap of 7000 characters was proposed and a fix was approved.

How it was found

  1. Choose a low-cost chain such as Polygon where the block gas limit is 30 million.
  2. Repeatedly call Hats.changeHatDetails to slowly increase the length of the details field.
  3. Continue until Hats.uri consumes more than 30 million gas and reverts.
  4. Optionally make the hat immutable so the string cannot be changed.

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.

A smart contract stores text fields for each hat, and a function called Hats.uri reads them and returns a JSON description. If those text fields are made extremely long, reading them costs more gas than a block allows, so the function always fails. The attacker can also lock the hat so the text can never be shortened.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by roguereddwarf

View all

12 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.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.