/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest01030.java

https://gitlab.com/disextr/BenchmarkJava · Java · 122 lines · 85 code · 15 blank · 22 comment · 10 complexity · 6e6f5984b345fa5151a6c5dfc21a7372 MD5 · raw file

  1. /**
  2. * OWASP Benchmark Project v1.2
  3. *
  4. * <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project. For
  5. * details, please see <a
  6. * href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>.
  7. *
  8. * <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms
  9. * of the GNU General Public License as published by the Free Software Foundation, version 2.
  10. *
  11. * <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. * PURPOSE. See the GNU General Public License for more details.
  14. *
  15. * @author Dave Wichers
  16. * @created 2015
  17. */
  18. package org.owasp.benchmark.testcode;
  19. import java.io.IOException;
  20. import javax.servlet.ServletException;
  21. import javax.servlet.annotation.WebServlet;
  22. import javax.servlet.http.HttpServlet;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. @WebServlet(value = "/pathtraver-01/BenchmarkTest01030")
  26. public class BenchmarkTest01030 extends HttpServlet {
  27. private static final long serialVersionUID = 1L;
  28. @Override
  29. public void doGet(HttpServletRequest request, HttpServletResponse response)
  30. throws ServletException, IOException {
  31. doPost(request, response);
  32. }
  33. @Override
  34. public void doPost(HttpServletRequest request, HttpServletResponse response)
  35. throws ServletException, IOException {
  36. response.setContentType("text/html;charset=UTF-8");
  37. String param = "";
  38. if (request.getHeader("BenchmarkTest01030") != null) {
  39. param = request.getHeader("BenchmarkTest01030");
  40. }
  41. // URL Decode the header value since req.getHeader() doesn't. Unlike req.getParameter().
  42. param = java.net.URLDecoder.decode(param, "UTF-8");
  43. String bar = new Test().doSomething(request, param);
  44. // FILE URIs are tricky because they are different between Mac and Windows because of lack
  45. // of standardization.
  46. // Mac requires an extra slash for some reason.
  47. String startURIslashes = "";
  48. if (System.getProperty("os.name").indexOf("Windows") != -1)
  49. if (System.getProperty("os.name").indexOf("Windows") != -1) startURIslashes = "/";
  50. else startURIslashes = "//";
  51. try {
  52. java.net.URI fileURI =
  53. new java.net.URI(
  54. "file",
  55. null,
  56. startURIslashes
  57. + org.owasp.benchmark.helpers.Utils.TESTFILES_DIR
  58. .replace('\\', java.io.File.separatorChar)
  59. .replace(' ', '_')
  60. + bar,
  61. null,
  62. null);
  63. java.io.File fileTarget = new java.io.File(fileURI);
  64. response.getWriter()
  65. .println(
  66. "Access to file: '"
  67. + org.owasp
  68. .esapi
  69. .ESAPI
  70. .encoder()
  71. .encodeForHTML(fileTarget.toString())
  72. + "' created.");
  73. if (fileTarget.exists()) {
  74. response.getWriter().println(" And file already exists.");
  75. } else {
  76. response.getWriter().println(" But file doesn't exist yet.");
  77. }
  78. } catch (java.net.URISyntaxException e) {
  79. throw new ServletException(e);
  80. }
  81. } // end doPost
  82. private class Test {
  83. public String doSomething(HttpServletRequest request, String param)
  84. throws ServletException, IOException {
  85. String bar;
  86. String guess = "ABC";
  87. char switchTarget = guess.charAt(1); // condition 'B', which is safe
  88. // Simple case statement that assigns param to bar on conditions 'A', 'C', or 'D'
  89. switch (switchTarget) {
  90. case 'A':
  91. bar = param;
  92. break;
  93. case 'B':
  94. bar = "bob";
  95. break;
  96. case 'C':
  97. case 'D':
  98. bar = param;
  99. break;
  100. default:
  101. bar = "bob's your uncle";
  102. break;
  103. }
  104. return bar;
  105. }
  106. } // end innerclass Test
  107. } // end DataflowThruInnerClass