harry0703/MoneyPrinterTurbo · warning · ValueError

empty path is not allowed

Error message

empty path is not allowed

What it means

Path-security guard used for whitelisted directories (uploads, materials, task artifacts): resolve_path_within_directory rejects an empty/blank unsafe_path with ValueError before doing any filesystem work. It is the first checkpoint before containment and existence checks.

Source

Thrown at app/utils/file_security.py:15

import os


def resolve_path_within_directory(
    base_dir: str,
    unsafe_path: str,
    *,
    require_file: bool = True,
) -> str:
    # 用户传入的路径可能是文件名、相对路径、绝对路径,也可能夹带 `../`。
    # 这里统一解析成真实路径,并用 commonpath 判断它是否仍在允许目录内。
    # 这样比简单判断字符串前缀可靠,可以覆盖符号链接、重复分隔符、相对路径
    # 等场景,适用于上传目录、素材目录、任务产物目录这类白名单目录。
    if not unsafe_path:
        raise ValueError("empty path is not allowed")

    base_dir_real = os.path.realpath(base_dir)
    candidate_path = unsafe_path
    if not os.path.isabs(candidate_path):
        candidate_path = os.path.join(base_dir_real, candidate_path)

    resolved_path = os.path.realpath(candidate_path)
    try:
        common_path = os.path.commonpath([base_dir_real, resolved_path])
    except ValueError as exc:
        # Windows 下不同盘符会触发 ValueError,这类路径一定不属于允许目录。
        raise ValueError("path is outside the allowed directory") from exc

    if common_path != base_dir_real:
        raise ValueError("path is outside the allowed directory")

    if require_file and not os.path.isfile(resolved_path):
        raise ValueError("file does not exist")

View on GitHub (pinned to 1f9f19c202)

Solutions

  1. Ensure the caller supplies a non-empty path; check required form/query fields before invoking the resolver.
  2. Return a 400-style validation message to the client instead of letting the ValueError propagate as a 500.

Example fix

# before
resolved = resolve_path_within_directory(task_dir, request.args.get("file") or "")

# after
raw = (request.args.get("file") or "").strip()
if not raw:
    raise BadRequest("file parameter is required")
resolved = resolve_path_within_directory(task_dir, raw)
Defensive patterns

Strategy: validation

Validate before calling

if not (unsafe_path or "").strip():
    raise ValueError("file parameter is required")  # fail before the resolver

Type guard

def has_nonempty_path(value: str | None) -> bool:
    return isinstance(value, str) and bool(value.strip())

Prevention

When it happens

Trigger: Calling resolve_path_within_directory(base_dir, '') or with None-like blank string; API request where the client omitted the path field but the handler still called the resolver.

Common situations: Frontend sends an empty file parameter; task record has a missing file name after a partial save; query param like ?file= present but blank.

Related errors


AI-assisted analysis of harry0703/MoneyPrinterTurbo@1f9f19c202 (2026-08-14). Data as JSON: /api/errors/c178d3b24edfb33d. Report an issue: GitHub.