You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Persist_XSS.java 1.9 kB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package Persist_XSS;
  2. import javax.servlet.http.*;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.io.IOException;
  8. import java.util.logging.Logger;
  9. public class Persist_XSS
  10. {
  11. public PreparedStatement statement = null;
  12. public ResultSet rs = null;
  13. static final Logger log = Logger.getLogger("local-logger");
  14. public void bad(HttpServletRequest request, HttpServletResponse response,Connection conn)
  15. {
  16. String data = ""; /* init data */
  17. try {
  18. statement = conn.prepareStatement("select name from users where name = 'lily'");
  19. rs = statement.executeQuery();
  20. data = rs.getString(1);
  21. } catch (SQLException e1) {
  22. log.info("SQLException");
  23. }finally {
  24. try {
  25. rs.close();
  26. } catch (SQLException se) {
  27. log.info("Error closing conn");
  28. }
  29. try {
  30. statement.close();
  31. } catch (SQLException se) {
  32. log.info("Error closing conn");
  33. }
  34. try {
  35. conn.close();
  36. } catch (SQLException se) {
  37. log.info("Error closing conn");
  38. }
  39. }
  40. if (data != null)
  41. {
  42. /* POTENTIAL FLAW: data not validated */
  43. try {
  44. response.getWriter().println("<br>bad() - Parameter name has value " + data); // bad 存储型XSS
  45. } catch (IOException e) {
  46. log.info("IOException");
  47. }
  48. }
  49. }
  50. public void good(HttpServletRequest request, HttpServletResponse response)
  51. {
  52. String data;
  53. /* FIX: Use a hardcoded string */
  54. data = "foo";
  55. /* POTENTIAL FLAW: data not validated */
  56. try {
  57. response.getWriter().println("<br>bad() - Parameter name has value " + data); // good 存储型XSS
  58. } catch (IOException e) {
  59. log.info("IOException");
  60. }
  61. }
  62. }

No Description

Contributors (1)