GitHub Security Advisory: GHSA-j3fw-wc48-29g3
Package: open-webui (PIP)
Severity: HIGH (CVSS 8.1)
CWE: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
Vulnerable versions: <= 0.6.9
Patched in: 0.6.10

Summary: Open WebUI Arbitrary File Write, Delete via Path Traversal

Details:
** CONFIDENTIAL **

Vulnerability Disclosure Analysis Documentation
-----------------------------------------------

Vulnerability Details
---------------------
1. Discoverer: Taylor Pennington of KoreLogic, Inc.
2. Date Submitted: June 11, 2024
3. Title: Open WebUI Arbitrary File Write, Delete via Path Traversal
4. High-level Summary:
     Attacker controlled files can be uploaded to arbitrary locations on the web
     server's filesystem by abusing a path traversal vulnerability. After the
     file is written, it is deleted.
5. Affected Vendor: Open WebUI
6. Affected Product(s): Open WebUI (Formerly Ollama WebUI)
7. Affected Version(s): 0.1.105
8. Platform/OS: Debian GNU/Linux 12 (bookworm)
9. Vector: HTTP web interface
10. CWE: 22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
11. Technical Analysis:
   
   When attaching files to a prompt by clicking the plus sign (+) on the left of
   the message input box when using the Open WebUI HTTP interface, the file is
   uploaded to a static upload directory. If the file is an audio file
   it will be sent to a second API that will attempt to transcribe it.

   The name of the file is derived from the original HTTP upload request and is
   not validated or sanitized. This allows for users to upload files with names
   containing dot-segments in the file path and traverse out of the intended
   uploads directory. Effectively, users can upload files anywhere on the
   filesystem the user running the web server has permission.

   This can be visualized by examining the python code for the
   "/ollama/models/upload" API route (https://github.com/open-webui/open-webui/blob/0399a69b73de9789c4221acedea70d528e1346c4/backend/apps/ollama/main.py#L1063-L1127):

   ```
   def upload_model(file: UploadFile = File(...), url_idx: Optional[int] = None):
    if url_idx == None:
        url_idx = 0
    ollama_url = app.state.OLLAMA_BASE_URLS[url_idx]

    file_path = f"{UPLOAD_DIR}/{file.filename}"

    # Save file in chunks
    with open(file_path, "wb+") as f:
        for chunk in file.file:
            f.write(chunk)

    def file_process_stream():
        nonlocal ollama_url
        total_size = os.path.getsize(file_path)
        chunk_size = 1024 * 1024
        try:
            with open(file_path, "rb") as f:
                total = 0
                done = False

                while not done:
                    chunk = f.read(chunk_size)
                    if not chunk:
                        done = True
                        continue

                    total += len(chunk)
                    progress = round((total / total_size) * 100, 2)

                    res = {
                        "progress": progress,
                        "total": total_size,
                        "completed": total,
                    }
                    yield f"data: {json.dumps(res)}\n\n"

                if done:
                    f.seek(0)
                    hashed = calcula