Common vulnerabilities and practices for safe programming in web application development (P1)

Tram Ho

1. Control database queries to avoid SQL Injection vulnerabilities

– Risk: When querying the database, programmers often use the input string from users, these queries may be affected by SQL Injection or HQL Injection (if using Hibernate). By taking advantage of these errors, an attacker can view, edit, delete data in the database, thereby gaining admin accounts, stealing user information, etc.

– Prevention:

  • SQL queries must use PrepareStatement, all parameters must be added with the function (setParam …), not using string addition in the query.
  • For some cases using ORDER BY, unable to use the setParam function, it is possible to define an array containing all the columns (fields) that need to be ORDER BY called a whitelist. Whenever ORDER BY is needed, double check that the column (field) belongs to the whitelist array defined. Example 1: The login login code with the username / password entered by the user:

  • With the above code, when entering the username is test ‘or’ 1 ‘=’ 1, the query will be: select from users where username = ‘test’ or ‘1’ = ‘1 ′ and password =’ ​​… ‘ . The where clause will be equivalent to user_name = ‘test’. Thus, even without a password, you can still log into the system.
  • Fix the code above as follows, with the userName and password parameterized when entering the query will avoid SQL Injection errors:

Example 2: Some cases cannot prevent SQL Injection errors through the “order by” command. Because the setParam function cannot be used, the following method can be used:

Maybe you are interested

67 useful tools, libraries and resources to save time for web developers

From MVC to the Modern Web Framework

2. Processing input data to avoid XSS vulnerability

– Risk: The server results returned to users mainly in HTML. The returned content usually includes values ​​that users enter into the system that may suffer XSS errors if they do not control the input data. XSS (Cross-Site Scripting) is a technique of attack by inserting into dynamic websites (JSP, ASP, PHP …) HTML tags or dangerous scripts that can be harmful to other users. In particular, the dangerous code inserted is mostly written in Cross-Site Scrip such as JavaScript, JScript, DHTML and also HTML tags.

– Prevention:

  • Encode as HTML special characters sent by the client include: <,>, &, ‘, ”, / in cases
    • Client data sent to the server.
    • Data retrieved from the database when returned to the client. The nature of the encode is to replace the above characters with the corresponding strings in the table below:

Example 1: The JSP page below shows the greeting with the username taken from the client

  • When you enter the browser address http://localhost/example?user=abc , the browser will display Hello abc!
  • When entering the browser address http://localhost/example?user=abc<script>alert('XSS')</script> , the JavaScript will execute the XSS notification JavaScript in the browser.
  • To overcome this error we can use the JSTL library to encode the HTML variable user

I would like to pause the article here! In the following article, I will continue with “Using tokens to avoid CSRF vulnerabilities” and “Controlling file uploads to the system.”

Viblo

Share the news now

Source : Người viết: maiphuoctung