Context
I have a website where users can write their own articles. I use a contenteditable div that allows users to mark up their HTML (bold, italicize, etc.), and I want to make sure I prevent XSS attacks. To do so, I use htmlspecialchars() when outputting data onto the page. I want to leave the user's input data alone and encode it on output. However, I use the .html() method to get the HTML content in the contenteditable div, and it seems that user-inserted tags are automatically encoded by the .html() method or the contenteditable div.
The structure of the contenteditable div is
<div class="article" contenteditable="true"></div>
As an example, if the user attempts to do XSS on the contenteditable div by inserting <script>alert('hi')</script>, like so:
<div class="article" contenteditable="true">
<script>alert('hi')</script>
</div>
When I submit the data to the database (using var article = $(".article").html();), the data I see in the database isn't <script>alert('hi')</script>, but is instead <script>alert('hi')</script> despite the fact that I didn't encode anything on my own. Then, I have to avoid using htmlspecialchars() when outputting the article content so that the div encodes the string back to its original form instead of staying encoded.
Question
Does the .html() method automatically encode HTML, and if so, how would one prevent this functionality (if that is advisable)?
console.logwould answer this for you - GetSet.html()does encode HTML? Thanks for the response. - Carson D