Lab: DOM XSS in jQuery anchor href attribute sink using location.search source
Observe
- Open
Submit Feedbackpage. - Right click and
Inspectthe web page. - Read the source code and notice that
scripttag insideformtag we foundjQuerylike this. It reads query parameter namedreturnPathand placed the value tohrefattributes inatag withidnamedbacklink.
<script>
$(function() {
$('#backLink').attr("href", (new URLSearchParams(window.location.search)).get('returnPath'));
});
</script>- If we look at the source code again, we found that any
atag withidnamedbacklink. In script below, the value ofhrefattributes is/because by default if we open the web page, value ofreturnPathin URL is/.
<a id="backLink" href="/">Back</a>Solution
- Change the value of
returnPathin URL tojavascript:alert(document.cookie)like this:
?returnPath=javascript:alert(document.cookie)- Hit enter and click button
"back" - The script will executed
Conclusion
After change the value of returnPath in URL to javascript:alert(document.cookie) and hit enter, value javascript:alert(document.cookie) will be assigned to href attributes and it will constructed like <a id="backLink" href="javascript:alert(document.cookie)">Back</a>. And when we click button back, it will be executed and popup will showed.