Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: low
Valid

Missing Validation in HorseStore.sol::feedHorse Function

Summary

The feedHorse function currently lacks a critical check to confirm the existence of the horse with the provided horseId. This omission introduces a vulnerability, allowing users to feed non-existent horses, leading to wasted gas fees and potential inaccuracies in the application state.

Vulnerability Details

The vulnerable feedHorse function is as follows:

function feedHorse(uint256 horseId) external {
horseIdToFedTimeStamp[horseId] = block.timestamp;
}

Impact

The absence of a validation check in the feedHorse function permits users to feed horses that may not actually exist in the system. Consequently, gas fees are consumed for non-existent actions, and the feeding timestamp is updated inaccurately in the horseIdToFedTimeStamp mapping.

Tools Used

The vulnerability was discovered through manual review and foundry fuzz test, I wrote a fuzz test which attempts to feed a non-existent horse and subsequently checks if the system incorrectly registers the non-existent horse as "happy.
To run the test and confirm the vulnerability, the block of code below should be added to the Base_test.t.sol file:

function testFeedingHorseWithInvalidId_Fuzz(uint256 id) public {
vm.warp(horseStore.HORSE_HAPPY_IF_FED_WITHIN());
vm.roll(horseStore.HORSE_HAPPY_IF_FED_WITHIN());
vm.prank(user);
// feed non-existent horse
horseStore.feedHorse(id);
// check if horse is happy after feeding non-existent horse
assertEq(horseStore.isHappyHorse(id), true);
}

then run the test file

Recommendations

To address this vulnerability, it is crucial to enhance the feedHorse function by incorporating a validation check. Consider modifying the function as follows:

function feedHorse(uint256 horseId) external {
require(horseExists(horseId), "Horse does not exist");
horseIdToFedTimeStamp[horseId] = block.timestamp;
}

The added require statement ensures that the horse with the given horseId exists before proceeding with the feeding operation. The horseExists function should be designed to verify the existence of the specified horse in the system.

Updates

Lead Judging Commences

inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

Nonexistent horses can be fed

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.