[44] [DFN[[CODE(JS)@en[[[document.domain]]]]]] [[IDL属性]]は、
[[実効スクリプト起源]]の[[ホスト名]]部分を表すものです。

[FIG(important)[
[48] [CODE(JS)@en[[[document.domain]]]] はセキュリティーホールを容易に作れてしまい危険です。
ほとんどの場合は、 [CODE(DOMm)@en[[[postMessage]]]] など他の方法を探すべきです。
]FIG]

* 仕様書

[REFS[
- [17] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2015-04-25 04:40:19 +09:00]] 版) <https://html.spec.whatwg.org/#dom-document-domain>
-- [20] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2015-04-25 04:40:19 +09:00]] 版) <https://html.spec.whatwg.org/#the-document's-domain>
- [47] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2015-04-25 04:40:19 +09:00]] 版) <https://html.spec.whatwg.org/#sandboxed-document.domain-browsing-context-flag>
- [69] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2014-10-14 21:54:07 +09:00]] 版) <https://html.spec.whatwg.org/#environment-settings-object>
- [73] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2016-03-23 21:27:20 +09:00]] 版) <https://html.spec.whatwg.org/#origin-2>
- [78] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2016-03-31 07:16:44 +09:00]] 版) <https://html.spec.whatwg.org/#origin>
]REFS]

* 意味

[22] [CODE(JS)@en[[[document.domain]]]] は、同じ[[ドメイン]]の異なる[[ホスト]]のページが互いの
[[DOM]] のアクセスできるようにするものです [SRC[>>17]]。

[23] [CODE(JS)@en[[[document.domain]]]] を使って[[同一起源ポリシー]]の制約を緩和すると、
[[起源]]の[[ポート]]部分は無視されることになりますから、注意が必要です。
特に同じ[[IPアドレス]]で複数の(異なる[[著者]]の)異なる[[ポート]]の
[[HTTPサーバー]]が動作しているような場合には、[CODE(JS)@en[[[document.domain]]]]
によって通常ならアクセスできない[[起源]]のページから [[DOM]]
にアクセスできてしまうことがあります。 [SRC[>>17]]

* 取得器

[18] [CODE(JS)@en[document.domain]] [[IDL属性]]の[[取得器]]は、
次のようにしなければ[MUST[なりません]] [SRC[>>17]]。
[FIG(steps)[
= [63] [[文脈オブジェクト]]の[F[起源][文書の起源]]の[F[実効ドメイン]]が
[CODE[null]] の場合、
== [98] [[空文字列]]を返します。
= [99] それ以外の場合、
== [100] [[文脈オブジェクト]]の[F[起源][文書の起源]]の[F[実効ドメイン]]を返します。
]FIG]

;; [33] 諸々の定義から、 [CODE(JS)@en[document.domain]] が返す値は常に
[[ASCIIドメイン名]]となります。

* 設定器

[19] [CODE(JS)@en[document.domain]] [[IDL属性]]の[[設定器]]は、
次のようにしなければ[MUST[なりません]] [SRC[>>17]]。

[FIG(steps)[
= [59] [VAR[新しい値]]を、新しい値を [CODE(DOMi)@en[USVString]] として解釈した結果に設定します。
= [28]
[FIG(list)[
- [29] [[文脈オブジェクト]]の[F[閲覧文脈]]が [CODE[null]]
- [30] [[文脈オブジェクト]]の[F[活性砂箱化フラグ集合]]の
[F[[DFN[[RUBYB[[[砂箱化[CODE(JS)@en[document.domain]]閲覧文脈フラグ]]]@en[[[sandboxed [CODE(JS)@en[document.domain]] browsing context flag]]]]]]]] [SRC[>>47]]
が[[真]]
- [34] [[文脈オブジェクト]]の[F[起源][文書の起源]]の[F[実効ドメイン]]が [CODE[null]]
- [38] [VAR[新しい値]]が[[文脈オブジェクト]]の[F[起源][文書の起源]]の[F[実効ドメイン]]と[[登録可能ドメイン接尾辞であるか等しい]]のでは''ない''
- [109] 
[[文脈オブジェクト]]が
[DFN[[CODE[document-domain]]]]
[[機能][Feature Policy]]を[[利用することを認められている]]ので''ない''

]FIG]
... の''いずれか''の場合、
== [97] [CODE(DOMe)@en[SecurityError]] [CODE[DOMException]]
を[[投げ]]、ここで停止します。
= [39] [[文脈オブジェクト]]の[F[起源][文書の起源]]の[F[ドメイン]]を、
[VAR[新しい値]]に[[ホスト構文解析器]]を適用した結果に設定します。
]FIG]

