[PR #23102] Add comprehensive security tests for file upload controller #30161

Closed
opened 2026-02-21 20:46:58 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/23102

State: closed
Merged: Yes


Description

Add comprehensive unit tests for file upload security functionality in controllers/console/files.py module.

Core Functionality Tests

  • File type validation and extension checking
  • MIME type verification and content matching
  • File size limit enforcement across different file types
  • Filename sanitization and character validation
  • Permission validation for dataset editors
  • Multiple file upload prevention
  • Basic file upload workflow validation

Security-Focused Tests

  • Malicious file type detection: Prevents upload of PHP, executable, and script files with various extensions and case variations
  • Double extension attack prevention: Blocks files like image.jpg.php and document.pdf.exe
  • MIME type forgery detection: Validates that file content matches declared MIME type
  • Embedded script detection: Identifies malicious content in SVG, PDF, and Office documents
  • Path traversal attack prevention: Blocks directory traversal attempts using ../, ..\\, and encoded variants
  • Null byte injection protection: Prevents file.jpg\x00.php style attacks
  • Unicode attack mitigation: Handles right-to-left override and invisible character attacks
  • Compression bomb detection: Protects against malicious compressed files
  • Cross-tenant isolation: Ensures proper tenant-level access control
  • Concurrent upload security: Validates safe handling of simultaneous uploads

Edge Cases

  • Empty filename handling and missing file detection
  • Zero-byte file processing
  • Oversized filename truncation (200+ characters)
  • Windows reserved filename validation (CON, PRN, AUX, etc.)
  • Special character injection in filenames (<script>, SQL injection patterns)
  • Permission boundary testing for different user roles
  • File size limit edge cases for different file categories
  • Network error simulation during upload process

The extensive test coverage is intentional given the critical nature of file upload security functionality in the system. File uploads represent a major attack vector, and this test suite provides comprehensive protection against common and advanced file upload vulnerabilities, ensuring the security and reliability.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/23102 **State:** closed **Merged:** Yes --- ## Description ### Add comprehensive unit tests for file upload security functionality in `controllers/console/files.py` module. ### Core Functionality Tests - File type validation and extension checking - MIME type verification and content matching - File size limit enforcement across different file types - Filename sanitization and character validation - Permission validation for dataset editors - Multiple file upload prevention - Basic file upload workflow validation ### Security-Focused Tests - **Malicious file type detection**: Prevents upload of PHP, executable, and script files with various extensions and case variations - **Double extension attack prevention**: Blocks files like `image.jpg.php` and `document.pdf.exe` - **MIME type forgery detection**: Validates that file content matches declared MIME type - **Embedded script detection**: Identifies malicious content in SVG, PDF, and Office documents - **Path traversal attack prevention**: Blocks directory traversal attempts using `../`, `..\\`, and encoded variants - **Null byte injection protection**: Prevents `file.jpg\x00.php` style attacks - **Unicode attack mitigation**: Handles right-to-left override and invisible character attacks - **Compression bomb detection**: Protects against malicious compressed files - **Cross-tenant isolation**: Ensures proper tenant-level access control - **Concurrent upload security**: Validates safe handling of simultaneous uploads ### Edge Cases - Empty filename handling and missing file detection - Zero-byte file processing - Oversized filename truncation (200+ characters) - Windows reserved filename validation (`CON`, `PRN`, `AUX`, etc.) - Special character injection in filenames (`<script>`, SQL injection patterns) - Permission boundary testing for different user roles - File size limit edge cases for different file categories - Network error simulation during upload process The extensive test coverage is intentional given the critical nature of file upload security functionality in the system. File uploads represent a major attack vector, and this test suite provides comprehensive protection against common and advanced file upload vulnerabilities, ensuring the security and reliability. ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:46:58 -05:00
yindo closed this issue 2026-02-21 20:46:58 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#30161