Skip to content

security: bind media paths and validate upload codes - #676

Open
obaidur-dev wants to merge 1 commit into
sergix44:3.xfrom
obaidur-dev:security/media-path-validation
Open

obaidur-dev wants to merge 1 commit into
sergix44:3.xfrom
obaidur-dev:security/media-path-validation

Conversation

@obaidur-dev

Copy link
Copy Markdown

Two related defects share one input.

UploadController::saveMedia() builds the storage path from the client-supplied code and file extension without validating either:

$storagePath = "$user->user_code/$code.$fileInfo[extension]";
  • A code containing .. segments is normalized by Flysystem into a write inside another user's directory or at the storage root.
  • A code or extension containing a double quote reaches the IN ("...") list in MediaRepository::runWithFileSort() when a gallery is sorted by size. It injects a UNION row and leaks arbitrary database values into the page.

This change:

  • restricts code to ^[A-Za-z0-9_-]{1,64}$ and the extension to ^[A-Za-z0-9]{0,32}$, returning 400 for anything else,
  • replaces the concatenated IN (...) lists in MediaRepository, including the tag query at line 350, with bound placeholders through a small buildPlaceholders() helper,
  • skips the listing query when the path list is empty, which avoids an IN () syntax error.

Affected release: 3.8.2. Files: app/Controllers/UploadController.php lines 190 to 201 and app/Database/Repositories/MediaRepository.php lines 234, 245 and 350.

Tests

  • tests/Feature/Media/StoragePathSqlInjectionTest.php uploads a file whose code carries the UNION payload and checks that the gallery no longer returns the injected row. Before the fix 3 of its 4 tests fail; after the fix all 4 pass.
  • tests/Feature/Upload/PathTraversalTest.php uploads with ../pubusrAA/... and checks that the request is rejected and nothing is written outside the uploader's directory. Before the fix 1 test fails (201 instead of 400); after the fix both pass.
git apply pr.patch
composer install
vendor/bin/phpunit

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant