首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >烧瓶防止成型注射

烧瓶防止成型注射
EN

Stack Overflow用户
提问于 2019-12-22 22:25:00
回答 1查看 1.6K关注 0票数 2

python /烧瓶如何阻止异型注射?

考虑以下几点:

app.py

代码语言:javascript
运行
复制
from flask import Flask, request, render template

app = Flask(__name__)

@app.route('/', methods=['GET','POST'])
def helloworld():
    if request.method == 'GET':
        return render_template('index.html') 
    if request.method == 'POST':
        print(request.form['info'])

        ## do something with the info, like write to a database

        return 'nothing'

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

模板/index.html

代码语言:javascript
运行
复制
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type='text/javascript' src="{{ url_for('static', filename='js/fire.js') }}"></script>
</head>

<body>
<p>Hello world!</p>
</body>
</html>

静态/js/Fire.js

代码语言:javascript
运行
复制
$(document).click(function() {

    // post data to flask

    $.post('/', {'info': 'test'});

    return false;

};

我的问题是:

  1. 是否有可能从国外网站注入?跟进:这是怎么做到的?(例如,也许可以通过一份发布到我的网站url的表单)
  2. ,如果可以注入,我可以在app.py脚本中做些什么来阻止注入?

编辑

这里有一个非常基本的脚本,可以用来测试上面的瓶子应用程序的注射。接受的答案阻止此脚本:

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>
<body>

<h2>Malicious Form Injection</h2>

<form action='http://127.0.0.1:5000/' method='post'>
  Input 1:<br>
  <input name="info" value="mal1"><br>
  <input type="submit" value="Submit">
</form>


</body>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-24 03:22:51

app.py

代码语言:javascript
运行
复制
from flask import Flask, request, render template
from flask_wtf.csrf import CSRFProtect

app = Flask(__name__)

CSRFProtect(app)

app.config['SECRET_KEY'] = 'somethignrandom'

@app.route('/', methods=['GET','POST'])
def helloworld():
    if request.method == 'GET':
        return render_template('index.html') 
    if request.method == 'POST': # anything post will autocheck csrf
        print(request.form['info'])

        ## do something with the info, like write to a database

        return 'nothing'

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

不需要将秘密密钥传递给html模板,因为CSRFProtect将自动传递秘密密钥。

模板/index.html

代码语言:javascript
运行
复制
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<meta name='csrf-token' content="{{ csrf_token() }}">
<script type='text/javascript' src="{{ url_for('static', filename='js/fire.js') }}"></script>

</head>

<body>
<p>Hello world!</p>
</body>
</html>

script.js

代码语言:javascript
运行
复制
$(document).click(function() {

    // post data to flask

    $.post('/', {'info': 'test', '_csrf_token':$('meta[name="csrf-token"]').attr('content')});

    return false;

};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59448482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档