Skip to content

Commit dc3f6ca

Browse files
committed
fix: improved validation
Signed-off-by: degenaro <lou.degenaro@gmail.com>
1 parent 261ca29 commit dc3f6ca

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

tests/trestle/core/commands/author/jinja_cmd_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ def test_jinja_docs_profile_path_traversal_protection(tmp_trestle_dir: pathlib.P
355355
PathSecurityValidator.validate_local_path(output_file, tmp_trestle_dir)
356356
assert 'Security violation' in str(exc_info.value)
357357

358-
# Test 3: Valid relative path should succeed
358+
# Test 3: Directory creation path traversal should fail
359+
with pytest.raises(TrestleError) as exc_info:
360+
group_dir = tmp_trestle_dir / '../../../etc/malicious'
361+
PathSecurityValidator.validate_local_path(group_dir, tmp_trestle_dir)
362+
assert 'Security violation' in str(exc_info.value)
363+
364+
# Test 4: Valid relative path should succeed
359365
output_file = tmp_trestle_dir / 'controls_output/ac/ac-1.md'
360366
PathSecurityValidator.validate_local_path(output_file, tmp_trestle_dir) # Should not raise

trestle/core/commands/author/jinja.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,11 @@ def jinja_multiple_md(
278278
control_path = catalog_interface.get_control_path(control.id)
279279
for sub_dir in control_path:
280280
group_dir = group_dir / sub_dir
281-
if not group_dir.exists():
282-
group_dir.mkdir(parents=True, exist_ok=True)
281+
# Validate directory path to prevent path traversal before creating directories
282+
full_group_dir = trestle_root / group_dir
283+
PathSecurityValidator.validate_local_path(full_group_dir, trestle_root)
284+
if not full_group_dir.exists():
285+
full_group_dir.mkdir(parents=True, exist_ok=True)
283286

284287
control_writer = DocsControlWriter()
285288

0 commit comments

Comments
 (0)