Page MenuHomePhabricator

CVE-2024-47841: Path traversal when loading stylesheets from /w/skins/ can load files outside of /w/skins/
Closed, ResolvedPublicSecurity

Description

Steps to reproduce:

  1. Install MediaWiki
  2. Install Extension:CSS
  3. Save the following into the page CSS/Path traversal/styles.css:
.purple {
    width: 500px;
    height: 500px;
    background-color: purple;
}
  1. Save the following into the page CSS/Path traversal:
{{#css: /..\index.php?title=CSS/Path traversal/styles.css&action=raw&ctype=text/css}}<!--
--><div class="purple"></div>

Expected behavior:
The HTML output to the browser would be <!-- Invalid/malicious path -->

Actual behavior:
The HTML sent includes the URL, which loads CSS from the /styles.css page.

Versions:

  • MediaWiki: 1.42.1 (8a2dc91); 06:43, 4 July 2024
  • CSS: 3.5.0 (6becec9) 03:47, 20 June 2024

Miscellaneous information:
Screenshot:

Screenshot 2024-07-08 at 18-24-02 CSS_Path traversal - [...].png (1×3 px, 311 KB)

The extension attempts to check if the URL is on the correct path, and uses wfExpandUrl to normalize the URL (CSS.class.php line 47). Compliant URL parsers would treat the backslash alone, but in practice, browsers would silently convert backslashes into slashes.

Event Timeline

Nice find.

I wonder if core wfExpandUrl() should be changed to match https://url.spec.whatwg.org/#url-parsing . I think the way extension:CSS is currently written is risky, and should use wfParseUrl instead regardless (to follow a parse and deserialize pattern instead of a validation pattern) however core mediawiki does seem at least partially at fault here.

Huh, I didn't know that there's a living standard for URLs! I guess my statement about compliant URL parsers might be wrong since MediaWiki isn't complying with it.

I initially thought that wfExpandUrl was technically not incorrect, but I suppose in this circumstance it is. Perhaps both fixes could be done.

Mstyles changed Risk Rating from N/A to High.

For checking if $url is relative to $base, I suppose we could check for all three conditions:

  • $url's origin (scheme, domain, port) is equal to $base's
  • $url's path starts with $base's path with trailing slashses stripped, and appended to "/"
  • $url's search starts with $base's

This should be reasonable to expect. However, it appears that we'll need to use our own parser or modify MediaWiki's, because:

> docker compose exec mediawiki php maintenance/run.php shell
PHP Notice:  Writing to directory /.config/psysh is not allowed. in /var/www/html/w/vendor/psy/psysh/src/ConfigPaths.php on line 327
Psy Shell v0.12.4 (PHP 8.1.20 — cli) by Justin Hileman
> $wgStylePath
= "/w/skins"

> wfParseUrl($wgStylePath)
= false

I want to try to update MediaWiki's URL parser to the standard. I've searched for wfExpandUrl and UrlUtils on Phab, and couldn't find anything related to it. Should the bug report about wfExpandUrl/UrlUtils::expand be public, or should it be fixed here privately?

I was thinking you could maybe use wfAssembleUrl(), and that passing it through that would cause questionable characters like \ to be escaped when re-assembling, but it seems like that is not the case, and wfAssembleUrl has the same problem as the other functions.

I wrote the first versions of wfRemoveDotSegments and wfAssembleUrl for reasons like this, but that was back in 2011 and based on the RFC available at the time. It seems these have not aged well.
https://static-bugzilla.wikimedia.org/bug32168.html

I'm not sure whether or not I should ship a better parser in MediaWiki core and have the extension use that, or if I should ship the spec-compliant parser in the extension, or I should replace \ with / and call it a day.

I'm not sure whether or not I should ship a better parser in MediaWiki core and have the extension use that, or if I should ship the spec-compliant parser in the extension, or I should replace \ with / and call it a day.

IMO, the first option is ideal, as there actually seems to be an issue with some of core's url-parsing functions, while the latter two are probably far more convenient and easy to do. I'm not sure any of these are "wrong", per se.

BlankEclair added a project: Patch-For-Review.

I'm busy lately, so I'll do the \ -> / method and will leave the URL parser rewrite to some other person who wants to tackle this (if there are any).

@BlankEclair since this is not deployed on WMF production it can be pushed through Gerrit and noted in the supplemental release. We can also hold for supplemental release and apply the patch then, but I don't recommend that.

@Mstyles: can you add this to the supplemental and seek CVE record?

Reopen so security see at triage

Change #1067324 had a related patch set uploaded (by Alejandro Alcaide; author: BlankEclair):

[mediawiki/extensions/CSS@REL1_42] SECURITY: Workaround path traversal abusing backslashes

https://gerrit.wikimedia.org/r/1067324

Change #1067324 merged by jenkins-bot:

[mediawiki/extensions/CSS@REL1_42] SECURITY: Workaround path traversal abusing backslashes

https://gerrit.wikimedia.org/r/1067324

Mstyles moved this task from Incoming to Our Part Is Done on the Security-Team board.

I added to the supplemental release so I'll go ahead and close it. If there's anything else, please reopen.

BlankEclair changed Author Affiliation from N/A to Wikimedia Communities.Oct 2 2024, 3:02 AM

@Mstyles @sbassett: any reason this can't be public

Nope. It should be out with the next supplemental release, which should hopefully come out very early next week.

sbassett changed the visibility from "Custom Policy" to "Public (No Login Required)".
sbassett changed the edit policy from "Custom Policy" to "All Users".
Mstyles renamed this task from Path traversal when loading stylesheets from /w/skins/ can load files outside of /w/skins/ to CVE-2024-47841: Path traversal when loading stylesheets from /w/skins/ can load files outside of /w/skins/.Oct 5 2024, 1:03 AM

Change #1199055 had a related patch set uploaded (by SomeRandomDeveloper; author: BlankEclair):

[mediawiki/extensions/CSS@REL1_39] SECURITY: Workaround path traversal abusing backslashes

https://gerrit.wikimedia.org/r/1199055

Change #1199055 merged by jenkins-bot:

[mediawiki/extensions/CSS@REL1_39] SECURITY: Workaround path traversal abusing backslashes

https://gerrit.wikimedia.org/r/1199055