如何从移动设备(Android)应用程序中使用CSRF令牌发送邮政请求到flask API?
问题:
如何将数据从我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在这里没有提供足够的信息,但是当我开始学习 Flask 时,我遇到了类似的问题。所以,我认为这对你来说也应该是类似的情况。
我正在创建一个简单的 Webhook,它将接受来自另一个应用程序的 POST 请求。如果我关闭 CSRF,POST 请求将起作用,但打开 CSRF 保护后,POST 请求将返回 400 状态代码。
有一种简单的方法可以使 Flask 中的任何视图或蓝图免受 CSRF 保护。我们可以使用flask_wtf.csrf.CSRFProtect.exempt装饰器来装饰不需要csrf保护的路由。请看
下面的代码。
我还提供了下面官方 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.
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!!
您可以使用下面的类似
you can sent it using like below