from flask import Flask, request, render_template_string, render_template app = Flask(__name__) @app.route('/hello-template-injection') defhello_ssti(): person = {'name':"world", 'secret':"UGhldmJoZj8gYWl2ZnZoei5wYnovcG5lcnJlZg=="} if request.args.get('name'): person['name'] = request.args.get('name') template = '''<h2>Hello %s!</h2>'''%person["name"] return render_template_string(template, person=person) ##### Private function if the user has local files.###
defget_user_file(f_name): withopen(f_name) as f: return f.readlines()
app.jinja_env.globals['get_user_file'] = get_user_file # Allows for use in Jinja2 templates if __name__ == "__main__": app.run(debug=True)
{% autoescape true %} <h2>Good</h2> <p> Hello {{ name }}! I don't trust your input. I escaped it, just in case. </p> {% endautoescape %} <h2>Bad</h2> <p> I trust all data! How are you {{ name }}? </p>
from flask import Flask, request, render_template_string, render_template
app = Flask(__name__) @app.route('/hello-template-injection') defhello_ssti(): person = {'name':"world", 'secret':"UGhldmJoZj8gYWl2ZnZoei5wYnovcG5lcnJlZg=="} if request.args.get('name'): person['name'] = request.args.get('name') template = '<h2>Hello {{person.name}}!</h2>' return render_template_string(template, person=person) ##### Private function if the user has local files.###
@app.route('/hello-xss') defhello_xss(): name = "world" template = 'hello.unsafe'# 'unsafe' file extension... totally legit. if request.args.get('name'): name = request.args.get('name') return render_template(template, name=name)
defget_user_file(f_name): withopen(f_name) as f: return f.readlines()
#app.jinja_env.globals['get_user_file'] = get_user_file # Allows for use in Jinja2 templates if __name__ == "__main__": app.run(debug=True)
from flask import Flask, request, render_template_string, render_template
app = Flask(__name__) @app.route('/hello-template-injection') defhello_ssti(): person = {'name':"world", 'secret':"UGhldmJoZj8gYWl2ZnZoei5wYnovcG5lcnJlZg=="} if request.args.get('name'): person['name'] = request.args.get('name') template = '<h2>Hello {{person.name}}!</h2>' return render_template_string(template, person=person) ##### Private function if the user has local files.###
@app.route('/hello-xss') defhello_xss(): name = "world" template = 'hello.unsafe'# 'unsafe' file extension... totally legit. if request.args.get('name'): name = request.args.get('name') return render_template(template, name=name)
@app.route("/hello-hi") defhello_hi(): template = '''<title>No Injection Allowed!</title> <a href='{{ url_for('hello_xss')}}?name={{ name e}}'> Click here for a welcome message</a>''' name = "world" if request.args.get('name'): name = request.args.get('name') return render_template_string(template, name=name)
defget_user_file(f_name): withopen(f_name) as f: return f.readlines()
#app.jinja_env.globals['get_user_file'] = get_user_file # Allows for use in Jinja2 templates if __name__ == "__main__": app.run(debug=True)