;; [36] [[IPアドレス]]を指定できるのは[[IPアドレス]]の[[文書]]だけですし、
[[IPアドレス]]の[[文書]]で指定できるのは[[IPアドレス]]だけで、
しかも同じ[[IPアドレス]]しか指定できません。そうでなければ
[CODE(DOMe)@en[[[SecurityError]]]] になります。

;; [37] [CODE(JS)@en[[[document.domain]]]] を何度も新しい値に変えていく場合は、
どんどん短くしていかなければなりません。 (そしていつか変えれなくなります。)

[40] [[起源]]オブジェクトは、
複数の[[文書]]の[F[起源][文書の起源]]として共有されていることがあります。
[CODE(JS)@en[document.domain]] が変更されると、
その変更は同じ[[起源]]オブジェクトを共有する他の[[文書]]にも当然に伝播します。

* 起源の[F[ドメイン]]

[162] [[項組起源]]の[DFN[[F[ドメイン]]]] [SRC[>>78]] は、
[CODE(JS)@en[document.domain]] に関する値を表しています。

[79] その値は、 [CODE[null]] または[[ドメイン]]です。初期値は [CODE[null]] です。 [SRC[>>78]]

;; [80] [[項組起源]]も参照。

[82] [[起源]][VAR[起源]]の[DFN[[RUBYB[実効ドメイン]@en[effective domain]]]]は、
次のようにして得られる値です [SRC[>>78]]。

[FIG(steps)[
= [83] [VAR[起源]]が[[不透明起源]]の場合、
== [85] [CODE[null]] を返します。
= [84] それ以外の場合、
== [86] [VAR[起源]]の[F[ドメイン]]が [CODE[null]] なら、
=== [87] [VAR[起源]]の[F[ホスト]]を返します。
== [88] それ以外の場合、
=== [89] [VAR[起源]]の[F[ドメイン]]を返します。
]FIG]

;; [[ドメイン]]でなく、[[IPアドレス]]が返される場合もあります。

* 関連

[46] [[関連類似起源閲覧文脈の単位]]は、 [CODE(JS)@en[[[document.domain]]]]
の用法を考慮した[[閲覧文脈]]群の単位です。

* 歴史

;; [76] [[起源]]の歴史の項も参照。

