Skip to main content
Injection vulnerabilities are among the most common and dangerous security flaws. They occur when an application sends untrusted data to an interpreter without proper validation or sanitization. This allows attackers to inject malicious code that can manipulate the interpreter into executing unintended commands or accessing data without proper authorization.Common types of injection vulnerabilities include SQL injection, NoSQL injection, OS command injection, LDAP injection, and template injection. These vulnerabilities can lead to data theft, data loss, data corruption, denial of service, or complete host takeover.
SQL Injection occurs when user-supplied data is not properly validated and is directly included in SQL queries. This can allow attackers to manipulate the query’s logic, bypass authentication, access sensitive data, or even delete data.To prevent SQL injection:
  • Use parameterized queries or prepared statements
  • Apply input validation and sanitization
  • Use stored procedures
  • Implement least privilege principles for database accounts
  • Use an ORM (Object-Relational Mapping) framework that handles parameter sanitization
NoSQL Injection attacks target applications that use NoSQL databases like MongoDB, Cassandra, or Redis. Attackers can inject operators or modify the query structure to manipulate the application’s logic.To prevent NoSQL injection:
  • Validate and sanitize all user inputs
  • Use type checking
  • Avoid using operators directly from user input
  • Implement proper authentication and authorization
  • Use MongoDB’s aggregation framework instead of direct queries when possible
OS Command Injection occurs when an application passes unsafe user-supplied data to a system shell for execution. Attackers can inject additional commands to be executed with the privileges of the application.To prevent OS Command Injection:
  • Avoid calling OS commands directly when possible
  • Use safer library alternatives instead of shell commands
  • If OS commands must be used, validate and sanitize all inputs
  • Use allowlists for permitted characters or commands
  • Implement proper privilege separation
  • Use subprocess modules with arrays instead of string commands
LDAP Injection occurs when an application fails to properly sanitize user input before using it in LDAP statements. This can allow attackers to modify LDAP statements to bypass authentication or access unauthorized information.To prevent LDAP Injection:
  • Validate and sanitize all user inputs
  • Use LDAP-specific encoding functions to escape special characters
  • Implement proper authentication and authorization
  • Use bind variables where possible
  • Apply the principle of least privilege for LDAP accounts
Template Injection occurs when user input is embedded directly into templates before they are rendered. If the template engine evaluates expressions, attackers can inject code that will be executed by the template engine.To prevent Template Injection:
  • Never allow users to modify or submit templates
  • Use a template engine that automatically escapes output
  • Apply context-specific escaping
  • Sandbox template execution environments
  • Minimize the use of dangerous template features (like eval or execute)
XML Injection occurs when untrusted data is inserted into an XML document without proper encoding. This can lead to malformed XML documents or, in some cases, the execution of malicious code.To prevent XML Injection:
  • Use XML libraries to create XML documents instead of string concatenation
  • Apply proper XML encoding for special characters
  • Validate XML against a strict schema
  • Implement XML parser hardening
  • Disable external entity processing
Code Injection occurs when an application executes user-supplied code. This is often done through functions like eval() in various programming languages. This vulnerability can lead to complete system compromise.To prevent Code Injection:
  • Avoid using dynamic code execution functions like eval()
  • If dynamic code execution is necessary, implement strict input validation
  • Use safer alternatives like expression evaluators or template engines
  • Implement proper sandboxing and isolation
  • Apply the principle of least privilege
HTTP Header Injection occurs when an application includes untrusted data in HTTP response headers. This can lead to response splitting attacks, cache poisoning, or cross-site scripting.To prevent HTTP Header Injection:
  • Validate and sanitize all user inputs used in HTTP headers
  • Remove or encode CR and LF characters (\r, \n)
  • Use framework functions that handle header encoding
  • Implement proper output encoding
  • Use allowlists for redirect URLs
Path Traversal (Directory Traversal) allows attackers to access files and directories outside of the intended directory by manipulating variables that reference files with “dot-dot-slash (../)” sequences or absolute file paths.To prevent Path Traversal:
  • Validate and sanitize file paths
  • Use allowlists for permitted files or extensions
  • Convert relative paths to absolute paths and validate them
  • Implement proper access controls
  • Use file system permissions as an additional layer of defense
