Files
cleveragents-core/features/steps/pyyaml_runtime_dependency_steps.py
T
HAL9000 e6094d1fb7 fix(deps): address reviewer feedback on PyYAML security hardening
- Fix step definitions: remove unused imports (sys, Any, Dict), move
  all imports to module level, drop noqa suppressor, fix docstring step
  to use context.text, use packaging.version for correct semver check
- Upgrade version floor from 6.0.2 to 6.0.3 in step text and feature
  file to match pyproject.toml constraint and issue requirement
- Fix CONTRIBUTORS.md: correct PR number (#11012 -> #11017), issue
  reference (#13605 -> #11012), and version string (6.0.2 -> 6.0.3)

ISSUES CLOSED: #11012
2026-06-15 02:24:03 -04:00

64 lines
2.1 KiB
Python

"""Step definitions for PyYAML runtime dependency verification BDD scenarios."""
from __future__ import annotations
import json
import os
import tempfile
import yaml
from behave import given, then, when
from packaging.version import Version
@when("I verify that PyYAML can be imported")
def step_verify_pyyaml_importable(context) -> None:
"""Attempt to import PyYAML module."""
context.pyyaml_import_result = "success"
@then("PyYAML should be importable without error")
def step_pyyaml_import_succeeds(context) -> None:
"""Verify PyYAML import was successful."""
assert context.pyyaml_import_result == "success", (
f"PyYAML import failed: {context.pyyaml_import_result}"
)
@then("its version should be >= 6.0.3 to mitigate CVE-2025-8045")
def step_pyyaml_version_check(context) -> None:
"""Verify PyYAML version meets security floor."""
version_str = getattr(yaml, "__version__", "unknown")
assert Version(version_str) >= Version("6.0.3"), (
f"PyYAML version {version_str} is below security floor 6.0.3 (CVE-2025-8045)"
)
@given(
'a valid YAML actor config file "test.yaml" with content:',
)
def step_give_yaml_config_file(context) -> None:
"""Create a temporary YAML config file for testing."""
content = context.text
context.temp_dir = tempfile.mkdtemp()
config_path = os.path.join(context.temp_dir, "test.yaml")
with open(config_path, "w", encoding="utf-8") as f:
f.write(content)
context.config_file = config_path
@when("I load the YAML config using PyYAML safe_load")
def step_load_yaml_safe_load(context) -> None:
"""Load the YAML config file using yaml.safe_load."""
with open(context.config_file, encoding="utf-8") as f:
context.loaded_config = yaml.safe_load(f)
@then("the loaded config should equal {expected}")
def step_config_matches_expected(context, expected: str) -> None:
"""Verify the loaded YAML config matches expected value."""
expected_dict = json.loads(expected)
assert context.loaded_config == expected_dict, (
f"Expected {expected_dict}, got {context.loaded_config}"
)