WEBA-2Lesson 2 of 311 min

Injection: when data becomes a command

Programs often build commands for other software, such as a database, from pieces of text. If typed input is pasted into a command, the input can change what the command does. This is injection, one of the most damaging web bugs.

Data mixed into a command

Figure WEBA-2.1When typed text becomes part of a command

command built by joining text

find students where name = ' + Asha + '
find students where name = 'Asha'1 ROW
1/4

A college search page finds a student's record by name. The program builds its command to the database by joining fixed text and whatever was typed, here Asha.

Step 1 of 4

Keep data out of the command

The fix is not to hunt for dangerous characters, but to make it impossible for data to become a command. A parameterised query (fixed template) sends the command and the data separately. The database never reads the data as instructions, whatever it contains. Two extra layers: check input against an allowlist (lesson 1), and give the website’s database account only the access it needs, so even a successful injection reaches little.

One family of bugs

The same mistake appears in many places: commands to the operating system, page templates, search filters, and now instructions to AI tools (station AISE). The cause is always the same: data and commands travel mixed together. The fix is always to keep them apart.

Lab: the injection form

Lab WEBA-L1Joined text or fixed template?

A made-up search page over four made-up students. Search for Asha. Then type a name containing a quote mark, such as D'Souza, and then x' OR ALWAYS-TRUE. In this simulation, ALWAYS-TRUE stands for any condition that is always true; it is not real database syntax. Switch to the fixed template and try the same three.

WEBA-2 counts as completed once you step through the figure to its last step.