Skip to content

Commit c1460cf

Browse files
committed
fix linting
1 parent 01b4b6e commit c1460cf

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

codeflash/cli_cmds/cmd_init.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
from codeflash.cli_cmds.cli_common import apologize_and_exit
2727
from codeflash.cli_cmds.console import console, logger
2828
from codeflash.cli_cmds.extension import install_vscode_extension
29+
from codeflash.code_utils.code_utils import validate_relative_directory_path
2930
from codeflash.code_utils.compat import LF
3031
from codeflash.code_utils.config_parser import parse_config_file
3132
from codeflash.code_utils.env_utils import check_formatter_installed, get_codeflash_api_key
3233
from codeflash.code_utils.git_utils import get_git_remotes, get_repo_owner_and_name
3334
from codeflash.code_utils.github_utils import get_github_secrets_page_url
3435
from codeflash.code_utils.oauth_handler import perform_oauth_signin
3536
from codeflash.code_utils.shell_utils import get_shell_rc_path, is_powershell, save_api_key_to_rc
36-
from codeflash.code_utils.code_utils import validate_relative_directory_path
3737
from codeflash.either import is_successful
3838
from codeflash.lsp.helpers import is_LSP_enabled
3939
from codeflash.telemetry.posthog_cf import ph
@@ -372,7 +372,7 @@ def collect_setup_info() -> CLISetupInfo:
372372
custom_answers = inquirer.prompt(custom_questions, theme=CodeflashTheme())
373373
if not custom_answers:
374374
apologize_and_exit()
375-
return # unreachable but satisfies type checker
375+
return None # unreachable but satisfies type checker
376376

377377
custom_path_str = str(custom_answers["custom_path"])
378378
# Validate the path is safe
@@ -455,7 +455,7 @@ def collect_setup_info() -> CLISetupInfo:
455455
custom_tests_answers = inquirer.prompt(custom_tests_questions, theme=CodeflashTheme())
456456
if not custom_tests_answers:
457457
apologize_and_exit()
458-
return # unreachable but satisfies type checker
458+
return None # unreachable but satisfies type checker
459459

460460
custom_tests_path_str = str(custom_tests_answers["custom_tests_path"])
461461
# Validate the path is safe

codeflash/code_utils/code_utils.py

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -380,24 +380,26 @@ def extract_unique_errors(pytest_output: str) -> set[str]:
380380

381381
def validate_relative_directory_path(path: str) -> tuple[bool, str]:
382382
"""Validate that a path is a safe relative directory path.
383-
383+
384384
Prevents path traversal attacks and invalid paths.
385385
Works cross-platform (Windows, Linux, macOS).
386-
386+
387387
Args:
388388
path: The path string to validate
389-
389+
390390
Returns:
391391
tuple[bool, str]: (is_valid, error_message)
392392
- is_valid: True if path is valid, False otherwise
393393
- error_message: Empty string if valid, error description if invalid
394+
394395
"""
396+
# Check for empty path
395397
if not path or not path.strip():
396398
return False, "Path cannot be empty"
397-
399+
398400
# Normalize whitespace
399401
path = path.strip()
400-
402+
401403
# Check for shell commands or dangerous patterns
402404
dangerous_patterns = [
403405
"cd ",
@@ -423,36 +425,26 @@ def validate_relative_directory_path(path: str) -> tuple[bool, str]:
423425
for pattern in dangerous_patterns:
424426
if pattern in path_lower:
425427
return False, f"Path contains invalid characters or commands: {pattern.strip()}"
426-
428+
427429
# Check for path traversal attempts (cross-platform)
428-
# Normalize path separators for checking
429430
normalized = path.replace("\\", "/")
430431
if ".." in normalized:
431432
return False, "Path cannot contain '..' (parent directory traversal)"
432-
433-
# Check for absolute paths (Windows and Unix)
434-
if os.path.isabs(path):
435-
return False, "Path must be relative, not absolute"
436-
437-
# Check for invalid characters (OS-specific)
438-
invalid_chars = set()
439-
if os.name == "nt": # Windows
440-
invalid_chars = {'<', '>', ':', '"', '|', '?', '*'}
441-
else: # Unix-like
442-
invalid_chars = {'\0'}
443-
444-
if any(char in path for char in invalid_chars):
445-
return False, f"Path contains invalid characters for this operating system"
446-
447-
# Validate using pathlib to ensure it's a valid path structure
448-
try:
449-
path_obj = Path(path)
450-
# Check if path would resolve outside the current directory
451-
# This is a safety check for edge cases
452-
parts = path_obj.parts
453-
if any(part == ".." for part in parts):
454-
return False, "Path cannot contain '..' (parent directory traversal)"
455-
except (ValueError, OSError) as e:
456-
return False, f"Invalid path format: {str(e)}"
457-
458-
return True, ""
433+
434+
# Check for absolute paths and invalid characters
435+
invalid_chars = {"<", ">", ":", '"', "|", "?", "*"} if os.name == "nt" else {"\0"}
436+
error_msg = ""
437+
if Path(path).is_absolute():
438+
error_msg = "Path must be relative, not absolute"
439+
elif any(char in path for char in invalid_chars):
440+
error_msg = "Path contains invalid characters for this operating system"
441+
else:
442+
# Validate using pathlib to ensure it's a valid path structure
443+
try:
444+
Path(path)
445+
except (ValueError, OSError) as e:
446+
error_msg = f"Invalid path format: {e!s}"
447+
448+
if error_msg:
449+
return False, error_msg
450+
return True, ""

0 commit comments

Comments
 (0)