Lesson 3 of 7
Other interpreters
Listen to an AI summary of Other interpreters
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:
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:
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.
- Hello words of the email
- {{ name }}the gap for the data called name
- , welcome to the sports shop.words of the 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:
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.
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:
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.
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
rawin Django for Python,$queryRawUnsafein Prisma for JavaScript,find_by_sqlin Rails for Ruby andDB::selectin Laravel for PHP.- A function that runs a string as code
evalandexecin Python,evalandnew Functionin JavaScript, andevalandinstance_evalin Ruby.- An XML library with external entities off already
defusedxmlin 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 asraw. - 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
evalorexec.
Check your understanding
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.Identify the version of the welcome email that joins
nameinto 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.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.
- Log4ShellReal CVEs, IntermediateOpen