Skip to content
Business logic Audit finding Intermediate

Missing validation on referral percentage sum

The smart contract's referral percentage setter only checks the array length, not that each value or their sum is at most 100%. A malicious or compromised factory owner could set excessive percentages, causing referral fees to exceed the payment amount and either panic from underflow or drain funds.

kenzo Follow

Originally disclosed on Immunefi

Informational View original
  • Disclosed after under an hour

57910 sc insight missing validation on referral percentage sum

Submitted on Oct 29th 2025 at 12:37:53 UTC by @kenzo for Audit Comp | Belong

Description

The _set_referral_percentages function in nftfactory.cairo only validates that the input array has exactly 5 elements. It does not check:

  • Whether each percentage value is <= 10000 (100%)
  • Whether the sum of all percentages is <= 10000 (100%)

This allows the factory owner to set invalid percentages like [15000, 0, 0, 0, 0] or [5000, 4000, 3000, 2000, 1000] (sums to 15000).

When these percentages are used in getReferralRate, the calculation rate = amount * percentage / SKALING_FACTOR can return a value larger than the input amount. In the _pay function, this causes fees_to_platform = fees - referral_fees to underflow and panic, or incorrectly allocate more funds than available.

Impact

A compromised or malicious factory owner can set percentages and drain creator/platform funds by over-allocating referral fees.

Recommendation

Add validation in _set_referral_percentages to check both individual values and the sum. Example implementation:

{% code title="_set_referral_percentages (suggested validation)" %}

CAIRO
fn _set_referral_percentages(ref self: ContractState, percentages: Span<u16>) {
    assert(percentages.len() == 5, super::Errors::WRONG_PERCENTAGES_LEN);

    let mut sum: u32 = 0;
    for i in 0..percentages.len() {
        let percentage = *percentages.at(i);
        assert(percentage <= SKALING_FACTOR.try_into().unwrap(), 'Percentage exceeds 100%');
        sum += percentage.into();
    }
    assert(sum <= SKALING_FACTOR.try_into().unwrap(), 'Percentages exceed 100%');

    // Clear existing values before setting new ones
    self.used_to_percentage.clear();
    for i in 0..percentages.len() {
        self.used_to_percentage.append().write(*percentages.at(i));
    };
}

{% endcode %}

Proof of Concept

Add these tests to test_nftfactory.cairo and run with:

  • snforge test test_setReferralPercentages_sum_exceeds_100_percent
  • snforge test test_setReferralPercentages_individual_exceeds_100_percent
Rust
#[test]
fn test_setReferralPercentages_sum_exceeds_100_percent() {
    let contract = deploy();
    let nft_factory = INFTFactoryDispatcher { contract_address: contract };

    let percentages = array![5000, 4000, 3000, 2000, 1000].span();

    start_cheat_caller_address(contract, constants::OWNER());
    nft_factory.setReferralPercentages(percentages);
    stop_cheat_caller_address(contract);

    assert_eq!(nft_factory.usedToPercentage(0), 5000);
    assert_eq!(nft_factory.usedToPercentage(1), 4000);
    assert_eq!(nft_factory.usedToPercentage(2), 3000);
    assert_eq!(nft_factory.usedToPercentage(3), 2000);
    assert_eq!(nft_factory.usedToPercentage(4), 1000);
}

#[test]
fn test_setReferralPercentages_individual_exceeds_100_percent() {
    let contract = deploy();
    let nft_factory = INFTFactoryDispatcher { contract_address: contract };

    let percentages = array![15000, 0, 0, 0, 0].span();

    start_cheat_caller_address(contract, constants::OWNER());
    nft_factory.setReferralPercentages(percentages);
    stop_cheat_caller_address(contract);

    assert_eq!(nft_factory.usedToPercentage(0), 15000);
}

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

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 function _set_referral_percentages lacks validation that each percentage and their sum do not exceed the scaling factor (100%).

Impact

A compromised or malicious factory owner can set percentages and drain creator/platform funds by over-allocating referral fees.

What the attacker needed

Compromised or malicious factory owner

The fix

Add validation in _set_referral_percentages to check both individual values and the sum.

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 lets the owner set how much of each payment goes to referrers. The code only checks that exactly five numbers are provided, but not that they add up to 100% or less. So the owner could set numbers that add up to more than 100%, which would make the contract try to pay out more than it received, potentially breaking it or stealing funds.

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 kenzo

View all

53 more disclosed reports, best first.

More from Belong

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.