如何从移动设备(Android)应用程序中使用CSRF令牌发送邮政请求到flask API?

发布于 2025-01-19 05:11:38 字数 836 浏览 1 评论 0原文

问题:

如何将数据从我的Android应用程序发布到使用CSRF保护的Blask Web应用程序?

背景: 我已经使用烧瓶构建了一个网站,并通过全球部署CSRFProtect()(来自烧瓶 - 沃特图式包装包)来保护它免受CSRF攻击。

我正在构建一个电话应用程序,该应用程序将允许用户每天在烧瓶数据库上自动将数据发送到其帐户。

我可以使用Android应用程序的GET请求成功访问烧瓶API。 除非我关闭烧瓶API中的全局CSRF保护,否则我无法成功从Android应用程序发送帖子请求。

到目前为止我的想法: 选项One - 如果请求来自应用程序,请关闭CSRF保护。 从阅读中,我了解CSRF攻击需要Cookie,这仅由浏览器生成,因此,如果我的请求来自我的应用程序,那么我可以免受CSRF攻击,并且可以关闭针对特定URL的CSRF保护。 但是,,如果任何人都可以发现此URL,则可以访问此URL,因此我需要保留CSRF保护是否来自浏览器,如果来自我的Android,请将其关闭应用程序。这可能吗?

选项二 - 在我的Android应用程序上获取CSRF令牌。 我不认为将令牌编码到我的应用程序中是安全的,因为任何人都可以下载该应用程序并可能访问代码(对吗?)。如果是这样,那么我需要通过烧瓶应用程序的身份验证过程以某种方式从烧瓶中获取令牌。 但是,如果CSRF保护阻止我的邮政请求,我该如何将表单数据发送到烧瓶应用程序?

请指教。通常,通过足够的谷歌搜索,我可以找出一个答案,但是我被卡住了! 谢谢你!

Question:

How can I POST data from my android app to my flask web app which is employing CSRF protection?

Background:
I've built a website using Flask, and have protected it from CSRF attacks by globally deploying CSRFProtect(), which comes from the Flask-WTForms package.

I am building a phone app that will allow a user to automatically send data to their account on the Flask database every day.

I can successfully access the Flask API using a GET request from my android app.
I am unable to successfully send a POST request from my android app, unless I turn off global CSRF protection within my Flask API.

My thoughts so far:
Option one - turn off CSRF protection if request is coming from an application.
From reading I understand that CSRF attacks require cookies, which are only generated by browsers, and thus if my request is coming from my app, then I am safe from CSRF attacks and could turn off CSRF protection for a specific URL. BUT, this URL could be accessed by anyone if they were to discover it, so I would need to keep CSRF protection on if the request was coming from a browser, and turn it off if coming from my android app. Is this possible?

Option two - get the CSRF token on my android app.
I don't think that coding the token into my app would be safe, as anyone would be able to download the app and potentially access the code (right?). If that's true, then I would need to somehow get the token from Flask via an authentication process with the Flask app. BUT, how can I send form data to the flask app if CSRF protection is blocking my POST requests?

Please advise. Normally with enough googling I can figure out an answer, but on this I'm stuck!
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长不大的小祸害 2025-01-26 05:11:38

您在这里没有提供足够的信息,但是当我开始学习 Flask 时,我遇到了类似的问题。所以,我认为这对你来说也应该是类似的情况。

我正在创建一个简单的 Webhook,它将接受来自另一个应用程序的 POST 请求。如果我关闭 CSRF,POST 请求将起作用,但打开 CSRF 保护后,POST 请求将返回 400 状态代码。

有一种简单的方法可以使 Flask 中的任何视图或蓝图免受 CSRF 保护。我们可以使用flask_wtf.csrf.CSRFProtect.exempt装饰器来装饰不需要csrf保护的路由。请看
下面的代码。

from flask import Flask, request, make_response, jsonify
from flask_wtf.csrf import CSRFProtect

app = Flask(__name__) # this will take name of the project
csrf = CSRFProtect()
csrf.init_app(app)

@[email protected]("/newhook", methods=['GET', 'POST'])
@csrf.exempt #this will exempt the csrf for this view
def newhook():
    if request.method == 'POST':
        alldata = request.get_json()
        resp = alldata['message']
        num = alldata["from"]
        myres = make_response(jsonify(resp, num))
        
        return myres

我还提供了下面官方 Flask_wtf CSRF 保护文档的链接以供参考。
https://flask-wtf.readthedocs .io/en/0.15.x/csrf/#exclude-views-from-protection

希望这有帮助!

You have not provided enough information here, but I faced a similar issue when I started learning about flask. So, I think this should be a similar case for you too.

I was creating a simple webhook that would accept POST requests from another application. If I turned CSRF off, POST requests would work, but with CSRF protection turned on, POST requests returned with a 400 status code.

There is a simple way to exempt any views or blueprints in Flask from CSRF protection. We can decorate the route that does not need the csrf protection with a flask_wtf.csrf.CSRFProtect.exempt decorator. Please look at the
below code.

from flask import Flask, request, make_response, jsonify
from flask_wtf.csrf import CSRFProtect

app = Flask(__name__) # this will take name of the project
csrf = CSRFProtect()
csrf.init_app(app)

@[email protected]("/newhook", methods=['GET', 'POST'])
@csrf.exempt #this will exempt the csrf for this view
def newhook():
    if request.method == 'POST':
        alldata = request.get_json()
        resp = alldata['message']
        num = alldata["from"]
        myres = make_response(jsonify(resp, num))
        
        return myres

I am also providing a link to the official flask_wtf CSRF protect documentation below for reference.
https://flask-wtf.readthedocs.io/en/0.15.x/csrf/#exclude-views-from-protection

Hope this helps!!

往日情怀 2025-01-26 05:11:38

您可以使用下面的类似

<script type="text/javascript">
    var csrf_token = "{{ csrf_token() }}";

    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrf_token);
            }
        }
    });
</script>

you can sent it using like below

<script type="text/javascript">
    var csrf_token = "{{ csrf_token() }}";

    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrf_token);
            }
        }
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文