{"id":531,"date":"2018-01-23T23:48:09","date_gmt":"2018-01-23T14:48:09","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2018\/01\/23\/angular_httpclientxsrfmodule_flask_csrf\/"},"modified":"2018-01-23T23:48:09","modified_gmt":"2018-01-23T14:48:09","slug":"angular_httpclientxsrfmodule_flask_csrf","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2018\/01\/23\/angular_httpclientxsrfmodule_flask_csrf\/","title":{"rendered":"Angular + HttpClientXsrfModule + Flask \u3067 CSRF"},"content":{"rendered":"<p>Angular \u3068 Flask\u3092\u4f7f\u3063\u3066\u3001CSRF\u3092\u5b9f\u73fe\u3057\u3088\u3046\u3068\u3057\u305f\u306e\u3060\u304c\u3001\u82e5\u5e72\u624b\u6570\u304c\u304b\u304b\u3063\u305f\u306e\u3067\u30e1\u30e2\u3002<\/p>\n<h2>1.Angular + HttpClientXsrfModule<\/h2>\n<p><a href=\"https:\/\/www.amazon.co.jp\/Angular%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0-%E5%B1%B1%E7%94%B0-%E7%A5%A5%E5%AF%9B\/dp\/4774191302\/ref=as_li_ss_il?ie=UTF8&amp;qid=1516718607&amp;sr=8-1&amp;keywords=Angular&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=cd3d2cf1675343f30e25dcc668fd9736\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4774191302&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" border=\"0\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"1\" height=\"1\" style=\"margin: 0px !important; border: currentcolor !important; border-image: none !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4774191302\" border=\"0\"><\/p>\n<blockquote>\n<p><a href=\"http:\/\/amzn.to\/2DAOiaW\">Angular\u306e\u5168\u4f53\u50cf\u304b\u3089\u3001\u30b3\u30f3\u30bb\u30d7\u30c8\u3001\u8c4a\u5bcc\u306a\u5b9f\u4f8b\u3068\u5fc5\u643a\u306e\u826f\u66f8<\/a><\/p>\n<\/blockquote>\n<p>Angular \u3067\u3001CSRF\u3092\u5b9f\u88c5\u3057\u3088\u3046\u3068\u3057\u3066\u30b0\u30b0\u3063\u3066\u307f\u305f\u308a\u3057\u305f\u306e\u3060\u304c\u3001\u30b5\u30f3\u30d7\u30eb\u3068\u3057\u3066\u8a00\u53ca\u3055\u308c\u3066\u3044\u308b\u3001XSRFStrategy \u306f\u3001Deprecated \u3067\u3001@angular\/common\/http \u3092\u4ee3\u308f\u308a\u306b\u4f7f\u7528\u3059\u308b\u3088\u3046\u306b\u3068\u306e\u3053\u3068\u3002<\/p>\n<p><a title=\"https:\/\/angular.io\/api\/http\/XSRFStrategy\" href=\"https:\/\/angular.io\/api\/http\/XSRFStrategy\">https:\/\/angular.io\/api\/http\/XSRFStrategy<\/a><\/p>\n<p>\u305d\u3082\u305d\u3082 @angular\/http \u304c\u3001Deprecated \u306a\u3093\u3067\u3059\u306d\u3002\u3053\u3061\u3089\u3082\u3001common\/http\u3092\u4f7f\u3046\u3088\u3046\u306b\u66f8\u304b\u308c\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<p><a title=\"https:\/\/angular.io\/api\/http\/HttpModule\" href=\"https:\/\/angular.io\/api\/http\/HttpModule\">https:\/\/angular.io\/api\/http\/HttpModule<\/a>&nbsp; \u3053\u3061\u3089\u3067\u306f\u306a\u304f\u3001<\/p>\n<h3>1.1 \u30e2\u30b8\u30e5\u30fc\u30eb\u5ba3\u8a00<\/h3>\n<p><a title=\"https:\/\/angular.io\/guide\/http#security-xsrf-protection\" href=\"https:\/\/angular.io\/guide\/http#security-xsrf-protection\">https:\/\/angular.io\/guide\/http#security-xsrf-protection<\/a> \u306b\u6cbf\u3063\u3066\u5b9f\u88c5\u3059\u308b\u3002<\/p>\n<p>\u307e\u305a\u3001\u4ee5\u4e0b\u306e2\u3064\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092 app.module \u306b import\u3002<\/p>\n<p><a title=\"https:\/\/angular.io\/api\/common\/http\/HttpClientModule\" href=\"https:\/\/angular.io\/api\/common\/http\/HttpClientModule\">https:\/\/angular.io\/api\/common\/http\/HttpClientModule<\/a><\/p>\n<p><a title=\"https:\/\/angular.io\/api\/common\/http\/HttpClientXsrfModule\" href=\"https:\/\/angular.io\/api\/common\/http\/HttpClientXsrfModule\">https:\/\/angular.io\/api\/common\/http\/HttpClientXsrfModule<\/a><\/p>\n<p>\u30b5\u30fc\u30d0\u30fc\u5074\u3067Cookie \u306b\u30bb\u30c3\u30c8\u3057\u305f\u3001Cookie\u540d\u300cXSFR-TOKEN\u300d\u306eCSRF\u30c8\u30fc\u30af\u30f3\u3092\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u30d8\u30c3\u30c0\u30fc\u306b\u30ad\u30fc\u540d\u300cX-XSRF-TOKEN\u300d\u3068\u3057\u3066\u8fd4\u3059\u3053\u3068\u3092\u3001HttpClientModule\u3001HttpClientXsrfModule \u306e\u5ba3\u8a00\u3092\u3059\u308b\u3060\u3051\u3067\u81ea\u52d5\u3067\u3084\u3063\u3066\u304f\u308c\u308b\u3002<\/p>\n<p>\u4e0a\u8a18\u306e\u30ad\u30fc\u540d\u3092\u5909\u66f4\u3057\u305f\u3044\u5834\u5408\u3001imports \u30bb\u30af\u30b7\u30e7\u30f3\u3067\u3001withOption \u306b\u5225\u540d\u3092\u6307\u5b9a\u3059\u308b\u3002\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>app.module<\/p>\n<pre class=\"prettyprint linenums\">import { HttpClientModule, HttpClientXsrfModule } from '@angular\/common\/http';\r\n@NgModule({\r\n  declarations: [\r\n     : \u7565\r\n  ],\r\n  imports: [\r\n     : \u7565\r\n    HttpClientModule,\r\n    HttpClientXsrfModule, \/*.withOptions({ cookieName: 'XSRF-TOKEN', headerName: 'X-XSRF-TOKEN'})*\/\r\n  ],\r\n  providers: [],\r\n  bootstrap: [AppComponent]\r\n})\r\nexport class AppModule { }\r\n\r\n<\/pre>\n<h3>1.2 \u30b5\u30fc\u30d3\u30b9\u306e\u5b9f\u88c5<\/h3>\n<p>HttpClient \u3092 import\u3057\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3002\u3068\u308a\u3042\u3048\u305a\u3001\u4eca\u56de\u306f\u3001CSRF\u3092\u9069\u7528\u3059\u308b\u30ea\u30af\u30a8\u30b9\u30c8\u306f POST\u306e\u307f\u5bfe\u5fdc\u3068\u3059\u308b\u305f\u3081\u3001POST\u3067\u9001\u4fe1\u3002<\/p>\n<pre class=\"prettyprint linenums\">import { Injectable } from '@angular\/core';\r\nimport { HttpClient } from '@angular\/common\/http'\r\n\r\n@Injectable()\r\nexport class HogeService {\r\n\r\n  configureRequest(req: Request): void {\r\n    \r\n  }\r\n\r\n  constructor(private http: HttpClient) { }\r\n\r\n  hello(name: string){\r\n    this.http.post('\/api\/hello',\r\n      JSON.stringify({ params: { name: name } })\r\n    ).subscribe(\r\n      response =&gt; {\r\n        console.log(response);\r\n      },\r\n      error =&gt; {\r\n        console.log(error);\r\n      }\r\n    );\r\n  }\r\n}\r\n<\/pre>\n<p>Angular\u5074\u306f\u4ee5\u4e0a\u3002<\/p>\n<h2>2.Flask<\/h2>\n<p>Flask\u306e\u30b3\u30fc\u30c9\u30b9\u30cb\u30da\u30c3\u30c8\u3092\u53c2\u8003\u306bCSRF\u3092\u5b9f\u88c5\u3059\u308b\u3002<\/p>\n<p><a title=\"http:\/\/flask.pocoo.org\/snippets\/3\/\" href=\"http:\/\/flask.pocoo.org\/snippets\/3\/\">http:\/\/flask.pocoo.org\/snippets\/3\/<\/a><\/p>\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001HTML\u306e hidden \u306b\u30ad\u30fc\u3092\u8a2d\u5b9a\u3059\u308b\u306e\u3060\u304c\u3001Angular \u3067\u751f\u6210\u3057\u305fHTML\u30d5\u30a1\u30a4\u30eb\u3092\u3001Flask\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u3057\u3066\u3001Flask\u306e\u5909\u6570\u3092\u57cb\u3081\u8fbc\u3080\u306e\u306f\u96e3\u5100\u3001\u304b\u3064\u3001\u4e0a\u8a18Angular\u306e\u6226\u7565\u3068\u3057\u3066\u3001Cookie\u306bCSRF\u30c8\u30fc\u30af\u30f3\u3092\u57cb\u3081\u8fbc\u3093\u3067\u6e21\u3059\u3002Cookie\u306e\u4f5c\u6210\u306e\u30b9\u30cb\u30da\u30c3\u30c8\u306f\u4ee5\u4e0b\u3002<\/p>\n<p><a title=\"http:\/\/flask.pocoo.org\/snippets\/30\/\" href=\"http:\/\/flask.pocoo.org\/snippets\/30\/\">http:\/\/flask.pocoo.org\/snippets\/30\/<\/a><\/p>\n<h3>2.1 \u6ce8\u610f\u70b9\u306a\u3069<\/h3>\n<h4>app.secret_key = &#8216;hogehoge&#8217;<\/h4>\n<p>session \u3092\u4f7f\u7528\u3059\u308b\u3068\u304d\u306b\u3001secret_key \u306b\u4efb\u610f\u306e\u5024\u3092\u8a2d\u5b9a\u3057\u306a\u3044\u3068\u30a8\u30e9\u30fc\u3068\u306a\u308b\u3002<\/p>\n<p>session\u306b\u306f\u3001\u6700\u521d\u306b\u753b\u9762(index.html) \u3092\u8fd4\u3059\u6642\u306b\u751f\u6210\u3057\u305fCSRF\u30c8\u30fc\u30af\u30f3\u3092\u683c\u7d0d\u3059\u308b<\/p>\n<h4>redirect_ui_index_with_crsftoken()<\/h4>\n<p>Angular\u304c\u751f\u6210\u3057\u305f\u3001index.html \u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3057\u3001Http Response\u306b\u3001\u201dXSRF-TOKEN&#8221;\u3092\u8a2d\u5b9a\u3057\u3066\u8fd4\u3059<\/p>\n<h4>name = request.json[&#8216;params&#8217;][&#8216;name&#8217;]<\/h4>\n<p>application\/json \u3068\u3057\u3066\u9001\u4fe1\u3055\u308c\u305f\u30c7\u30fc\u30bf\u306f\u3001Flask \u3067\u3001request.json \u3068\u3057\u3066\u89e3\u6790\u6e08\u307f\u7d50\u679c\u3092\u53d6\u5f97\u3067\u304d\u308b\u3002<\/p>\n<h4>def server_error(e)<\/h4>\n<p>\u5171\u901a\u306e\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30e9\u30fc\u3002@app.errorhandler\u3067\u5bfe\u5fdc\u3059\u308b\u30a8\u30e9\u30fc\u30b3\u30fc\u30c9\u3092\u6307\u5b9a\u3002<\/p>\n<h4>csrf_protect()<\/h4>\n<p>@app.before_request \u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3067\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u524d\u51e6\u7406\u3002POST\u306e\u5834\u5408\u3001session\u304b\u3089\u3001&#8221;XSRF-TOKEN&#8221;\u3092\u53d6\u5f97\u3002\u30ea\u30af\u30a8\u30b9\u30c8\u30d8\u30c3\u30c0\u30fc\u3068\u3057\u3066\u9001\u4fe1\u3055\u308c\u305f\u3001\u201dX-XSRF-TOKEN\u201d\u306e\u5024\u3068\u6bd4\u8f03\u3057\u3001\u4e00\u81f4\u3059\u308c\u3070\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d7\u3051\u4ed8\u3051\u308b\u3002<\/p>\n<p>\u4e0d\u4e00\u81f4\u306e\u5834\u5408\u3001\u4e0d\u6b63\u30a2\u30af\u30bb\u30b9\u3068\u3057\u3066403\u30a8\u30e9\u30fc\u3068\u3059\u308b\u3002<\/p>\n<h4>generate_csrf_token()<\/h4>\n<p>session\u306bCSRF_TOKEN\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u751f\u6210(UUID)\u3057\u683c\u7d0d\u3002<\/p>\n<pre class=\"prettyprint linenums\">import logging\r\nimport uuid\r\n\r\nfrom flask import Flask, render_template, request, redirect, session, abort, jsonify\r\n\r\nCSRF_TOKEN = '_csrf_token'\r\n\r\napp = Flask(__name__, static_folder='app')\r\napp.secret_key = 'hogehoge'\r\n\r\n@app.route('\/', methods=['GET'])\r\ndef redirect_ui_index_with_crsftoken():\r\n    response = app.make_response(redirect(\"\/hoge\/index.html\"))\r\n    response.set_cookie('XSRF-TOKEN', value=generate_csrf_token()) \r\n    return response\r\n\r\n@app.route('\/api\/hello', methods=['POST'])\r\ndef api_hello():\r\n    name = request.json['params']['name']\r\n    return jsonify({\"result\":{\"name\":name}})\r\n\r\n@app.errorhandler(403)\r\n@app.errorhandler(500)\r\ndef server_error(e):\r\n    logging.exception(e)\r\n    return 'an error occurred.', e.code\r\n\r\n@app.before_request\r\ndef csrf_protect():\r\n    if request.method == \"POST\":\r\n        token = session[CSRF_TOKEN]\r\n        if not token or token != request.headers.get('X-XSRF-TOKEN'):\r\n            abort(403)\r\n\r\ndef generate_csrf_token():\r\n    if CSRF_TOKEN not in session:\r\n        session[CSRF_TOKEN] = str(uuid.uuid4())\r\n    return session[CSRF_TOKEN]\r\n<\/pre>\n<p>\u4ee5\u4e0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular \u3068 Flask\u3092\u4f7f\u3063\u3066\u3001CSRF\u3092\u5b9f\u73fe\u3057\u3088\u3046\u3068\u3057\u305f\u306e\u3060\u304c\u3001\u82e5\u5e72\u624b\u6570\u304c\u304b\u304b\u3063\u305f\u306e\u3067\u30e1\u30e2\u3002 1.Angular + HttpClientXsrfModule Angular\u306e\u5168\u4f53\u50cf\u304b\u3089\u3001\u30b3\u30f3\u30bb\u30d7\u30c8\u3001\u8c4a\u5bcc\u306a\u5b9f\u4f8b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[102,101,100],"tags":[],"class_list":["post-531","post","type-post","status-publish","format-standard","hentry","category-angular","category-flask","category-google-cloud-platform"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=531"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}