[REFS[
- [5] [CITE[Origin feedback]] ([[Ian Hickson <ian@...>]] 著, [TIME[2008-04-29 03:08:06 +09:00]] 版) <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/13912>
- [2] [CITE['''['''whatwg''']''' Communicating between different-origin frames]]
([TIME[2010-08-11 09:23:43 +09:00]] 版)
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2010-August/027839.html>
- [3] [CITE[Document Structure – SVG 1.1 (Second Edition)]]
( ([TIME[2011-08-10 12:35:27 +09:00]] 版))
<http://www.w3.org/TR/2011/REC-SVG11-20110816/struct.html#__svg__SVGDocument__domain>
- [4] [CITE@en[Web Applications 1.0 r6789     Go back to ignoring document.domain changes for the purposes of canvas tainting.]]
( ([TIME[2011-10-31 03:11:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=6788&to=6789>
]REFS]

[1]
[CITE[Abe Fettig’s Weblog &#187; How to make XmlHttpRequest calls to another server in your domain]] <http://fettig.net/weblog/2005/11/28/how-to-make-xmlhttprequest-connections-to-another-server-in-your-domain/>

[6] [CITE@en[IE8 Security Part VI: Beta 2 Update - IEBlog - Site Home - MSDN Blogs]]
( ([TIME[2012-11-18 01:18:13 +09:00]] 版))
<http://blogs.msdn.com/b/ie/archive/2008/09/02/ie8-security-part-vi-beta-2-update.aspx>

[7] [CITE[Document Object Model for MathML]]
( ([TIME[2001-02-20 21:36:10 +09:00]] 版))
<http://www.w3.org/TR/2001/REC-MathML2-20010221/appendixd.html#dom_Document>

[8] [CITE[Document Object Model for MathML]]
( ([TIME[2002-05-23 15:35:03 +09:00]] 版))
<http://www.w3.org/Math/DOM/mathml2/appendixd.html#dom_Document>

[9] [CITE[IRC logs: freenode / #whatwg / 20130110]]
( ([TIME[2013-01-13 18:26:04 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20130110>

[10] [CITE@en[Web Applications 1.0 r8275     Make sandboxed iframes block document.domain setting]]
( ([TIME[2013-11-14 04:32:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=8274&to=8275>

[11] [CITE[IRC logs: freenode / #whatwg / 20140311]]
( ([TIME[2014-03-12 21:02:53 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20140311#l-447>

[12] [CITE@en[Web Applications 1.0 r8262 Move the spec from a stack of incumbent scripts to a stack of script settings object. This should in theory have no concrete effects (though I may have changed some of the origin used for Web Workers started from document.domain-affected scripts that were called from other scripts with different original origins).]]
( ([TIME[2013-11-09 08:21:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=8261&to=8262>

[13] [CITE[IRC logs: freenode / #whatwg / 20141018]]
( ([TIME[2014-10-19 02:05:35 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20141018>

[14] [CITE@en[340494 – access document.domain at about:plugins produces NS_ERROR_FAILURE. No error at about:config]]
( ([TIME[2014-12-10 21:37:26 +09:00]] 版))
<https://bugzilla.mozilla.org/show_bug.cgi?id=340494>

[FIG(quote)[
[FIGCAPTION[
[15] [CITE@ja[ブラウザ JavaScript]]
([TIME[2015-03-14 20:56:03 +09:00]] 版)
<http://www.opera.com/docs/browserjs/>
]FIGCAPTION]

> 最後の TLD には document.domain はセットできない

]FIG]


[16] [CITE[Part2 - browsersec - Browser Security Handbook, part 2 - Browser Security Handbook - Google Project Hosting]]
([TIME[2015-03-31 16:28:13 +09:00]] 版)
<https://code.google.com/p/browsersec/wiki/Part2#Same-origin_policy_for_DOM_access>

[49] [CITE@en[Re: CSP policy to constrain cookies to origin]]
([[Mike West]] 著, [TIME[2015-08-28 14:02:18 +09:00]] 版)
<https://lists.w3.org/Archives/Public/public-webappsec/2015Aug/0127.html>

[50] [CITE@en[Allow user agents to consider usage of `document.domain` non-secure. · w3c/webappsec-secure-contexts@1bd4ef2]]
([TIME[2015-12-12 10:46:00 +09:00]] 版)
<https://github.com/w3c/webappsec-secure-contexts/commit/1bd4ef2ce9acb0c4ddca283415501b42b2a768ca>

[51] [CITE@en[Remove the storage mutex due to lack of implementation · whatwg/html@1b918cf]] ([TIME[2015-12-16 14:38:10 +09:00]] 版) <https://github.com/whatwg/html/commit/1b918cf72fcbba011f83b92ab5d1f483fb1cafa3>

** 実効スクリプト起源

[45] 2016年3月改訂まで存在した[[実効スクリプト起源]]は、 [CODE(JS)@en[[[document.domain]]]]
に相当する仕様書上の概念です。
多くの場合は[[起源]]と同じ値ですが、 [CODE(JS)@en[[[document.domain]]]]
によって異なる値に設定されることがあります。

[71] [[スクリプト]]を介した色々な操作のアクセス制御が[[同一起源ポリシー]]によって行われます。
その時、
[[環境設定群オブジェクト]]の[DFN[[F[実効スクリプト起源]]]] [SRC[>>69]]
が参照されることがあります。

[HISTORY[
[70] これらは以前は「[[スクリプト]]の[[実効スクリプト起源]]」などと呼ばれていたこともありました。

;; [[環境設定群オブジェクト]]の歴史の項も参照。
]HISTORY]

[72] [[環境設定群オブジェクト]]の[F[実効スクリプト起源]]は、
その作成時に決まります。その後も [CODE(JS)@en[document.domain]] の変更の影響を受けます。

;; [[環境設定群オブジェクト]]参照。

[74] [[文書]]は[DFN[[F[実効スクリプト起源]]]]を持ちます [SRC[>>73]]。

[75] その値は、作成時に決まります。多くの場合は[[文書の[F[起源]]]]と同じです。
作成方法によっては、作成元の[F[実効スクリプト起源]]の値を引き継ぎます。
その後も [CODE(JS)@en[document.domain]] の変更の影響を受けます。

;; [CODE(DOMi)@en[Document]] の作成の項、[[navigate]] の項も参照。

** 手動上書き

[41] 「[DFN[[RUBYB[手動上書き]@en[manual override]]]]」
は、[[起源の比較]]において「手動上書き」と同じで、
それ以外のどの値とも同じでないような値です [SRC[>>17]]。

[92] [CODE(JS)@en[document.domain]] に新しい値を設定すると、
[[起源]]の[F[ポート]]が「手動上書き」に設定されることになっていました。

;; [42] ですから、 [CODE(JS)@en[document.domain = document.domain]] と同じ値を設定すると、
元の[[実効スクリプト起源]]とは異なる[[起源]]になりますから、元の[[実効スクリプト起源]]の別の[[文書]]にはアクセスできなくなります。

;; [43] 「手動上書き」値は[[直列化]]できませんが、
[[実効スクリプト起源]]が[[直列化]]されることは無いはずなので、問題ありません。

[93] [F[実効スクリプト起源]]と[F[起源][文書の起源]]の統合により、
この値を使わない形の規定となりました。

** ホストの定義の明確化

[66] 2016年2月には、[[ホスト]]の解釈が [[URL Standard]] の定義に統一される形で明確化されました。

[52] [CITE@en[Revamp the way document.domain is defined · whatwg/html@2a03759]]
([TIME[2016-02-14 16:25:14 +09:00]] 版)
<https://github.com/whatwg/html/commit/2a03759f2fbf4199d187c046074d574a9ae0e209>

[54] [CITE@en[Fix document.domain for IPv6 addresses to match browser behavior with respect to brackets · Issue #670 · whatwg/html]] ([TIME[2016-02-14 16:29:11 +09:00]] 版) <https://github.com/whatwg/html/issues/670>

>
[25] [[文書のドメイン]]が [[IPv6アドレス]]なら、その前後の [ と ]
は省略した中身の部分だけを初期値としなければ[['''なりません''']] [SRC[>>17]]。

[53] という規定が以前はありましたが、 [[Firefox]] のみそう実装しており、
[[Firefox]] も他のブラウザーに合わせて仕様変更したため [SRC[>>54]]、
2016年2月に仕様からもこの規定は削除されました [SRC[>>52]]。

[21] [[文書]]は、[F[[DFN[[RUBYB[ドメイン]@en[domain]]]]]]を持ちます [SRC[>>20]]。

[24] [[文書]][VAR[文書]]の[F[[[ドメイン]]]]は、初期状態で次のように設定しなければ[['''なりません''']]
[SRC[>>17]]。
[FIG(steps)[
= [26] [VAR[文書]]の[F[[[閲覧文脈]]]]がない場合、
== [27] [[空]]の[[ドメイン]]に設定します。
= [55] それ以外で、[[[VAR[文書]]の[F[起源]]]]が[[三項組]]の場合、
== [56] [[[VAR[文書]]の[F[起源]]]]の[F[[[ホスト]]]]に設定します。
= [57] それ以外の場合、
== [58] [[空]]の[[ドメイン]]に設定します。
]FIG]

** 実効スクリプト起源と起源の統合

[91] 2016年3月の改訂で[[実効スクリプト起源]]は廃止され、[[起源]]と統合されました。

[67] [CITE@en[Remove the section on document.domain · w3c/webappsec-secure-contexts@40985fc]]
([TIME[2016-03-11 15:40:51 +09:00]] 版)
<https://github.com/w3c/webappsec-secure-contexts/commit/40985fc5a7d81c08cacf86b04a7d041ea683c5f9>

[68] [CITE@en[Remove the origin aliasing concept · whatwg/html@438155d]]
([TIME[2016-03-23 23:28:49 +09:00]] 版)
<https://github.com/whatwg/html/commit/438155d2a2255aa5ea84ae390744d8a8662ebec2>

[77] [CITE@en[Cleanup origin serialization · whatwg/html@5dcc1ee]]
([TIME[2016-03-24 17:15:00 +09:00]] 版)
<https://github.com/whatwg/html/commit/5dcc1ee2124b5e54955845790bb47f5d1351d672>

[81] [CITE@en[Merge effective script origin into origin · whatwg/html@8a843f2]]
([TIME[2016-03-31 16:10:51 +09:00]] 版)
<https://github.com/whatwg/html/commit/8a843f2169a6864a3024c4329528dccb2051d275>

[62] [CITE@en[Use USVString for document.domain/referrer/cookie · whatwg/html@8170d82]]
([TIME[2016-04-05 14:00:49 +09:00]] 版)
<https://github.com/whatwg/html/commit/8170d82a2fc93c8ff7981c54aa4ccafd54204552>

[64] [CITE@en[Fix minor document.domain regression]]
([[annevk]]著, [TIME[2016-09-28 23:03:42 +09:00]])
<https://github.com/whatwg/html/commit/05cab5eca759b5ad6773189635d3e8bf4f9aab36>

[65] [CITE@en["Allowed to use" should allow same-origin without the attribute · Issue #2184 · whatwg/html]]
([TIME[2017-01-26 18:39:21 +09:00]])
<https://github.com/whatwg/html/issues/2184>

[107] [CITE@en[Define document.domain in terms of a reusable algorithm]]
([[jcjones]]著, [TIME[2017-02-23 00:09:01 +09:00]])
<https://github.com/whatwg/html/commit/03bbc6e92b6b8c9735b4565722916d49fe3eab61>

[108] [CITE@en[Define document.domain in terms of a reusable algorithm]]
([[jcjones]]著, [TIME[2017-02-23 00:09:01 +09:00]])
<https://github.com/whatwg/html/commit/03bbc6e92b6b8c9735b4565722916d49fe3eab61>

[31] [CITE@en[Re: Single Trust and Same-Origin Policy v2]]
([[Daniel Veditz]]著, [TIME[2017-03-28 05:49:28 +09:00]])
<https://lists.w3.org/Archives/Public/public-webappsec/2017Mar/0051.html>

[32] [CITE@en[Phasing out document.domain · Issue #829 · whatwg/html]]
([TIME[2017-05-08 19:50:13 +09:00]])
<https://github.com/whatwg/html/issues/829>

[35] [CITE@en[Remove the risk of `document.domain`. · Issue #10 · w3c/webappsec-secure-contexts]]
([TIME[2017-05-08 19:58:27 +09:00]])
<https://github.com/w3c/webappsec-secure-contexts/issues/10>

[60] [CITE@en[1329764 - Support relaxing the same origin policy for WebAuthn]]
([TIME[2017-07-11 12:11:25 +09:00]])
<https://bugzilla.mozilla.org/show_bug.cgi?id=1329764>

[61] [CITE@en[Make document.domain's getter not conditonal on a browsing context]]
([[annevk]]著, [TIME[2018-06-06 23:45:44 +09:00]])
<https://github.com/whatwg/html/commit/c0d2a3208376d315c4101d09c72997415d23d8bc>

[90] [CITE@en[investigate if HTMLDocument.domain setter shouldn't throw when document doesn't have browsing context · Issue #1794 · whatwg/html]]
([TIME[2018-06-12 15:56:33 +09:00]])
<https://github.com/whatwg/html/issues/1794>

[94] [CITE@en[document.domain getter without browsing context · Issue #2697 · whatwg/html]]
([TIME[2018-06-12 15:58:06 +09:00]])
<https://github.com/whatwg/html/issues/2697>

[95] [CITE@en[Make document.domain's getter not conditonal on a browsing context by annevk · Pull Request #3742 · whatwg/html]]
([TIME[2018-06-12 16:00:36 +09:00]])
<https://github.com/whatwg/html/pull/3742>

[96] [CITE@en[Define hosts' public suffix and registrable domain. by mikewest · Pull Request #391 · whatwg/url]]
([TIME[2018-06-13 22:13:45 +09:00]])
<https://github.com/whatwg/url/pull/391>

[101] [CITE@en[Build upon URL rather than PSL]]
([[annevk]]著, [TIME[2018-06-07 18:31:20 +09:00]])
<https://github.com/whatwg/html/commit/326c64415ddff5f619433028bd5d99408dad6430>

[102] [CITE@en["If the given value is not a registrable domain ..." · Issue #3711 · whatwg/html]]
([TIME[2018-06-13 22:38:31 +09:00]])
<https://github.com/whatwg/html/issues/3711>

[103] [CITE@en["If the given value is not a registrable domain ..." · Issue #3711 · whatwg/html]]
([TIME[2018-06-13 22:46:46 +09:00]])
<https://github.com/whatwg/html/issues/3711>

[104] [CITE@en[Build upon URL rather than PSL by annevk · Pull Request #3735 · whatwg/html]]
([TIME[2018-06-13 22:47:33 +09:00]])
<https://github.com/whatwg/html/pull/3735>

[105] [CITE@en[Integrate Feature Policy concepts into HTML]]
([[clelland]]著, [TIME[2018-06-23 05:22:11 +09:00]])
<https://github.com/whatwg/html/commit/78741b7b5471ba213c68a22caff1a53c56d35ce8>

[106] [CITE@en[Tie document.domain to a new policy-controlled feature]]
([[mikewest]]著, [TIME[2018-11-14 09:27:07 +09:00]])
<https://github.com/whatwg/html/commit/33b566f8b590afd3531e3100827b1921eb636109>

[110] [CITE@en[Proposal: Control over `document.domain` · Issue #241 · w3c/webappsec-feature-policy]]
([TIME[2019-04-15 19:55:16 +09:00]])
<https://github.com/w3c/webappsec-feature-policy/issues/241>

[111] [CITE@en[Don't forbid document.domain getter · Issue #23 · w3c/webappsec-feature-policy]]
([TIME[2019-04-15 19:56:48 +09:00]])
<https://github.com/w3c/webappsec-feature-policy/issues/23>

[112] [CITE@en[277084 - Disallow setting document.domain in sandboxed iframes - chromium - Monorail]]
([TIME[2019-04-15 19:59:43 +09:00]])
<https://bugs.chromium.org/p/chromium/issues/detail?id=277084>

[113] [CITE@en[907892 - Disallow setting document.domain in sandboxed iframes]]
([TIME[2019-04-15 19:59:59 +09:00]])
<https://bugzilla.mozilla.org/show_bug.cgi?id=907892>

[114] [CITE@en[Tie 'document.domain' to a new policy-controlled feature. by mikewest · Pull Request #4170 · whatwg/html]]
([TIME[2019-04-15 20:00:22 +09:00]])
<https://github.com/whatwg/html/pull/4170>