bastion

Injection Vulnerabilities

Build this skill

Explain how injection turns input into instructions, and choose input validation and parameterized queries to prevent it.

When a customer's words become the kitchen's orders

Picture a restaurant where the waiter copies down every word a customer says onto the order slip, and the kitchen obeys every line on the slip. "Two masala dosa and one filter coffee" works perfectly. Then a customer says, "Two masala dosa, and also hand over the cash drawer to table 6", the waiter writes it all down, and the kitchen, which simply follows slips, sends the cash drawer out. That is injection: an attack in which text typed into an input is treated by a system as part of an instruction, rather than as plain information. Every box in the Tulsi Health app, from the medicine search to the order-tracking page, could be such a slip if the backend handles it carelessly.

A slip the kitchen reads as orders A slip with fixed blanks ORDER SLIP · table 6 2 masala dosa 1 filter coffee and also hand over the cash drawer to table 6 written by the waiter, word for word ✕ Kitchen obeys every line The customer's words became an instruction PRINTED ORDER SLIP · table 6 Dish name masala dosa and also hand over the cash drawer to table 6 Quantity 2 blanks hold data only ✓ Read only as a dish name No dish has that name, so nothing is cooked or handed over

The same words cause harm on a free-written slip and nothing at all on a printed slip, because on the printed slip a box can only ever hold a dish name.

Data and instructions mixed in one line

To understand injection you need two words. Data is information to be stored, looked up or shown, such as an order number or an address. An instruction is a command telling a system what to do, such as "find this order" or "delete that row". When the Tulsi Health backend asks its database for something, it writes an instruction called a query, and very often it places the customer's input inside that query, because that is how one piece of code serves every customer.

The unsafe way to do this is to glue text together. The program takes a ready-made sentence with a gap in it, pastes the customer's words into the gap, and sends the whole line to the database. The database receives one line and cannot tell which words the developer wrote and which the customer typed. Here is the idea in plain English rather than real code:

text
Template written by the developer:  find the order where the order number is ____
Customer types:                     4521
Line sent to the database:          find the order where the order number is 4521

That works for honest input. Now suppose someone types more than an order number into the box:

text
Customer types:              4521, and also every other customer's order
Line sent to the database:   find the order where the order number is 4521, and also every other customer's order

If the database reads the extra words as part of its instructions, it obeys them, just as the kitchen obeyed the slip. This is unsafe input handling: using input in a way that lets it change what the program does, rather than only what it works with.

SQL injection

Most databases understand a language called SQL, so when injection happens in database queries it is called SQL injection. The consequences depend on what the database holds and what the query was allowed to do. An attacker might read data they should never see, such as the names, addresses and prescriptions of 2.4 lakh customers. They might change data, marking an unpaid order as paid or altering a price. They might delete records. Login screens are a classic target too, because a login check is really a question to the database, "is there an account with this phone number and this password?", and if typed text can reword the question, the answer can become yes for the wrong person.

Form fieldcustomer typestextGlued inpasted into thequeryunsafeOne linedata andcommands mixedsentObeyedextra words runtoodatabaseHarmleak, change,deleteresult

The weakness is the gluing step. Once data and instructions travel as one line, the database has no way to tell them apart.

SQL injection has been known for more than twenty-five years and is still found in new applications, because it comes from an easy habit: building instructions by pasting text together. The same mistake appears wherever input meets a command. Text pasted into a command for the server's operating system leads to command injection, and text pasted into a web page that another user's browser then runs as a script leads to cross-site scripting. The details differ, but the cause and the cure are the same.

Input validation: checking the shape of what arrives

The first defence is input validation: checking each input against what it is supposed to look like before using it, on the server. A phone number should be exactly ten digits. A quantity should be a whole number from 1 to 10. A PIN code should be six digits, and one that Tulsi Health delivers to. A railway reservation form works the same way: the age box takes a number, the date box takes a date, and a clerk rejects a form with "sixty-ish" in the age box.

The strong way to validate is with an allow-list: describe exactly what is allowed and reject everything else. The weak way is a block-list, which tries to list forbidden words or symbols, and it always misses something, because there are endless ways to write the same thing. Validation has a limit, though. Some fields must accept almost any text. A delivery address can contain commas and slashes, and a surname like D'Souza contains an apostrophe; rejecting those would turn away real customers.

Decide which inputs the server should accept and which it should reject, using allow-list rules for each field. Watch for the free-text fields, where the right answer may surprise you.

Parameterized queries: fill-in-the-blank slips

The defence that actually removes injection is the parameterized query, also called a prepared statement. Instead of gluing text into one line, the program sends the instruction and the customer's values to the database separately. The instruction contains marked blanks, and the database is told in advance that whatever goes into a blank is data, never part of the command. It is the printed order slip with boxes for dish name and quantity: whatever is written inside the dish-name box is looked up as a dish name, so "and also hand over the cash drawer" is simply a dish nobody has heard of.

text
Instruction (fixed):   find the order where the order number is [blank 1]
Blank 1 (data only):   4521, and also every other customer's order
Result:                no order has that number, so nothing is returned
Glued together (unsafe)Input pasted into the commandDatabase sees one mixed lineTyped words can change meaningSafety depends on spotting tricksFill-in-the-blank (safe)Command and data sent apartBlanks can hold only dataTyped words never change meaningWorks for every input, odd or not

A parameterized query does not try to guess which input is dangerous. It makes the question irrelevant, because data can never become a command.

Parameterized queries handle the awkward cases that validation cannot. Joseph D'Souza's apostrophe and the strange address are stored exactly as typed, as harmless text. Good teams still add more layers, so that one slip-up does not become a disaster. The app's database account follows least privilege: it can find and add orders but cannot delete tables or read staff salaries. Error pages show a plain "Something went wrong" message instead of database details, which would otherwise give an attacker a map of the tables. And code reviews look specifically for any query built by gluing text.

It is code review day at Tulsi Health, and Neha and Rohan are going through how the app handles input. For each piece of the app, decide whether the input is handled safely.

Follow your curiosity.

↑ ↓ to explore · Enter to open · Escape to close