Files
cowork-local/core/task_script.py
T
minhanhpkproandClaude Opus 5 b78d48320c fix(sandbox): "Chặn mạng" chặn mọi đường ra mạng, trừ nhà cung cấp AI
Trước đây công tắc chỉ chặn tool mạng của agent; lệnh shell chỉ bị proxy
giả, còn M365, Teams, nút Test, MCP đang chạy, task script, link đính kèm
task, pip tự cài và tài nguyên web trong xem trước HTML vẫn ra mạng tự do.

- Cổng chung application/network/network_guard.py, nối vào cấu hình sống
  ở Composition Root; nhà cung cấp AI (chat, danh sách model, thử model)
  không đi qua cổng này.
- Lệnh shell của agent và task script chạy trong Windows AppContainer
  không có quyền mạng (macOS: sandbox-exec, Linux: unshare --net);
  không cô lập được thì từ chối chạy.
- Không cấp quyền kế thừa của AppContainer lên thư mục chứa PySide6:
  Chromium không nạp được Qt6WebEngineCore.dll và tab Graph bị hỏng.
- Bật chặn thì dừng MCP đang chạy; tool OneDrive đồng bộ trên máy vẫn dùng.
- Mặc định tắt khi mở app lần đầu; nhãn và tooltip 3 ngôn ngữ cập nhật.
- Test: tests/test_network_guard_lanes.py (có bài AppContainer thật).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 22:32:44 +09:00

48 lines
2.1 KiB
Python

"""Run a scheduled task of type ``script`` (tách khỏi ``task_executors.py``).
Khi công tắc "Chặn mạng" đang bật, lệnh của task chạy trong tiến trình bị hệ
điều hành cắt mạng — giống ``run_command`` của agent. Trước đây task script
chạy thẳng bằng ``subprocess.run``, không sandbox, nên lên mạng tự do.
"""
from __future__ import annotations
import subprocess
from pathlib import Path
def run_script(command: str, out_dir: Path, timeout_sec: int) -> str:
"""Chạy một task kiểu script bằng shell trong thư mục kết quả, có hạn giờ."""
if not command.strip():
raise RuntimeError("Script task has no command configured.")
from ..application.network import network_guard
if network_guard.is_blocked():
return _run_script_without_network(command, out_dir, timeout_sec)
proc = subprocess.run(command, shell=True, cwd=str(out_dir),
capture_output=True, text=True, timeout=max(1, timeout_sec))
output = (proc.stdout or "") + (("\n[stderr]\n" + proc.stderr) if proc.stderr else "")
if proc.returncode != 0:
raise RuntimeError(f"Script exited with code {proc.returncode}:\n{output[-2000:]}")
return output
def _run_script_without_network(command: str, out_dir: Path, timeout_sec: int) -> str:
"""Như :func:`run_script`, nhưng tiến trình không có mạng; không cô lập được thì không chạy."""
from .deps import network_blocked_env, run_cancellable
rc, output, _cancelled, timed_out, _exceeded = run_cancellable(
command, cwd=str(out_dir), timeout=max(1, timeout_sec), shell=True,
env=network_blocked_env(), isolate_network=True,
)
if timed_out:
raise subprocess.TimeoutExpired(command, timeout_sec)
if rc is None:
raise RuntimeError("Script not run: network is blocked and the script could not be "
f"isolated from the network ({output.strip()}).")
if rc != 0:
raise RuntimeError(f"Script exited with code {rc} (network blocked):\n{output[-2000:]}")
return output
__all__ = ["run_script"]