Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

3.6 KB
Raw Blame
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
"""Guards for local-path handling on the HTTP server."""

from __future__ import annotations

import re
from pathlib import Path
from typing import Optional

from openviking.utils.network_guard import ensure_public_remote_target
from openviking_cli.exceptions import PermissionDeniedError

_WINDOWS_DRIVE_RE = re.compile(r"^[A-Za-z]:[\\/]")
_NETWORK_SOURCE_PREFIXES = ("http://", "https://", "git@", "ssh://", "git://")
_PRIVATE_SOURCE_PREFIXES = ("tos://",)
_REMOTE_SOURCE_PREFIXES = _NETWORK_SOURCE_PREFIXES + _PRIVATE_SOURCE_PREFIXES

# Shape of temp_file_ids minted by TempUploadStore. Used by MCP add_resource to
# detect when an agent has passed a tfid as the `path` argument by mistake and
# give them a hint to use the `temp_file_id` kwarg instead.
TEMP_FILE_ID_RE = re.compile(r"^(upload_|shared_)[a-zA-Z0-9]+(\.[^/\\]+)?$")


def _is_configured_connector_source(source: str) -> bool:
    """Return whether Connector is enabled for the detected source type."""
    try:
        from openviking.connector.routing import detect_connector_add_type
        from openviking_cli.utils.config.open_viking_config import get_openviking_config

        config = get_openviking_config().connector
        if not config.enable:
            return False
        detected = detect_connector_add_type(source)
    except Exception:
        return False

    return detected is not None and detected[0] in config.allowed_add_types


def is_remote_resource_source(source: str) -> bool:
    """Return True if *source* is a remotely fetchable resource location."""
    return source.startswith(_REMOTE_SOURCE_PREFIXES) or _is_configured_connector_source(source)


def looks_like_local_path(value: str) -> bool:
    """Return True for strings that clearly look like filesystem paths."""
    if not value or "\n" in value or "\r" in value:
        return False
    return (
        value.startswith(("/", "./", "../", "~/", ".\\", "..\\", "~\\"))
        or "/" in value
        or "\\" in value
        or bool(_WINDOWS_DRIVE_RE.match(value))
    )


def require_remote_resource_source(
    source: str, *, declared_connector_add_type: Optional[str] = None
) -> str:
    """Reject direct host-path resource ingestion over HTTP.

    A declared Connector add_type skips the remote-shape requirement: such a
    request is either delegated to the Connector or rejected by the routing
    predicate with a clear error, and never enters local path resolution.
    URL-shaped sources still must point at public remote hosts.
    """
    if declared_connector_add_type is None and not is_remote_resource_source(source):
        raise PermissionDeniedError(
            "HTTP server only accepts remote resource URLs or temp-uploaded files; "
            "direct host filesystem paths are not allowed."
        )
    if source.startswith(_NETWORK_SOURCE_PREFIXES):
        ensure_public_remote_target(source)
    return source


def deny_direct_local_skill_input(value: str) -> None:
    """Reject obvious local filesystem paths for skill uploads over HTTP."""
    if looks_like_local_path(value):
        raise PermissionDeniedError(
            "HTTP server only accepts raw skill content or temp-uploaded files; "
            "direct host filesystem paths are not allowed."
        )


def _read_upload_meta(meta_path: Path) -> Optional[dict]:
    """Read upload metadata file if it exists."""
    import json

    try:
        if meta_path.exists():
            with open(meta_path, "r") as f:
                return json.load(f)
    except Exception:
        pass
    return None