womm-cli 4.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- womm/__init__.py +53 -0
- womm/__main__.py +48 -0
- womm/_version.py +10 -0
- womm/assets/copier/meta/copier.yml +15 -0
- womm/assets/copier/meta/template/README.md.jinja +32 -0
- womm/assets/copier/meta/template/copier.yml.jinja +13 -0
- womm/assets/copier/meta/template/template/.gitkeep +0 -0
- womm/assets/copier/official/javascript/copier.yml +104 -0
- womm/assets/copier/official/javascript/template/.gitignore.jinja +35 -0
- womm/assets/copier/official/javascript/template/.nvmrc.jinja +1 -0
- womm/assets/copier/official/javascript/template/.prettierignore.jinja +10 -0
- womm/assets/copier/official/javascript/template/README.md.jinja +81 -0
- womm/assets/copier/official/javascript/template/biome.json.jinja +54 -0
- womm/assets/copier/official/javascript/template/eslint.config.mjs.jinja +51 -0
- womm/assets/copier/official/javascript/template/index.html.jinja +12 -0
- womm/assets/copier/official/javascript/template/package.json.jinja +88 -0
- womm/assets/copier/official/javascript/template/prettier.config.mjs.jinja +8 -0
- womm/assets/copier/official/javascript/template/src/App.jsx.jinja +10 -0
- womm/assets/copier/official/javascript/template/src/App.tsx.jinja +10 -0
- womm/assets/copier/official/javascript/template/src/App.vue.jinja +18 -0
- womm/assets/copier/official/javascript/template/src/index.jsx.jinja +10 -0
- womm/assets/copier/official/javascript/template/src/index.tsx.jinja +11 -0
- womm/assets/copier/official/javascript/template/src/main.js.jinja +21 -0
- womm/assets/copier/official/javascript/template/src/main.ts.jinja +21 -0
- womm/assets/copier/official/javascript/template/tsconfig.json.jinja +35 -0
- womm/assets/copier/official/javascript/template/vite.config.mjs.jinja +10 -0
- womm/assets/copier/official/javascript/template/{% if with_ci %}.github{% endif %}/workflows/ci.yml.jinja +63 -0
- womm/assets/copier/official/javascript/template/{% if with_tests %}tests{% endif %}/smoke.test.js.jinja +12 -0
- womm/assets/copier/official/javascript/template/{% if with_tests %}tests{% endif %}/smoke.test.ts.jinja +12 -0
- womm/assets/copier/official/javascript/template/{% if with_vscode %}.vscode{% endif %}/extensions.json.jinja +4 -0
- womm/assets/copier/official/javascript/template/{% if with_vscode %}.vscode{% endif %}/settings.json.jinja +27 -0
- womm/assets/copier/official/python/copier.yml +107 -0
- womm/assets/copier/official/python/template/.gitignore.jinja +40 -0
- womm/assets/copier/official/python/template/.pre-commit-config.yaml.jinja +19 -0
- womm/assets/copier/official/python/template/.python-version.jinja +1 -0
- womm/assets/copier/official/python/template/README.md.jinja +83 -0
- womm/assets/copier/official/python/template/pyproject.toml.jinja +114 -0
- womm/assets/copier/official/python/template/{% if framework == 'django' %}manage.py{% endif %}.jinja +23 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/__init__.py.jinja +3 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/asgi.py.jinja +9 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/__init__.py.jinja +0 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/apps.py.jinja +10 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/models.py.jinja +1 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/views.py.jinja +8 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/py.typed +0 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/settings.py.jinja +81 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/urls.py.jinja +11 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/wsgi.py.jinja +9 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/__init__.py.jinja +3 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/asgi.py.jinja +9 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/__init__.py.jinja +0 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/apps.py.jinja +10 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/models.py.jinja +1 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/views.py.jinja +8 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/py.typed +0 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/settings.py.jinja +81 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/urls.py.jinja +11 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/wsgi.py.jinja +9 -0
- womm/assets/copier/official/python/template/{% if with_ci %}.github{% endif %}/workflows/ci.yml.jinja +78 -0
- womm/assets/copier/official/python/template/{% if with_tests %}tests{% endif %}/test_smoke.py.jinja +16 -0
- womm/assets/copier/official/python/template/{% if with_vscode %}.vscode{% endif %}/extensions.json.jinja +8 -0
- womm/assets/copier/official/python/template/{% if with_vscode %}.vscode{% endif %}/settings.json.jinja +28 -0
- womm/bin/refresh_env.cmd +130 -0
- womm/cli.py +111 -0
- womm/commands/__init__.py +30 -0
- womm/commands/core/__init__.py +29 -0
- womm/commands/core/doctor.py +52 -0
- womm/commands/core/path.py +337 -0
- womm/commands/project/__init__.py +31 -0
- womm/commands/project/create.py +128 -0
- womm/commands/project/setup.py +438 -0
- womm/commands/project/template.py +160 -0
- womm/commands/system/__init__.py +31 -0
- womm/commands/system/context/__init__.py +82 -0
- womm/commands/system/context/backup.py +344 -0
- womm/commands/system/context/inventory.py +111 -0
- womm/commands/system/context/registration.py +290 -0
- womm/commands/system/deps.py +165 -0
- womm/commands/system/system.py +130 -0
- womm/exceptions/__init__.py +69 -0
- womm/exceptions/common/__init__.py +55 -0
- womm/exceptions/common/command_service.py +91 -0
- womm/exceptions/common/file_service.py +60 -0
- womm/exceptions/common/security_service.py +44 -0
- womm/exceptions/common/validation_service.py +70 -0
- womm/exceptions/context/__init__.py +27 -0
- womm/exceptions/context/context_service.py +55 -0
- womm/exceptions/project/__init__.py +29 -0
- womm/exceptions/project/project_service.py +59 -0
- womm/exceptions/system/__init__.py +25 -0
- womm/exceptions/system/system_service.py +53 -0
- womm/interfaces/__init__.py +71 -0
- womm/interfaces/context/__init__.py +34 -0
- womm/interfaces/context/menu_interface.py +686 -0
- womm/interfaces/context/registry_interface.py +562 -0
- womm/interfaces/context/script_detector_interface.py +208 -0
- womm/interfaces/context/script_registrar.py +264 -0
- womm/interfaces/core/__init__.py +26 -0
- womm/interfaces/core/doctor_interface.py +102 -0
- womm/interfaces/dependencies/__init__.py +28 -0
- womm/interfaces/dependencies/deps_interface.py +160 -0
- womm/interfaces/project/__init__.py +36 -0
- womm/interfaces/project/create_interface.py +162 -0
- womm/interfaces/project/detection_interface.py +181 -0
- womm/interfaces/project/manager_interface.py +99 -0
- womm/interfaces/project/setup_interface.py +477 -0
- womm/interfaces/project/template_store_interface.py +196 -0
- womm/interfaces/system/__init__.py +32 -0
- womm/interfaces/system/detector_interface.py +84 -0
- womm/interfaces/system/environment_interface.py +110 -0
- womm/interfaces/system/path_interface.py +570 -0
- womm/services/__init__.py +70 -0
- womm/services/common/__init__.py +33 -0
- womm/services/common/base_validation_service.py +450 -0
- womm/services/common/command_runner_service.py +647 -0
- womm/services/common/file_scanner_service.py +483 -0
- womm/services/common/security_validator_service.py +348 -0
- womm/services/context/__init__.py +37 -0
- womm/services/context/backup_entries.py +148 -0
- womm/services/context/parameters.py +515 -0
- womm/services/context/registry_paths.py +56 -0
- womm/services/context/registry_service.py +573 -0
- womm/services/context/validation_service.py +691 -0
- womm/services/dependencies/__init__.py +29 -0
- womm/services/dependencies/probe.py +102 -0
- womm/services/project/__init__.py +48 -0
- womm/services/project/copier_project_creation_service.py +111 -0
- womm/services/project/destination_guard.py +96 -0
- womm/services/project/detection_service.py +354 -0
- womm/services/project/env_utils.py +333 -0
- womm/services/project/javascript_environment_service.py +348 -0
- womm/services/project/python_environment_service.py +245 -0
- womm/services/project/setup_profile_resolver.py +146 -0
- womm/services/project/template_store_service.py +344 -0
- womm/services/project/validation_service.py +418 -0
- womm/services/system/__init__.py +32 -0
- womm/services/system/detector_service.py +704 -0
- womm/services/system/environment_service.py +494 -0
- womm/services/system/path_service.py +642 -0
- womm/shared/__init__.py +30 -0
- womm/shared/configs/__init__.py +51 -0
- womm/shared/configs/context/__init__.py +43 -0
- womm/shared/configs/context/context_config.py +125 -0
- womm/shared/configs/context/context_file_types_config.py +159 -0
- womm/shared/configs/context/context_limits_config.py +187 -0
- womm/shared/configs/context/context_paths_config.py +115 -0
- womm/shared/configs/context/context_types_config.py +65 -0
- womm/shared/configs/context/script_config.py +219 -0
- womm/shared/configs/dependencies/__init__.py +32 -0
- womm/shared/configs/dependencies/runtime_config.py +46 -0
- womm/shared/configs/dependencies/runtime_package_manager_config.py +48 -0
- womm/shared/configs/logging_config.py +89 -0
- womm/shared/configs/project/__init__.py +33 -0
- womm/shared/configs/project/project_config.py +109 -0
- womm/shared/configs/project/project_structure_config.py +88 -0
- womm/shared/configs/project/python_project_config.py +165 -0
- womm/shared/configs/security/__init__.py +26 -0
- womm/shared/configs/security/security_patterns_config.py +242 -0
- womm/shared/configs/system/__init__.py +28 -0
- womm/shared/configs/system/system_detector_config.py +210 -0
- womm/shared/configs/system/system_environment_config.py +101 -0
- womm/shared/models/__init__.py +13 -0
- womm/shared/models/template_source.py +65 -0
- womm/shared/paths.py +192 -0
- womm/shared/results/__init__.py +138 -0
- womm/shared/results/base.py +85 -0
- womm/shared/results/command_results.py +61 -0
- womm/shared/results/context_results.py +345 -0
- womm/shared/results/dependency_results.py +110 -0
- womm/shared/results/doctor_results.py +41 -0
- womm/shared/results/file_results.py +97 -0
- womm/shared/results/project_results.py +163 -0
- womm/shared/results/security_results.py +128 -0
- womm/shared/results/system_results.py +268 -0
- womm/shared/results/template_results.py +43 -0
- womm/shared/runtime.py +36 -0
- womm/ui/__init__.py +31 -0
- womm/ui/common/__init__.py +39 -0
- womm/ui/common/interactive_menu.py +246 -0
- womm/ui/common/logging.py +184 -0
- womm/ui/common/prompts.py +93 -0
- womm/ui/context/__init__.py +31 -0
- womm/ui/context/display.py +485 -0
- womm/ui/context/menu_display.py +189 -0
- womm/ui/context/wizard.py +557 -0
- womm/ui/project/__init__.py +42 -0
- womm/ui/project/create/create_completion_summaries.py +55 -0
- womm/ui/project/project_wizard.py +174 -0
- womm/ui/project/setup/setup_completion_summaries.py +111 -0
- womm/ui/project/templates/template_store_display.py +128 -0
- womm/ui/system/__init__.py +60 -0
- womm/ui/system/display.py +531 -0
- womm/ui/system/path_display.py +387 -0
- womm/utils/__init__.py +23 -0
- womm/utils/common/__init__.py +49 -0
- womm/utils/common/file_scanner_utils.py +110 -0
- womm/utils/common/path_resolver_utils.py +127 -0
- womm/utils/common/safe_fs_utils.py +135 -0
- womm/utils/context/__init__.py +54 -0
- womm/utils/context/core_utils.py +345 -0
- womm/utils/context/icon_utils.py +259 -0
- womm/utils/project/__init__.py +106 -0
- womm/utils/project/asset_utils.py +154 -0
- womm/utils/project/core_utils.py +391 -0
- womm/utils/project/file_utils.py +494 -0
- womm/utils/project/platform_utils.py +170 -0
- womm/utils/project/validation_utils.py +273 -0
- womm/utils/security/__init__.py +46 -0
- womm/utils/security/security_validation_utils.py +258 -0
- womm/utils/system/__init__.py +69 -0
- womm/utils/system/detector_utils.py +299 -0
- womm/utils/system/environment_utils.py +172 -0
- womm/utils/system/path_utils.py +101 -0
- womm_cli-4.0.0.dist-info/METADATA +203 -0
- womm_cli-4.0.0.dist-info/RECORD +218 -0
- womm_cli-4.0.0.dist-info/WHEEL +4 -0
- womm_cli-4.0.0.dist-info/entry_points.txt +2 -0
- womm_cli-4.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# ///////////////////////////////////////////////////////////////
|
|
3
|
+
# SECURITY VALIDATOR SERVICE - Security Validation Service
|
|
4
|
+
# Project: works-on-my-machine
|
|
5
|
+
# ///////////////////////////////////////////////////////////////
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
Security Validator Service - Singleton service for security validation.
|
|
9
|
+
|
|
10
|
+
Handles security validation for command execution and file operations.
|
|
11
|
+
Provides comprehensive security checks to prevent dangerous operations.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
# ///////////////////////////////////////////////////////////////
|
|
17
|
+
# IMPORTS
|
|
18
|
+
# ///////////////////////////////////////////////////////////////
|
|
19
|
+
# Standard library imports
|
|
20
|
+
import logging
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from threading import Lock
|
|
23
|
+
from typing import ClassVar
|
|
24
|
+
|
|
25
|
+
# Local imports
|
|
26
|
+
from ...exceptions.common import SecurityServiceError
|
|
27
|
+
from ...shared.configs.security import SecurityPatternsConfig
|
|
28
|
+
from ...shared.results import (
|
|
29
|
+
CommandValidationResult,
|
|
30
|
+
PathValidationResult,
|
|
31
|
+
SecurityReportResult,
|
|
32
|
+
)
|
|
33
|
+
from ...utils.security import (
|
|
34
|
+
has_dangerous_command_patterns,
|
|
35
|
+
has_dangerous_file_patterns,
|
|
36
|
+
has_excessive_traversal,
|
|
37
|
+
is_dangerous_argument,
|
|
38
|
+
is_system_directory,
|
|
39
|
+
validate_permission_command,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# ///////////////////////////////////////////////////////////////
|
|
43
|
+
# SECURITY VALIDATOR SERVICE (SINGLETON)
|
|
44
|
+
# ///////////////////////////////////////////////////////////////
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class SecurityValidatorService:
|
|
48
|
+
"""Singleton service for validating commands and file paths for security concerns."""
|
|
49
|
+
|
|
50
|
+
_instance: ClassVar[SecurityValidatorService | None] = None
|
|
51
|
+
_initialized: ClassVar[bool] = False
|
|
52
|
+
_lock: ClassVar[Lock] = Lock()
|
|
53
|
+
|
|
54
|
+
def __new__(cls) -> SecurityValidatorService:
|
|
55
|
+
"""Create or return the singleton instance.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
SecurityValidatorService: The singleton instance
|
|
59
|
+
"""
|
|
60
|
+
with cls._lock:
|
|
61
|
+
if cls._instance is None:
|
|
62
|
+
cls._instance = super().__new__(cls)
|
|
63
|
+
return cls._instance
|
|
64
|
+
|
|
65
|
+
def __init__(self) -> None:
|
|
66
|
+
"""Initialize security validator service (only once)."""
|
|
67
|
+
if SecurityValidatorService._initialized:
|
|
68
|
+
return
|
|
69
|
+
|
|
70
|
+
self.logger = logging.getLogger(__name__)
|
|
71
|
+
SecurityValidatorService._initialized = True
|
|
72
|
+
|
|
73
|
+
# ///////////////////////////////////////////////////////////////
|
|
74
|
+
# PUBLIC METHODS
|
|
75
|
+
# ///////////////////////////////////////////////////////////////
|
|
76
|
+
|
|
77
|
+
def validate_command(self, command: list[str]) -> CommandValidationResult:
|
|
78
|
+
"""Validate a command for security concerns.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
command: Command to validate as list of strings
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
CommandValidationResult: Result with validation status and reason
|
|
85
|
+
|
|
86
|
+
Raises:
|
|
87
|
+
SecurityServiceError: If command validation fails
|
|
88
|
+
SecurityServiceError: If unexpected error occurs during validation
|
|
89
|
+
"""
|
|
90
|
+
try:
|
|
91
|
+
if not command:
|
|
92
|
+
return CommandValidationResult(
|
|
93
|
+
success=False,
|
|
94
|
+
message="Command validation failed",
|
|
95
|
+
command="",
|
|
96
|
+
is_valid=False,
|
|
97
|
+
validation_reason="Command cannot be empty",
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
if not isinstance(command, list):
|
|
101
|
+
return CommandValidationResult(
|
|
102
|
+
success=False,
|
|
103
|
+
message="Command validation failed",
|
|
104
|
+
command=str(command),
|
|
105
|
+
is_valid=False,
|
|
106
|
+
validation_reason=f"Command must be a list, got: {type(command)}",
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
if not all(isinstance(arg, str) for arg in command):
|
|
110
|
+
return CommandValidationResult(
|
|
111
|
+
success=False,
|
|
112
|
+
message="Command validation failed",
|
|
113
|
+
command=str(command),
|
|
114
|
+
is_valid=False,
|
|
115
|
+
validation_reason="All command arguments must be strings",
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# Get the base command (first argument)
|
|
119
|
+
base_command = command[0].lower()
|
|
120
|
+
|
|
121
|
+
# Check if command is in whitelist
|
|
122
|
+
if base_command not in SecurityPatternsConfig.ALLOWED_COMMANDS:
|
|
123
|
+
return CommandValidationResult(
|
|
124
|
+
success=False,
|
|
125
|
+
message="Command validation failed",
|
|
126
|
+
command=" ".join(command),
|
|
127
|
+
is_valid=False,
|
|
128
|
+
validation_reason=f"Command '{base_command}' is not in allowed list",
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
# Check for dangerous patterns in the base command
|
|
132
|
+
if has_dangerous_command_patterns(base_command):
|
|
133
|
+
return CommandValidationResult(
|
|
134
|
+
success=False,
|
|
135
|
+
message="Command validation failed",
|
|
136
|
+
command=" ".join(command),
|
|
137
|
+
is_valid=False,
|
|
138
|
+
validation_reason=f"Command '{base_command}' contains dangerous patterns",
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# Check for dangerous arguments
|
|
142
|
+
for arg in command[1:]:
|
|
143
|
+
if is_dangerous_argument(base_command, arg):
|
|
144
|
+
return CommandValidationResult(
|
|
145
|
+
success=False,
|
|
146
|
+
message="Command validation failed",
|
|
147
|
+
command=" ".join(command),
|
|
148
|
+
is_valid=False,
|
|
149
|
+
validation_reason=f"Dangerous argument '{arg}' for command '{base_command}'",
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# Additional command-specific validations
|
|
153
|
+
if base_command in ["chmod", "chown"] and not validate_permission_command(
|
|
154
|
+
command
|
|
155
|
+
):
|
|
156
|
+
return CommandValidationResult(
|
|
157
|
+
success=False,
|
|
158
|
+
message="Command validation failed",
|
|
159
|
+
command=" ".join(command),
|
|
160
|
+
is_valid=False,
|
|
161
|
+
validation_reason=f"Invalid permission command: {' '.join(command)}",
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
return CommandValidationResult(
|
|
165
|
+
success=True,
|
|
166
|
+
message="Command validated successfully",
|
|
167
|
+
command=" ".join(command),
|
|
168
|
+
is_valid=True,
|
|
169
|
+
validation_reason="Command passed all security checks",
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
except SecurityServiceError:
|
|
173
|
+
# Re-raise security exceptions as-is
|
|
174
|
+
raise
|
|
175
|
+
except (OSError, TypeError, ValueError) as e:
|
|
176
|
+
# Catch unexpected errors and wrap them
|
|
177
|
+
raise SecurityServiceError(
|
|
178
|
+
message=f"Unexpected error during command validation: {e}",
|
|
179
|
+
details=f"Exception type: {type(e).__name__}, Command: {command}",
|
|
180
|
+
) from e
|
|
181
|
+
|
|
182
|
+
def validate_file_path(self, file_path: str) -> PathValidationResult:
|
|
183
|
+
"""Validate a file path for security concerns.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
file_path: File path to validate
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
PathValidationResult: Result with validation status and reason
|
|
190
|
+
|
|
191
|
+
Raises:
|
|
192
|
+
SecurityServiceError: If file path validation fails
|
|
193
|
+
SecurityServiceError: If unexpected error occurs during validation
|
|
194
|
+
"""
|
|
195
|
+
try:
|
|
196
|
+
if not file_path:
|
|
197
|
+
return PathValidationResult(
|
|
198
|
+
success=False,
|
|
199
|
+
message="Path validation failed",
|
|
200
|
+
path="",
|
|
201
|
+
is_valid=False,
|
|
202
|
+
validation_reason="File path cannot be empty",
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
if not isinstance(file_path, str):
|
|
206
|
+
return PathValidationResult(
|
|
207
|
+
success=False,
|
|
208
|
+
message="Path validation failed",
|
|
209
|
+
path=str(file_path),
|
|
210
|
+
is_valid=False,
|
|
211
|
+
validation_reason=f"File path must be a string, got: {type(file_path)}",
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
# Check for dangerous patterns
|
|
215
|
+
if has_dangerous_file_patterns(file_path):
|
|
216
|
+
return PathValidationResult(
|
|
217
|
+
success=False,
|
|
218
|
+
message="Path validation failed",
|
|
219
|
+
path=file_path,
|
|
220
|
+
is_valid=False,
|
|
221
|
+
validation_reason="File path contains dangerous patterns",
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
# Check for directory traversal attempts
|
|
225
|
+
if has_excessive_traversal(file_path):
|
|
226
|
+
traversal_count = file_path.count("..")
|
|
227
|
+
return PathValidationResult(
|
|
228
|
+
success=False,
|
|
229
|
+
message="Path validation failed",
|
|
230
|
+
path=file_path,
|
|
231
|
+
is_valid=False,
|
|
232
|
+
validation_reason=f"Excessive directory traversal: {traversal_count} levels",
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
# Check for absolute paths to system directories
|
|
236
|
+
try:
|
|
237
|
+
path = Path(file_path)
|
|
238
|
+
if path.is_absolute() and is_system_directory(path):
|
|
239
|
+
return PathValidationResult(
|
|
240
|
+
success=False,
|
|
241
|
+
message="Path validation failed",
|
|
242
|
+
path=file_path,
|
|
243
|
+
is_valid=False,
|
|
244
|
+
validation_reason=f"Access to system directory: {file_path}",
|
|
245
|
+
)
|
|
246
|
+
except (OSError, ValueError):
|
|
247
|
+
return PathValidationResult(
|
|
248
|
+
success=False,
|
|
249
|
+
message="Path validation failed",
|
|
250
|
+
path=file_path,
|
|
251
|
+
is_valid=False,
|
|
252
|
+
validation_reason=f"Invalid file path format: {file_path}",
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
return PathValidationResult(
|
|
256
|
+
success=True,
|
|
257
|
+
message="Path validated successfully",
|
|
258
|
+
path=file_path,
|
|
259
|
+
is_valid=True,
|
|
260
|
+
validation_reason="Path passed all security checks",
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
except SecurityServiceError:
|
|
264
|
+
# Re-raise security exceptions as-is
|
|
265
|
+
raise
|
|
266
|
+
except (OSError, TypeError, ValueError) as e:
|
|
267
|
+
# Catch unexpected errors and wrap them
|
|
268
|
+
raise SecurityServiceError(
|
|
269
|
+
message=f"Unexpected error during file path validation: {e}",
|
|
270
|
+
details=f"Exception type: {type(e).__name__}, Path: {file_path}",
|
|
271
|
+
) from e
|
|
272
|
+
|
|
273
|
+
def validate_directory_path(self, dir_path: str) -> PathValidationResult:
|
|
274
|
+
"""Validate a directory path for security concerns.
|
|
275
|
+
|
|
276
|
+
Args:
|
|
277
|
+
dir_path: Directory path to validate
|
|
278
|
+
|
|
279
|
+
Returns:
|
|
280
|
+
PathValidationResult: Result with validation status and reason
|
|
281
|
+
|
|
282
|
+
Raises:
|
|
283
|
+
SecurityServiceError: If directory path validation fails
|
|
284
|
+
SecurityUtilityError: If unexpected error occurs during validation
|
|
285
|
+
"""
|
|
286
|
+
# Use the same validation as file paths
|
|
287
|
+
return self.validate_file_path(dir_path)
|
|
288
|
+
|
|
289
|
+
def get_security_report(self, command: list[str]) -> SecurityReportResult:
|
|
290
|
+
"""Get a detailed security report for a command.
|
|
291
|
+
|
|
292
|
+
Args:
|
|
293
|
+
command: Command to analyze
|
|
294
|
+
|
|
295
|
+
Returns:
|
|
296
|
+
SecurityReportResult: Result with security report
|
|
297
|
+
|
|
298
|
+
Raises:
|
|
299
|
+
SecurityServiceError: If unexpected error occurs during analysis
|
|
300
|
+
"""
|
|
301
|
+
try:
|
|
302
|
+
# Try to validate the command
|
|
303
|
+
validation_result = self.validate_command(command)
|
|
304
|
+
is_safe = validation_result.is_valid
|
|
305
|
+
reason = validation_result.validation_reason
|
|
306
|
+
|
|
307
|
+
checks_performed = []
|
|
308
|
+
|
|
309
|
+
if command:
|
|
310
|
+
base_command = command[0].lower()
|
|
311
|
+
checks_performed.append(
|
|
312
|
+
f"Command '{base_command}' in whitelist: "
|
|
313
|
+
f"{base_command in SecurityPatternsConfig.ALLOWED_COMMANDS}"
|
|
314
|
+
)
|
|
315
|
+
checks_performed.append(
|
|
316
|
+
f"Command has dangerous patterns: "
|
|
317
|
+
f"{has_dangerous_command_patterns(base_command)}"
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
# Check each argument
|
|
321
|
+
for i, arg in enumerate(command[1:], 1):
|
|
322
|
+
is_dangerous = is_dangerous_argument(base_command, arg)
|
|
323
|
+
checks_performed.append(
|
|
324
|
+
f"Argument {i} '{arg}' is dangerous: {is_dangerous}"
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
return SecurityReportResult(
|
|
328
|
+
success=True,
|
|
329
|
+
message="Security report generated successfully",
|
|
330
|
+
command=" ".join(command),
|
|
331
|
+
is_safe=is_safe,
|
|
332
|
+
reason=reason,
|
|
333
|
+
base_command=command[0] if command else "",
|
|
334
|
+
arguments=command[1:] if len(command) > 1 else [],
|
|
335
|
+
checks_performed=checks_performed,
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
except (OSError, SecurityServiceError, TypeError, ValueError) as e:
|
|
339
|
+
return SecurityReportResult(
|
|
340
|
+
success=False,
|
|
341
|
+
error=f"Unexpected error during security report generation: {e}",
|
|
342
|
+
command=" ".join(command) if command else "",
|
|
343
|
+
is_safe=False,
|
|
344
|
+
reason=str(e),
|
|
345
|
+
base_command=command[0] if command else "",
|
|
346
|
+
arguments=command[1:] if len(command) > 1 else [],
|
|
347
|
+
checks_performed=[],
|
|
348
|
+
)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# ///////////////////////////////////////////////////////////////
|
|
3
|
+
# SERVICES CONTEXT - Context Services
|
|
4
|
+
# Project: works-on-my-machine
|
|
5
|
+
# ///////////////////////////////////////////////////////////////
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
Context services for WOMM CLI.
|
|
9
|
+
|
|
10
|
+
This module provides services for managing Windows context menu operations,
|
|
11
|
+
including parameter handling, registry operations, and validation.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
# ///////////////////////////////////////////////////////////////
|
|
17
|
+
# IMPORTS
|
|
18
|
+
# ///////////////////////////////////////////////////////////////
|
|
19
|
+
# Local imports
|
|
20
|
+
from .backup_entries import ContextBackupEntriesReader
|
|
21
|
+
from .parameters import ContextParameters, ContextType
|
|
22
|
+
from .registry_paths import context_type_from_registry_path
|
|
23
|
+
from .registry_service import ContextRegistryService
|
|
24
|
+
from .validation_service import ContextValidationService
|
|
25
|
+
|
|
26
|
+
# ///////////////////////////////////////////////////////////////
|
|
27
|
+
# PUBLIC API
|
|
28
|
+
# ///////////////////////////////////////////////////////////////
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"ContextBackupEntriesReader",
|
|
32
|
+
"ContextParameters",
|
|
33
|
+
"ContextRegistryService",
|
|
34
|
+
"ContextType",
|
|
35
|
+
"ContextValidationService",
|
|
36
|
+
"context_type_from_registry_path",
|
|
37
|
+
]
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# ///////////////////////////////////////////////////////////////
|
|
3
|
+
# CONTEXT BACKUP ENTRIES - Backup file reading and entry normalization
|
|
4
|
+
# Project: works-on-my-machine
|
|
5
|
+
# ///////////////////////////////////////////////////////////////
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
Reading and normalization of context menu entries stored in backup files.
|
|
9
|
+
|
|
10
|
+
This module owns everything about *what* a backup file contains: locating the
|
|
11
|
+
files, tolerating the legacy list format, de-duplicating entries by key name,
|
|
12
|
+
and pulling a script path back out of a stored command. It never formats
|
|
13
|
+
anything for display and never touches the registry.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
# ///////////////////////////////////////////////////////////////
|
|
19
|
+
# IMPORTS
|
|
20
|
+
# ///////////////////////////////////////////////////////////////
|
|
21
|
+
# Standard library imports
|
|
22
|
+
import json
|
|
23
|
+
import logging
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
# ///////////////////////////////////////////////////////////////
|
|
27
|
+
# CONSTANTS
|
|
28
|
+
# ///////////////////////////////////////////////////////////////
|
|
29
|
+
|
|
30
|
+
BACKUP_FILE_PATTERN = "context_menu_backup_*.json"
|
|
31
|
+
BACKUP_CONTEXT_TYPES = ("directory", "background", "file", "files", "root")
|
|
32
|
+
|
|
33
|
+
# ///////////////////////////////////////////////////////////////
|
|
34
|
+
# CLASSES
|
|
35
|
+
# ///////////////////////////////////////////////////////////////
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class ContextBackupEntriesReader:
|
|
39
|
+
"""Reads context menu entries out of backup files."""
|
|
40
|
+
|
|
41
|
+
def __init__(self, logger: logging.Logger | None = None):
|
|
42
|
+
"""
|
|
43
|
+
Initialize the reader.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
logger: Logger used for unreadable backup files
|
|
47
|
+
"""
|
|
48
|
+
self.logger = logger or logging.getLogger(__name__)
|
|
49
|
+
|
|
50
|
+
def collect_entries(self, backup_dir: Path) -> list[dict]:
|
|
51
|
+
"""
|
|
52
|
+
Collect all unique context menu entries from a backup directory.
|
|
53
|
+
|
|
54
|
+
Entries are tagged with their source file (``_source_backup``) and the
|
|
55
|
+
context type they were stored under (``_context_type``). The first
|
|
56
|
+
occurrence of a key name wins; unreadable files are skipped.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
backup_dir: Directory holding the backup files
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
List of unique entries with metadata
|
|
63
|
+
"""
|
|
64
|
+
all_entries: dict[str, dict] = {}
|
|
65
|
+
|
|
66
|
+
for backup_file in sorted(backup_dir.glob(BACKUP_FILE_PATTERN)):
|
|
67
|
+
try:
|
|
68
|
+
with open(backup_file, encoding="utf-8") as handle:
|
|
69
|
+
data = json.load(handle)
|
|
70
|
+
except (OSError, ValueError) as e:
|
|
71
|
+
self.logger.debug(f"Error reading {backup_file.name}: {e}")
|
|
72
|
+
continue
|
|
73
|
+
|
|
74
|
+
for context_type, entry in self._iter_entries(data):
|
|
75
|
+
key_name = entry.get("key_name")
|
|
76
|
+
if key_name and key_name not in all_entries:
|
|
77
|
+
entry["_source_backup"] = backup_file.name
|
|
78
|
+
entry["_context_type"] = context_type
|
|
79
|
+
all_entries[key_name] = entry
|
|
80
|
+
|
|
81
|
+
return list(all_entries.values())
|
|
82
|
+
|
|
83
|
+
@staticmethod
|
|
84
|
+
def filter_available(all_entries: list[dict], current_keys: set[str]) -> list[dict]:
|
|
85
|
+
"""
|
|
86
|
+
Filter out entries that are already installed.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
all_entries: All entries from backups
|
|
90
|
+
current_keys: Set of currently installed entry keys
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
List of entries not yet installed
|
|
94
|
+
"""
|
|
95
|
+
return [
|
|
96
|
+
entry
|
|
97
|
+
for entry in all_entries
|
|
98
|
+
if entry.get("key_name") and entry.get("key_name") not in current_keys
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
@staticmethod
|
|
102
|
+
def extract_script_path(command: str) -> str | None:
|
|
103
|
+
"""
|
|
104
|
+
Extract an existing script path from a stored command line.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
command: Command line as stored in the backup
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
The quoted path if it exists on disk, otherwise ``None``.
|
|
111
|
+
"""
|
|
112
|
+
if '"' not in command:
|
|
113
|
+
return None
|
|
114
|
+
candidate = command.split('"')[1]
|
|
115
|
+
if candidate and Path(candidate).exists():
|
|
116
|
+
return candidate
|
|
117
|
+
return None
|
|
118
|
+
|
|
119
|
+
# ///////////////////////////////////////////////////////////////
|
|
120
|
+
# PRIVATE METHODS
|
|
121
|
+
# ///////////////////////////////////////////////////////////////
|
|
122
|
+
|
|
123
|
+
@staticmethod
|
|
124
|
+
def _iter_entries(data: object):
|
|
125
|
+
"""Yield ``(context_type, entry)`` pairs from backup file data."""
|
|
126
|
+
if not isinstance(data, dict):
|
|
127
|
+
return
|
|
128
|
+
entries_dict = data.get("entries", {})
|
|
129
|
+
if not isinstance(entries_dict, dict):
|
|
130
|
+
# Legacy format: a bare list of directory entries
|
|
131
|
+
entries_dict = {
|
|
132
|
+
"directory": entries_dict if isinstance(entries_dict, list) else []
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
for context_type in BACKUP_CONTEXT_TYPES:
|
|
136
|
+
entries = entries_dict.get(context_type, [])
|
|
137
|
+
if not isinstance(entries, list):
|
|
138
|
+
continue
|
|
139
|
+
for entry in entries:
|
|
140
|
+
if isinstance(entry, dict):
|
|
141
|
+
yield context_type, entry
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# ///////////////////////////////////////////////////////////////
|
|
145
|
+
# PUBLIC API
|
|
146
|
+
# ///////////////////////////////////////////////////////////////
|
|
147
|
+
|
|
148
|
+
__all__ = ["ContextBackupEntriesReader"]
|