GivingThanks

First Flight #28
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: medium
Valid

Incorrect Registry Assignment in Constructor

Summary

The constructor in GivingThanks.sol incorrectly assigns the registry variable to msg.sender rather than the _registry address passed as a parameter. This results in GivingThanks referencing the deployer’s address as the CharityRegistry contract, which disrupts donation functionality, as verification checks will not work as intended. And currently makes it so that some of the tests included in the protocol do NOT pass.

Vulnerability Details

In the current constructor, registry is assigned to CharityRegistry(msg.sender), which treats the deployer’s address as the registry. This prevents the contract from correctly interacting with the CharityRegistry, causing functions that rely on it (like donate) to revert unexpectedly.

Current Code:

constructor(address _registry) ERC721("DonationReceipt", "DRC") {
registry = CharityRegistry(msg.sender);
owner = msg.sender;
tokenCounter = 0;
}

Failing Tests (No tests were added, this is simply what happens when running forge test with the code given):

Failing tests:
Encountered 2 failing tests in test/GivingThanks.t.sol:GivingThanksTest
[FAIL: EvmError: Revert] testDonate() (gas: 27751)
[FAIL: EvmError: Revert; counterexample: calldata=0x06a6cd890000000000000000000000000000000000000000005bb5a09ffc4b2a1eacbfe8 args=[110869960925554244647174120 [1.108e26]]] testFuzzDonate(uint96) (runs: 0, μ: 0, ~: 0)
Encountered a total of 2 failing tests, 1 tests succeeded

Impact

By assigning registry incorrectly, donation functionality is disrupted, as verification checks fail. This effectively disables core functionality in GivingThanks, undermining the protocol’s usability and integrity.

Tools Used

  • Manual code review.

  • Testing (testDonate and testFuzzDonate) to confirm the error’s impact with Foundry.

Recommendations

To resolve this issue, assign registry to _registry directly in the constructor, actually using the parameter given in the constructor, as shown below:

constructor(address _registry) ERC721("DonationReceipt", "DRC") {
registry = CharityRegistry(_registry);
owner = msg.sender;
tokenCounter = 0;
}

This change ensures the contract references the correct CharityRegistry, restoring functionality to the protocol.

And the tests ran earlier should also pass accordingly:

Ran 3 tests for test/GivingThanks.t.sol:GivingThanksTest
[PASS] testCannotDonateToUnverifiedCharity() (gas: 51061)
[PASS] testDonate() (gas: 293401)
[PASS] testFuzzDonate(uint96) (runs: 257, μ: 296028, ~: 295394)
Suite result: ok. 3 passed; 0 failed; 0 skipped; finished in 35.14ms (34.72ms CPU time)
Updates

Lead Judging Commences

n0kto Lead Judge 12 months ago
Submission Judgement Published
Validated
Assigned finding tags:

finding-bad-registry-set-at-construction

Likelyhood: High, the parameter is not well used and won't be set. Impact: Low, can be changed with the setter and no one will be able to donate to malicious charity.

Support

FAQs

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