Race condition In wwbn/avideo
Description
AVideo Vulnerable to Wallet Balance Double-Spend via TOCTOU Race Condition in transferBalance
Summary
The transferBalance() method in plugin/YPTWallet/YPTWallet.php contains a Time-of-Check-Time-of-Use (TOCTOU) race condition. The method reads the sender's wallet balance, checks sufficiency in PHP, then writes the new balance — all without database transactions or row-level locking. An attacker with multiple authenticated sessions can send concurrent transfer requests that all read the same stale balance, each passing the balance check independently, resulting in only one deduction being applied while the recipient is credited multiple times.
Details
The vulnerable code path in plugin/YPTWallet/YPTWallet.php:450-517:
// Line 473-474: READ - fetch current balance (plain SELECT, no FOR UPDATE) $senderWallet = self::getWallet($fromUserId); $senderBalance = $senderWallet->getBalance(); $senderNewBalance = $senderBalance - $amount; // Line 477: CHECK - verify sufficient funds in PHP if ($senderNewBalance < 0) { return false;...
The getWallet() method (YPTWallet.php:244) calls Wallet::getFromUser() (Wallet.php:69-84) which executes a plain SELECT * FROM wallet WHERE users_id = $users_id with no FOR UPDATE clause. The save() method (Wallet.php:105) calls ObjectYPT::save() (Object.php:293) which executes a plain UPDATE — no transaction wrapping.
Race window: Between the SELECT (step 1) and UPDATE (step 3), all concurrent requests see the same original balance. Each independently computes original - amount, passes the check, and writes back. The last writer wins for the sender (only one deduction effective), but the receiver gets credited once per request.
Why concurrent requests succeed: PHP's file-based session locking serializes requests per session. However, an attacker can create multiple login sessions (different PHPSESSID cookies) for the same user account. Each session has its own lock and can execute concurrently. Each session needs its own captcha, but the captcha validation in objects/captcha.php:58-73 compares $_SESSION['palavra'] without unsetting it after validation, allowing unlimited reuse within each session.
Entry point: plugin/YPTWallet/view/transferFunds.json.php:39 calls YPTWallet::transferBalance(User::getId(), $_POST['users_id'], $_POST['value']) — requires only User::isLogged() and a valid captcha.
PoC
# Accomplice has a registered account (recipient) TARGET="https://target-avideo-instance" ACCOMPLICE_ID=123 # recipient user ID # Step 1: Create 5 independent sessions for the same attacker account declare -a SESSIONS declare -a CAPTCHA_ANSWERS...
Impact
An authenticated attacker can exploit this race condition to:
Create wallet balance from nothing: With a $10 balance and N concurrent requests, the recipient can receive up to $10×N while the sender only loses $10.
Bypass pay-per-view charges: Inflate wallet balance, then purchase paid content without real payment.
Bypass subscription fees: Use inflated balance to purchase subscriptions.
Financial integrity compromise: The wallet ledger becomes inconsistent — total balances across all users no longer match total deposits.
The attack requires solving one captcha per session (captchas are reusable within a session), creating multiple login sessions, and timing concurrent requests — achievable with basic scripting.
Recommended Fix
Replace the read-check-write pattern with an atomic database operation using a transaction and row-level locking:
public static function transferBalance($fromUserId, $toUserId, $amount, $customDescription = "", $forceTransfer = false) { global $global; // ... existing auth and validation checks ... $amount = floatval($amount); if ($amount <= 0) {...
Additionally, fix the captcha reuse issue in objects/captcha.php:58-73 by unsetting $_SESSION['palavra'] after successful validation:
public static function validation($word) { // ... existing checks ... $validation = (strcasecmp($word, $_SESSION["palavra"]) == 0); if ($validation) { unset($_SESSION["palavra"]); // Consume the captcha token } return $validation;...
Mitigation
Update Impact
Minimal update. May introduce new vulnerabilities or breaking changes.
Ecosystem | Package | Affected version | Patched versions |
|---|---|---|---|
packagist | 29.0 |
Aliases
References