Skip to content

Never Trust the Input: Why SQL Injection Is Dangerous

Students learn what databases and queries are, why gluing user input into a query lets that input be treated as instructions, how parameterised queries and other defences stop it, and the ethics and New Zealand law around testing other people's systems. Conceptual only, with no attack techniques.
WorksheetTechnologyYear 10Shared by Heapso

Preview

Answers and teacher notes are hidden here. Verified teachers see them once it's copied into Heapso.

Read the text

When data turns into instructions

  1. [1]

    Most apps and websites store their information in a database: tables of rows and columns, like a set of linked spreadsheets. A school library app might have a table called books, with columns for title, author and shelf. Programs talk to the database using SQL (Structured Query Language). A query such as SELECT title FROM books WHERE author = 'Joy Cowley' asks the database for the titles of every book by that author.

  2. [2]

    Many queries include something a user typed, such as a search word or a username. The danger comes when a program builds the query by gluing the user's text straight into the SQL. The database receives one long piece of text and cannot tell which part the programmer wrote and which part came from the user. If someone types text that contains SQL, the database may treat it as part of the instructions instead of as a value to look up. This is called SQL injection.

  3. [3]

    Think of a library request slip that says "Please fetch the book called ____". A trusting librarian reads the whole slip as instructions. If someone writes a book title followed by "and also hand over the staff room keys" in the blank, the librarian might do both. The slip should have kept the blank as just a book title, whatever was written in it.

  4. [4]

    SQL injection has been behind some of the largest data breaches in the world, exposing millions of people's passwords, addresses and card details. It is also one of the easiest problems to prevent. The main defence is a parameterised query: the program sends the SQL with a placeholder, and sends the user's text separately as a value. The database always treats that value as data, never as instructions. Other layers help too: checking that input looks the way it should, giving the app's database account only the permissions it needs, and showing users simple error messages instead of technical details.

  5. [5]

    In New Zealand, accessing a computer system without authorisation is a criminal offence under the Crimes Act 1961. Trying out an attack on a school, business or government website "just to see" is not a harmless experiment. Security professionals only test systems with written permission, and someone who finds a weakness by accident should report it to the owner or to the National Cyber Security Centre rather than exploring further.