是否可以编写易受 XSS 攻击的 Flask Web 应用程序?

问题描述

我正在尝试编写一个存储的 xss 易受攻击的 Flask Web 应用程序。我的应用程序通过此输入字段 <input type="text" name="content" id="content" /> 接收输入,然后将用户输入显示到 HTML 表中。 我试图在输入字段中插入一个像 <script>alert(1)</script> 这样的脚本,但是当它显示时,脚本没有被触发。
这是我的代码:
app.py

from flask import Flask,render_template,request,redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class Todo(db.Model):
  id = db.Column(db.Integer,primary_key=True)
  content = db.Column(db.String(200),nullable=False)
  date_created = db.Column(db.DateTime,default=datetime.utcnow)

  def __repr__(self):
    return '<Task %r>' % self.id

@app.route('/',methods = ['POST','GET'])
def index():
  if request.method == 'POST':
    task_content = request.form['content']
    new_task = Todo(content=task_content)
    try:
      db.session.add(new_task)
      db.session.commit()
      return redirect('/')
    except:
      return "There was an issue adding your task"
  else:
    tasks = Todo.query.order_by(Todo.date_created).all()
    return render_template("index.html",tasks=tasks)

if __name__ == "__main__":
  app.run(debug=True)

index.html

{% extends 'base.html' %} 
{% block head %}
<title>Task Master</title>
{% endblock %}
{% block body %}
<div class="content">
  <h1 style="text-align: center">Task Master</h1>
  {% if tasks|length < 1 %}
  <h4 style="text-align: center">There are no tasks. Create one below!</h4>
  {% else %}
  <table>
    <tr>
      <th>Task</th>
      <th>Added</th>
      <th>Actions</th>
    </tr>
    {% for task in tasks %}
    <tr>
      <td>{{ task.content }}</td>
      <td>{{ task.date_created.date() }}</td>
      <td>
        <a href="/delete/{{task.id}}">Delete</a>
        <br />
        <a href="/update/{{task.id}}">Update</a>
      </td>
    </tr>
    {% endfor %}
  </table>
  {% endif %}

  <div class="form">
    <form action="/" method="POST">
      <input type="text" name="content" id="content" />
      <input type="submit" value="Add Task" />
    </form>
  </div>
</div>
{% endblock %}

base.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" >
    <meta http-equiv="X-UA-Compatible" content="IE=edge" >
    <meta name="viewport" content="width=device-width,initial-scale=1.0" >
    <link
      rel="stylesheet"
      href="{{ url_for('static',filename='css/main.css') }}"
    >
    {% block head %}{% endblock %}
  </head>
  <body>
    {% block body %}{% endblock %}
  </body>
</html>

我遵循 this 示例。
这是表单的屏幕:
(https://i.stack.imgur.com/870tY.png)
这是脚本提交后的 HTML 屏幕:
(https://i.stack.imgur.com/U1Jdd.png)

加载页面时如何使脚本可执行?

解决方法

Flask 使用 Jinja2 模板引擎,Flask 默认在 Jinja2 上启用自动转义。

如果您真的想允许 XSS,请将模板上的 {{ task.content }} 更改为 {{ task.content|safe }}

更多信息:Notebook colors

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...