API Reference
File Security Module
A comprehensive file security system for validating uploads and preventing attacks.
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
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 | |
__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
22 23 24 25 26 27 28 29 | |
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
31 32 33 34 35 36 37 38 39 40 41 42 43 | |
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
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 | |
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
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
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
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 | |
__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
36 37 38 39 40 41 42 43 | |
validate
validate(file_content, compressed_size)
Validate the compression ratio of a ZIP file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_content
|
bytes
|
Raw bytes of the uploaded file. |
required |
compressed_size
|
int
|
Size of the file after compression in bytes. |
required |
Raises:
| Type | Description |
|---|---|
ZipBombError
|
If compression ratio exceeds maximum allowed. |
CompressionSecurityError
|
If ZIP structure is invalid. |
FileProcessingError
|
If unexpected error occurs. |
Source code in safeuploads/validators/compression_validator.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
validate_zip_compression_ratio
validate_zip_compression_ratio(file_content, compressed_size)
Validate ZIP archive against security limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_content
|
bytes
|
Raw bytes of the ZIP archive. |
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
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 | |
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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
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
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 | |
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 | |
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
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 | |
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
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 | |
__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
24 25 26 27 28 29 30 31 | |
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
86 87 88 89 90 91 92 93 94 95 96 97 | |
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
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 | |
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
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
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 |
set[str]
|
Dangerous file extensions to block. |
COMPOUND_BLOCKED_EXTENSIONS |
set[str]
|
Multi-part extensions to block. |
DANGEROUS_UNICODE_CHARS |
set[int]
|
Unicode characters for filename attacks. |
WINDOWS_RESERVED_NAMES |
set[str]
|
Platform-specific reserved filenames. |
Source code in safeuploads/config.py
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 | |
__init_subclass__
classmethod
__init_subclass__(**kwargs)
Hook for subclass creation to validate configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Subclass initialization arguments. |
{}
|
Source code in safeuploads/config.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
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
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
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
205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
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
235 236 237 238 239 240 241 242 243 244 245 246 | |
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
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
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
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 | |
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
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 | |
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
31 32 33 34 35 36 37 38 39 40 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
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
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
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 | |
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
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 | |
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
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
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
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 | |
__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
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 | |
validate_image_file
async
validate_image_file(file)
Validate uploaded image by checking filename, 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
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 | |
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
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 | |
FilenameSecurityError
Bases: FileValidationError
Filename failed security checks.
Source code in safeuploads/exceptions.py
177 178 179 180 | |
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
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 | |
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
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 | |
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
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 | |
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
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 | |
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
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 | |
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 | |
__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 | |
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
122 123 124 125 126 127 128 129 130 131 132 | |
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
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 | |
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
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 | |
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 | |
__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
84 85 86 87 88 89 90 91 92 93 94 95 | |
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 | |
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
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 | |
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
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 | |
ZipContentInspector
Inspects ZIP archive contents for security threats.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
File security configuration. |
Source code in safeuploads/inspectors/zip_inspector.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 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 | |
__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
30 31 32 33 34 35 36 37 | |
inspect_zip_content
inspect_zip_content(file_content)
Inspect ZIP archive for potential security threats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_content
|
bytes
|
Raw bytes of ZIP archive. |
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
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 | |
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. |
Source code in safeuploads/enums.py
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 | |