AssetInjector::prepareOutput() breaks HTML with “ in the markup
-
The
AssetInjector::prepareOutput()
injects unrelated HTML tags anywhere the string</head>
appears in the page source.Now let’s consider a page with this script inside:
<script> var foo = {"htmlExample":"<!DOCTYPE html><html><head><link rel=\"stylesheet\" type=\"text\/css\" href=\"https:\/\/example.com\/style.css\" media=\"all\"><\/head><body><\/body><\/html>"}; </script>
This is perfectly valid HTML containing a script that contains an object literal variable declaration that happens to store a piece of HTML under an object property.
Smart Slider 3 injects
< link rel="stylesheet" type="text/css" href="..." media="all">
etc. before the</head>
inside the object literal breaking website in two ways:- It breaks the whole
<script
by injecting unescaped"
character at arbitrary position into a JS object literal, triggering syntax error. - Or if the given string may happen to be written using single quotes, the script will not break, but now Smart Slider 3 is basically injecting unrelated links, scripts and styles to a HTML handled by an unrelated JS application. Don’t even think about how bad it can become if that application may later use the HTML as an actual page of a site. In that case Smart Slider 3 has just prepared an XSS on that site. Which is mundane considering the payload, but if that is an independent site, from their perspective I can now change a javascript file on my site to trigger an attack. Basically the plugin made me plant a backdoor to a HTML handled by my web-app.
WordPress offers stable way of adding scripts and styles to the real head of pages:
wp_enqueue_script
,wp_enqueue_style
. If those methods were used, this issue did not happen.Also it is a huge anti-pattern to process HTML using regex or similar low level tools, and there’s a reason to that! Even such simple thing that looking for the closing
</head
tag can go catastrophic breaking sites and applications.It is really not nice to waste time on troubleshooting issues that should not be there in the first place. Why reinvent the wheel by writing all these complex classes to do something simple as adding scripts and styles? WordPress has an API for a reason, and that actually works.
- It breaks the whole
- The topic ‘AssetInjector::prepareOutput() breaks HTML with “ in the markup’ is closed to new replies.