/* goodB2G() - use badsource and goodsink */ private void goodB2G(HttpServletRequest request, HttpServletResponse response) throws Throwable { String data = (new CWE643_Unsafe_Treatment_of_XPath_Input__getCookiesServlet_61b()) .goodB2G_source(request, response); final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } /* FIX: validate input using StringEscapeUtils */ String uname = StringEscapeUtils.escapeXml(tokens[0]); String pword = StringEscapeUtils.escapeXml(tokens[1]); /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); }
/* goodG2B() - use goodsource and badsink */ private void goodG2B(HttpServletRequest request, HttpServletResponse response) throws Throwable { String data = (new CWE643_Unsafe_Treatment_of_XPath_Input__getCookiesServlet_61b()) .goodG2B_source(request, response); final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String uname = tokens[0]; String pword = tokens[1]; /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); }
public void action(String data) throws Throwable { String xmlFile = null; if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { /* running on Windows */ xmlFile = "\\src\\testcases\\CWE643_Xpath Injection\\CWE643_Xpath_Injection__Helper.xml"; } else { /* running on non-Windows */ xmlFile = "./src/testcases/CWE643_Xpath Injection/CWE643_Xpath_Injection__Helper.xml"; } if (data != null) { /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String username = tokens[0]; String password = tokens[1]; /* build xpath */ XPath xPath = XPathFactory.newInstance().newXPath(); InputSource inputXml = new InputSource(xmlFile); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* POTENTIAL FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + username + "' and pass/text()='" + password + "']" + "/secret/text()"; String secret = (String) xPath.evaluate(query, inputXml, XPathConstants.STRING); } }
/* goodB2G() - use badsource and goodsink */ public void goodB2G_sink( CWE643_Unsafe_Treatment_of_XPath_Input__fromFile_67a.Container data_container) throws Throwable { String data = data_container.a; final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } /* FIX: validate input using StringEscapeUtils */ String uname = StringEscapeUtils.escapeXml(tokens[0]); String pword = StringEscapeUtils.escapeXml(tokens[1]); /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); }
public void bad_sink( CWE643_Unsafe_Treatment_of_XPath_Input__fromFile_67a.Container data_container) throws Throwable { String data = data_container.a; final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String uname = tokens[0]; String pword = tokens[1]; /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); }
/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */ private void goodG2B2() throws Throwable { String data; switch (6) { case 6: /* FIX: Use a hardcoded string */ data = "foo"; break; default: /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = null; break; } switch (7) { case 7: String xmlFile = null; if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { /* running on Windows */ xmlFile = "\\src\\testcases\\CWE643_Xpath Injection\\CWE643_Xpath_Injection__Helper.xml"; } else { /* running on non-Windows */ xmlFile = "./src/testcases/CWE643_Xpath Injection/CWE643_Xpath_Injection__Helper.xml"; } if (data != null) { /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String username = tokens[0]; String password = tokens[1]; /* build xpath */ XPath xPath = XPathFactory.newInstance().newXPath(); InputSource inputXml = new InputSource(xmlFile); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* POTENTIAL FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + username + "' and pass/text()='" + password + "']" + "/secret/text()"; String secret = (String) xPath.evaluate(query, inputXml, XPathConstants.STRING); } break; default: /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ IO.writeLine("Benign, fixed string"); break; } }
/* goodB2G1() - use badsource and goodsink by changing second privateReturnsTrue() to privateReturnsFalse() */ private void goodB2G1() throws Throwable { String data; if (privateReturnsTrue()) { /* get system property user.home */ /* POTENTIAL FLAW: Read data from a system property */ data = System.getProperty("user.home"); } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = null; } if (privateReturnsFalse()) { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ IO.writeLine("Benign, fixed string"); } else { String xmlFile = null; if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { /* running on Windows */ xmlFile = "\\src\\testcases\\CWE643_Xpath Injection\\CWE643_Xpath_Injection__Helper.xml"; } else { /* running on non-Windows */ xmlFile = "./src/testcases/CWE643_Xpath Injection/CWE643_Xpath_Injection__Helper.xml"; } if (data != null) { /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } /* FIX: validate input using StringEscapeUtils */ String username = StringEscapeUtils.escapeXml(tokens[0]); String password = StringEscapeUtils.escapeXml(tokens[1]); /* build xpath */ XPath xPath = XPathFactory.newInstance().newXPath(); InputSource inputXml = new InputSource(xmlFile); String query = "//users/user[name/text()='" + username + "' and pass/text()='" + password + "']" + "/secret/text()"; String secret = (String) xPath.evaluate(query, inputXml, XPathConstants.STRING); } } }
public void bad(HttpServletRequest request, HttpServletResponse response) throws Throwable { String data; if (PRIVATE_STATIC_FINAL_TRUE) { /* POTENTIAL FLAW: Read data from a querystring using getParameter */ data = request.getParameter("name"); } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = null; } if (PRIVATE_STATIC_FINAL_TRUE) { String xmlFile = null; if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { /* running on Windows */ xmlFile = "\\src\\testcases\\CWE643_Xpath Injection\\CWE643_Xpath_Injection__Helper.xml"; } else { /* running on non-Windows */ xmlFile = "./src/testcases/CWE643_Xpath Injection/CWE643_Xpath_Injection__Helper.xml"; } if (data != null) { /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String username = tokens[0]; String password = tokens[1]; /* build xpath */ XPath xPath = XPathFactory.newInstance().newXPath(); InputSource inputXml = new InputSource(xmlFile); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* POTENTIAL FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + username + "' and pass/text()='" + password + "']" + "/secret/text()"; String secret = (String) xPath.evaluate(query, inputXml, XPathConstants.STRING); } } }
public void bad() throws Throwable { String data; if (IO.staticFive == 5) { data = ""; /* Initialize data */ { File file = new File("C:\\data.txt"); FileInputStream streamFileInput = null; InputStreamReader readerInputStream = null; BufferedReader readerBuffered = null; try { /* read string from file into data */ streamFileInput = new FileInputStream(file); readerInputStream = new InputStreamReader(streamFileInput, "UTF-8"); readerBuffered = new BufferedReader(readerInputStream); /* POTENTIAL FLAW: Read data from a file */ /* This will be reading the first "line" of the file, which * could be very long if there are little or no newlines in the file */ data = readerBuffered.readLine(); } catch (IOException exceptIO) { IO.logger.log(Level.WARNING, "Error with stream reading", exceptIO); } finally { /* Close stream reading objects */ try { if (readerBuffered != null) { readerBuffered.close(); } } catch (IOException exceptIO) { IO.logger.log(Level.WARNING, "Error closing BufferedReader", exceptIO); } try { if (readerInputStream != null) { readerInputStream.close(); } } catch (IOException exceptIO) { IO.logger.log(Level.WARNING, "Error closing InputStreamReader", exceptIO); } try { if (streamFileInput != null) { streamFileInput.close(); } } catch (IOException exceptIO) { IO.logger.log(Level.WARNING, "Error closing FileInputStream", exceptIO); } } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = null; } if (IO.staticFive == 5) { String xmlFile = null; if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { /* running on Windows */ xmlFile = "\\src\\testcases\\CWE643_Xpath Injection\\CWE643_Xpath_Injection__Helper.xml"; } else { /* running on non-Windows */ xmlFile = "./src/testcases/CWE643_Xpath Injection/CWE643_Xpath_Injection__Helper.xml"; } if (data != null) { /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String username = tokens[0]; String password = tokens[1]; /* build xpath */ XPath xPath = XPathFactory.newInstance().newXPath(); InputSource inputXml = new InputSource(xmlFile); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* POTENTIAL FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + username + "' and pass/text()='" + password + "']" + "/secret/text()"; String secret = (String) xPath.evaluate(query, inputXml, XPathConstants.STRING); } } }
public void bad() throws Throwable { String data; switch (6) { case 6: data = ""; /* Initialize data */ /* Read data from a database */ { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { /* setup the connection */ connection = IO.getDBConnection(); /* prepare and execute a (hardcoded) query */ preparedStatement = connection.prepareStatement("select name from users where id=0"); resultSet = preparedStatement.executeQuery(); /* POTENTIAL FLAW: Read data from a database query resultset */ data = resultSet.getString(1); } catch (SQLException exceptSql) { IO.logger.log(Level.WARNING, "Error with SQL statement", exceptSql); } finally { /* Close database objects */ try { if (resultSet != null) { resultSet.close(); } } catch (SQLException exceptSql) { IO.logger.log(Level.WARNING, "Error closing ResultSet", exceptSql); } try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException exceptSql) { IO.logger.log(Level.WARNING, "Error closing PreparedStatement", exceptSql); } try { if (connection != null) { connection.close(); } } catch (SQLException exceptSql) { IO.logger.log(Level.WARNING, "Error closing Connection", exceptSql); } } } break; default: /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = null; break; } switch (7) { case 7: String xmlFile = null; if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { /* running on Windows */ xmlFile = "\\src\\testcases\\CWE643_Xpath Injection\\CWE643_Xpath_Injection__Helper.xml"; } else { /* running on non-Windows */ xmlFile = "./src/testcases/CWE643_Xpath Injection/CWE643_Xpath_Injection__Helper.xml"; } if (data != null) { /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String username = tokens[0]; String password = tokens[1]; /* build xpath */ XPath xPath = XPathFactory.newInstance().newXPath(); InputSource inputXml = new InputSource(xmlFile); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* POTENTIAL FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + username + "' and pass/text()='" + password + "']" + "/secret/text()"; String secret = (String) xPath.evaluate(query, inputXml, XPathConstants.STRING); } break; default: /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ IO.writeLine("Benign, fixed string"); break; } }
/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */ private void goodB2G2(HttpServletRequest request, HttpServletResponse response) throws Throwable { String data; switch (6) { case 6: { Logger log_bad = Logger.getLogger("local-logger"); /* read parameter from request */ data = request.getParameter("name"); } break; default: /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ { java.util.logging.Logger log_good = java.util.logging.Logger.getLogger("local-logger"); /* FIX: Use a hardcoded string */ data = "foo"; } break; } switch (7) { case 7: { final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } /* FIX: validate input using StringEscapeUtils */ String uname = StringEscapeUtils.escapeXml(tokens[0]); String pword = StringEscapeUtils.escapeXml(tokens[1]); /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); } break; default: /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ { final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String uname = tokens[0]; String pword = tokens[1]; /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); } break; } }
/* goodB2G2() - use badsource and goodsink by reversing statements in second if */ private void goodB2G2() throws Throwable { String data; if (IO.static_returns_t()) { Logger log_bad = Logger.getLogger("local-logger"); data = ""; /* init data */ Connection conn = null; PreparedStatement statement = null; ResultSet rs = null; BufferedReader buffread = null; InputStreamReader instrread = null; try { /* setup the connection */ conn = IO.getDBConnection(); /* prepare the query */ statement = conn.prepareStatement("select name from users where id=?"); /* get user input for the userid */ IO.writeLine("Enter a userid to login as (number): "); instrread = new InputStreamReader(System.in); buffread = new BufferedReader(instrread); int num = Integer.parseInt(buffread.readLine()); statement.setInt(1, num); rs = statement.executeQuery(); data = rs.getString(1); } catch (IOException ioe) { log_bad.warning("Error with stream reading"); } finally { /* clean up stream reading objects */ try { if (buffread != null) { buffread.close(); } } catch (IOException ioe) { log_bad.warning("Error closing buffread"); } finally { try { if (instrread != null) { instrread.close(); } } catch (IOException ioe) { log_bad.warning("Error closing instrread"); } } /* clean up database objects */ try { if (rs != null) { rs.close(); } } catch (SQLException se) { log_bad.warning("Error closing rs"); } finally { try { if (statement != null) { statement.close(); } } catch (SQLException se) { log_bad.warning("Error closing statement"); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException se) { log_bad.warning("Error closing conn"); } } } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ java.util.logging.Logger log_good = java.util.logging.Logger.getLogger("local-logger"); /* FIX: Use a hardcoded string */ data = "foo"; } if (IO.static_returns_t()) { final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } /* FIX: validate input using StringEscapeUtils */ String uname = StringEscapeUtils.escapeXml(tokens[0]); String pword = StringEscapeUtils.escapeXml(tokens[1]); /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String uname = tokens[0]; String pword = tokens[1]; /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); } }
public void bad() throws Throwable { String data; /* INCIDENTAL: CWE 571 Statement is Always True */ if (private_returns_t()) { Logger log_bad = Logger.getLogger("local-logger"); /* get environment variable ADD */ data = System.getenv("ADD"); } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ java.util.logging.Logger log_good = java.util.logging.Logger.getLogger("local-logger"); /* FIX: Use a hardcoded string */ data = "foo"; } /* INCIDENTAL: CWE 571 Statement is Always True */ if (private_returns_t()) { final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } String uname = tokens[0]; String pword = tokens[1]; /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* FLAW: user input is used without validate */ String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ final String xmldoc = "\\src\\testcases\\CWE643_Unsafe_Treatment_of_XPath_Input\\console_to_evaluate\\CWE643_Unsafe_Treatment_of_XPath_Input__helper.xml"; /* assume username||password as source */ String[] tokens = data.split("||"); if (tokens.length < 2) { return; } /* FIX: validate input using StringEscapeUtils */ String uname = StringEscapeUtils.escapeXml(tokens[0]); String pword = StringEscapeUtils.escapeXml(tokens[1]); /* build xpath */ XPath xp = XPathFactory.newInstance().newXPath(); InputSource inxml = new InputSource(xmldoc); String query = "//users/user[name/text()='" + uname + "' and pass/text()='" + pword + "']" + "/secret/text()"; String secret = (String) xp.evaluate(query, inxml, XPathConstants.STRING); } }