Skip to content

Lesson 3 of 7

Other interpreters

Listen to an AI summary of Other interpreters

0:00 / 0:38

More interpreters

Injection is untrusted data that an interpreter treats as instructions. A SQL database is one interpreter, and the sports shop has more. In each one, the fix keeps the input apart from the instructions.

A library that writes the SQL

An ORM (object-relational mapper) is a library that lets code read and change the records of a database as ordinary values. The library writes the SQL.

The sports shop finds the orders of one customer in three ways. In each line, name holds text from a request:

  1. Order.objects.filter(owner=name)# the ORM writes the SQL for the table of orders
  2. Order.objects.raw("SELECT * FROM orders WHERE owner = '" + name + "'")# the code joins name into the SQL
  3. Order.objects.raw("SELECT * FROM orders WHERE owner = %s", [name])# %s marks the place for name, and name is sent apart from the SQL
Three searches for the orders of one customer.

The function raw on lines 2 and 3 runs SQL that the programmer wrote. Line 2 joins name into the SQL, so line 2 has SQL injection (CWE-89). Line 3 is a parameterised query.

A database with no SQL

A NoSQL database stores data without tables and is searched without SQL. The sports shop keeps messages from customers in a NoSQL database named MongoDB. Look at what line 2 searches for:

  1. sender = request.json["sender"]# request.json reads the body of the request
  2. messages.find({"sender": sender})# finds every message where the sender matches
A search of a NoSQL database.

The code expects sender to be a string, such as maya. The body of a request can hold more than a string. An attacker sends a rule in place of a name, so the search becomes {"sender": {"$ne": null}}.

The rule means not equal to nothing. Every message has a sender, so the database returns every message. NoSQL injection (CWE-943) is untrusted data that changes what a NoSQL database searches for.

Safer code turns the input into a string first, with str(sender). The database never reads a string as a rule.

A page with gaps

A template is a page with gaps, and the server fills the gaps with data to make the finished page.

Hello {{ name }}, welcome to the sports shop.

  1. Hello words of the email
  2. {{ name }}the gap for the data called name
  3. , welcome to the sports shop.words of the email
The template of the welcome email.

The function render_template_string is the interpreter. The function fills the gaps and carries out any instruction between curly brackets. The template {{ 7 * 7 }} gives the text 49.

Look at where name appears in two versions of the code:

  1. name = request.form["name"]# reads the name a new customer typed into a form
  2. render_template_string("Hello " + name + ", welcome to the sports shop.")# version 1 joins name into the template
  3. render_template_string("Hello {{ name }}, welcome to the sports shop.", name=name)# version 2 passes name as data for the gap
The welcome email in two versions.

An attacker types the name {{ 7 * 7 }}. Version 1 joins the name into the template, so the email says Hello 49, welcome to the sports shop. Version 2 passes the name as data, so the email says Hello {{ 7 * 7 }}, welcome to the sports shop.

Server-side template injection (CWE-1336) is untrusted data used as part of a template. The server then runs instructions an attacker wrote. Other template instructions can read files or start programs on the server.

A template that a user writes, such as a custom email, has the same weakness.

A function that runs a string

The Python function eval runs a string as program code, so eval("2 + 3") returns 5. The sports shop works out the price of tickets with eval(quantity + " * 4"), and quantity comes from a form.

A quantity of 3 gives 12. A quantity that contains Python code runs the code on the server. Safer code turns the input into a whole number, as in int(quantity) * 4.

Documents and line breaks

The last two interpreters read a document and a line of text.

What is XML

XML is a text format for data. Each piece of data has a name written before and after the data.

  1. <order><!-- the order starts -->
  2. <item>tie</item><!-- the data tie, with the name item before and after -->
  3. <quantity>30</quantity><!-- the data 30, with the name quantity before and after -->
  4. </order><!-- the order ends -->
An order for 30 ties, written in XML.

The shop reads the document with a library, which is the interpreter. An instruction called an external entity tells the library to fetch a file or a web address. The next order is from an attacker:

  1. <!DOCTYPE order [<!-- a block of instructions for the library starts -->
  2. <!ENTITY stolen SYSTEM "file:///etc/passwd"><!-- the external entity, where the name stolen stands for a file on the server -->
  3. ]><!-- the block of instructions ends -->
  4. <order>
  5. <item>&stolen;</item><!-- the library puts the contents of the file here -->
  6. </order>
The library reads a file on the server.

The file /etc/passwd lists the accounts on some servers. The shop shows the order back to the attacker with the file as the item.

XML external entity injection (XXE, CWE-611) is an XML document that tells the library reading the document to fetch a file or a web address. Safer code switches external entities off in the library.

A line break in a header or a log

Each header of a response is one line. Text after a line break is a new header.

The sports shop writes the language a visitor chose into a header. An attacker sends fr, then a line break, then a second header.

  1. HTTP/1.1 200 OKthe first line of the response
  2. Content-Language: frthe header the code wrote, with the input fr
  3. Set-Cookie: basket=999the header the attacker added, which stores data in the browser
A response with a header from the attacker.

Header injection (CWE-113) is untrusted data with a line break written into a header, so the attacker adds a header. A library that writes headers can refuse a line break.

A log has one line for each event. A line break in input adds a false line to the log (a later lesson on logs).

The same fix each time

Every fix keeps the input as data, apart from the instructions.

A database, which reads SQL
An attacker adds SQL. The input belongs in a parameterised query.
A NoSQL database, which reads a search
An attacker adds a rule. The input belongs in a string.
A template function, which reads a template
An attacker adds an instruction. The input belongs in the data for a gap.
eval, which reads program code
An attacker adds program code. The input belongs in a number, never in eval.
An XML library, which reads a document
An attacker adds an external entity. External entities belong switched off.
A browser or a log, which reads lines
An attacker adds a line break. A header refuses input with a line break, and a log writes the line break as the two characters \n (Logging).

Examples of names in other languages

Other languages have names of their own for the same jobs:

An ORM function that runs SQL the programmer wrote
raw in Django for Python, $queryRawUnsafe in Prisma for JavaScript, find_by_sql in Rails for Ruby and DB::select in Laravel for PHP.
A function that runs a string as code
eval and exec in Python, eval and new Function in JavaScript, and eval and instance_eval in Ruby.
An XML library with external entities off already
defusedxml in Python and the default XML settings in recent versions of .NET.

Examples of what to look for

  • A string joined with +, an f-string or a template string and passed to an ORM function that runs SQL, such as raw.
  • A NoSQL search that uses input from a request with no check that the input is a string.
  • A template built by joining input into the template, or a template that a user wrote.
  • A library that reads XML from a request with external entities left on.
  • A header or a log line built from input with no check for a line break.
  • Input from a request passed to eval or exec.

Check your understanding

  1. Name three interpreters other than a SQL database that can be injected.

    Show the answer

    Any three of a NoSQL database, a template function, eval, a library that reads XML, and a header or a log.

  2. Identify the version of the welcome email that joins name into the template, and state what the email says for the name {{ 7 * 7 }}.

    Show the answer

    Version 1. The email says Hello 49, welcome to the sports shop.

  3. Give one reason why the same fix works for every interpreter in the lesson.

    Show the answer

    Every weakness is input that an interpreter reads as instructions. The fix keeps the input as data, apart from the instructions.

Practise it

Challenges with a weakness this lesson teaches.