GHSA-q898-frwq-f3qp: Minecraft MOTD Parser's HtmlGenerator vulnerable to XSS
SummaryThe `HtmlGenerator` class is subject to potential cross-site scripting (XSS) attack through a parsed malformed Minecraft server MOTD.ContextMinecraft server owners can set a so-called MOTD (Message of the Day) for their server that appears next to the server icon and below the server name on the multiplayer server list of a player's Minecraft client. The Minecraft server sends the MOTD in the `description` property of the [Status Response](https://wiki.vg/Server_List_Ping#Status_Response) packet. The [jgniecki/MinecraftMotdParser](https://github.com/jgniecki/MinecraftMotdParser) PHP library is able to parse the value of the `description` property, which can be either a string or an array of text components. By utilizing the aforementioned `HtmlGenerator` class, it is also able to transform the value into an HTML string that can be used to visualize the MOTD on a web page.DetailsThe `HtmlGenerator` iterates through objects of `MotdItem` that are contained in an object of `MotdItemCollection` to generate a HTML string. An attacker can make malicious inputs to the `color` and `text` properties of `MotdItem` to inject own HTML into a web page during web page generation. For example by sending a malicious MOTD from a Minecraft server under their control that was queried and passed to the `HtmlGenerator`.This XSS vulnerability exists because the values of these properties are neither filtered nor escaped, as can be seen here:https://github.com/jgniecki/MinecraftMotdParser/blob/0412f68eeb91729a00444a8d6c00c45623884aa5/src/Generator/HtmlGenerator.php#L49https://github.com/jgniecki/MinecraftMotdParser/blob/0412f68eeb91729a00444a8d6c00c45623884aa5/src/Generator/HtmlGenerator.php#L80Proof of ConceptJavaScript code can be injected into the `HtmlGenerator` by parsing either a string via `TextParser` or an array via `ArrayParser`. The following code examples demonstrate the vulnerability by triggering the alert dialog of the browser.XSS via `TextParser`XSS via `ArrayParser`ImpactIf the `HtmlGenerator` class of this library is used, this XSS vulnerability can potentially affect:Players visiting Minecraft server list websites (of which there are several dozen online, written in PHP) that display the MOTD.Users visiting Minecraft server status websites to query information about a Minecraft server.Server owners managing their Minecraft server via a web interface that displays the MOTD, where the attack could be carried out by a malicious Minecraft server plugin that modifies the MOTD without the server owner's consent.It is not clear if and which platforms depend on this library.RemediationI suggest converting all HTML special characters in the values of the `color` and `text` properties to HTML entities. The display of the HTML entities will still be correct in the browser, but the XSS vulnerability will be eliminated as the values will no longer be interpreted as HTML by the browser.This could be achieved by introducing a new private `escape` function in the `HtmlGenerator` class:This function should be called in the following two lines:https://github.com/jgniecki/MinecraftMotdParser/blob/0412f68eeb91729a00444a8d6c00c45623884aa5/src/Generator/HtmlGenerator.php#L49
Change to: `$tags['span'][] = sprintf('color: %s;', $this->escape($motdItem->getColor()));`https://github.com/jgniecki/MinecraftMotdParser/blob/0412f68eeb91729a00444a8d6c00c45623884aa5/src/Generator/HtmlGenerator.php#L80
Change to: `$value = sprintf($value, $this->escape($motdItem->getText()));`
CVSS v4.0: 6.9
CVSS v3.1: 6.1