| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 
 | <!doctype html>
<html>
<head>
  <title>Test content script at data:-URLs</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
add_setup(async () => {
  await SpecialPowers.pushPrefEnv({
    set: [["security.data_uri.block_toplevel_data_uri_navigations", false]],
  });
});
add_task(async function data_url_via_match_about_blank_true() {
  const manifest = {
    content_scripts: [
      {
        // match_about_blank is documented to match the parent URL for
        // about:blank / about:srcdoc, which inherit the parent principal.
        // data:-URLs used to inherit the principal, until bug 1324406 changed
        // this. For comprehensive test coverage, verify that we don't match
        // the parent URL with match_about_blank and data:-URLs.
        match_about_blank: true,
        // This HTML file is the test itself.
        matches: ["*://*/*/test_ext_contentscript_data_url.html"],
        all_frames: true,
        js: ["match_about_blank:matches.js"],
        run_at: "document_start",
      }, {
        // match_about_blank may match top-level about:blank documents with a
        // null principal, as a special case. This same logic was expanded to
        // top-level data:-URLs in bug 1451463 (undocumented!).
        match_about_blank: true,
        matches: ["*://*/*"],
        js: ["match_about_blank:wildcard.js"],
        all_frames: true,
        run_at: "document_start",
      }, {
        // Sanity check: without matching matches, this should never match.
        match_about_blank: true,
        matches: ["*://*/never_matched"],
        all_frames: true,
        js: ["never_matched.js"],
        run_at: "document_start",
      },
    ],
  };
  const files = {
    "match_about_blank:matches.js": () => {
      if (location.protocol === "data:") {
        browser.test.sendMessage("match_about_blank:matches", location.href);
      }
    },
    "match_about_blank:wildcard.js": () => {
      if (location.protocol === "data:") {
        browser.test.sendMessage("match_about_blank:wildcard", location.href);
      }
    },
    "never_matched.js": () => {
      browser.test.fail(`Unexpected match at: ${location.href}`);
    },
  };
  const extension = ExtensionTestUtils.loadExtension({ manifest, files });
  await extension.startup();
  const DATA_URL_FRAME = "data:,iframe_data";
  const DATA_URL = `data:text/html,<iframe src="${DATA_URL_FRAME}"></iframe>`;
  let win = window.open(DATA_URL);
  is(
    await extension.awaitMessage("match_about_blank:wildcard"),
    DATA_URL,
    "match_about_blank with wildcard matches data:-URL popup"
  );
  // Notably: match_about_blank:wildcard for DATA_URL_FRAME is not sent, because
  // data:-URLs are not really supported with match_about_blank. Undocumented
  // support for top-level data:-URLs was introduced in bug 1451463.
  win.close();
  await extension.unload();
});
add_task(async function data_url_via_match_origin_as_fallback_true() {
  const manifest = {
    content_scripts: [
      {
        match_origin_as_fallback: true,
        // mochi.test is where the test is running from.
        matches: ["*://mochi.test/*"],
        all_frames: true,
        js: ["matched_fallback:host.js"],
        run_at: "document_start",
      }, {
        // When a data:-URL has a precursor, such as when it is opened through
        // a http(s) as done in this test task, then matching is based on the
        // origin. include_globs matches the origin.
        match_origin_as_fallback: true,
        matches: ["*://mochi.test/*"],
        include_globs: ["http*"], // matches http://mochi.test:8888
        all_frames: true,
        js: ["matched_fallback:glob.js"],
        run_at: "document_start",
      }, {
        // Sanity check: without matching matches, this should never match.
        match_origin_as_fallback: true,
        matches: ["*://*/never_matched"],
        all_frames: true,
        js: ["never_matched.js"],
        run_at: "document_start",
      },
    ],
  };
  const files = {
    "matched_fallback:host.js": () => {
      if (location.protocol === "data:") {
        browser.test.sendMessage("matched_fallback:host", location.href);
      }
    },
    "matched_fallback:glob.js": () => {
      if (location.protocol === "data:") {
        browser.test.sendMessage("matched_fallback:glob", location.href);
      }
    },
    "never_matched.js": () => {
      browser.test.fail(`Unexpected match at: ${location.href}`);
    },
  };
  const extension = ExtensionTestUtils.loadExtension({ manifest, files });
  await extension.startup();
  const DATA_URL_FRAME = "data:,iframe_data";
  const DATA_URL = `data:text/html,<iframe src="${DATA_URL_FRAME}"></iframe>`;
  let win = window.open(DATA_URL);
  is(
    await extension.awaitMessage("matched_fallback:host"),
    DATA_URL,
    "match_origin_as_fallback matches data:-URL popup via matches"
  );
  is(
    await extension.awaitMessage("matched_fallback:glob"),
    DATA_URL,
    "match_origin_as_fallback matches data:-URL popup via glob"
  );
  is(
    await extension.awaitMessage("matched_fallback:host"),
    DATA_URL_FRAME,
    "match_origin_as_fallback matches frame in data:-URL popup via matches"
  );
  is(
    await extension.awaitMessage("matched_fallback:glob"),
    DATA_URL_FRAME,
    "match_origin_as_fallback matches frame in data:-URL popup via glob"
  );
  win.close();
  await extension.unload();
});
add_task(async function top_level_data_url_without_precursor() {
  const manifest = {
    content_scripts: [
      {
        // This is special compared to *://*/*: The data:-schema is part of
        // <all_urls>, so even if the origin were to somehow not match anything,
        // then <all_urls> + match_origin_as_fallback could potentially match
        // data:-URLs without precursor principal, such as when the user
        // navigates to a data:-URL from the address bar.
        match_origin_as_fallback: true,
        matches: ["<all_urls>"],
        include_globs: ["data:,iframe_data"],
        all_frames: true,
        js: ["matched_fallback:glob_iframe.js"],
        run_at: "document_start",
      }, {
        // There is a special case similar to about:blank which is also tested
        // by top_level_about_blank in test_ext_contentscript_about_blank.html:
        // When the top-level document is opaque, and 'matches' matches any URL,
        // then data:-URLs are injected (even though there is no precursor to
        // match).
        // Note: this does not match the iframe, because the "origin" to be used
        // for matching is the data:-URL of the iframe, and "*://*/*" does not
        // match data:-URLs, unlike <all_urls>.
        match_origin_as_fallback: true,
        matches: ["*://*/*"],
        all_frames: true,
        js: ["matched_fallback:wildcard.js"],
        run_at: "document_start",
      }, {
        // Sanity check: include_globs can also reject a match.
        match_origin_as_fallback: true,
        matches: ["<all_urls>"],
        include_globs: ["data:,not-matching"],
        all_frames: true,
        js: ["never_matched.js"],
        run_at: "document_start",
      }, {
        // Not expected to match: fall through to data:-URL matching only
        // happens when match_origin_as_fallback is true, not when
        // match_about_blank is true.
        match_about_blank: true,
        matches: ["<all_urls>"],
        include_globs: ["data:*"],
        all_frames: true,
        js: ["never_matched2.js"],
        run_at: "document_start",
      },
    ],
  };
  const files = {
    "matched_fallback:wildcard.js": () => {
      if (location.protocol === "data:") {
        browser.test.sendMessage("matched_fallback:wildcard", location.href);
      }
    },
    "matched_fallback:glob_iframe.js": () => {
      if (location.protocol === "data:") {
        browser.test.sendMessage("matched_fallback:glob_iframe", location.href);
      }
    },
    "never_matched.js": () => {
      browser.test.fail(`Unexpected match at: ${location.href}`);
    },
    "never_matched2.js": () => {
      browser.test.fail(`Unexpected match 2 at: ${location.href}`);
    },
  };
  const extension = ExtensionTestUtils.loadExtension({ manifest, files });
  await extension.startup();
  const DATA_URL_FRAME = "data:,iframe_data";
  const DATA_URL = `data:text/html,<iframe src="${DATA_URL_FRAME}"></iframe>`;
  let tab = await AppTestDelegate.openNewForegroundTab(window, DATA_URL, true);
  is(
    await extension.awaitMessage("matched_fallback:wildcard"),
    DATA_URL,
    "match_origin_as_fallback matches data:-URL popup without http(s) origin"
  );
  is(
    await extension.awaitMessage("matched_fallback:glob_iframe"),
    DATA_URL_FRAME,
    "match_origin_as_fallback matches data:-URL frame by data:-glob pattern"
  );
  await AppTestDelegate.removeTab(window, tab);
  await extension.unload();
});
</script>
</body>
</html>
 |