Mã bên dưới là từ SAMATE Reference Dataset. Tôi đã sử dụng nó để kiểm tra một công cụ phân tích tĩnh. Như bạn có thể thấy mã nên ngăn chặn SQL-Injection cả bằng cách sử dụng một phương pháp vệ sinh cũng như sử dụng một tuyên bố chuẩn bị.Câu lệnh chuẩn bị sẵn sàng ngăn SQL-Injection ở đây
Vì các công cụ SCA không thể biết các phương thức santitzation tùy chỉnh, sẽ không phát hiện ra phương pháp allowed
được sử dụng để ngăn chặn việc tiêm.
public class SQLInjection_good_089 extends HttpServlet
{
private static final long serialVersionUID = 1L;
public SQLInjection_good_089()
{
super();
}
// Table of allowed names to use
final String allowed_names[] = { "Mickael", "Mary",
"Peter", "Laura", "John"};
// Function to check if the current name takes part of the allowed ones
public boolean allowed(String in)
{
boolean bool = false;
for(int i = 0; i < 5; i++)
{
if(in.equals(allowed_names[i]))
{
// the current name is allowed to use
bool = true;
break;
}
}
return bool;
}
// Method which will be called to handle HTTP GET requests
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// Initialize the output stream
resp.setContentType("text/html");
ServletOutputStream out = resp.getOutputStream();
out.println("<HTML><BODY><blockquote><pre>");
Connection conn = null;
// Get the parameter "name" from the data provided by the user
String name = req.getParameter("name");
if ((name != null) && (allowed(name) == true))
{
try
{
// Set the context factory to use to create the initial context
System.setProperty (Context.INITIAL_CONTEXT_FACTORY, "your.ContextFactory");
// Create the initial context and use it to lookup the data source
InitialContext ic = new InitialContext();
DataSource dataSrc = (DataSource) ic.lookup ("java:comp/env/jdbc:/mydb");
// Create a connection to the SQL database from the data source
conn = dataSrc.getConnection();
// Send a SQL request to the database
PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE firstname LIKE ?");
// replace the first parameter by name
ps.setString(1, name);
ps.executeQuery();
}
catch(NamingException e)
{
out.println("Naming exception");
}
catch(SQLException e)
{
out.println("SQL exception");
}
finally
{
try
{
if (conn != null)
conn.close();
}
catch (SQLException se)
{
out.println("SQL Exception");
}
}
}
else
return;
out.println("</pre></blockquote></body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
}
}
Tuy nhiên, tôi nghĩ rằng việc sử dụng câu lệnh chuẩn bị sẽ ngăn chặn việc tiêm ở đây. Tôi có nhầm không?
Về mặt kỹ thuật, đây là một trục trặc của bản thân tôi. Tôi vô tình nhìn vào kết quả cho dự án sai. Đừng bận tâm. Cảm ơn – er4z0r