-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
442 lines (343 loc) · 12.6 KB
/
Copy pathutils.py
File metadata and controls
442 lines (343 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""
NLPL Security Utilities
This module provides security utilities for safe handling of:
- File paths (path traversal prevention)
- Subprocess execution (command injection prevention)
- Input validation
- Output sanitization
"""
import os
import re
import subprocess
from typing import List, Optional, Union
from pathlib import Path
class SecurityError(Exception):
"""Base class for security-related errors."""
pass
class PathTraversalError(SecurityError):
"""Raised when path traversal attempt is detected."""
pass
class CommandInjectionError(SecurityError):
"""Raised when command injection attempt is detected."""
pass
class ValidationError(SecurityError):
"""Raised when input validation fails."""
pass
# =============================================================================
# Path Validation
# =============================================================================
def normalize_path(path: str) -> str:
"""
Normalize a file path, resolving .. and . components.
Args:
path: Path to normalize
Returns:
Normalized absolute path
"""
return os.path.normpath(os.path.abspath(path))
def validate_path(path: str, allowed_dirs: Optional[List[str]] = None,
allow_absolute: bool = False) -> str:
"""
Validate a file path to prevent path traversal attacks.
Args:
path: Path to validate
allowed_dirs: Optional list of allowed directory prefixes
allow_absolute: Whether to allow absolute paths
Returns:
Validated normalized path
Raises:
PathTraversalError: If path is unsafe
"""
# Normalize the path
normalized = normalize_path(path)
# Check for null bytes (directory traversal trick)
if '\x00' in path:
raise PathTraversalError("Path contains null byte")
# Check if absolute path is disallowed
if not allow_absolute and os.path.isabs(path):
raise PathTraversalError(f"Absolute paths not allowed: {path}")
# Check for path traversal patterns
dangerous_patterns = [
'../',
'..\\',
'..',
]
for pattern in dangerous_patterns:
if pattern in path:
raise PathTraversalError(f"Path contains dangerous pattern '{pattern}': {path}")
# If allowed_dirs specified, ensure path is within one of them
if allowed_dirs:
normalized_dirs = [normalize_path(d) for d in allowed_dirs]
is_allowed = False
for allowed_dir in normalized_dirs:
if normalized.startswith(allowed_dir):
is_allowed = True
break
if not is_allowed:
raise PathTraversalError(
f"Path '{path}' is outside allowed directories: {allowed_dirs}"
)
return normalized
def is_safe_path(path: str, allowed_dirs: Optional[List[str]] = None) -> bool:
"""
Non-throwing version of validate_path.
Args:
path: Path to check
allowed_dirs: Optional list of allowed directories
Returns:
True if path is safe, False otherwise
"""
try:
validate_path(path, allowed_dirs)
return True
except PathTraversalError:
return False
def get_safe_filename(filename: str) -> str:
"""
Sanitize a filename by removing dangerous characters.
Args:
filename: Original filename
Returns:
Sanitized filename safe for filesystem use
"""
# Remove path separators
filename = os.path.basename(filename)
# Remove or replace dangerous characters
# Allow: letters, digits, dots, dashes, underscores
safe_filename = re.sub(r'[^a-zA-Z0-9._-]', '_', filename)
# Prevent hidden files (starting with .)
if safe_filename.startswith('.'):
safe_filename = '_' + safe_filename[1:]
# Prevent empty filename
if not safe_filename:
safe_filename = 'unnamed_file'
return safe_filename
# =============================================================================
# Subprocess Execution (Safe)
# =============================================================================
def safe_execute(program: str, args: List[str],
allowed_programs: Optional[List[str]] = None,
capture_output: bool = True,
timeout: Optional[int] = None) -> subprocess.CompletedProcess:
"""
Execute a subprocess safely without shell expansion.
This function:
- Never uses shell=True (prevents command injection)
- Validates program path
- Passes arguments as list (no string concatenation)
- Supports whitelist of allowed programs
Args:
program: Path or name of program to execute
args: List of arguments (each as separate string)
allowed_programs: Optional whitelist of allowed program names/paths
capture_output: Whether to capture stdout/stderr
timeout: Optional timeout in seconds
Returns:
CompletedProcess instance with results
Raises:
CommandInjectionError: If program is not allowed or looks suspicious
FileNotFoundError: If program doesn't exist
subprocess.TimeoutExpired: If execution times out
"""
# Validate program name doesn't contain shell metacharacters
shell_metacharacters = ['&', '|', ';', '$', '`', '(', ')', '<', '>', '\n', '\r']
for char in shell_metacharacters:
if char in program:
raise CommandInjectionError(
f"Program name contains shell metacharacter '{char}': {program}"
)
# Check against whitelist if provided
if allowed_programs:
program_name = os.path.basename(program)
if program_name not in allowed_programs and program not in allowed_programs:
raise CommandInjectionError(
f"Program '{program}' not in allowed list: {allowed_programs}"
)
# Build command list (never use shell)
cmd = [program] + args
# Execute safely
try:
result = subprocess.run(
cmd,
shell=False, # CRITICAL: never use shell
capture_output=capture_output,
text=True,
timeout=timeout,
check=False # Don't raise on non-zero exit
)
return result
except FileNotFoundError:
raise FileNotFoundError(f"Program not found: {program}")
# =============================================================================
# Input Validation
# =============================================================================
def validate_email(email: str) -> bool:
"""
Validate email address format.
Args:
email: Email address to validate
Returns:
True if valid, False otherwise
"""
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email) is not None
def validate_url(url: str, allowed_schemes: Optional[List[str]] = None) -> bool:
"""
Validate URL format and optionally check scheme.
Args:
url: URL to validate
allowed_schemes: Optional list of allowed schemes (e.g., ['http', 'https'])
Returns:
True if valid, False otherwise
"""
# Basic URL pattern
pattern = r'^[a-zA-Z][a-zA-Z0-9+.-]*://[^\s]+'
if not re.match(pattern, url):
return False
# Check scheme if whitelist provided
if allowed_schemes:
scheme = url.split('://')[0].lower()
if scheme not in allowed_schemes:
return False
return True
def validate_integer(value: str, min_val: Optional[int] = None,
max_val: Optional[int] = None) -> bool:
"""
Validate that string is a valid integer within optional bounds.
Args:
value: String to validate
min_val: Optional minimum value
max_val: Optional maximum value
Returns:
True if valid integer within bounds, False otherwise
"""
try:
num = int(value)
if min_val is not None and num < min_val:
return False
if max_val is not None and num > max_val:
return False
return True
except ValueError:
return False
def sanitize_sql_identifier(identifier: str) -> str:
"""
Sanitize a SQL identifier (table/column name).
Args:
identifier: SQL identifier to sanitize
Returns:
Sanitized identifier
Raises:
ValidationError: If identifier contains dangerous characters
"""
# Only allow alphanumeric and underscores
if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', identifier):
raise ValidationError(
f"SQL identifier contains invalid characters: {identifier}"
)
# Check for SQL keywords (basic list)
sql_keywords = {
'select', 'insert', 'update', 'delete', 'drop', 'create',
'alter', 'table', 'from', 'where', 'and', 'or', 'union'
}
if identifier.lower() in sql_keywords:
raise ValidationError(
f"SQL identifier cannot be a reserved keyword: {identifier}"
)
return identifier
# =============================================================================
# Output Sanitization
# =============================================================================
def escape_html(text: str) -> str:
"""
Escape HTML special characters to prevent XSS.
Args:
text: Text to escape
Returns:
HTML-escaped text
"""
escape_table = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
}
return ''.join(escape_table.get(c, c) for c in text)
def escape_shell_arg(arg: str) -> str:
"""
Escape a string for safe use as shell argument.
WARNING: This is a fallback. Prefer using safe_execute() instead.
Args:
arg: Argument to escape
Returns:
Shell-escaped argument
"""
import shlex
return shlex.quote(arg)
# =============================================================================
# Pattern Matching & Validation
# =============================================================================
def is_safe_regex(pattern: str, max_length: int = 1000) -> bool:
"""
Check if a regex pattern is safe (prevents ReDoS attacks).
Args:
pattern: Regex pattern to check
max_length: Maximum allowed pattern length
Returns:
True if pattern appears safe, False otherwise
"""
# Check length
if len(pattern) > max_length:
return False
# Check for dangerous patterns that can cause ReDoS
# Check for dangerous patterns that can cause ReDoS.
# These are patterns on the user-supplied *pattern string* itself.
dangerous_patterns = [
# Nested quantifiers: (X+)+ or (X*)+ or (X+)* or (X*)* etc.
r'\([^)]*[+*][^)]*\)[+*{]',
# Alternation inside repeated group: (X|Y)+
r'\([^)]*\|[^)]*\)[+*{]',
# Complex repetition with {N,}: (...)+ followed later by {N,}
r'\(.*\)\{[0-9]*,',
# Escaped dot-star repeated: (.*)+ or (.*)*
r'\(\.\*\)[+*]',
# Legacy exact patterns retained for known bad forms
r'\(\?.*\)\+',
]
for danger in dangerous_patterns:
if re.search(danger, pattern):
return False
return True
# =============================================================================
# Utility Functions
# =============================================================================
def check_rate_limit(identifier: str, max_calls: int, window_seconds: int) -> bool:
"""
Simple rate limiting check.
Args:
identifier: Identifier for rate limit (e.g., IP address, user ID)
max_calls: Maximum calls allowed in window
window_seconds: Time window in seconds
Returns:
True if within rate limit, False if exceeded
Note: This is a simple in-memory implementation. Production systems
should use Redis or similar for distributed rate limiting.
"""
import time
from collections import defaultdict, deque
# In-memory storage (not thread-safe, for demo only)
if not hasattr(check_rate_limit, 'call_history'):
check_rate_limit.call_history = defaultdict(deque)
now = time.time()
calls = check_rate_limit.call_history[identifier]
# Remove old calls outside window
while calls and calls[0] < now - window_seconds:
calls.popleft()
# Check if limit exceeded
if len(calls) >= max_calls:
return False
# Record this call
calls.append(now)
return True