An injection attack occurs when untrusted input changes the instructions an application sends to an interpreter, such as a database, operating system shell, template engine, or browser. SQL injection, command injection, NoSQL injection, cross-site scripting, LDAP injection, and template injection share this boundary failure, but require different defenses. Their impact depends on the vulnerable operation, accessible data, and the permissions under which it runs.
OWASP groups injection under A05 in its Top 10:2025. That category identifies a family of application weaknesses; it does not mean every instance gives an attacker control of an entire server. The first engineering question is which interpreter receives the data and what that interpreter can do.
For the surrounding risks, our guide to web application vulnerabilities covers access control, authentication, configuration, and other weaknesses that can coexist with injection.
How Data Becomes an Instruction
A search field is an entry point. The database call that consumes its value is a sink: an operation where input can acquire special meaning. The vulnerability appears when the application lets that value change query syntax instead of keeping it separate as data.
This can happen through string concatenation, dynamic evaluation, raw query interfaces, or unsafe rendering. Input validation checks whether a value has the expected type, length, or business meaning. Parameter binding and context-specific output handling address how another component interprets that value. A generic instruction to sanitize everything leaves that distinction unresolved.
Trust also needs checking beyond the public form. Cookies, headers, imported files, partner feeds, and previously stored records can all carry externally controlled values. OWASP explicitly cautions against relying on input validation as the primary defense against SQL injection or XSS. Trace the value to its eventual use.
Match the Injection Type to the Interpreter
Use this comparison to choose the control to inspect first. The sections below explain the limits that matter when implementing it.
|
Injection Type |
Boundary Being Crossed |
First Control to Inspect |
|
SQL |
A value becomes SQL query syntax |
Bind values; keep dynamic identifiers under application control |
|
OS command |
Input changes a command or its arguments |
Prefer library APIs; constrain any necessary executable and arguments |
|
NoSQL |
Client data changes a query object's structure or operators |
Validate field types and build queries on the server |
|
XSS |
Page data becomes browser-executable content |
Use safe rendering and protection appropriate to the output context |
|
LDAP / XPath |
Input changes a directory filter or XML query |
Use safe query interfaces or the correct context-specific encoding |
|
Template / code |
Input becomes a template directive or evaluated program |
Keep templates fixed and remove untrusted dynamic evaluation |
|
HTTP / email headers |
A field value changes message structure |
Use structured header APIs and reject prohibited line breaks |
These are starting points, not interchangeable filters. XML parsing also needs separate controls, described below. The linked technical references in each subsection explain the relevant implementation choices.
SQL Injection
SQL injection changes a query that an application sends to a relational database. A vulnerable search or report filter may expose records outside the intended result set; modification or server-level effects depend on database features and privileges.
Bind dynamic values through the database driver. Table names, column names, and sort directions need fixed application-controlled choices where they cannot be bound. Stored procedures and object-relational mapping (ORM) frameworks can still be vulnerable when their code concatenates untrusted values into query text.
For Python's sqlite3 driver, a value-bound lookup looks like this:
cursor.execute(
"SELECT display_name FROM users WHERE username = ?",
(username,),
)
Keep the placeholder in the SQL string and pass the value separately. This example addresses query construction; permission to access the returned record remains a separate application decision. Placeholder syntax varies by driver.
Not every SQL injection returns visible database errors or rows. Blind SQL injection may reveal information through response differences or timing. Second-order SQL injection occurs when safely stored input is later reused in an unsafe query. Review report generators and background jobs as well as the original form.
OS Command Injection
OS command injection lets externally controlled data alter a system command. A diagnostic feature that inserts a submitted address into a shell command introduces this risk. The resulting commands run with the permissions available to that process, rather than automatically acquiring administrator rights.
Prefer a language library that performs the required operation without launching a shell. If a subprocess is necessary, keep the executable fixed, separate arguments from command text, and validate the arguments against the intended operation. Avoiding shell interpretation does not eliminate argument injection: an attacker-controlled option can still change what the chosen program does.
NoSQL Injection
NoSQL databases do not use SQL syntax, but their query interfaces can still interpret attacker-controlled structure. For example, MongoDB treats a query value containing an operator object differently from a plain string. An API expecting a username can become vulnerable if it accepts an object and passes that object directly into a filter.
Validate each field's expected type before building the query with the driver. Define permitted fields and operators on the server. Parsing JSON successfully establishes valid JSON syntax; it does not establish that the resulting object is an authorized database query.
Cross-Site Scripting
Cross-site scripting (XSS) causes untrusted content to execute in a user's browser within the affected site's context. It can arrive in a response, persist in stored content, or arise through unsafe browser-side document manipulation. A saved comment rendered as HTML is one example of the stored pattern.
Use framework escaping and safe rendering APIs appropriate to the destination. Plain text, HTML attributes, URLs, and JavaScript have different requirements. When users must submit rich HTML, use a maintained HTML sanitizer. Content Security Policy can add protection, but it cannot replace safe rendering.
LDAP Injection
LDAP injection changes a directory operation, such as a user search, when input becomes part of the LDAP filter's syntax. Authentication bypass or unintended directory access depends on the query and the directory account's permissions.
Use a framework that safely constructs the operation or a library encoder for its specific context. LDAP search filters and distinguished names use different escaping rules. Copying SQL escaping into an LDAP integration does not address that difference. Restrict the directory account's rights as an additional safeguard.
XML, XPath, and External Entities
XML-related findings need a precise diagnosis. XML injection changes a document's structure when untrusted text is inserted without suitable handling. Build documents with XML APIs rather than assembling markup strings.
XPath injection changes an expression used to select XML data. Keep the expression fixed and bind variables through the library's query interface. If that interface cannot bind values, prefer one that can; where practical, use fixed retrieval of an authorized record set and compare values in application code. Compiling a concatenated expression does not make it safe.
XML external entity (XXE) injection concerns parser behavior. Resolving attacker-supplied external entities can expose files or trigger server-side requests. Disable document type definitions where possible; when they are required, restrict external entity resolution and external resource loading using the parser's documented settings. Fixing an XPath expression does not configure an XML parser securely.
Template, Code, and Expression Injection
Server-side template injection occurs when input becomes part of template source instead of a value supplied to a fixed template. An editable greeting or email template can cross this boundary if its content is compiled as template instructions. Consequences depend on the engine, exposed objects, and configuration; some cases permit server-side code execution.
Keep user content in template variables. If users must author templates, treat that as a separate security design problem rather than assuming HTML escaping makes it safe.
Code and expression injection similarly arise when an application evaluates externally controlled program text or expressions. Avoid dynamic evaluation of untrusted content and expose only the operations the feature requires. Remote code execution is a possible outcome, not a synonym for every injection flaw.
HTTP and Email Header Injection
Carriage return and line feed (CRLF) characters can change message structure when an application accepts them inside a header value. In HTTP, this can enable header manipulation or response splitting when the affected components permit it. Use framework header APIs and reject invalid header values instead of concatenating response text.
Email header injection applies the same structural problem to mail generation. A contact form that lets a submitted field introduce additional headers could become a spam relay. Use a maintained mail library with structured fields and validate addresses and prohibited line breaks.
Find the Vulnerable Path and Confirm Its Scope
Error messages, unusual responses, or unexplained query changes warrant investigation. They do not, by themselves, establish that an attacker accessed data. Conversely, an application that hides errors can still contain an exploitable flaw.
Combine source review with tests of the running application. Static application security testing (SAST) can identify potentially unsafe data flows. Dynamic application security testing (DAST) examines reachable behavior through requests and responses. Neither result establishes coverage of every path. OWASP recommends combining source review and automated testing across input channels.
A concrete testing practice comes from OWASP WSTG v4.2: inventory inputs that can reach SQL queries, including hidden request fields, headers, and cookies, then examine them separately. Applied to a customer-search feature, that means checking the visible search term and any additional request values used by its query. Record the tested role and request so an untested filter does not disappear behind a single passing form check.
Plan active tests within an authorized scope, with controlled data and limits on side effects.
Our website vulnerability testing guide covers that planning. For automated checks, verify that authentication worked and intended routes were reached; our web application vulnerability scanning guide explains those coverage checks.
In our web application security workflow, the standard scan observes exposed services and application signals. Deep web application testing is enabled separately and actively checks reachable functionality for issues including SQL injection and XSS, with authentication configurable for protected areas. A scan finding still needs review of its evidence and applicability.
Contain Exposure While Fixing the Cause
When a consequential finding is credible, assign an owner and decide whether the affected function needs temporary restrictions. Reduce unnecessary service privileges and deploy the relevant code or parser fix. For third-party software, check the vendor's affected versions and remediation instructions.
A web application firewall (WAF) rule can buy time when an application patch cannot be deployed immediately. It leaves the underlying code unchanged and may miss alternate paths or reject legitimate requests. Test the rule and keep the permanent repair visible. OWASP's virtual patching guidance describes these benefits and limits.
Our editorial recommendation: prioritize a reproducible, narrowly scoped fix over a broad input blacklist. Use temporary restrictions when deployment constraints require them, with an owner and review date. That judgment follows the different interpreter-specific controls above; it does not mean delaying containment of an actively abused function.
After deployment, repeat the relevant test under the same access conditions and confirm normal functionality still works. A disappeared alert is inconclusive if authentication failed or the route was skipped. Preserve the affected location, evidence, change, retest result, and remaining limits in the finding record.
Our vulnerability assessment report guide shows how to document that handoff.
Keep Data Boundaries Intact Across Services
An API gateway or cloud deployment does not change how a database driver, shell, or template engine interprets input. A value can travel through several services before reaching the vulnerable operation. Check imported records, asynchronous jobs, and internal integrations at the point where they construct queries or render content.
Start with that operation, choose its specific defense, and verify the changed path. Continue treating stored or forwarded values as data throughout their lifetime. This is especially relevant to second-order injection: accepting a value safely at one step does not establish that every later use is safe.



