{"id":1019,"date":"2022-03-27T11:45:06","date_gmt":"2022-03-27T02:45:06","guid":{"rendered":"http:\/\/gomocool.net\/gomokulog\/?p=1019"},"modified":"2022-03-27T12:00:07","modified_gmt":"2022-03-27T03:00:07","slug":"cakephp4-csrf%e3%83%97%e3%83%ad%e3%83%86%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3","status":"publish","type":"post","link":"https:\/\/gomokulog.gomocool.net\/?p=1019","title":{"rendered":"[CakePHP4] CSRF\u30d7\u30ed\u30c6\u30af\u30b7\u30e7\u30f3"},"content":{"rendered":"\n<p>CakePHP4\u3067\u306fCSRF\u306e\u5bfe\u7b56\u3068\u3057\u3066\u3001<br>CsrfProtectionMiddleware<br>\u3068<br>SessionCsrfProtectionMiddleware <br>\u306e2\u7a2e\u985e\u304c\u3042\u308b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> CsrfProtectionMiddleware<\/h2>\n\n\n\n<p>CakePHP3.5\u3067\u8ffd\u52a0\u3055\u308cCakePHP4\u3067\u3082\u4f7f\u308f\u308c\u3066\u3044\u308b\u30d7\u30ed\u30c6\u30af\u30b7\u30e7\u30f3\u3002<br>\u30a2\u30af\u30bb\u30b9\u6642\u306b\u3001Cookie\u306bCSRF\u30c8\u30fc\u30af\u30f3\u3092\u6301\u305f\u305b\u3001POST\u6642\u306b\u305d\u306e\u30c8\u30fc\u30af\u30f3\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u540c\u4e00\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u306e\u30a2\u30af\u30bb\u30b9\u3067\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u3089\u3057\u3044\u3002<br>\u4ed5\u7d44\u307f\u306f\u3088\u304f\u308f\u304b\u3089\u306a\u3044\u304c\u3001\u30b5\u30fc\u30d0\u30fc\u306bstate\u304c\u7121\u304f\u3066\u3082\u3001CSRF\u30c1\u30a7\u30c3\u30af\u304c\u3067\u304d\u308b\u3089\u3057\u3044\u3002<br>\u306a\u306e\u3067\u3001\u5165\u529b\u753b\u9762\u3067\u3046\u306a\u3063\u3066\u308b\u3046\u3061\u306b\u30bb\u30c3\u30b7\u30e7\u30f3\u304c\u5207\u308c\u3066\u3082\u3001CSRF\u30c1\u30a7\u30c3\u30af\u306f\u3067\u304d\u308b\u306e\u3067\u3001\u4fbf\u5229\u3068\u3044\u3048\u3070\u4fbf\u5229\u306a\u6a21\u69d8\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> SessionCsrfProtectionMiddleware <\/h2>\n\n\n\n<p>CakePHP4.2\u3067\u8ffd\u52a0\u3055\u308c\u305f\u30d7\u30ed\u30c6\u30af\u30b7\u30e7\u30f3\u3002<br>\u5f93\u6765\u306e CsrfProtectionMiddleware\u306f\u3001Cookie\u306b\u3042\u308bCSRF\u30c8\u30fc\u30af\u30f3\u3060\u3051\u3067\u5b8c\u7d50\u3057\u3066\u3044\u305f\u304c\u3001\u3053\u3063\u3061\u306f\u3088\u304f\u3042\u308b\u30bb\u30c3\u30b7\u30e7\u30f3\u306b\u5024\u3092\u6301\u305f\u305b\u3066\u3044\u308b\u30bf\u30a4\u30d7\u3002\u30bb\u30c3\u30b7\u30e7\u30f3\u304c\u6709\u52b9\u306a\u3046\u3061\u3060\u3051\u4f7f\u3048\u308b\u306e\u3067\u3001\u3088\u308a\u77ed\u671f\u9593\u3067\u53b3\u5bc6\u306a\u5834\u5408\u306b\u3044\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u3061\u306a\u307f\u306b\u540c\u6642\u306b\u4f7f\u3046\u3053\u3068\u306f\u3067\u304d\u306a\u3044\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u3044\u65b9<\/h2>\n\n\n\n<p>\u3069\u3063\u3061\u3082\u3001Application.php\u3067\u8a2d\u5b9a\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>\/\/ src\/Application.php\n\/\/ \u30af\u30c3\u30ad\u30fc\u30d9\u30fc\u30b9\u306e CSRF \u30c8\u30fc\u30af\u30f3\u306e\u5834\u5408\nuse Cake\\Http\\Middleware\\CsrfProtectionMiddleware;\n\n\/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u30d9\u30fc\u30b9\u306e CSRF \u30c8\u30fc\u30af\u30f3\u306e\u5834\u5408\nuse Cake\\Http\\Middleware\\SessionCsrfProtectionMiddleware;\n\npublic function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue\n{\n    $options = [\n        \/\/ ...\n    ];\n    $csrf = new CsrfProtectionMiddleware($options);\n    \/\/ \u307e\u305f\u306f\n    $csrf = new SessionCsrfProtectionMiddleware($options);\n\n    $middlewareQueue-&gt;add($csrf);\n    return $middlewareQueue;\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u7279\u5b9a\u306e\u7bc4\u56f2\u3060\u3051CSRF\u3092\u304b\u3051\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u6a21\u69d8\u3002SessionCsrf\u306e\u307b\u3046\u306f\u3067\u304d\u308b\u304b\u4e0d\u660e<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>\/\/ src\/Application.php\nuse Cake\\Http\\Middleware\\CsrfProtectionMiddleware;\n\npublic function routes(RouteBuilder $routes) : void\n{\n    $options = [\n        \/\/ ...\n    ];\n    $routes-&gt;registerMiddleware(&#39;csrf&#39;, new CsrfProtectionMiddleware($options));\n    parent::routes($routes);\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>\/\/ config\/routes.php\n$routes-&gt;scope(&#39;\/&#39;, function (RouteBuilder $routes) {\n    $routes-&gt;applyMiddleware(&#39;csrf&#39;);\n});<\/code><\/pre><\/div>\n\n\n\n<p>\u3042\u3068\u306f\u3001Form\u30d8\u30eb\u30d1\u30fc\u3067create\u3092\u547c\u3079\u3070\u3001\u52dd\u624b\u306bhidden\u5c5e\u6027\u306einput\u304c\u542b\u307e\u308c\u308b<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-html\" data-lang=\"HTML\"><code>&lt;input type=&quot;hidden&quot; name=&quot;_csrfToken&quot; autocomplete=&quot;off&quot; value=&quot;b9eb6\u30fb\u30fb\u30fb\u30fb\u30fbedee555&quot;\/&gt;<\/code><\/pre><\/div>\n\n\n\n<p>Form\u30d8\u30eb\u30d1\u30fc\u3092\u4f7f\u308f\u306a\u3044\u5834\u5408\u306f\u624b\u52d5\u3067\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br>request\u304b\u3089\u3068\u308c\u308b\u306e\u3067\u3001\u3069\u3063\u304b\u306b\u5165\u308c\u8fbc\u3081\u3070\u52d5\u304f\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$token = $this-&gt;request-&gt;getAttribute(&#39;csrfToken&#39;);<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Ajax\u3067\u4f7f\u3046\u5834\u5408<\/h3>\n\n\n\n<p>javascript\u304b\u3089POST\u3059\u308b\u5834\u5408\u306a\u3069\u3067\u30c8\u30fc\u30af\u30f3\u3092\u5fc5\u8981\u3068\u3059\u308b\u5834\u5408\u3082\u3001<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>echo $this-&gt;Html-&gt;meta(&#39;csrfToken&#39;, $this-&gt;request-&gt;getAttribute(&#39;csrfToken&#39;));<\/code><\/pre><\/div>\n\n\n\n<p>\u3063\u3066\u611f\u3058\u3067\u3001HTML\u5185\u306b\u7a81\u3063\u8fbc\u3093\u3067\u3001jQuery\u3068\u304b\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u53d6\u3063\u3066\u304d\u3066\u3001X-CSRF-Token \u30d8\u30c3\u30c0\u30fc\u3092\u4ecb\u3057\u3066\u9001\u4fe1\u3067\u304d\u308b\u306e\u3067\u3001\u3081\u3063\u3061\u3083\u7c21\u5358\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>var csrf = $(&#39;meta[name=&quot;csrfToken&quot;]&#39;).attr(&#39;content&#39;);\n\t\t$.ajax({\n\t\t\tmethod: &quot;POST&quot;,\n\t\t\turl: &quot;\/Like\/add&quot;,\n\t\t\theaders:{&#39;X-CSRF-Token&#39;: csrf},\n\t\t\tdata: {&#39;data&#39; : &#39;mogemoge&#39;}\n\t\t}).done(function(){\n\t\t\t\/\/\n\t\t}).error(function(){\n      \/\/\n\t\t});<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u305d\u306e\u307b\u304b\u306e\u6a5f\u80fd<\/h2>\n\n\n\n<p>\u7279\u5b9a\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u306f\u30c1\u30a7\u30c3\u30af\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u305f\u308a\u3001Cookie\u306eexpire\u3084samesite\u3068\u304b\u3092\u5909\u3048\u308c\u305f\u308a\u3059\u308b\u6a21\u69d8\u3002<br>\u307e\u3041\u3001\u305d\u306e\u8fba\u308a\u306f\u3001\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u53c2\u8003\u306b\u3057\u305f\u307b\u3046\u304c\u3044\u3044\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>Session\u30d9\u30fc\u30b9\u306e\u307b\u3046\u306f\u4f7f\u3063\u3066\u306a\u3044\u306e\u3067\u3001\u307e\u3060\u3088\u304f\u5206\u304b\u3089\u306a\u3044\u304c\u3001CakePHP2\u306e\u9803\u306b\u6bd4\u3079\u308b\u3068\u3001\u3053\u3093\u306a\u3093\u3067\u5927\u4e08\u592b\u306a\u306e\u304b\u306a\u3063\u3066\u3044\u3046\u304f\u3089\u3044\u7c21\u5358\u306b\u4f7f\u3048\u308b\u3002<br><br>\u30b5\u30fc\u30d0\u30fc\u306bstate\u304c\u7121\u304f\u3066\u3082\u3001CSRF\u30c1\u30a7\u30c3\u30af\u304c\u3067\u304d\u308b\u4ed5\u7d44\u307f\u304c\u3001\u3088\u304f\u5206\u304b\u3089\u306a\u3044\u306e\u3067\u3001\u3082\u3046\u5c11\u3057\u305d\u3053\u3092\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3067\u8a18\u8ff0\u3057\u3066\u307b\u3057\u3044\u306a\u30fb\u30fb\u30fb\u3002<br>\u30b3\u30fc\u30c9\u3092\u8ffd\u3044\u304b\u3051\u308b\u306e\u3081\u3093\u3069\u3044\u3057\u3002<br><br>\u4e0d\u5b89\u3063\u3066\u3044\u3046\u4eba\u304cSession\u30d9\u30fc\u30b9\u30924.2\u3067\u8ffd\u52a0\u3057\u305f\u306e\u304b\u306a\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>CakePHP4\u3067\u306fCSRF\u306e\u5bfe\u7b56\u3068\u3057\u3066\u3001CsrfProtectionMiddleware\u3068SessionCsrfProtectionMiddleware \u306e2\u7a2e\u985e\u304c\u3042\u308b\u3002 CsrfProtectionMiddlewar&hellip;  <a href=\"https:\/\/gomokulog.gomocool.net\/?p=1019\" class=\"more-link\">more <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68,56,76],"tags":[],"class_list":["post-1019","post","type-post","status-publish","format-standard","hentry","category-cakephp","category-cakephp3","category-cakephp4"],"_links":{"self":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/1019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1019"}],"version-history":[{"count":4,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/1019\/revisions"}],"predecessor-version":[{"id":1023,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/1019\/revisions\/1023"}],"wp:attachment":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}