API Reference
File Security Module.
A comprehensive file security system for validating uploads and preventing attacks.
AuditEvent
dataclass
A structured security audit event.
Attributes:
| Name | Type | Description |
|---|---|---|
event_type |
AuditEventType
|
Category of the audit event. |
correlation_id |
str
|
Unique ID linking related events. |
filename |
str
|
Name of the file being validated. |
result |
str
|
Outcome description. |
details |
str
|
Additional context information. |
duration_ms |
float
|
Elapsed time in milliseconds. |
source_ip |
str | None
|
Optional client IP address. |
timestamp |
float
|
Monotonic time when event was created. |
Source code in safeuploads/audit.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
AuditEventType
Bases: Enum
Types of security audit events.
Attributes:
| Name | Type | Description |
|---|---|---|
VALIDATION_START |
Validation process started. |
|
VALIDATION_SUCCESS |
Validation completed successfully. |
|
VALIDATION_FAILURE |
Validation failed with error. |
|
THREAT_DETECTED |
Security threat detected. |
|
RESOURCE_LIMIT |
Resource limit exceeded. |
Source code in safeuploads/audit.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
BaseValidator
Bases: ABC
Abstract base class for file security validators.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
File security configuration parameters. |
Source code in safeuploads/validators/base.py
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 | |
__init__
__init__(config)
Initialize validator with configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig
|
File security settings to apply. |
required |
Source code in safeuploads/validators/base.py
20 21 22 23 24 25 26 27 | |
validate
abstractmethod
validate(*args, **kwargs)
Validate data using subclass-specific logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments for concrete validator. |
()
|
|
**kwargs
|
Keyword arguments for concrete validator. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Validated result defined by subclass. |
Source code in safeuploads/validators/base.py
29 30 31 32 33 34 35 36 37 38 39 40 | |
BinaryFileCategory
Bases: Enum
Enumeration of binary file categories.
Attributes:
| Name | Type | Description |
|---|---|---|
FITNESS_FILES |
Fitness/activity binary file extensions (e.g., '.fit'). |
Source code in safeuploads/enums.py
6 7 8 9 10 11 12 13 14 15 16 | |
CompoundExtensionCategory
Bases: Enum
Categorized compound file extensions that combine multiple suffixes.
Attributes:
| Name | Type | Description |
|---|---|---|
COMPRESSED_ARCHIVES |
Multi-part archive formats. |
|
JAVASCRIPT_VARIANTS |
Specialized JavaScript files. |
|
WEB_CONTENT |
Minified static web assets. |
Source code in safeuploads/enums.py
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 | |
CompressionSecurityError
Bases: FileValidationError
Compressed file security check failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional filename of compressed file. |
None
|
error_code
|
str | None
|
Optional error code (defaults to COMPRESSION_GENERIC). |
None
|
Source code in safeuploads/exceptions.py
382 383 384 385 386 387 388 389 390 391 392 393 394 | |
CompressionSecurityValidator
Bases: BaseValidator
Validates ZIP uploads against zip bombs and compression attacks.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
Security configuration for validation limits. |
Source code in safeuploads/validators/compression_validator.py
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 | |
__init__
__init__(config)
Initialize the compression validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig
|
Security configuration with compression limits. |
required |
Source code in safeuploads/validators/compression_validator.py
35 36 37 38 39 40 41 42 43 44 45 | |
validate
validate(file_obj, compressed_size)
Validate the compression ratio of a ZIP file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_obj
|
SeekableFile
|
Seekable file-like object of the ZIP. |
required |
compressed_size
|
int
|
Size of the file after compression in bytes. |
required |
Raises:
| Type | Description |
|---|---|
ZipBombError
|
If compression ratio exceeds maximum. |
CompressionSecurityError
|
If ZIP structure is invalid. |
FileProcessingError
|
If unexpected error occurs. |
Source code in safeuploads/validators/compression_validator.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
validate_zip_compression_ratio
validate_zip_compression_ratio(file_obj, compressed_size)
Validate ZIP archive against security limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_obj
|
SeekableFile
|
Seekable file-like object containing ZIP data. |
required |
compressed_size
|
int
|
Size of the compressed archive in bytes. |
required |
Raises:
| Type | Description |
|---|---|
ZipBombError
|
If compression ratio exceeds maximum allowed or total uncompressed size is too large. |
CompressionSecurityError
|
If ZIP structure is invalid, too many entries, nested archives detected, or individual file too large. |
FileProcessingError
|
If unexpected error occurs during validation such as memory errors or I/O errors. |
Source code in safeuploads/validators/compression_validator.py
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 | |
ConfigValidationError
dataclass
Configuration validation issue with severity and recommendation.
Attributes:
| Name | Type | Description |
|---|---|---|
error_type |
str
|
Type of the validation error. |
message |
str
|
Human-readable error message. |
severity |
str
|
Error severity level ('error', 'warning', 'info'). |
component |
str
|
Component that failed validation. |
recommendation |
str
|
Optional recommendation to fix the issue. |
Source code in safeuploads/exceptions.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
ContentSecurityInspector
Scans file content for embedded malware and scripts.
Three detection layers: 1. Executable signatures — PE, ELF, Mach-O, Java class headers in files that should not be executables. 2. Script injection — Common web shell and script markers in non-script files. 3. Polyglot detection — Secondary format signatures (ZIP/JAR, RAR, Java class) embedded after a valid image or document header.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
File security configuration. |
Source code in safeuploads/inspectors/content_inspector.py
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 | |
__init__
__init__(config)
Initialize the content inspector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig
|
File security configuration. |
required |
Source code in safeuploads/inspectors/content_inspector.py
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 | |
scan_content
scan_content(content, filename='', expected_type='')
Scan file content for embedded threats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
bytes
|
Raw bytes to inspect (typically the
first |
required |
filename
|
str
|
Original filename for context. |
''
|
expected_type
|
str
|
Logical file type the content should represent (e.g. "image", "zip"). |
''
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of threat descriptions found. Empty list |
list[str]
|
means content is clean. |
Source code in safeuploads/inspectors/content_inspector.py
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 | |
DangerousExtensionCategory
Bases: Enum
File extension categories considered potentially dangerous for uploads.
Attributes:
| Name | Type | Description |
|---|---|---|
WINDOWS_EXECUTABLES |
Traditional Windows executable formats. |
|
SCRIPT_FILES |
Script files that can execute code. |
|
WEB_SCRIPTS |
Web server and dynamic content scripts. |
|
UNIX_EXECUTABLES |
Unix/Linux executables and shell scripts. |
|
MACOS_EXECUTABLES |
macOS specific executables and applications. |
|
JAVA_EXECUTABLES |
Java related executables and bytecode. |
|
MOBILE_APPS |
Mobile application packages. |
|
BROWSER_EXTENSIONS |
Browser extensions and web applications. |
|
PACKAGE_FORMATS |
Modern package managers and distribution formats. |
|
ARCHIVE_FORMATS |
Archive formats that can contain executables. |
|
VIRTUALIZATION_FORMATS |
Virtualization and container formats. |
|
OFFICE_MACROS |
Office documents with macro capabilities. |
|
SYSTEM_FILES |
System shortcuts and configuration files. |
|
SYSTEM_DRIVERS |
System drivers and low-level components. |
|
WINDOWS_THEMES |
Windows theme and customization files. |
|
HELP_FILES |
Help and documentation files that can execute code. |
Source code in safeuploads/enums.py
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 | |
ErrorCode
Machine-readable error codes for file validation failures.
Source code in safeuploads/exceptions.py
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 | |
ExtensionSecurityError
Bases: FilenameSecurityError
Dangerous file extension detected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional filename with dangerous extension. |
None
|
extension
|
str | None
|
Optional specific extension that was blocked. |
None
|
error_code
|
str | None
|
Optional error code (defaults to EXTENSION_BLOCKED). |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
extension |
The specific extension that was blocked. |
Source code in safeuploads/exceptions.py
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 | |
__init__
__init__(
message, filename=None, extension=None, error_code=None
)
Initialize with blocked extension.
Source code in safeuploads/exceptions.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
ExtensionSecurityValidator
Bases: BaseValidator
Validates filenames against configured forbidden extensions.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
File security configuration settings. |
Source code in safeuploads/validators/extension_validator.py
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 | |
__init__
__init__(config)
Initialize the validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig
|
File security configuration settings. |
required |
Source code in safeuploads/validators/extension_validator.py
26 27 28 29 30 31 32 33 34 35 36 37 38 | |
validate
validate(filename)
Validate the given filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the file to validate. |
required |
Raises:
| Type | Description |
|---|---|
ExtensionSecurityError
|
If filename extension is not permitted. |
Source code in safeuploads/validators/extension_validator.py
102 103 104 105 106 107 108 109 110 111 112 113 | |
validate_extensions
validate_extensions(filename)
Validate filename against blocked extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the file to validate. |
required |
Raises:
| Type | Description |
|---|---|
ExtensionSecurityError
|
If blocked compound or single extension detected in filename. |
Source code in safeuploads/validators/extension_validator.py
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 | |
FileProcessingError
Bases: FileSecurityError
Unexpected processing error during file validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
original_error
|
Exception | None
|
Optional original exception that was caught. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
original_error |
The original exception that was caught. |
Source code in safeuploads/exceptions.py
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
__init__
__init__(message, original_error=None)
Initialize with original error.
Source code in safeuploads/exceptions.py
480 481 482 483 | |
FileSecurityConfig
Centralizes file upload security settings and validation.
Attributes:
| Name | Type | Description |
|---|---|---|
limits |
Security limits configuration instance. |
|
ALLOWED_IMAGE_MIMES |
set[str]
|
Permitted MIME types for images. |
ALLOWED_ZIP_MIMES |
set[str]
|
Permitted MIME types for ZIP files. |
ALLOWED_IMAGE_EXTENSIONS |
set[str]
|
Permitted image file extensions. |
ALLOWED_ZIP_EXTENSIONS |
set[str]
|
Permitted ZIP file extensions. |
BLOCKED_EXTENSIONS |
frozenset[str]
|
Dangerous file extensions to block. |
COMPOUND_BLOCKED_EXTENSIONS |
frozenset[str]
|
Multi-part extensions to block. |
DANGEROUS_UNICODE_CHARS |
frozenset[int]
|
Unicode characters for filename attacks. |
WINDOWS_RESERVED_NAMES |
frozenset[str]
|
Platform-specific reserved filenames. |
Source code in safeuploads/config.py
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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 | |
__init_subclass__
classmethod
__init_subclass__(**kwargs)
Validate configuration on subclass creation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Subclass initialization arguments. |
{}
|
Source code in safeuploads/config.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
get_compound_extensions_by_category
classmethod
get_compound_extensions_by_category(category)
Return compound extensions for a category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
CompoundExtensionCategory
|
The compound extension category. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Copy of compound extensions in the specified category. |
Source code in safeuploads/config.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
get_extension_category
classmethod
get_extension_category(extension)
Return the dangerous extension category for an extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extension
|
str
|
The file extension to evaluate. |
required |
Returns:
| Type | Description |
|---|---|
DangerousExtensionCategory | None
|
Matching category if dangerous, None otherwise. |
Source code in safeuploads/config.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
get_extensions_by_category
classmethod
get_extensions_by_category(category)
Return extensions for a dangerous extension category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
DangerousExtensionCategory
|
The dangerous extension category. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Copy of extensions in the specified category. |
Source code in safeuploads/config.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
get_unicode_chars_by_category
classmethod
get_unicode_chars_by_category(category)
Return Unicode code points for an attack category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
UnicodeAttackCategory
|
The Unicode attack category. |
required |
Returns:
| Type | Description |
|---|---|
set[int]
|
Copy of code points in the specified category. |
Source code in safeuploads/config.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
is_extension_in_category
classmethod
is_extension_in_category(extension, category)
Check if extension belongs to a dangerous category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extension
|
str
|
File extension to evaluate. |
required |
category
|
DangerousExtensionCategory
|
Category to check against. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if extension is in the category, False otherwise. |
Source code in safeuploads/config.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
validate_and_report
classmethod
validate_and_report(strict=True)
Validate configuration and log outcomes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict
|
bool
|
If True, raise on errors/warnings. |
True
|
Raises:
| Type | Description |
|---|---|
FileSecurityConfigurationError
|
If strict and issues found. |
Source code in safeuploads/config.py
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 | |
validate_configuration
classmethod
validate_configuration(strict=True)
Run all configuration validation routines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict
|
bool
|
Reserved for future behavior adjustments. |
True
|
Returns:
| Type | Description |
|---|---|
list[ConfigValidationError]
|
List of detected validation errors. |
Source code in safeuploads/config.py
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 | |
FileSecurityConfigurationError
Bases: Exception
Configuration validation failed with aggregated errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
errors
|
list[ConfigValidationError]
|
List of ConfigValidationError instances. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
errors |
List of validation errors that caused failure. |
Source code in safeuploads/exceptions.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
__init__
__init__(errors)
Initialize with validation errors.
Source code in safeuploads/exceptions.py
41 42 43 44 45 46 47 48 49 | |
FileSecurityError
Bases: Exception
Base exception for all file security validation failures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
error_code
|
str | None
|
Optional machine-readable error code. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Human-readable error message. |
|
error_code |
Machine-readable error code from ErrorCode. |
Source code in safeuploads/exceptions.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
__init__
__init__(message, error_code=None)
Initialize with message and error code.
Source code in safeuploads/exceptions.py
146 147 148 149 150 | |
FileSignatureError
Bases: FileValidationError
File header signature invalid or mismatched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional filename with signature issue. |
None
|
expected_type
|
str | None
|
Optional expected file type based on extension. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
expected_type |
The expected file type based on extension. |
Source code in safeuploads/exceptions.py
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 | |
__init__
__init__(message, filename=None, expected_type=None)
Initialize with expected type.
Source code in safeuploads/exceptions.py
362 363 364 365 366 367 368 369 370 371 372 373 374 | |
FileSizeError
Bases: FileValidationError
File exceeds configured size limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional filename that exceeded size limits. |
None
|
size
|
int | None
|
Optional actual file size in bytes. |
None
|
max_size
|
int | None
|
Optional maximum allowed size in bytes. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
size |
The actual file size in bytes. |
|
max_size |
The maximum allowed size in bytes. |
Source code in safeuploads/exceptions.py
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 | |
__init__
__init__(message, filename=None, size=None, max_size=None)
Initialize with size details.
Source code in safeuploads/exceptions.py
299 300 301 302 303 304 305 306 307 308 309 310 311 | |
FileValidationError
Bases: FileSecurityError
File validation failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional name of the file that failed validation. |
None
|
error_code
|
str | None
|
Optional machine-readable error code. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
filename |
Name of the file that failed validation. |
Source code in safeuploads/exceptions.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
__init__
__init__(message, filename=None, error_code=None)
Initialize with message and filename.
Source code in safeuploads/exceptions.py
171 172 173 174 175 176 177 178 179 | |
FileValidator
Coordinated security validation for uploaded files.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
Active security configuration. |
|
unicode_validator |
Validator for Unicode-related checks. |
|
extension_validator |
Validator for file extension rules. |
|
windows_validator |
Validator enforcing Windows-specific constraints. |
|
compression_validator |
Validator handling compressed file limits. |
|
zip_inspector |
Inspector for ZIP archive contents. |
|
magic_mime |
MIME type detector based on python-magic. |
|
magic_available |
Whether python-magic was successfully initialized. |
Source code in safeuploads/file_validator.py
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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 | |
__init__
__init__(config=None)
Initialize file validator with configuration and detection utilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig | None
|
Optional configuration object defining file security rules. Defaults to new FileSecurityConfig instance. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
config |
Active security configuration. |
|
unicode_validator |
Validator for Unicode-related checks. |
|
extension_validator |
Validator for file extension rules. |
|
windows_validator |
Validator enforcing Windows constraints. |
|
compression_validator |
Validator for compressed file limits. |
|
zip_inspector |
Inspector for ZIP archive contents. |
|
magic_mime |
MIME type detector based on python-magic. |
|
magic_available |
Whether python-magic initialized successfully. |
Source code in safeuploads/file_validator.py
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 | |
validate_activity_file
async
validate_activity_file(file)
Validate uploaded activity file (GPX, TCX, FIT).
For XML-based formats (GPX/TCX) performs XXE-safe
parsing via defusedxml. For FIT files validates
the binary signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFileProtocol
|
Uploaded activity file to validate. |
required |
Raises:
| Type | Description |
|---|---|
FilenameSecurityError
|
Filename fails security. |
ExtensionSecurityError
|
Extension not allowed. |
FileSizeError
|
File exceeds size limit or empty. |
MimeTypeError
|
MIME type not allowed. |
FileSignatureError
|
Signature mismatch. |
FileProcessingError
|
XML parsing or other error. |
Source code in safeuploads/file_validator.py
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 | |
validate_gzip_file
async
validate_gzip_file(file)
Validate uploaded gzip archive.
Checks filename, extension, size, MIME type, signature, and performs decompression bomb detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFileProtocol
|
Uploaded gzip file to validate. |
required |
Raises:
| Type | Description |
|---|---|
FilenameSecurityError
|
Filename fails security. |
ExtensionSecurityError
|
Extension not allowed. |
FileSizeError
|
File exceeds size limit or empty. |
MimeTypeError
|
MIME type not allowed. |
FileSignatureError
|
Signature mismatch. |
ZipBombError
|
Decompression bomb detected. |
CompressionSecurityError
|
Invalid gzip structure. |
FileProcessingError
|
Unexpected error. |
Source code in safeuploads/file_validator.py
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 | |
validate_image_file
async
validate_image_file(file)
Validate uploaded image by checking filename.
Check extension, size, MIME type, and signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFileProtocol
|
Uploaded file to validate. |
required |
Raises:
| Type | Description |
|---|---|
FilenameSecurityError
|
Filename is empty, invalid, or fails security checks. |
ExtensionSecurityError
|
File extension is not allowed or is blocked. |
FileSizeError
|
File size exceeds maximum or file is empty. |
MimeTypeError
|
MIME type is not in allowed image types. |
FileSignatureError
|
File signature doesn't match expected image format. |
FileProcessingError
|
Unexpected error during validation. |
Source code in safeuploads/file_validator.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 | |
validate_zip_file
async
validate_zip_file(file)
Validate uploaded ZIP archive against service configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFileProtocol
|
Incoming ZIP file-like object to validate. |
required |
Raises:
| Type | Description |
|---|---|
FilenameSecurityError
|
Filename is empty, invalid, or fails security checks. |
ExtensionSecurityError
|
File extension is not allowed or is blocked. |
FileSizeError
|
File size exceeds maximum or file is empty. |
MimeTypeError
|
MIME type is not in allowed ZIP types. |
FileSignatureError
|
File signature doesn't match expected ZIP format. |
CompressionSecurityError
|
ZIP compression validation failed (zip bomb detected). |
FileProcessingError
|
Unexpected error during validation. |
Source code in safeuploads/file_validator.py
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | |
FilenameSecurityError
Bases: FileValidationError
Filename failed security checks.
Source code in safeuploads/exceptions.py
187 188 | |
GzipContentInspector
Inspects gzip archives for decompression bomb attacks.
Reads the compressed stream in chunks, tracking the compression ratio and total uncompressed size against configurable limits.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
File security configuration. |
Source code in safeuploads/inspectors/gzip_inspector.py
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 | |
__init__
__init__(config)
Initialize gzip inspector with configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig
|
File security configuration. |
required |
Source code in safeuploads/inspectors/gzip_inspector.py
37 38 39 40 41 42 43 44 45 46 47 | |
inspect_gzip_content
inspect_gzip_content(file_obj, compressed_size)
Inspect gzip archive for decompression bombs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_obj
|
SeekableFile
|
Seekable file containing gzip data. |
required |
compressed_size
|
int
|
Size of the compressed file in bytes. |
required |
Raises:
| Type | Description |
|---|---|
ZipBombError
|
If compression ratio or uncompressed size exceeds configured limits. |
CompressionSecurityError
|
If the gzip structure is invalid or corrupted. |
FileProcessingError
|
If an unexpected error occurs. |
Source code in safeuploads/inspectors/gzip_inspector.py
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 | |
MalwareSignatureCategory
Bases: Enum
Byte-level signatures for embedded malware detection.
Attributes:
| Name | Type | Description |
|---|---|---|
PE_EXECUTABLE |
Windows PE/MZ executable headers. |
|
ELF_EXECUTABLE |
Linux ELF executable headers. |
|
MACHO_EXECUTABLE |
macOS Mach-O executable headers. |
|
JAVA_CLASS |
Java class file headers. |
|
WINDOWS_SHORTCUT |
Windows .lnk shortcut headers. |
|
WEBSHELL_PATTERNS |
Common web shell script markers. |
|
POLYGLOT_SIGNATURES |
Multi-format file signatures. |
Source code in safeuploads/enums.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | |
MimeTypeError
Bases: FileValidationError
File MIME type not allowed or mismatches extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional filename with MIME type issue. |
None
|
detected_mime
|
str | None
|
Optional detected MIME type string. |
None
|
allowed_mimes
|
list[str] | None
|
Optional list of allowed MIME types. |
None
|
error_code
|
str | None
|
Optional error code (defaults to MIME_TYPE_INVALID). |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
detected_mime |
The detected MIME type string. |
|
allowed_mimes |
List of allowed MIME types. |
Source code in safeuploads/exceptions.py
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 | |
__init__
__init__(
message,
filename=None,
detected_mime=None,
allowed_mimes=None,
error_code=None,
)
Initialize with MIME type details.
Source code in safeuploads/exceptions.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
ResourceLimitError
Bases: FileProcessingError
Validation exceeded configured resource limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
error_code
|
str
|
Machine-readable error code. |
RESOURCE_LIMIT_EXCEEDED
|
elapsed_seconds
|
float | None
|
Optional wall-clock time consumed. |
None
|
memory_bytes
|
int | None
|
Optional peak memory consumed in bytes. |
None
|
original_error
|
Exception | None
|
Optional original exception. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
elapsed_seconds |
Wall-clock seconds consumed. |
|
memory_bytes |
Peak memory consumed in bytes. |
Source code in safeuploads/exceptions.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
__init__
__init__(
message,
error_code=ErrorCode.RESOURCE_LIMIT_EXCEEDED,
elapsed_seconds=None,
memory_bytes=None,
original_error=None,
)
Initialize with resource limit details.
Source code in safeuploads/exceptions.py
507 508 509 510 511 512 513 514 515 516 517 518 519 | |
ResourceMonitor
Context manager that enforces wall-clock and memory limits.
Tracks elapsed time continuously and samples memory usage
via resource.getrusage on entry and exit. Raises
ResourceLimitError when either limit is exceeded.
Attributes:
| Name | Type | Description |
|---|---|---|
max_time_seconds |
Maximum allowed wall-clock seconds. |
|
max_memory_bytes |
Maximum allowed memory delta in bytes. |
|
start_time |
float
|
Timestamp when the context was entered. |
start_memory |
int
|
RSS memory in bytes at context entry. |
Source code in safeuploads/utils.py
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 | |
elapsed
property
elapsed
Return elapsed seconds since context entry.
Returns:
| Type | Description |
|---|---|
float
|
Elapsed wall-clock seconds. |
memory_delta
property
memory_delta
Return memory delta since context entry.
Returns:
| Type | Description |
|---|---|
int
|
Memory delta in bytes (may be negative). |
__enter__
__enter__()
Record baseline time and memory on context entry.
Returns:
| Type | Description |
|---|---|
ResourceMonitor
|
Self for use in |
Source code in safeuploads/utils.py
46 47 48 49 50 51 52 53 54 55 | |
__exit__
__exit__(exc_type, exc_val, exc_tb)
Check resource usage on context exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
Exception type if raised inside block. |
required | |
exc_val
|
Exception value if raised inside block. |
required | |
exc_tb
|
Exception traceback if raised inside block. |
required |
Raises:
| Type | Description |
|---|---|
ResourceLimitError
|
If time or memory limits were exceeded during the monitored block. |
Source code in safeuploads/utils.py
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 | |
__init__
__init__(max_time_seconds=30.0, max_memory_mb=512)
Initialize the resource monitor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_time_seconds
|
float
|
Wall-clock timeout in seconds. |
30.0
|
max_memory_mb
|
int
|
Maximum memory delta in megabytes. |
512
|
Source code in safeuploads/utils.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
check_time
check_time()
Check elapsed time mid-operation.
Raises:
| Type | Description |
|---|---|
ResourceLimitError
|
If the wall-clock time limit has been exceeded since context entry. |
Source code in safeuploads/utils.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
SecurityAuditLogger
Emits structured audit log records.
All records are emitted under the safeuploads.audit
logger with extra fields containing the structured
AuditEvent data. The application configures handlers
on this logger (or a parent) to capture events.
Attributes:
| Name | Type | Description |
|---|---|---|
enabled |
Whether audit logging is active. |
Source code in safeuploads/audit.py
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 | |
__init__
__init__(enabled=False)
Initialize the audit logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
Whether to emit audit events. |
False
|
Source code in safeuploads/audit.py
153 154 155 156 157 158 159 160 | |
failure
failure(
filename, correlation_id, duration_ms, error, details=""
)
Log a validation failure event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the failed file. |
required |
correlation_id
|
str
|
Unique operation identifier. |
required |
duration_ms
|
float
|
Validation duration in milliseconds. |
required |
error
|
str
|
Short error description. |
required |
details
|
str
|
Additional failure context. |
''
|
Source code in safeuploads/audit.py
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 | |
log_event
log_event(event)
Emit an audit event as a structured log record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AuditEvent
|
The audit event to record. |
required |
Source code in safeuploads/audit.py
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 | |
start
start(filename, correlation_id)
Log a validation start event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the file being validated. |
required |
correlation_id
|
str
|
Unique operation identifier. |
required |
Source code in safeuploads/audit.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
success
success(filename, correlation_id, duration_ms)
Log a validation success event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the validated file. |
required |
correlation_id
|
str
|
Unique operation identifier. |
required |
duration_ms
|
float
|
Validation duration in milliseconds. |
required |
Source code in safeuploads/audit.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
threat
threat(filename, correlation_id, threat_description)
Log a threat detection event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the threatening file. |
required |
correlation_id
|
str
|
Unique operation identifier. |
required |
threat_description
|
str
|
Description of the threat. |
required |
Source code in safeuploads/audit.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
SecurityLimits
dataclass
Security constraints for file submissions.
Attributes:
| Name | Type | Description |
|---|---|---|
max_image_size |
int
|
Maximum size in bytes for image files. |
max_zip_size |
int
|
Maximum size in bytes for ZIP archives. |
max_compression_ratio |
int
|
Maximum expansion ratio for ZIP files. |
max_uncompressed_size |
int
|
Maximum cumulative size of ZIP contents. |
max_individual_file_size |
int
|
Maximum size of single file in ZIP. |
max_zip_entries |
int
|
Maximum number of file entries in ZIP. |
zip_analysis_timeout |
float
|
Maximum seconds for ZIP analysis. |
max_zip_depth |
int
|
Maximum directory nesting depth in ZIP. |
max_filename_length |
int
|
Maximum length for filenames in ZIP. |
max_path_length |
int
|
Maximum length for full paths in ZIP. |
allow_nested_archives |
bool
|
Whether nested archives are permitted. |
allow_symlinks |
bool
|
Whether symbolic links are permitted. |
allow_absolute_paths |
bool
|
Whether absolute paths are permitted. |
scan_zip_content |
bool
|
Whether deep content inspection is enabled. |
Source code in safeuploads/config.py
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 | |
SeekableFile
Bases: Protocol
Protocol for seekable binary file-like objects.
Implemented by BytesIO, SpooledTemporaryFile, and regular file objects opened in binary mode.
Attributes:
| Name | Type | Description |
|---|---|---|
read |
bytes
|
Read bytes from the file. |
seek |
int
|
Move file pointer to specified position. |
tell |
int
|
Return current file pointer position. |
Source code in safeuploads/protocols.py
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 | |
read
read(size=-1)
Read bytes from the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Number of bytes to read. -1 reads all. |
-1
|
Returns:
| Type | Description |
|---|---|
bytes
|
Bytes read from the file. |
Source code in safeuploads/protocols.py
26 27 28 29 30 31 32 33 34 35 36 | |
seek
seek(offset, whence=0)
Move file pointer to specified position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
int
|
Position offset in bytes. |
required |
whence
|
int
|
Reference point for offset (0=start, 1=current, 2=end). |
0
|
Returns:
| Type | Description |
|---|---|
int
|
New absolute position in the file. |
Source code in safeuploads/protocols.py
38 39 40 41 42 43 44 45 46 47 48 49 50 | |
tell
tell()
Return current file pointer position.
Returns:
| Type | Description |
|---|---|
int
|
Current position in the file. |
Source code in safeuploads/protocols.py
52 53 54 55 56 57 58 59 | |
SuspiciousFilePattern
Bases: Enum
Categorized patterns used to flag potentially malicious uploads.
Attributes:
| Name | Type | Description |
|---|---|---|
DIRECTORY_TRAVERSAL |
Directory traversal attack patterns. |
|
SUSPICIOUS_NAMES |
Suspicious filename patterns. |
|
EXECUTABLE_SIGNATURES |
Dangerous file content signatures. |
|
SUSPICIOUS_PATHS |
Suspicious path components. |
Source code in safeuploads/enums.py
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 | |
UnicodeAttackCategory
Bases: Enum
Categorized Unicode code points used in obfuscation attacks.
Attributes:
| Name | Type | Description |
|---|---|---|
DIRECTIONAL_OVERRIDES |
Right-to-left and directional controls. |
|
ZERO_WIDTH_CHARACTERS |
Zero-width and invisible characters. |
|
LANGUAGE_MARKS |
Language and format specific characters. |
|
CONFUSING_PUNCTUATION |
Punctuation that can disguise extensions. |
Source code in safeuploads/enums.py
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 | |
UnicodeSecurityError
Bases: FilenameSecurityError
Dangerous Unicode characters detected in filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional filename containing dangerous Unicode. |
None
|
dangerous_chars
|
list[tuple[str, int, int]] | None
|
Optional list of (char, code_point, position) tuples for each dangerous character found. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
dangerous_chars |
List of dangerous character tuples. |
Source code in safeuploads/exceptions.py
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 | |
__init__
__init__(message, filename=None, dangerous_chars=None)
Initialize with dangerous characters.
Source code in safeuploads/exceptions.py
205 206 207 208 209 210 211 212 213 214 215 216 217 | |
UnicodeSecurityValidator
Bases: BaseValidator
Validates filenames for Unicode security threats.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
Runtime configuration for file security rules. |
Source code in safeuploads/validators/unicode_validator.py
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 | |
__init__
__init__(config)
Initialize the Unicode validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig
|
Runtime configuration that controls file security rules. |
required |
Source code in safeuploads/validators/unicode_validator.py
27 28 29 30 31 32 33 34 35 36 37 38 39 | |
validate
validate(filename)
Validate a filename for Unicode security issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The name of the file to assess. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The validated and normalized filename. |
Source code in safeuploads/validators/unicode_validator.py
145 146 147 148 149 150 151 152 153 154 155 | |
validate_unicode_security
validate_unicode_security(filename)
Validate filename for unsafe Unicode characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The filename to validate and normalize. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The NFC-normalized filename. |
Raises:
| Type | Description |
|---|---|
UnicodeSecurityError
|
If dangerous Unicode characters are detected in the filename or result from normalization. |
Source code in safeuploads/validators/unicode_validator.py
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 | |
WindowsReservedNameError
Bases: FilenameSecurityError
Windows reserved device name used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional filename using a reserved name. |
None
|
reserved_name
|
str | None
|
Optional specific reserved name detected. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
reserved_name |
The specific reserved name that was detected. |
Source code in safeuploads/exceptions.py
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 | |
__init__
__init__(message, filename=None, reserved_name=None)
Initialize with reserved name.
Source code in safeuploads/exceptions.py
264 265 266 267 268 269 270 271 272 273 274 275 276 | |
WindowsSecurityValidator
Bases: BaseValidator
Validator for Windows reserved device names.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
File security configuration settings. |
Source code in safeuploads/validators/windows_validator.py
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 | |
__init__
__init__(config)
Initialize the validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig
|
File security configuration settings. |
required |
Source code in safeuploads/validators/windows_validator.py
27 28 29 30 31 32 33 34 | |
validate
validate(filename)
Validate filename against Windows reserved naming rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The filename to validate. |
required |
Raises:
| Type | Description |
|---|---|
WindowsReservedNameError
|
If filename matches a Windows reserved device name. |
Source code in safeuploads/validators/windows_validator.py
95 96 97 98 99 100 101 102 103 104 105 106 | |
validate_windows_reserved_names
validate_windows_reserved_names(filename)
Validate filename against Windows reserved device names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The filename to validate. |
required |
Raises:
| Type | Description |
|---|---|
WindowsReservedNameError
|
If filename matches a Windows reserved device name. |
Source code in safeuploads/validators/windows_validator.py
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 | |
XmlSecurityValidator
Bases: BaseValidator
Validates XML-based activity files for XXE and entity attacks.
Uses defusedxml to parse XML safely. Rejects files
containing DTD declarations, external entities, or
excessive entity expansion.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
Security configuration for validation limits. |
Source code in safeuploads/validators/xml_validator.py
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 | |
__init__
__init__(config)
Initialize the XML security validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig
|
Security configuration with file limits. |
required |
Source code in safeuploads/validators/xml_validator.py
34 35 36 37 38 39 40 41 | |
validate
validate(file_obj)
Validate XML file for security threats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_obj
|
SeekableFile
|
Seekable file containing XML data. |
required |
Raises:
| Type | Description |
|---|---|
FileProcessingError
|
If the XML fails safety checks. |
Source code in safeuploads/validators/xml_validator.py
85 86 87 88 89 90 91 92 93 94 95 | |
validate_xml_safety
validate_xml_safety(file_obj)
Parse XML with XXE protections and validate structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_obj
|
SeekableFile
|
Seekable file containing XML data. |
required |
Raises:
| Type | Description |
|---|---|
FileProcessingError
|
If the XML is malformed, contains XXE attacks, or fails safety checks. |
Source code in safeuploads/validators/xml_validator.py
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 | |
ZipBombError
Bases: CompressionSecurityError
Zip archive exceeds compression ratio or uncompressed size limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional filename of zip bomb. |
None
|
compression_ratio
|
float | None
|
Optional actual compression ratio detected. |
None
|
uncompressed_size
|
int | None
|
Optional total uncompressed size in bytes. |
None
|
max_ratio
|
float | None
|
Optional maximum allowed compression ratio. |
None
|
max_size
|
int | None
|
Optional maximum allowed uncompressed size in bytes. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
compression_ratio |
Actual compression ratio detected. |
|
uncompressed_size |
Total uncompressed size in bytes. |
|
max_ratio |
Maximum allowed compression ratio. |
|
max_size |
Maximum allowed uncompressed size in bytes. |
Source code in safeuploads/exceptions.py
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 | |
__init__
__init__(
message,
filename=None,
compression_ratio=None,
uncompressed_size=None,
max_ratio=None,
max_size=None,
)
Initialize with compression details.
Source code in safeuploads/exceptions.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
ZipContentError
Bases: CompressionSecurityError
Zip archive contains dangerous content or structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable error description. |
required |
filename
|
str | None
|
Optional filename of problematic archive. |
None
|
threats
|
list[str] | None
|
Optional list of detected threat descriptions. |
None
|
error_code
|
str | None
|
Optional error code (defaults to ZIP_CONTENT_THREAT). |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
threats |
List of detected threat descriptions. |
Source code in safeuploads/exceptions.py
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
__init__
__init__(
message, filename=None, threats=None, error_code=None
)
Initialize with threat details.
Source code in safeuploads/exceptions.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
ZipContentInspector
Inspects ZIP archive contents for security threats.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
File security configuration. |
Source code in safeuploads/inspectors/zip_inspector.py
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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | |
__init__
__init__(config)
Initialize ZIP inspector with configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FileSecurityConfig
|
File security configuration. |
required |
Source code in safeuploads/inspectors/zip_inspector.py
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 | |
inspect_nested_archives
inspect_nested_archives(
file_obj,
*,
depth=0,
seen_hashes=None,
total_entries=0,
start_time=None,
)
Recursively inspect nested archives.
Only called when allow_nested_archives is True.
Tracks depth, cumulative entry count, elapsed time,
and archive hashes to detect recursive/quine
structures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_obj
|
SeekableFile
|
Seekable file containing ZIP data. |
required |
depth
|
int
|
Current nesting depth (0 = outermost). |
0
|
seen_hashes
|
set[str] | None
|
Set of SHA-256 hashes already seen. |
None
|
total_entries
|
int
|
Cumulative entry count so far. |
0
|
start_time
|
float | None
|
Monotonic timestamp of initial call. |
None
|
Raises:
| Type | Description |
|---|---|
ZipContentError
|
If recursive structure, quine, or complexity attack is detected. |
Source code in safeuploads/inspectors/zip_inspector.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | |
inspect_zip_content
inspect_zip_content(file_obj)
Inspect ZIP archive for potential security threats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_obj
|
SeekableFile
|
Seekable file-like object containing ZIP data. |
required |
Raises:
| Type | Description |
|---|---|
ZipContentError
|
If security threats are detected in ZIP content such as directory traversal, symlinks, nested archives, or suspicious patterns. |
FileProcessingError
|
If ZIP structure is invalid or unexpected error occurs during inspection. |
Source code in safeuploads/inspectors/zip_inspector.py
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 | |
ZipThreatCategory
Bases: Enum
Categories of potentially harmful contents within ZIP archives.
Attributes:
| Name | Type | Description |
|---|---|---|
NESTED_ARCHIVES |
Archive format threats. |
|
EXECUTABLE_FILES |
Executable content threats. |
|
SCRIPT_FILES |
Script and code threats. |
|
SYSTEM_FILES |
System and configuration threats. |
|
RECURSIVE_STRUCTURE |
Self-referencing ZIP structures. |
|
QUINE_ARCHIVE |
ZIP that contains a copy of itself. |
|
COMPLEXITY_ATTACK |
Algorithmic complexity exploits. |
Source code in safeuploads/enums.py
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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
get_correlation_id
get_correlation_id()
Return the current correlation ID.
Returns:
| Type | Description |
|---|---|
str | None
|
Correlation ID string or None if not set. |
Source code in safeuploads/audit.py
33 34 35 36 37 38 39 40 | |
set_correlation_id
set_correlation_id(cid=None)
Set a new correlation ID for the current context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cid
|
str | None
|
Explicit ID to use. Generates a UUID4 if None. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The correlation ID that was set. |
Source code in safeuploads/audit.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |