
PKumar022351 (Community Member) asked a question.
In our Code :
out.println("<font id=\"searchResultsInfoAdd\">" + anchorTagPartyName + name + "</a></font></td>");
Using this Approach: its not working
out.println("<font id=\"searchResultsInfoAdd\">" + ESAPI.encoder().encodeForHTML(anchorTagPartyName) + ESAPI.encoder().encodeForHTML(name) + "</a></font></td>");
But in my UI Screen javaScript code displaying. Can you please assist how can I resolve this flaws ? Thanks in Advance !
.png)
Hi @PKumar022351 (Community Member) ,
The primary defence against CWE 80 Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS) is correctly contextually HTML encoding using one of our Supported Cleansing Functions: https://help.veracode.com/reader/4EKhlLSMHm5jC8P8j3XccQ/IiF_rOE79ANbwnZwreSPGA .
ESAPI.encoder().encodeForHTML is one of our supported cleansing functions and if applied correctly and consistently should get the flaw automatically closed.
However, you can only encode at the point where the general variable data becomes HTML. If you encode the data too late you risk breaking the HTML (it sounds like this is what's happening).
So in your example if your application does something like this:
String anchorTagPartyName = "<a href="/user-profile">" + name + "</a>";
out.println("<font id=\"searchResultsInfoAdd\">" + anchorTagPartyName + name + "</a></font></td>");
Then doing:
String anchorTagPartyName = "<a href="/user-profile">" + name + "</a>";
out.println("<font id=\"searchResultsInfoAdd\">" + ESAPI.encoder().encodeForHTML(anchorTagPartyName) + ESAPI.encoder().encodeForHTML(name) + "</a></font></td>");
will off course break the <a> tag.
In this case we'd recommend doing this instead:
String anchorTagPartyNameHTML = "<a href="/user-profile">" + ESAPI.encoder().encodeForHTML(name) + "</a>";
out.println("<font id=\"searchResultsInfoAdd\">" + anchorTagPartyNameHTML + ESAPI.encoder().encodeForHTML(name) + "</a></font></td>");
Unfortunately, determining where you should be encoding depends very much on where you are adding HTML and the design of your application. If you have any questions on this I would recommend scheduling a Consultation with a Veracode Application Security Consultant. You can find more on how to do this here: https://help.veracode.com/reader/_xqy2rBkFBfaxzuiNu~wsg/kAO0UeevRvOPnidK6_e5IA .
Thank you,
Boy Baukema