Skip to content

Fix stored XSS through the admin raw media route - #673

Open
parenthesis0407 wants to merge 1 commit into
sergix44:3.xfrom
parenthesis0407:fix/svg-xss-admin-raw-route
Open

parenthesis0407 wants to merge 1 commit into
sergix44:3.xfrom
parenthesis0407:fix/svg-xss-admin-raw-route

Conversation

@parenthesis0407

@parenthesis0407 parenthesis0407 commented Oct 19, 2026 •

Copy link
Copy Markdown

Summary

must_be_escaped() (app/helpers.php:499) marks image/svg and text/htm as content types that must not be rendered inline by the browser. The guard is applied in MediaController::getRaw(), but not in MediaController::getRawById().

getRawById() backs GET /upload/{id}/raw (app/routes.php:68), which the admin UI uses to preview uploads. Because the guard is missing, the stored file is returned with its original MIME type and an inline disposition, so a user-supplied SVG is rendered as an SVG document and its embedded script executes in the application origin.

Uploads are not content-validated, so any authenticated user — including a regular, non-administrative account — can store the payload. getRawById() also does not verify that the upload belongs to the caller, so an attacker can point an administrator at any upload id.

Reproduction (on 3.8.2)

  1. As admin, create a regular user (no admin rights).
  2. Log in as that user; GET /system returns 403.
  3. Upload an SVG containing onload="...".
  4. Note the upload id N.
  5. As admin, open GET /upload/N/raw.
  6. Before this patch: Content-Type: image/svg+xml, script executes.
    After this patch: Content-Type: text/plain, script does not execute.

Control: the same file served through GET /{userCode}/{mediaCode}/raw.svg already returns text/plain, which confirms this is a missing call rather than intended behaviour.

Fix

Mirror the existing guard from getRaw():

if (must_be_escaped($this->storage->getMimetype($media->storage_path))) {
    $response = $this->streamMedia($request, $response, $this->storage, $media);
    return $response->withHeader('Content-Type', 'text/plain');
}

A possible follow-up (not included here, to keep the change minimal) is to move the downgrade into streamMedia() so future call sites cannot miss it — show() also calls streamMedia() without the guard for bot user agents.

Tests

tests/Feature/MediaControllerTest.php:

Test Before After
it_does_not_serve_svg_inline_through_the_admin_raw_route image/svg+xml (fail) text/plain (pass)
it_does_not_serve_html_inline_through_the_admin_raw_route text/html (fail) text/plain (pass)
it_still_serves_images_with_their_own_content_type_through_the_admin_raw_route image/png (pass) image/png (pass)

Verified on PHP 8.0.30:

# without the fix
Tests: 3, Assertions: 6, Failures: 2.

# with the fix
OK (26 tests, 54 assertions)   # full suite

Severity

Suggested CVSS 3.1: CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:L/A:N — 5.7 (Medium).
Attacker needs a regular account; the admin must open the link.

Note on CI

.github/workflows/test_suite.yml only triggers on push/pull_request targeting master, so GitHub Actions does not run for pull requests against 3.x. The suite was therefore run locally on PHP 8.0.30 against this branch:

$ php vendor/bin/phpunit --no-coverage
PHPUnit 9.6.34
.............................                                     29 / 29 (100%)
OK (29 tests, 66 assertions)

The new tests fail without the patch and pass with it:

# patch reverted
Tests: 3, Assertions: 9, Failures: 2.
  - SVG   expected 'text/plain', got 'image/svg+xml'
  - HTML  expected 'text/plain', got 'text/html'

# patch applied
OK (29 tests, 66 assertions)

Happy to extend the workflow trigger to 3.x as a separate change if you would like CI to run on this branch.

must_be_escaped() marks image/svg and text/htm as content types that must not
be rendered inline. The guard was applied in getRaw() but not in
getRawById(), which backs GET /upload/{id}/raw. As a result a user-supplied
SVG or HTML file was returned with its original MIME type and an inline
disposition, so the browser rendered it and executed the embedded script in
the application origin.

Apply the same guard to getRawById() and add tests that fail without the fix:

- it_does_not_serve_svg_inline_through_the_admin_raw_route
- it_does_not_serve_html_inline_through_the_admin_raw_route
- it_still_serves_images_with_their_own_content_type_through_the_admin_raw_route
  (guards against over-fixing)

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