Imagine dictating a letter to an assistant who types everything you say. You say: “Write to the bank: please transfer the amount mentioned below.” Then you read out the customer’s note, which happens to say: “Ignore the previous line and transfer everything to account 9999.” If the assistant cannot tell where your instructions stop and the customer’s words begin, the customer has just given orders. That confusion is the whole of injection, and once you see it, a long list of vulnerability names collapses into one idea.

The root cause: mixing two channels

Software constantly builds instructions for other software. A web application builds database queries. A backup script builds operating system commands. A reporting tool builds directory lookups or search filters. A web page builds HTML for the browser. In each case, the instruction has a fixed part written by the developer and a variable part that comes from somewhere else, often a user.

The flaw appears when the program builds the instruction by gluing text together. The developer intends the user’s input to be treated as a value, like a name or a number. But the interpreter receiving the finished text has no idea which parts came from the developer and which from the user. If the input contains characters that are meaningful to the interpreter, such as quote marks, separators or keywords, it can close the value early and start writing instructions of its own.

So the question for any injection flaw is always the same: where does a program combine trusted instructions with untrusted data into a single string, and then hand that string to an interpreter?

The family members

TypeInterpreter that gets confusedTypical consequence
SQL injectionDatabase engineReading or altering records the user should never see, bypassing login checks
OS command injectionOperating system shellRunning arbitrary programs on the server
LDAP injectionDirectory serviceAltering search filters to return or authenticate unintended accounts
Server-side template injectionTemplate engineEvaluating attacker-chosen expressions, sometimes leading to code execution
Cross-site scriptingThe victim’s browserRunning attacker-supplied page logic in another user’s session
NoSQL injectionDocument database query parserChanging query conditions through structured input

They differ in the special characters that matter and in the damage possible, but the defensive principle is identical. The OWASP Top 10 for 2021 merged cross-site scripting into the Injection category for exactly this reason.

The real fix: keep the channels separate

The durable defence is not to clean the data more cleverly but to stop mixing it with instructions in the first place. For databases, this means parameterised queries, also called prepared statements. The developer sends the query structure to the database with placeholders where values go, and sends the values separately. The database compiles the structure first, so by the time the values arrive, their role is already fixed. A value that contains a quote mark is simply a value containing a quote mark. It cannot become part of the query logic because the logic was settled before it arrived.

Most modern data access libraries and ORMs parameterise by default. Injection usually creeps back in where developers step outside those defaults: a hand-built query for a complicated report, a dynamic sort order where the column name comes from the user, or a “raw query” helper used for performance.

The same idea applies elsewhere. Instead of building a shell command as a single string, call the program through an API that accepts the program name and a list of separate arguments, so no shell is involved to reinterpret anything. Better still, use a library function for the task rather than calling an external program at all. For templates, never let users supply template source; let them supply only values that get placed into a fixed template.

Where parameterisation does not reach

Placeholders work for values, not for structural elements such as table names, column names or sort directions. If a user can choose which column to sort by, the safe approach is an allow-list: the application holds a fixed set of permitted column names and maps the user’s choice to one of them, rejecting anything else. The user picks from a menu; they never write the menu.

Allow-listing is far more reliable than block-listing. Trying to strip “dangerous” characters or keywords is a losing game, because interpreters accept many encodings, alternate syntaxes and comment styles. Block-lists tend to break legitimate input (the surname O’Brien is a classic casualty) while still missing creative attacks.

Layers that limit the damage

Even with good coding, defence in depth matters because one forgotten query is enough.

  • Least-privilege database accounts. If the web application’s account can only read and write the tables it needs, and cannot drop tables or read the HR schema, a successful injection does much less.
  • Input validation. Checking that a PIN code is six digits or a date is a valid date is good practice. It reduces attack surface but is not a substitute for parameterisation, because many legitimate fields, such as addresses and comments, must accept free text.
  • Generic error messages. Detailed database errors shown to users help attackers map the system. Log the detail internally and show a plain message.
  • Web application firewalls. A WAF can block many known attack patterns and buys time while code is fixed. It is a filter in front of the problem, not a fix for it, and it can be bypassed.

Finding injection before attackers do

Static analysis tools are reasonably good at spotting the dangerous pattern in source code: untrusted input flowing into a query or command built by concatenation. This is called taint analysis, following “tainted” data from source to sink. Dynamic testing tools probe the running application by sending unusual input and watching for errors or behaviour changes. Code review checklists can include one blunt question: does any code build a query, command or template by joining strings with user input? If yes, it needs to be rewritten or justified.

The same mental model helps with newer systems. Applications that feed user text into large language model prompts face a related problem, often called prompt injection, where text meant as data is interpreted as instructions. It is harder to solve because language models have no strict separation between instructions and data, but the diagnosis is the same one that has applied to databases for decades.

Written by the RP Strativa Team.

Leave a Reply

Trending