|
| 1 | +package demo.security.servlet; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.PrintWriter; |
| 5 | +import java.sql.Connection; |
| 6 | +import java.sql.DriverManager; |
| 7 | +import java.sql.PreparedStatement; |
| 8 | +import java.sql.ResultSet; |
| 9 | +import java.sql.Statement; |
| 10 | +import java.util.logging.Logger; |
| 11 | +import javax.servlet.ServletException; |
| 12 | +import javax.servlet.annotation.WebServlet; |
| 13 | +import javax.servlet.http.HttpServlet; |
| 14 | +import javax.servlet.http.HttpServletRequest; |
| 15 | +import javax.servlet.http.HttpServletResponse; |
| 16 | + |
| 17 | +@WebServlet("/api/search") |
| 18 | +public class SearchServlet extends HttpServlet { |
| 19 | + private static final long serialVersionUID = 1L; |
| 20 | + private static final Logger logger = Logger.getLogger(SearchServlet.class.getName()); |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| 24 | + throws ServletException, IOException { |
| 25 | + |
| 26 | + // SonarQube Issue: Taint Analysis - Complex data flow vulnerability |
| 27 | + String searchTerm = request.getParameter("q"); |
| 28 | + String category = request.getParameter("category"); |
| 29 | + String sortBy = request.getParameter("sort"); |
| 30 | + |
| 31 | + // This creates a taint flow from user input to SQL query |
| 32 | + String dynamicQuery = buildSearchQuery(searchTerm, category, sortBy); |
| 33 | + |
| 34 | + try { |
| 35 | + Connection conn = DriverManager.getConnection( |
| 36 | + "jdbc:mysql://localhost:3306/products", "root", "password"); |
| 37 | + |
| 38 | + // SonarQube Issue: SQL Injection - taint analysis will catch this |
| 39 | + Statement stmt = conn.createStatement(); |
| 40 | + ResultSet rs = stmt.executeQuery(dynamicQuery); |
| 41 | + |
| 42 | + response.setContentType("text/html"); |
| 43 | + PrintWriter out = response.getWriter(); |
| 44 | + out.print("<h2>Search Results</h2>"); |
| 45 | + |
| 46 | + while (rs.next()) { |
| 47 | + // SonarQube Issue: XSS vulnerability - direct output |
| 48 | + out.print("<div>Product: " + rs.getString("name") + |
| 49 | + " - Price: $" + rs.getString("price") + "</div>"); |
| 50 | + } |
| 51 | + |
| 52 | + rs.close(); |
| 53 | + stmt.close(); |
| 54 | + conn.close(); |
| 55 | + |
| 56 | + } catch (Exception e) { |
| 57 | + // SonarQube Issue: Information disclosure in error message |
| 58 | + logger.severe("Search failed for term: " + searchTerm + " - " + e.getMessage()); |
| 59 | + response.getWriter().print("Error: " + e.getMessage()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // SonarQube Issue: Taint Analysis - This method propagates taint |
| 64 | + private String buildSearchQuery(String searchTerm, String category, String sortBy) { |
| 65 | + StringBuilder query = new StringBuilder("SELECT * FROM products WHERE 1=1"); |
| 66 | + |
| 67 | + if (searchTerm != null && !searchTerm.isEmpty()) { |
| 68 | + // Taint flows through string concatenation |
| 69 | + query.append(" AND name LIKE '%").append(searchTerm).append("%'"); |
| 70 | + } |
| 71 | + |
| 72 | + if (category != null && !category.isEmpty()) { |
| 73 | + // Taint flows through string concatenation |
| 74 | + query.append(" AND category = '").append(category).append("'"); |
| 75 | + } |
| 76 | + |
| 77 | + if (sortBy != null && !sortBy.isEmpty()) { |
| 78 | + // Taint flows through string concatenation - ORDER BY injection |
| 79 | + query.append(" ORDER BY ").append(sortBy); |
| 80 | + } |
| 81 | + |
| 82 | + return query.toString(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 87 | + throws ServletException, IOException { |
| 88 | + |
| 89 | + // SonarQube Issue: Taint Analysis - File upload vulnerability |
| 90 | + String fileName = request.getParameter("filename"); |
| 91 | + String fileContent = request.getParameter("content"); |
| 92 | + |
| 93 | + if (fileName != null && fileContent != null) { |
| 94 | + // SonarQube Issue: Path Traversal - taint analysis will catch this |
| 95 | + String filePath = "/uploads/" + fileName; |
| 96 | + |
| 97 | + try { |
| 98 | + // Simulate file writing (in real app, this would write to filesystem) |
| 99 | + logger.info("Writing file: " + filePath + " with content: " + fileContent); |
| 100 | + |
| 101 | + // SonarQube Issue: Command Injection - taint analysis will catch this |
| 102 | + String command = "process_file " + fileName; |
| 103 | + Runtime.getRuntime().exec(command); |
| 104 | + |
| 105 | + response.getWriter().print("File uploaded successfully: " + fileName); |
| 106 | + |
| 107 | + } catch (Exception e) { |
| 108 | + // SonarQube Issue: Generic exception handling |
| 109 | + throw new RuntimeException("File upload failed", e); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // SonarQube Issue: Security Hotspot - Weak cryptographic algorithm |
| 115 | + private String hashPassword(String password) { |
| 116 | + // This would be flagged as a security hotspot |
| 117 | + return Integer.toString(password.hashCode()); |
| 118 | + } |
| 119 | + |
| 120 | + // SonarQube Issue: Code Smell - Long method |
| 121 | + private void processComplexData(HttpServletRequest request) { |
| 122 | + String data1 = request.getParameter("data1"); |
| 123 | + String data2 = request.getParameter("data2"); |
| 124 | + String data3 = request.getParameter("data3"); |
| 125 | + String data4 = request.getParameter("data4"); |
| 126 | + String data5 = request.getParameter("data5"); |
| 127 | + |
| 128 | + // Process data1 |
| 129 | + if (data1 != null) { |
| 130 | + String processed1 = data1.toUpperCase(); |
| 131 | + logger.info("Processed data1: " + processed1); |
| 132 | + } |
| 133 | + |
| 134 | + // Process data2 |
| 135 | + if (data2 != null) { |
| 136 | + String processed2 = data2.toLowerCase(); |
| 137 | + logger.info("Processed data2: " + processed2); |
| 138 | + } |
| 139 | + |
| 140 | + // Process data3 |
| 141 | + if (data3 != null) { |
| 142 | + String processed3 = data3.trim(); |
| 143 | + logger.info("Processed data3: " + processed3); |
| 144 | + } |
| 145 | + |
| 146 | + // Process data4 |
| 147 | + if (data4 != null) { |
| 148 | + String processed4 = data4.replace(" ", "_"); |
| 149 | + logger.info("Processed data4: " + processed4); |
| 150 | + } |
| 151 | + |
| 152 | + // Process data5 |
| 153 | + if (data5 != null) { |
| 154 | + String processed5 = data5.substring(0, Math.min(10, data5.length())); |
| 155 | + logger.info("Processed data5: " + processed5); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments