Skip to content

Commit cff49a6

Browse files
committed
Contribution to Cyber Vulnerability Avoidance
Preventing Path Traversal: The application's use of _validate_path_within_project (as seen in threat_analysis/__main__.py for handling --project and IaC plugin paths) is a critical defense mechanism against path traversal vulnerabilities, ensuring that file operations are confined to the intended project directory. This commit ensures the proper execution flow that leverages such existing security controls. - Overall Application Security: By fixing bugs and improving test coverage, the overall quality and security posture of the threat analysis framework are enhanced, reducing the likelihood of the tool itself becoming a source of vulnerabilities
1 parent f67c5f0 commit cff49a6

13 files changed

Lines changed: 666 additions & 275 deletions

tests/test_iac_plugins.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from threat_analysis.iac_plugins.ansible_plugin import AnsiblePlugin
33
from unittest.mock import patch, mock_open
44

5+
@pytest.fixture
6+
def project_tmp_path(tmp_path_factory):
7+
return tmp_path_factory.mktemp("iac_tests", numbered=True)
8+
59
@pytest.fixture
610
def ansible_plugin():
711
"""Fixture for the AnsiblePlugin."""
@@ -58,10 +62,10 @@ def ansible_plugin():
5862
"""
5963

6064
@pytest.fixture
61-
def ansible_test_env(tmp_path):
65+
def ansible_test_env(project_tmp_path):
6266
"""Creates a temporary ansible environment with a playbook and inventory."""
63-
playbook_path = tmp_path / "playbook.yml"
64-
inventory_path = tmp_path / "hosts.ini"
67+
playbook_path = project_tmp_path / "playbook.yml"
68+
inventory_path = project_tmp_path / "hosts.ini"
6569

6670
playbook_path.write_text(SAMPLE_PLAYBOOK_CONTENT)
6771
inventory_path.write_text(SAMPLE_INVENTORY_CONTENT_WITH_VARS)
@@ -73,9 +77,10 @@ def test_plugin_name_and_description(ansible_plugin):
7377
assert ansible_plugin.name == "ansible"
7478
assert "Ansible playbooks and inventories" in ansible_plugin.description
7579

76-
def test_parse_iac_config_success_with_vars(ansible_plugin, ansible_test_env):
80+
def test_parse_iac_config_success_with_vars(ansible_plugin, ansible_test_env, project_tmp_path):
7781
"""Tests successful parsing of a playbook and its inventory including host variables."""
78-
parsed_data = ansible_plugin.parse_iac_config(str(ansible_test_env))
82+
with patch("threat_analysis.iac_plugins.ansible_plugin._validate_path_within_project", return_value=ansible_test_env) as mock_validate:
83+
parsed_data = ansible_plugin.parse_iac_config(str(ansible_test_env))
7984

8085
assert "inventory" in parsed_data
8186
assert "playbook" in parsed_data
@@ -92,21 +97,23 @@ def test_parse_iac_config_success_with_vars(ansible_plugin, ansible_test_env):
9297
playbook = parsed_data["playbook"]
9398
assert playbook[0]["name"] == "Configure web server"
9499

95-
def test_parse_iac_config_inventory_not_found(ansible_plugin, tmp_path):
100+
def test_parse_iac_config_inventory_not_found(ansible_plugin, project_tmp_path):
96101
"""Tests that parsing fails if the inventory file is missing."""
97-
playbook_path = tmp_path / "playbook.yml"
102+
playbook_path = project_tmp_path / "playbook.yml"
98103
playbook_path.write_text(SAMPLE_PLAYBOOK_CONTENT)
99104

100-
with pytest.raises(FileNotFoundError, match="Inventory file not found"):
101-
ansible_plugin.parse_iac_config(str(playbook_path))
105+
with patch("threat_analysis.iac_plugins.ansible_plugin._validate_path_within_project", return_value=playbook_path) as mock_validate:
106+
with pytest.raises(FileNotFoundError, match="Inventory file not found"):
107+
ansible_plugin.parse_iac_config(str(playbook_path))
102108

103-
def test_parse_iac_config_unsupported_file_type(ansible_plugin, tmp_path):
109+
def test_parse_iac_config_unsupported_file_type(ansible_plugin, project_tmp_path):
104110
"""Tests that parsing fails for unsupported playbook file types."""
105-
unsupported_file = tmp_path / "playbook.txt"
111+
unsupported_file = project_tmp_path / "playbook.txt"
106112
unsupported_file.write_text("This is not a playbook.")
107113

108-
with pytest.raises(ValueError, match="Unsupported Ansible config path"):
109-
ansible_plugin.parse_iac_config(str(unsupported_file))
114+
with patch("threat_analysis.iac_plugins.ansible_plugin._validate_path_within_project", return_value=unsupported_file) as mock_validate:
115+
with pytest.raises(ValueError, match="Unsupported Ansible config path"):
116+
ansible_plugin.parse_iac_config(str(unsupported_file))
110117

111118
def test_generate_threat_model_components(ansible_plugin):
112119
"""Tests the generation of Markdown components from parsed data."""

0 commit comments

Comments
 (0)