Ste (Community Member) asked a question.
I'm looking for a solution for CWE 159 in JavaScript code embedded in HTML code inside a JSP page like in the following example:
<script>
const id = '<%=org.owasp.encoder.Encode.forJavaScript(id)%>';
</script>
From the JavaDoc of OWASP's Encode.forJavaScript(String):
"Encodes for a JavaScript string. It is safe for use
in HTML script attributes (such as onclick), script
blocks, JSON files, and JavaScript source."
Despite the statement that this method should be safe for the context in which I use it, Veracode Static Analysis keeps flagging lines like the above with a CWE 159 issue. We have over 200 (!) such issues flagged by Veracode in our application. This is why I'm desperately looking for a solution.
I have been testing various other cleansing methods from the official list published by Veracode. And it turns out that there is a method which seems to be accepted by Veracode for the above context: If I use Encode.forHtmlAttribute(String) instead of Encode.forJavaScript(String), Veracode does not complain anymore.
BUT ...
Encode.forHtmlAttribute(String) is NOT SAFE for this context. It does not encode newline characters or backslash characters. If those characters appear in the text but are not encoded, the resulting JavaScript code will become illegal.
My current working assumption is that the Veracode scan engine is not correctly detecting the context. It probably sees the leading and trailing quotes and then believes that this must be an HTML attribute value. This is why it accepts Encode.forHtmlAttribute(String) as a fix, but not Encode.forJavaScript(String) or Encode.forJavaScriptBlock(String).
Has someone experienced this as well?
And if yes, have you found a solution for this problem?
Any help would be very appreciated!
Merry X-mas and a Happy New Year to everyone,
Stephan
PS: On request, I can provide a mini webapp (WAR file) which can be used to reproduce my scan results.
.png)
Here are some screenshots from the "Triage Flaws" page in Veracode. The first shows the flaws in JavaScript context. The orange mark on the left is for the (correctly identified) CWE 80. All others are for CWE 159.
The second shows the same cleansing methods used in the context of HTML attributes. As you can see, Veracode is flagging the same type of issue for the same type of cleansing method as above. In both contexts, Encode.forHtmlAttribute(String) is considered safe.
Hi @Ste (Community Member) ,
Veracode Static Analysis reports an informational flaw of CWE 159 if it believes an encoder is used in the incorrect context. This flaw is intentionally marked as 'Informational' as it can be very difficult for our engine to be able to detect the correct context especially if it requires runtime information.
Unfortunately, it appears that we are incorrectly detecting the context for the example given. We do not recommend using the 'HTML Attribute' context cleansers inside a <script> tag, as you mentioned the 'forJavaScript' cleanser is correct.
Please mark propose a mitigation ( https://help.veracode.com/reader/DGHxSJy3Gn3gtuSIN2jkRQ/~p4MSKOS8F8X8h0KwFTKoQ ) as 'Potential False Positive' in the Veracode Platform and contact your security team to approve these proposals. Please feel free to link to this community post in your proposal.
If you could provide a zip file of the example source code via email to me I would be more than happy to provide this to Veracode Engineering for future improvement. Please send it to 'bbaukema' on the 'veracode.com' domain.
Thank you,
Boy Baukema
Hi Boy,
Seems like it has been 2 years since you posted your message.
Is there any progress on this? We are in the same boat, looking forward Veracode Scanner recognizes the CWE159 fix.
Thank
Onut
Hi @OUran164170 (Community Member) ,
Improvements for CWE 159 are not currently on the roadmap.
To help inform Veracode Product Management please consider registering your request for improved CWE 159 scan quality with Veracode Community Ideas at https://community.veracode.com/s/ideas .
Thank you,
Boy Baukema