Files
qwenpaw/tests/integration/test_safety_checks_module.py
agent-radar 1a3a4ccde1
Creator App Tests / PR Spam Gate (push) Skipped
Frontend Tests / PR Spam Gate (push) Skipped
NPM Format (website & console) / PR Spam Gate (push) Skipped
Pre-commit Checks / PR Spam Gate (push) Skipped
QwenPaw Creator Plugin Release / check-version (push) Canceled after 0s
QwenPaw Creator Plugin Release / upload-oss (push) Canceled after 0s
Tests / PR Spam Gate (push) Skipped
CodeQL / CodeQL (python) (push) Canceled after 0s
CodeQL / CodeQL (typescript) (push) Canceled after 0s
Creator App Tests / Creator Backend Tests (push) Canceled after 0s
Creator App Tests / Creator UI Build & Tests (push) Canceled after 0s
E2E Smoke Tests / Playwright UI Smoke (push) Canceled after 0s
Frontend Tests / Detect console changes (push) Canceled after 0s
NPM Format (website & console) / website format check (push) Canceled after 0s
NPM Format (website & console) / console format check (push) Canceled after 0s
Pre-commit Checks / Pre-commit (push) Canceled after 0s
QwenPaw Data Plugin Release / check-version (push) Canceled after 0s
Tests / Detect code changes (push) Canceled after 0s
Frontend Tests / Vitest Unit Tests (push) Canceled after 0s
Frontend Tests / Frontend Summary (push) Canceled after 0s
QwenPaw Data Plugin Release / upload-oss (push) Canceled after 0s
Tests / Maintainer Approval (push) Canceled after 0s
Tests / Unit Tests - py3.11 - macos-latest (push) Canceled after 0s
Tests / Unit Tests - py3.11 - ubuntu-latest (push) Canceled after 0s
Tests / Unit Tests - py3.13 - ubuntu-latest (push) Canceled after 0s
Tests / Unit Tests - py3.11 - windows-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - macos-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - ubuntu-latest (push) Canceled after 0s
Tests / Contract Tests - py3.13 - ubuntu-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - windows-latest (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - fallback (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p2 (push) Canceled after 0s
Tests / Coverage Report (push) Canceled after 0s
Tests / Test Summary (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:01:50 +08:00

182 lines
5.1 KiB
Python

# -*- coding: utf-8 -*-
"""Integration tests for ToolGuard destructive-command safety checks.
Covers src/qwenpaw/security/tool_guard/safety_checks.py (337 uncovered
lines): catastrophic wipe / mkfs / dd / fork-bomb classification,
system-power commands, path-boundary checks.
"""
from __future__ import annotations
from pathlib import Path
import pytest
@pytest.mark.integration
@pytest.mark.p1
def test_is_command_catastrophic_rm_root() -> None:
"""rm -rf / is catastrophic."""
from qwenpaw.security.tool_guard.safety_checks import (
is_command_catastrophic,
)
assert is_command_catastrophic("rm -rf /") is True
@pytest.mark.integration
@pytest.mark.p1
def test_is_command_catastrophic_wildcard_wipe() -> None:
"""Broad wildcard deletes are catastrophic."""
from qwenpaw.security.tool_guard.safety_checks import (
is_command_catastrophic,
)
assert is_command_catastrophic("rm -rf /*") is True
@pytest.mark.integration
@pytest.mark.p1
def test_is_command_catastrophic_mkfs() -> None:
"""Formatting a filesystem is catastrophic."""
from qwenpaw.security.tool_guard.safety_checks import (
is_command_catastrophic,
)
assert is_command_catastrophic("mkfs.ext4 /dev/sda1") is True
@pytest.mark.integration
@pytest.mark.p1
def test_is_command_catastrophic_fork_bomb() -> None:
"""Classic fork bombs are catastrophic."""
from qwenpaw.security.tool_guard.safety_checks import (
is_command_catastrophic,
)
assert is_command_catastrophic(":(){ :|:& };:") is True
@pytest.mark.integration
@pytest.mark.p1
def test_is_command_catastrophic_benign_rm() -> None:
"""Removing a regular file under a workspace is not catastrophic."""
from qwenpaw.security.tool_guard.safety_checks import (
is_command_catastrophic,
)
assert is_command_catastrophic("rm -f notes.txt") is False
@pytest.mark.integration
@pytest.mark.p1
def test_is_command_destructive_shutdown() -> None:
"""shutdown in command position is destructive (system_power)."""
from qwenpaw.security.tool_guard.safety_checks import (
is_command_destructive,
)
assert is_command_destructive("shutdown -h now") is True
@pytest.mark.integration
@pytest.mark.p1
def test_is_command_destructive_benign() -> None:
"""Harmless commands are not destructive."""
from qwenpaw.security.tool_guard.safety_checks import (
is_command_destructive,
)
assert is_command_destructive("ls -la") is False
assert is_command_destructive("echo hello") is False
@pytest.mark.integration
@pytest.mark.p1
def test_classify_destructive_command_kinds() -> None:
"""Classification distinguishes catastrophic, power, and none."""
from qwenpaw.security.tool_guard.safety_checks import (
classify_destructive_command,
)
assert classify_destructive_command("rm -rf /") == "catastrophic"
assert classify_destructive_command("ls") is None
@pytest.mark.integration
@pytest.mark.p1
def test_is_path_outside_boundary_outside(tmp_path: Path) -> None:
"""Path outside the workspace boundary reports True."""
from qwenpaw.security.tool_guard.safety_checks import (
is_path_outside_boundary,
)
ws = tmp_path / "workspace"
ws.mkdir()
outside = tmp_path / "outside" / "secret.txt"
outside.parent.mkdir()
outside.touch()
assert is_path_outside_boundary(outside, ws) is True
@pytest.mark.integration
@pytest.mark.p1
def test_is_path_outside_boundary_inside(tmp_path: Path) -> None:
"""Path inside the workspace boundary reports False."""
from qwenpaw.security.tool_guard.safety_checks import (
is_path_outside_boundary,
)
ws = tmp_path / "workspace"
ws.mkdir()
inside = ws / "data" / "a.txt"
inside.parent.mkdir()
inside.touch()
assert is_path_outside_boundary(inside, ws) is False
@pytest.mark.integration
@pytest.mark.p1
def test_is_path_outside_boundary_sibling_prefix(tmp_path: Path) -> None:
"""Sibling directory sharing a name prefix are outside boundary.
Guards against string-prefix bypass (/foo/bar_evil vs /foo/bar).
"""
from qwenpaw.security.tool_guard.safety_checks import (
is_path_outside_boundary,
)
ws = tmp_path / "bar"
ws.mkdir()
sibling = tmp_path / "bar_evil" / "x.txt"
sibling.parent.mkdir()
sibling.touch()
assert is_path_outside_boundary(sibling, ws) is True
@pytest.mark.integration
@pytest.mark.p1
def test_posix_root_treated_catastrophically() -> None:
"""POSIX absolute root wipe token is catastrophic on any host."""
from qwenpaw.security.tool_guard.safety_checks import (
is_command_catastrophic,
)
assert is_command_catastrophic("sudo rm -rf /etc") is True
@pytest.mark.integration
@pytest.mark.p1
def test_safe_temp_tree_not_catastrophic() -> None:
"""Wiping a temp-tree target is not classified catastrophic."""
from qwenpaw.security.tool_guard.safety_checks import (
classify_destructive_command,
)
# Removing under a tmp scratch dir should not auto-deny.
result = classify_destructive_command("rm -rf /tmp/qwenpaw-scratch")
assert result != "catastrophic" or result is None