from pathlib import Path import pytest from appsec import PathTraversalError, redact_secrets, safe_join def test_safe_join_allows_nested_and_blocks_traversal(tmp_path: Path) -> None: ok = safe_join(tmp_path, "configs/app.yaml") assert ok == (tmp_path / "configs" / "app.yaml").resolve() with pytest.raises(PathTraversalError): safe_join(tmp_path, "../../etc/passwd") with pytest.raises(PathTraversalError): safe_join(tmp_path, "/etc/passwd") def test_redact_secrets_masks_credentials() -> None: line = "auth failed header=Bearer s3cr3tTok3n user=admin password=hunter2 token=abc_123" cleaned = redact_secrets(line) assert "s3cr3tTok3n" not in cleaned assert "hunter2" not in cleaned assert "abc_123" not in cleaned assert "user=admin" in cleaned