Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 1 | # `document.domain` Setting is Deprecated |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 2 | |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 3 | Setting [`document.domain`](https://developer.mozilla.org/en-US/docs/Web/API/Document/domain) |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 4 | is [deprecated](https://html.spec.whatwg.org/multipage/origin.html#relaxing-the-same-origin-restriction). |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 5 | `document.domain` setting can be used to relax the same-origin restrictions |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 6 | between different frames, hosted within the same site but on different origins. |
| 7 | Doing so make themselves effectively same-origin for the purpose of |
| 8 | synchronous access. |
| 9 | |
| 10 | For example, `https.//www.example.test/` might host a media player on |
| 11 | `media.example.org`. If both the main page and the frame execute |
| 12 | `document.domain = "example.org";` they may then access each others' DOM |
| 13 | tree, which they normally couldn't. (A better way would be to cooperate |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 14 | by `postMessage()`.) |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 15 | |
| 16 | This usage is now being deprecated. (More information can be found |
| 17 | [here](https://developer.chrome.com/blog/immutable-document-domain/) and |
| 18 | [there](https://github.com/mikewest/deprecating-document-domain)). |
| 19 | |
| 20 | ## What is happening, and when? |
| 21 | |
| 22 | * M100: Chrome will show deprecation warnings when document.domain is set. |
| 23 | * M106: Chrome will disable document.domain by default. |
| 24 | |
| 25 | Note that the second milestone is tentative: When the time comes, we will |
| 26 | examine how many pages will be impacted by this change, and will start a |
| 27 | separate discussion (intent to remove) on the |
| 28 | [blink-dev mailing list](https://groups.google.com/a/chromium.org/g/blink-dev). |
| 29 | |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 30 | ## `document.domain` and Origin-keyed Agent Clusters |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 31 | |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 32 | Most documentation on this change is phrased in terms of origin-keyed |
| 33 | agent clusters. This is [a concept in the HTML |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 34 | specification](https://html.spec.whatwg.org/multipage/origin.html#origin-keyed-agent-clusters). |
| 35 | Here we focus on the behaviour of the `document.domain` setter, which is |
| 36 | the visible effect. |
| 37 | |
| 38 | A web browser can cluster sites (in order to assign them to operating |
| 39 | system processes) and sites can be clustered by origin, or by site. |
| 40 | Origin-keyed agent clustering is preferable for security reasons. However |
| 41 | when sites are clustered by origin, synchronous access to frames outside of |
| 42 | that origin (but within the same site) is no longer possible. Thus sites in |
| 43 | origin-keyed agent clusters disable the `document.domain` setter. This is the |
| 44 | mechanism underlying this change: From M106 on pages will be assigned to |
| 45 | origin-keyed agent clusters by default and therefore `document.domain` |
| 46 | will no longer be settable by default, either. |
| 47 | |
| 48 | This also gives us an opt-out mechanism for pages who do not wish to follow |
| 49 | this change: By setting the `Origin-Agent-Cluster: ?0` http header, a site |
| 50 | can request assignment to a site-keyed agent cluster, and `document.domain` |
| 51 | will continue to work for them as it currently does. Note that adding this |
| 52 | header has no other observable effect and thus retains the current |
| 53 | (pre-M106) behaviour. This makes it an easy and _safe_ way to opt-out. |
| 54 | |
| 55 | Setting this header is a no-op in current versions of Chromium since it |
| 56 | matches the default setting, and will preserve this behaviour in the future. |
| 57 | It is also a no-op in other browsers, since they either match Chromium's |
| 58 | current default or have not implemented the `Origin-Agent-Cluster` header at |
| 59 | all. |
| 60 | |
Daniel Vogelheim | 97651350d | 2022-03-18 10:46:12 | [diff] [blame] | 61 | ## Where are the deprecation warnings found? |
| 62 | |
| 63 | The deprecation warnings are found in the [issues tab](https://developer.chrome.com/docs/devtools/issues/). |
| 64 | |
| 65 | ## What does the deprecation warning tell me? |
| 66 | |
| 67 | There are two deprecation warnings: One for setting the `document.domain` |
| 68 | accessors, which modifies the security behaviour. And from M101 on, |
| 69 | a second warning when a cross-domain access is made that is facilitated by |
| 70 | the modified `document.domain` property. The first warning tells you where |
| 71 | the setup happens, and the second one tells you where it is being used (and |
| 72 | thus likely why this is being done in the first place). |
| 73 | |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 74 | ## How Can I Test This? |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 75 | |
| 76 | In the DevTools console, for a page `www.example.test`: |
| 77 | |
| 78 | ``` |
| 79 | document.domain = "example.test"; |
| 80 | document.domain; // "example.test" in a site-keyed agent cluster. |
| 81 | // "www.example.test" in an origin-keyed agent cluster. |
| 82 | ``` |
| 83 | |
| 84 | One can also directly query whether a page is assigned to an origin-keyed |
| 85 | agent cluster, by querying `window.originAgentCluster`. |
| 86 | |
| 87 | ``` |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 88 | window.originAgentCluster; // true, if page is assigned to an origin-keyed |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 89 | // agent cluster. |
| 90 | ``` |
| 91 | |
| 92 | How to enable/disable the deprecation: |
| 93 | |
Daniel Vogelheim | 97651350d | 2022-03-18 10:46:12 | [diff] [blame] | 94 | ### Enable the Warning (Before M100) |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 95 | |
| 96 | * Start Chrome with `--enable-features=OriginAgentClusterDefaultWarning` |
| 97 | |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 98 | ### Enable the Deprecation (Scheduled for M106) |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 99 | |
| 100 | * In [chrome://flags](chrome://flags#origin-agent-cluster-default), go to |
| 101 | the "Origin Agent Cluster Default" setting and enable it. |
| 102 | * Or start Chrome with `--enable-features=OriginAgentClusterDefaultEnable` |
| 103 | * Or add the `Origin-Agent-Cluster: ?1` header to your pages and frames. |
| 104 | (E.g. in a testing instance of your site.) |
| 105 | |
| 106 | ### Testing at Scale / Reporting API |
| 107 | |
| 108 | The deprecation warnings are delivered through the |
| 109 | [Reporting API](https://web.dev/reporting-api/). They can |
| 110 | be pragrammatically processed using `ReportingObserver`. For example, the |
| 111 | first code snippet in |
| 112 | https://developers.google.com/web/updates/2018/07/reportingobserver |
| 113 | will report these warnings. The message object delivered by this API is a |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 114 | [`DeprecationReportBody`](https://developer.mozilla.org/en-US/docs/Web/API/DeprecationReportBody) |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 115 | instance which offers information about the source code location that triggered |
| 116 | the warning. |
| 117 | |
| 118 | ## What can I do? |
| 119 | |
| 120 | If your site does not use `document.domain` setting you don't have to do |
| 121 | anything. You could explicitly set the `Origin-Agent-Cluster: ?1` header. |
| 122 | But after M106 this would be the default behaviour anyhow. |
| 123 | |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 124 | If your site uses `document.domain` setting to enable cross-origin Javascript |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 125 | access, you should refactor the code to instead use |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 126 | [`window.postMessage()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) (or any other mechanism) to cooperate across origins. Or alternatively |
| 127 | reduce the need for cross-origin cooperation by moving the cooperating pieces |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 128 | onto the same origin. |
| 129 | |
| 130 | ## What if I don't have the time right now, or want to continue setting `document.domain`? |
| 131 | |
Domenic Denicola | cae2b5a | 2022-02-07 16:49:49 | [diff] [blame] | 132 | You can add the `Origin-Agent-Cluster: ?0` HTTP header to your site. Note that |
Daniel Vogelheim | 2c609dc | 2022-02-04 18:14:25 | [diff] [blame] | 133 | the header must be set both for the main page, as well as for the embedded |
| 134 | frame(s) that wish to use `document.domain` setting. |
| 135 | |
| 136 | ## Enterprise Users |
| 137 | |
| 138 | Users of [Chrome for Enterprise](https://chromeenterprise.google/) can set |
| 139 | the `OriginAgentClusterDefaultEnabled` policy to `False` to retain the |
| 140 | current (pre-M106) default for all of their users, until all their internal |
| 141 | sites and customers have migrated off of `document.domain` usage. |
| 142 | |