The vulnerability is a Time-of-check Time-of-use (TOCTOU) race condition in the Outray tunnel registration endpoint. The root cause is the non-atomic nature of checking the user's current tunnel count and creating a new tunnel. The vulnerable code is located in the post handler for the /api/tunnel/register route in the file apps/web/src/routes/api/tunnel/register.ts.
The analysis of the patch commit 08c61495761349e7fd2965229c3faa8d7b1c1581 confirms this. The original code performs a redis.scard to get the active tunnel count, and if the count is below the limit, it proceeds to db.insert a new tunnel. An attacker can exploit this by sending multiple concurrent requests to register tunnels. These requests can all read the tunnel count before it is updated by any of the other requests, leading to the creation of more tunnels than the subscription plan allows.
The patch fixes this by wrapping the entire check-and-create logic within a database transaction (db.transaction) and using row-level locking (.for("update")) on the subscription and tunnel tables. This serializes the requests and ensures that each request operates on the most up-to-date data, preventing the race condition.
The vulnerable function is the post handler itself, which would appear in a runtime profile during exploitation.