Course contentsShow
← Security
Lesson 977 of 3,05220. Web Security - File Upload and ProcessingFree lesson

Server-Side Template Injection via Files

Discover SSTI vulnerabilities when user-uploaded content is processed through template engines.

Server-Side Template Injection via Files

What you'll learn: How attackers can exploit template engines by uploading malicious files that get processed server-side.

The Core Vulnerability

Server-Side Template Injection (SSTI) via files occurs when an application processes user-uploaded content through a template engine without proper sanitization. Template engines (like Jinja2, Twig, or Handlebars) are designed to dynamically generate HTML or other output by combining templates with data. When user-controlled file content is passed directly into these engines, attackers can inject malicious template code that executes on the server.

How It Works

Think of a template engine as a form letter generator. You provide a template like "Hello {{name}}, welcome!" and the engine fills in {{name}} with actual data. But what if an attacker uploads a file containing {{7*7}} or worse — commands that read files, execute system commands, or access sensitive data? If the application naively feeds that uploaded content into the template engine, the malicious code runs server-side.

This commonly happens when:

  • Applications convert uploaded documents (PDFs, markdown, rich text) into HTML using templates
  • User-uploaded configuration files are processed through template engines
  • Image metadata or embedded text is extracted and rendered via templates
  • CSV/Excel files are imported and their contents are templated for display

The Attack Chain

  1. Attacker uploads a file (document, image, config file) containing template injection payloads
  2. The server processes the file and extracts content
  3. That content is passed to a template engine for rendering
  4. The malicious template code executes, potentially giving the attacker remote code execution

Unlike direct SSTI through URL parameters or form fields (which you haven't covered yet), file-based SSTI can be harder to detect because the payload is hidden inside what appears to be a legitimate file.

Key Takeaway: Always sanitize and escape user-uploaded file content before passing it to template engines — treat file contents as untrusted input just like form data.