When a new user/address creates an account for the FIRST time, they either put someone else referralCode or don't put any referral at all. But in this case userA can put the addressX as referralCode which doesn't have any account in the protocol, later addressX can create account and put the userA as referralCode
Eg:- userA can refer userB and userB can refer userA, which is contradictory.
Eg:- userA refers userB, userB refers userC, userC refers userA. This also contradicts.
There are 2 ways to exploit this:-
Github:- https://github.com/Cyfrin/2024-07-zaros/blob/d687fe96bb7ace8652778797052a38763fbcbb1b/src/perpetuals/branches/TradingAccountBranch.sol#L265
1. TradingAccountBranch.sol::createTradingAccount()else condition executes when referralCode is non-custom. When user creates a new tradingAccount, the code is not checking if the account exist or not for the provided referralCode, means we can fake a referral code and the later create a new tradingAccount using the address that we provided as referralCode and pass the address of user as the referralCode.
2. userA and userB both have tradingAccount in the protocol, they creates a new tradingAccount and put each other's address as referralCode
Both path can be used to exploit the referral program.
Eg:- userA refers userB, userB refers userA.
Eg:- userA refers userB, userB refers userC, userC refers userA.
This itself is contradictory but possible in the protocol.
Run this test inside https://github.com/Cyfrin/2024-07-zaros/blob/d687fe96bb7ace8652778797052a38763fbcbb1b/test/integration/perpetuals/trading-account-branch/createTradingAccount/createTradingAccount.t.sol#L10
This test runs successfully, and we can confirm userA refers userB and userB refers userA using the emit events.
This will disrupts the entire referral program as everyone will use their own referral code, making the referral program useless and less and less people will promote the protocol as they will not get any refferal rewards.
Manual Review
Make the refferals based on per account/address and not based on tradingAccount.
TradingAccountBranch.sol:-
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.