...
Code Block | ||
---|---|---|
| ||
private void createXMLStream(BufferedOutputStream outStream, String quantity) throws IOException { String xmlString; xmlString = "<item>\n<description>Widget</description>\n" + "<price>500.0</price>\n" + "<quantity>" + quantity + "</quantity></item>"; outStream.write(xmlString.getBytes()); outStream.flush(); } |
Compliant Solution (
...
Input Validation)
Depending on the specific data and command interpreter or parser to which data is being sent, appropriate methods must be used to sanitize untrusted user input. This compliant solution uses whitelisting to sanitize the input. In this compliant solution, the method requires that the quantity
field be a number between 0 and 9validates that quantity
is an unsigned integer.
Code Block | ||
---|---|---|
| ||
private void createXMLStream(BufferedOutputStream outStream, String quantity) throws IOException { // Write XML string if quantity contains numbers only. // Blacklisting of invalid characters can be performed // in conjunction. if (!Pattern.matches("[0-9]+", quantity)) { // Format violation } String xmlString = "<item>\n<description>Widget</description>\n" + "<price>500</price>\n" + "<quantity>" + quantity + "</quantity></item>"; outStream.write(xmlString.getBytes()); outStream.flush(); } |
...