Injection and trust boundaries
When data becomes instructions
Injection occurs when an application lets untrusted data change the instructions interpreted by another component. A bookstore search term is supposed to be a value, not part of a database query or a program. Keep the structure of instructions under application control. A trust boundary is where data crosses from a source with different trust assumptions into a component that must enforce its own rules.
Follow the data
Imagine a customer enters the title Sam’s Guide. The browser sends the title to the server, the server searches its catalog, and the browser displays the result. Three boundaries need attention: request validation, database value binding, and text rendering. A valid title is not automatically safe in every destination. A previously stored title is still untrusted when retrieved.
Different interpreters, different controls
SQL injection can change database query meaning; command injection can alter operating-system operations; cross-site scripting (XSS) can cause browser script execution; template injection can change template evaluation. Prefer library functions instead of launching commands, fixed templates with data variables, parameterized SQL, and text-only rendering. Avoid constructing executable expressions from incoming text.
Validation is not a universal shield
A title may legitimately contain an apostrophe or Persian characters. Rejecting punctuation is not a reliable injection defense. Validate business rules such as length and type, then use an API that preserves the data/instruction distinction at the destination. Least privilege limits consequences if another control fails; authorization still determines which records a user may read.
Practice: map a search request
Draw customer → request parser → catalog query → result renderer. Label the data and the trusted instruction at each arrow. Choose one control for each boundary and explain why it cannot replace the others. Source: OWASP Injection Prevention, OS Command Injection Defense and Input Validation cheat sheets.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Injection and trust boundaries — knowledge check
Apply defensive data handling to a fictional bookstore.
What distinguishes injection from an ordinary invalid input?
A stored catalog title is read back later. What trust assumption is appropriate?
Which approach avoids invoking OS commands for a routine string operation?
Why is rejecting apostrophes insufficient?
Which control determines whether a customer can read another customer’s record?