All courses

Trust boundaries and untrusted input

Identify where data crosses from an untrusted source (users, network, files, third parties) into your code. Explain that injection bugs come from treating that data as code or commands, and that the defence is to validate and encode at the boundary.

Read it card by card Start the whole course

Who chose that file name?

You ask the agent for an endpoint that returns a saved report by name. It adds one, requests `?name=q1.pdf` in a test, gets the file back and reports done. The name comes straight from the URL. You did not choose that string, and neither did your code. Anyone who can reach the server did. What could they put there?

Where outside data meets your code

A **trust boundary** is where data from something you don't control enters your code: request parameters, headers, cookies, uploads, files, third-party API responses, queue messages. The agent tests with friendly inputs and sees them pass. That green run is only a proxy signal. **Injection** happens when outside data gets read as code or commands: a path, SQL, a shell line, HTML. Later cost: attackers read files or data, or run commands.

Checklist at every boundary

1. **Find it**: trace each value back. If any part came from outside, it is untrusted, even data from "our partner's API". 2. **Validate on entry**: check type, length and range, and match an allowlist of allowed shapes. Reject everything else. 3. **Keep data as data**: where the API allows it, pass the value as a separate argument or parameter. Never splice it into text that another program parses. 4. **Encode for the destination**: where it must go into text, encode it: HTML-escape for pages, URL-encode for links. **Tell**: input concatenated into a path, query, command or markup. **Later cost**: an attacker reads or changes data, or runs commands.

The agent's report endpoint

BAD. Follow `name`. It goes from `request.args` straight into a file path with no check. `?name=../config/.env` walks out of the folder (path traversal). Worse, `os.path.join` discards every earlier part when a later part is absolute, so `?name=/etc/passwd` opens exactly that file. The happy-path test with `q1.pdf` could never show this. Later cost: anyone who can reach the server can download your secrets and system files.
// Python
--- a/reports/views.py
+++ b/reports/views.py
@@ -1,1 +1,8 @@
 REPORT_DIR = "/srv/app/reports"
+
+@app.get("/report")
+def report():
+    name = request.args["name"]
+    path = os.path.join(REPORT_DIR, name)
+    with open(path, "rb") as f:
+        return f.read()

Fixing it at the boundary

BETTER: validate on entry. `re.fullmatch` accepts only word characters or `-` followed by `.pdf`, so anything else gets a 400. As a second layer, resolve the full path and confirm it still lies inside the folder with `is_relative_to` (Python 3.9+). Flask's `send_from_directory` also joins paths safely. Ask the agent to test hostile names such as `../x` and `/etc/passwd`, not just `q1.pdf`.
// Python
@app.get("/report")
def report():
    name = request.args.get("name", "")
    if not re.fullmatch(r"[\w-]+\.pdf", name):
        abort(400)
    base = Path(REPORT_DIR).resolve()
    path = (base / name).resolve()
    if not path.is_relative_to(base):
        abort(400)
    return path.read_bytes()

Untrusted until checked at the door

Every value from outside your code is untrusted until you check it at the boundary where it enters. Validate its shape there. Then hand it on as data, never as part of a path, query, command or page that another program will interpret. Where it must go into text, encode it for that destination.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.