{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/cookie-domain-same-site-difference","result":{"data":{"markdownRemark":{"id":"3f2e6b84-9e6c-5045-9809-613309b1c528","html":"<h2 id=\"はじめに\" style=\"position:relative;\"><a href=\"#%E3%81%AF%E3%81%98%E3%82%81%E3%81%AB\" aria-label=\"はじめに permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>はじめに</h2>\n<p>Cookie の安全な設定について調べていると登場する SameSite 属性は、Google Chrome の仕様変更の話もあり、耳にしたことがある方も少なくないと思います。</p>\n<p>Cookie には SameSite 属性と間違えやすい設定として Domain 属性もあり、片方だけ設定すればいいのか、どう使い分けるものなのかが一見分かりにくいのではないでしょうか。</p>\n<h2 id=\"結論\" style=\"position:relative;\"><a href=\"#%E7%B5%90%E8%AB%96\" aria-label=\"結論 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>結論</h2>\n<p>結論としては、<strong>Cookie の Domain 属性は送信「先」の制限、SameSite は送信「元」の制限</strong>という違いです。</p>\n<p>Domain 属性は Cookie を送る先を設定するもので、誤って設定すると第三者のサイトに Cookie を送信することになり、セッションハイジャックが発生する可能性があります。</p>\n<p>SameSite 属性は Cookie を送信するもとになるサイトを指定する属性で、CSRF 攻撃に対するいくらかの防御を提供します。</p>\n<p>設定内容も目的も全く異なるものなので、<strong>どちらか片方を設定すればいいというものではなく、両方の設定について考える必要があります</strong>。</p>\n<h2 id=\"解説\" style=\"position:relative;\"><a href=\"#%E8%A7%A3%E8%AA%AC\" aria-label=\"解説 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>解説</h2>\n<h3 id=\"domain-属性\" style=\"position:relative;\"><a href=\"#domain-%E5%B1%9E%E6%80%A7\" aria-label=\"domain 属性 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Domain 属性</h3>\n<p><a href=\"https://tools.ietf.org/html/rfc6265#section-4.1.2.3\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">RFC 6265, セクション 4.1.2.3: The Domain Attribute</a> によると</p>\n<blockquote>\n<p>The Domain attribute specifies those hosts to which the cookie will be sent.</p>\n</blockquote>\n<p>とのことで、つまり、<strong>Domain 属性はどのドメインへのアクセスでこの Cookie を送信するかという設定</strong>です。</p>\n<p>1 つのドメインからだけ成り立つサービスであれば意識することはありませんが、<strong>複数のドメイン (サブドメイン) から成り立つサービスでセッションを共有する際などに使われます</strong>。</p>\n<p>例えば、「<a href=\"https://qiita.com/il-m-yamagishi/items/9aad5737c80d5bfd5eb8\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">サブドメインの異なるサービス間で cookie を共有する</a>」という記事では</p>\n<blockquote>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">https://www.nicovideo.jp  // ニコニコ動画\nhttps://live.nicovideo.jp // ニコニコ生放送\nhttps://ch.nicovideo.jp   // ニコニコチャンネル</code></pre></div>\n</blockquote>\n<p>という例が挙げられています。</p>\n<p>なお、Domain 属性は、指定しなければ Cookie を発行したドメインのみ許可され、指定するとサブドメインまで許可されるため、指定しないのが一番安全です。\nこのことは「<a href=\"https://blog.tokumaru.org/2011/10/cookiedomain.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">CookieのDomain属性は <em>指定しない</em> が一番安全</a>」という記事で解説されています。</p>\n<h3 id=\"samesite-属性\" style=\"position:relative;\"><a href=\"#samesite-%E5%B1%9E%E6%80%A7\" aria-label=\"samesite 属性 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Samesite 属性</h3>\n<p><a href=\"https://tools.ietf.org/html/draft-west-first-party-cookies-07\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">draft-west-first-party-cookies-07 - Same-site Cookies</a> によると、</p>\n<blockquote>\n<p>This document updates RFC6265 by defining a “SameSite” attribute\nwhich allows servers to assert that a cookie ought not to be sent\nalong with cross-site requests.  This assertion allows user agents to\nmitigate the risk of cross-origin information leakage, and provides\nsome protection against cross-site request forgery attacks.</p>\n</blockquote>\n<p>とのことで、つまり、<strong>SameSite 属性は異なるドメインへの Cookie の送信を防ぎ、CSRF 攻撃へのある程度の防御を提供する</strong>ものです。</p>\n<p>CSRF 攻撃は、第三者のサイトから自分たちのサービスに不正にアクセスされるというものです。\n<strong>SameSite を適切に設定すると、Cookie を取得したドメインからのリクエストでしか Cookie が送信されないようになるため、CSRF 攻撃への防御としてはたらく</strong>ということです。\n具体的な設定の解説は検索するとたくさん出てくるので、他の記事などを参照ください。</p>\n<p>SameSite 属性については、「<a href=\"https://qiita.com/ahera/items/0c8276da6b0bed2b580c\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Chrome 80が密かに呼び寄せる地獄 ～ SameSite属性のデフォルト変更を調べてみた</a>」という記事に書かれているように、Google Chrome がデフォルトの挙動を変更することで影響を受けるサイトが少なくないことが話題になっていました。</p>\n<h2 id=\"参考\" style=\"position:relative;\"><a href=\"#%E5%8F%82%E8%80%83\" aria-label=\"参考 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>参考</h2>\n<h3 id=\"web\" style=\"position:relative;\"><a href=\"#web\" aria-label=\"web permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Web</h3>\n<p>Stack Overflow に Domain 属性と SameSite 属性の違いに関する質問・回答があったので、回答をこちらに引用しておきます。</p>\n<blockquote>\n<p>The Domain attribute restricts the hosts that the cookie will be sent to. The SameSite attribute restricts the origins from which the cookie may be sent.</p>\n</blockquote>\n<p><a href=\"https://stackoverflow.com/questions/57090774/what-are-the-security-differences-between-cookies-with-domain-vs-samesite-strict\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">What are the security differences between cookies with Domain vs SameSite strict?</a></p>\n<h3 id=\"書籍\" style=\"position:relative;\"><a href=\"#%E6%9B%B8%E7%B1%8D\" aria-label=\"書籍 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>書籍</h3>\n<p>Cookie の Domain 属性と SameSite 属性について、それぞれ解説している書籍として『<a href=\"https://amzn.to/3dfTxMC\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Real World HTTP 第2版 ―歴史とコードに学ぶインターネットとウェブ技術</a>』があります。Cookie だけでなく、HTTP について一歩深く学ぶのに非常に良い書籍です。</p>\n<p><a href=\"https://www.amazon.co.jp/dp/4873119030/ref=as_li_ss_il?ie=UTF8&linkCode=li2&tag=oshimayuki0d-22&linkId=ad78f4cd24a7df150c35d6f2501606b3&language=ja_JP\" target=\"_blank\"><img border=\"0\" src=\"//ws-fe.amazon-adsystem.com/widgets/q?_encoding=UTF8&ASIN=4873119030&Format=_SL160_&ID=AsinImage&MarketPlace=JP&ServiceVersion=20070822&WS=1&tag=oshimayuki0d-22&language=ja_JP\" ></a><img src=\"https://ir-jp.amazon-adsystem.com/e/ir?t=oshimayuki0d-22&amp;language=ja_JP&amp;l=li2&amp;o=9&amp;a=4873119030\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\"></p>","fields":{"slug":"/posts/cookie-domain-same-site-difference","tagSlugs":["/tag/web/"],"autoRecommendPosts":["what-is-service-class","books-about-how-web-and-web-applications-works","rest-graphql-grpc-books","ddd-books"]},"frontmatter":{"date":"2020-07-29T03:39:39.184Z","description":"Cookie の安全な設定について調べていると登場する SameSite 属性は、Google Chrome の仕様変更の話もあり、耳にしたことがある方も少なくないと思います。\nCookie には SameSite 属性と間違えやすい設定として Domain 属性もあり、片方だけ設定すればいいのか、どう使い分けるものなのかが一見分かりにくいのではないでしょうか。\n","tags":["web"],"title":"Cookie の Domain 属性と SameSite 属性の違い","socialImage":null,"recommendPosts":["https://www.kanzennirikaisita.com/posts/books-about-how-web-and-web-applications-works","https://www.kanzennirikaisita.com/posts/web-security-books","https://www.kanzennirikaisita.com/posts/software-developer-books-index"]}}},"pageContext":{"slug":"/posts/cookie-domain-same-site-difference"}},"staticQueryHashes":["251939775","3942705351","401334301"]}