SSTI
Server-Side Template Injection (SSTI)
Example 1: Python (Flask)
Vulnerable Code:
pythonCopy code@app.route('/greet')
def greet():
name = request.args.get('name')
return render_template_string('Hello {{ name }}', name=name)Reason for vulnerability: User input is directly used in the template, allowing SSTI.
Fixed Code:
pythonCopy code@app.route('/greet')
def greet():
name = request.args.get('name')
return render_template('greet.html', name=name)Reason for fix: Use static templates instead of rendering user input directly.
Example 2: Python (Jinja2)
Vulnerable Code:
pythonCopy codefrom jinja2 import Template
template = Template("Hello {{ name }}")
output = template.render(name=request.args.get('name'))Reason for vulnerability: User input is directly used in the template, allowing SSTI.
Fixed Code:
Reason for fix: Escape user input before rendering the template.
Java Example
Vulnerable Code:
Reason for Vulnerability:
This code directly incorporates user input into a FreeMarker template, allowing potential injection of malicious templates.
Fixed Code:
Reason for Fix:
The fixed code separates the template from user input and uses FreeMarker's built-in HTML escaping to prevent SSTI.
Python Example
Vulnerable Code:
Reason for Vulnerability:
This code directly incorporates user input into a template string, allowing potential injection of malicious templates.
Fixed Code:
Reason for Fix:
The fixed code uses a separate template file and Flask's automatic escaping to prevent SSTI.
Ruby Example
Vulnerable Code:
Reason for Vulnerability:
This Sinatra route directly incorporates user input into an ERB template, allowing potential injection of malicious Ruby code.
Fixed Code:
Reason for Fix:
The fixed code uses a separate template file and Rack's HTML escaping to prevent SSTI.
Last updated