Email Header Injection occurs when an application uses untrusted data in email headers. Attackers can inject additional headers to send spam, phishing emails, or perform other malicious activities.To prevent Email Header Injection:
  • Validate and sanitize all user inputs used in email headers
  • Remove or encode CR and LF characters (\r, \n)
  • Use email libraries or frameworks that handle header encoding
  • Implement proper output encoding
  • Use allowlists for email addresses
Format String Injection occurs when an application passes untrusted data to a format string parameter in certain functions. This can lead to information disclosure, memory corruption, or code execution.To prevent Format String Injection:
  • Always use format specifiers (%s, %d, etc.) in format strings
  • Never pass user input directly as a format string
  • Validate and sanitize all user inputs
  • Use safer alternatives to format string functions when possible
  • Implement proper compiler protections
Log Injection occurs when an application logs untrusted data without proper sanitization. This can lead to log forgery, log file corruption, or cross-site scripting if logs are displayed in web interfaces.To prevent Log Injection:
  • Sanitize all user inputs before logging
  • Remove or encode CR and LF characters (\r, \n)
  • Use logging frameworks that handle encoding
  • Implement proper output encoding
  • Use structured logging formats like JSON
Server-Side Includes (SSI) Injection occurs when an application embeds untrusted data into SSI directives. This can allow attackers to execute commands, access sensitive information, or perform other malicious activities.To prevent SSI Injection:
  • Validate and sanitize all user inputs used in SSI directives
  • Disable unnecessary SSI directives
  • Use allowlists for permitted SSI directives
  • Implement proper access controls
  • Consider using alternative technologies to SSI
CRLF (Carriage Return Line Feed) Injection occurs when an application includes untrusted data in HTTP headers without proper sanitization. This can lead to response splitting attacks, cache poisoning, or cross-site scripting.To prevent CRLF Injection:
  • Remove or encode CR and LF characters (\r, \n) in user inputs
  • Use framework functions that handle header encoding
  • Implement proper output encoding
  • Validate and sanitize all user inputs
  • Use allowlists for permitted characters
XPath Injection occurs when an application uses untrusted data in XPath queries without proper sanitization. This can allow attackers to bypass authentication or access unauthorized information.To prevent XPath Injection:
  • Use parameterized XPath queries
  • Validate and sanitize all user inputs
  • Implement proper authentication and authorization
  • Use XPath 2.0 or later, which supports parameterized queries
  • Apply the principle of least privilege
JSON Injection occurs when an application constructs JSON strings manually using string concatenation with untrusted data. This can lead to malformed JSON, data corruption, or in some cases, security vulnerabilities like XSS.To prevent JSON Injection:
  • Use JSON serialization functions instead of string concatenation
  • Validate and sanitize all user inputs
  • Implement proper output encoding
  • Use Content-Type headers with charset
  • Consider using JSON schema validation
HTML Injection occurs when an application includes untrusted data in HTML output without proper encoding. This can lead to cross-site scripting (XSS) attacks, where attackers can inject client-side scripts.To prevent HTML Injection:
  • Use proper HTML encoding for all user inputs
  • Implement Content Security Policy (CSP)
  • Use template engines that automatically escape output
  • Validate and sanitize all user inputs
  • Use allowlists for permitted HTML tags and attributes
CSS Injection occurs when an application includes untrusted data in CSS code without proper sanitization. This can lead to style manipulation, data exfiltration, or in some cases, cross-site scripting.To prevent CSS Injection:
  • Validate and sanitize all user inputs used in CSS
  • Use allowlists for permitted CSS properties and values
  • Implement Content Security Policy (CSP)
  • Use separate CSS files instead of inline styles
  • Consider using CSS sanitization libraries
Shell Metacharacter Injection occurs when an application passes untrusted data to a system shell without proper sanitization. This can allow attackers to execute arbitrary commands with the privileges of the application.To prevent Shell Metacharacter Injection:
  • Avoid calling shell commands directly
  • Use safer alternatives like subprocess modules with arrays
  • Validate and sanitize all user inputs
  • Use allowlists for permitted characters
  • Implement proper privilege separation
OGNL (Object-Graph Navigation Language) Injection occurs when an application uses untrusted data in OGNL expressions without proper validation. This vulnerability is particularly relevant to applications using frameworks like Apache Struts.To prevent OGNL Injection:
  • Validate and sanitize all user inputs used in OGNL expressions
  • Use allowlists for permitted expressions
  • Implement proper access controls
  • Keep frameworks up-to-date
  • Consider using alternatives to OGNL