Mitigating XSS, CSRF, and Injection Attacks in Web Applications

Learn how to protect Erlang web applications from XSS, CSRF, and injection attacks using secure coding practices, frameworks, and libraries.

20.13 Mitigating XSS, CSRF, and Injection Attacks in Web Applications

In today’s digital landscape, web applications are a prime target for various security threats. Among these, Cross-Site Scripting (XSS), Cross-Site Request Forgery (CSRF), and injection attacks are some of the most prevalent and dangerous. This section will guide you through understanding these threats and implementing strategies to mitigate them in Erlang web applications.

Understanding Cross-Site Scripting (XSS)

Cross-Site Scripting (XSS) is a vulnerability that allows attackers to inject malicious scripts into web pages viewed by other users. These scripts can steal cookies, session tokens, or other sensitive information, and even manipulate the content displayed to users.

Types of XSS Attacks

  1. Stored XSS: The malicious script is stored on the server (e.g., in a database) and is served to users when they request the affected page.
  2. Reflected XSS: The script is reflected off a web server, such as in an error message or search result, and is immediately executed in the user’s browser.
  3. DOM-based XSS: The vulnerability exists in the client-side code, where the script is executed as a result of modifying the DOM environment.

Mitigation Strategies for XSS

  • Input Validation and Output Encoding: Always validate and sanitize user inputs. Use libraries that automatically encode output to prevent scripts from being executed.
  • Content Security Policy (CSP): Implement CSP headers to restrict the sources from which scripts can be loaded.
  • HTTPOnly and Secure Cookies: Use HTTPOnly and Secure flags for cookies to prevent them from being accessed via JavaScript.
1% Example of setting HTTPOnly and Secure flags in Erlang
2set_cookie(CookieName, CookieValue) ->
3    Cookie = {CookieName, CookieValue, [{http_only, true}, {secure, true}]},
4    % Set the cookie in the response
5    set_cookie_in_response(Cookie).

Understanding Cross-Site Request Forgery (CSRF)

Cross-Site Request Forgery (CSRF) is an attack that tricks a user into performing actions they did not intend to perform. This is done by exploiting the user’s authenticated session with a web application.

Mitigation Strategies for CSRF

  • Anti-CSRF Tokens: Include unique tokens in forms and verify them on the server side to ensure the request is legitimate.
  • SameSite Cookies: Use the SameSite attribute for cookies to prevent them from being sent along with cross-site requests.
 1% Example of generating and validating CSRF tokens in Erlang
 2generate_csrf_token(UserId) ->
 3    Token = crypto:strong_rand_bytes(16),
 4    store_token_in_session(UserId, Token),
 5    Token.
 6
 7validate_csrf_token(UserId, Token) ->
 8    case get_token_from_session(UserId) of
 9        Token -> true;
10        _ -> false
11    end.

Understanding Injection Attacks

Injection attacks occur when untrusted data is sent to an interpreter as part of a command or query. SQL injection is the most common form, but similar vulnerabilities exist in other contexts, such as LDAP, XPath, and NoSQL databases.

Mitigation Strategies for Injection Attacks

  • Parameterized Queries: Use parameterized queries to ensure user input is treated as data, not executable code.
  • Input Sanitization: Sanitize inputs to remove or escape characters that could be interpreted as code.
  • Use of ORM Libraries: Utilize Object-Relational Mapping (ORM) libraries that automatically handle input sanitization and query parameterization.
1% Example of using parameterized queries in Erlang with an SQL database
2execute_query(UserInput) ->
3    Query = "SELECT * FROM users WHERE username = ?",
4    Params = [UserInput],
5    % Execute the query with parameters
6    execute_sql_query(Query, Params).

Role of Secure Frameworks and Libraries

Leveraging secure frameworks and libraries can significantly reduce the risk of security vulnerabilities in your web applications. These tools often come with built-in protections against common threats.

  • Cowboy: A small, fast, and modern HTTP server for Erlang/OTP that can be configured to handle security concerns.
  • Plug: A specification for composable modules in web applications, often used with Elixir but applicable in Erlang contexts for handling requests and responses securely.

Visualizing Security Threats and Mitigations

To better understand how these attacks work and how to prevent them, let’s visualize the flow of a typical web request and the points at which these threats can be mitigated.

    graph TD;
	    A["User"] -->|Sends Request| B["Web Server"];
	    B -->|Validates Input| C["Application Logic"];
	    C -->|Executes Query| D["Database"];
	    D -->|Returns Data| C;
	    C -->|Generates Response| B;
	    B -->|Sends Response| A;
	    B -->|Checks CSRF Token| C;
	    B -->|Applies CSP| C;
	    B -->|Encodes Output| C;

Diagram Description: This flowchart illustrates the typical flow of a web request, highlighting key points where security measures such as input validation, CSRF token checks, CSP application, and output encoding can be applied to mitigate XSS, CSRF, and injection attacks.

Try It Yourself

To deepen your understanding, try modifying the code examples provided:

  • Experiment with different input validation techniques: Implement additional checks for specific input types, such as email addresses or URLs.
  • Enhance the CSRF token mechanism: Add expiration times to tokens and implement token rotation.
  • Test the impact of CSP: Modify the CSP headers to allow or block different types of content and observe the effects on your application.

Knowledge Check

  • What are the key differences between stored, reflected, and DOM-based XSS?
  • How do anti-CSRF tokens work, and why are they effective?
  • Why is it important to use parameterized queries in database interactions?

Summary

In this section, we’ve explored the nature of XSS, CSRF, and injection attacks, and discussed strategies to mitigate these threats in Erlang web applications. By understanding these vulnerabilities and implementing robust security measures, you can protect your applications and users from potential harm.

References and Further Reading

Quiz: Mitigating XSS, CSRF, and Injection Attacks in Web Applications

Loading quiz…

Remember, this is just the beginning. As you progress, you’ll build more secure and robust web applications. Keep experimenting, stay curious, and enjoy the journey!

Revised on Thursday, April 23, 2026