I have a javascript function (with some PHP) on my website that will add an element to the DOM. The element will be styled via the element's style
attribute. And part of the value of that attribute will be provided by the user.
A simplified example:
function addEl(bgColorStr) {
$('<div/>')
.attr('id', 'overlay')
.css({
width: '100vw',
height: '100vh',
background-color: '<?php print getAdminBG() ?>'
}).appendTo($('body'));
}
Since the value returned by getAdminBG()
will (or should) ALWAYS be sanitized/escaped/etc-ed before being inserted into the database, this question is more of a theoretical one, I guess.
Let's pretend an attacker found a way to get by the cleansing performed on the server prior to adding to the database and could add any string he wanted. Is there anything he could add that could any sort of damage?
With an "allowed" value, the element would look like this:
<div id="overlay" style="width:100vw;height:100vh;background-color:rgba(0,0,0,.75);"></div>
but an attacker might provide a value to try to make it look more like this:
<div id="overlay" style="width:100vw;height:100vh;" onclick="executeSomethingMightyMalicious();"></div>
As far as I can tell, if you try to assign an invalid value for a given CSS property, the browser will exclude that property (and its value) from the value of the style
attribute.
So by that logic, even if nothing were escaped or stripped from the user-provided value before being inserted into the database and nothing is escaped or stripped on the client-side before creating the new element and inserting it into the DOM with the user-supplied value included in the string making up the value of the element's style
attribute, it's still not possible to execute (or really even add) any code via an attribute (most likely an event handler, I'm guessing?) by modifying the style
attribute in evil ways. Is this accurate/correct?
** Sorry for the crazy run-on sentence at the end there.