SQL Injection AttacksSQL Injection attacks use crafted, unusual user input to cause undesired SQL statements to be executed. An application is vulnerable to such attacks whenever it builds SQL statements directly out of raw user input. If an application builds SQL statements out of user input by using PreparedStatement correctly, then it will be protected from SQL injection attacks. If, on the other hand, it uses Statement to dynamically build SQL, or uses PreparedStatement incorrectly then it will not be protected from such attacks.
Cross-Site Scripting (XSS) AttacksHTML allows for scripting. Scripting needs some extra care, since it allows all kinds of code to execute on the client, where it has access to the user's private environment. If you do not exercise care, it is very easy to create web applications that allow users to enter malicious scripts as regular data, into forms or URLs. When such malicious input is later fetched from the database and rendered in a view, it becomes available for execution by the browser.
Note that such scripts will execute in any browser that renders the data, not just for the browser of the hacker who happened to enter the script. When the malicious script is executing in an innocent victim's browser, it gets access to sensitive information, and sends it back to the hacker. This is called a Cross-Site Scripting (XSS) attack.
To prevent XSS attacks, ask yourself two questions:
- can this form control accept a script as input?
- if so, how will the script be disabled when the user input is ultimately rendered in a page?
When the user POSTs this form, the data should always be validated as strongly as possible by the server. For instance, the Movie Decade should be checked against a fixed set of values, to ensure that form has not been altered by a malicious user. If the Movie Decade is indeed checked in such a manner, then it's not possible for the user to enter an arbitrary script as a value for the decade. If such strong checks are performed, then no special precautions against XSS attacks are needed.
The Movie Title is a different story, however. Since it's free-form user input, it's not possible to tightly constrain the Movie Title. The server cannot perform tight validation checks on its content, since there is no 'whitelist' of allowed values.
In this case, the user may indeed enter a script. To prevent the script from being activated when rendered in a page as regular data, special care must be taken: any special characters in free-form user input must be escaped. The Open Web App Security Project recommends that you escape these 12 characters :
The escaping can be performed in various places :
- in the view, when the data is ultimately rendered
- in the Model Object
- in the database
The WEB4J tool, for example, recommends performing the escaping in the Model Object, by using its SafeText class to model free-form user input instead of String.
In any case, remember that data should not be escaped more than once.
Note as well that JSTL's <c:out> tag performs escaping only for XML (5 characters), and not for HTML (12 characters). In addition, note that the JSP Expression Language performs no escaping at all.
Cross-Site Request Forgery (CSRF) AttacksThe fundamental idea in a Cross-Site Request Forgery (CSRF) attack is that of hijacking a victim's session to perform a malicious task. Since the session becomes available to the hacker, the hacker can perform tasks that would otherwise not be possible.
For example, consider a simple logging-off operation. On many sites, logging off is implemented with a simple link (GET), and not a POSTed form. A hacker can log you off such a site simply by sending you an email. Here's the scenario:
- you legitimately log in to the target site
- the hacker sends you an email containing a bogus IMG tag whose 'src' attribute points to the log-off link
- you receive the email and open it in some client (while still logged in to the target site)
- your email client automatically parses the email content
- when the IMG tag is encountered by your email client, it extracts the src target and sends an HTTP GET request over the network, ostensibly to fetch the 'image'
- bingo! you are now logged out, just by opening an email
Defending against CSRF attacks isn't as simple as defending against XSS attacks. Usually, the defenses take this form:
- for operations that have side-effects of any kind (database edits, logging off), use a form with method='POST'
- for operations without any side-effects (listings, reports, search operations), use either a form with method='GET' or a link
- for each user log-in, create a random, hard-to-guess token, and store it in the session. This token (also called a nonce) is injected as a hidden parameter into each form served by your application. For each POSTed form, the server verifies that the hidden parameter is present, and has the expected value for the current session. All of this has the intent of answering a simple question: 'Did this POSTed form really come originally from the legitimate server, or from some unknown third party?'
- specify the content-type of the response as an HTTP header. When using form tokens, the content-type lets tools know when you are serving HTML - if your are serving plain text or XML (which doesn't contain a form), then there's no need to inject form tokens.