Injection Fundamentals and Prevention
Learn how injection vulnerabilities arise when untrusted data is interpreted as instructions, how to recognize potential risks, and how to preserve the data–instruction boundary through secure design and layered defenses.
How Injection Happens and What to Look For
What injection means
Software often passes information to an interpreter—a component such as a database, operating-system shell, or browser that parses instructions in a language. An occurs when untrusted data is supplied in a way that changes the meaning of those instructions. The central problem is a failure to preserve the boundary between data and control syntax.
Consider an application that builds a SQL query by concatenating a supplied username into the query text. The application intends the username to be a value to look up. But if it merges that value into the SQL instruction itself, the database may interpret part of the supplied text as SQL syntax and process a query different from the one intended. Similar boundary failures can occur with commands or browser markup. Depending on the interpreter and the application's permissions, the result may include exposed or altered data, bypassed controls, or changed program behavior.
Why trust boundaries matter
Data can cross into a component from form fields, URL parameters, headers, cookies, uploaded files, stored user content, or responses from other services. Passing through an internal service or being saved in a database does not erase its origin: data may still be attacker-controlled when it is later used to construct instructions.
Risk grows when software concatenates untrusted values into queries, commands, templates, or markup; assumes data is safe because it came from a particular source; or checks it once and later uses it in a different context or after a transformation. A denylist of suspicious characters is not a dependable substitute for preserving the syntax/data boundary. Nor is a wrapper or stored procedure automatically safe if it constructs dynamic instructions internally.
Signs to investigate
Possible indicators include unexpected interpreter errors, unusual changes in query results, behavior that varies with special characters, or output that appears as markup or commands rather than ordinary data. Code review and logs may also reveal dynamic query construction, shell calls assembled from concatenated strings, or unsafe evaluation of user-controlled text.
Treat these as clues, not proof. They merit investigation, but none by itself establishes that a system is exploitable. The practical question is whether untrusted data can reach an interpreter in a form that changes its instructions.
Keep Data Separate from Instructions
Preserve the boundary first
The primary defense is to keep untrusted values separate from interpreter instructions. Prefer APIs that accept values as parameters instead of assembling a language string that combines instructions and data.
For SQL, use a . The supplied username is passed separately, so the database treats it as a value rather than as part of the query syntax:
cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
Use the parameter style supported by the database library. By contrast, concatenating username into the query text mixes the value with SQL instructions and risks changing what the database processes.
The same principle applies to operating-system commands. Prefer a language's file or process APIs over assembling shell commands. If a command must run, use a structured process API that keeps its arguments separate, validate those arguments against what that specific command permits, and avoid passing untrusted text to a shell.
A wrapper or stored procedure is not protective just because it has a safer-sounding interface. If it constructs dynamic instructions internally, the same boundary problem may remain.
Add validation for expected input
Validate at the point of entry against expected type, format, length, and allowed values. When the permitted values or patterns are defined, prefer an . This helps reject data that does not meet the application's requirements.
Validation has a different job from parameterization: it checks whether input meets expected rules, while parameterization keeps a value from becoming instruction syntax. Validation does not replace parameterization, and it does not replace output encoding. A value that is acceptable for one use may still need different handling when it is used in another context.
Encode for the browser destination
When untrusted text is placed in a browser page, encode it for its exact destination context so it remains text instead of becoming markup or executable code. Encoding appropriate to one context is not automatically safe for another. If an application accepts user-authored HTML, use a maintained HTML sanitizer rather than treating ordinary text encoding as a universal solution.
These defenses complement one another: preserve the data/instruction boundary with parameterized or structured APIs, validate against the rules for the input, and encode text for its particular browser output context. No single check makes data safe for every later use.
Limit Impact and Review Data Flows
Reduce impact and examine data flows
Even with preventive controls, limit the potential consequences of a flaw by applying : give each component only the access it needs. If an affected component has fewer permissions, the possible impact can be reduced.
Review how data travels from its sources to interpreters. Trace input channels such as parameters and other user-supplied data through storage, services, transformations, and eventual use in queries, commands, templates, or markup. Test those channels and inspect code or logs for unsafe construction, such as dynamic instructions assembled from untrusted strings.
These are supporting layers: limits impact, while review and testing help find risky data flows. The primary goal remains preserving the boundary between data and instructions so untrusted values cannot change what an interpreter is told to do.
: A point where data moves from a less-trusted context into a component that may act on it.
: Converting text so it is treated as text in its intended output context rather than as syntax.