Home / Learn Secure SQL / Injection via File Metadata: the Concept

Injection via File Metadata: the Concept

A filename or embedded document property is still untrusted input.

The gistA filename or embedded document property is still untrusted input.

The idea, explained simply

File metadata is still inputfile uploadedoriginal filename,content-type header,…any of these reachinga queryneeds the samebound-parameter and…
File metadata is still input

What it is

This lesson connects the file-upload project's pattern to the injection lens directly: an uploaded file's original filename, declared content type, or metadata read from inside the file (such as a document's author field) are all attacker-influenced values, and any of them reaching a database query needs the same bound-parameter treatment as any other input.

Why it matters

File-related metadata is another case (like headers and cookies) where it is tempting to treat the value as "just a filename" rather than untrusted input, but the query construction step cannot tell the difference, and neither should the defense.

Mechanically speaking

Treat every piece of file-related metadata (filename, declared type, embedded properties) that reaches a database query as untrusted input requiring the same binding and validation already covered throughout this course, exactly as the file-upload project demonstrated for storage safety more broadly.

Shows up in

  • Web application code review
  • Backend development
  • Database administration
Filename bound, never trusted as a path or identifier
const storedPath = generateRandomName() + extensionFrom(file.mimetype);
db.execute("INSERT INTO uploads (original_name, stored_path) VALUES (?, ?)",
           [file.originalName, storedPath]); // both values bound

This is the same principle the file-upload project applied for storage (a random stored_path instead of trusting the original filename); the injection-specific version of that same principle is to bind, never concatenate, any such metadata used in a query.

Check yourself

1. An uploaded file's original filename, when stored in a database, should be...