|
| 1 | +<!DOCTYPE html> |
| 2 | +<title> |
| 3 | + Location#ancestorOrigins test |
| 4 | +</title> |
| 5 | +<script src="/resources/testharness.js"></script> |
| 6 | +<script src="/resources/testharnessreport.js"></script> |
| 7 | +<script src="/resources/testdriver.js"></script> |
| 8 | +<script src="/resources/testdriver-vendor.js"></script> |
| 9 | +<script src="./resources/create-iframe.js"></script> |
| 10 | + |
| 11 | +<body> |
| 12 | + <script> |
| 13 | + function waitFor(action, frameName) { |
| 14 | + return new Promise((resolve) => { |
| 15 | + window.addEventListener("message", function listener(e) { |
| 16 | + if (e.data.event === action && e.data.name === frameName) { |
| 17 | + window.removeEventListener("message", listener); |
| 18 | + resolve(event.data); |
| 19 | + } |
| 20 | + }); |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + const frameCreatorPath = "html/browsers/history/the-location-interface/resources/recursive-iframe.html" |
| 25 | + |
| 26 | + // first src shares origin with self.origin here |
| 27 | + let iframeSources = [ |
| 28 | + { |
| 29 | + src: `http://{{hosts[][]}}:{{ports[http][0]}}/${frameCreatorPath}?a`, |
| 30 | + }, |
| 31 | + { |
| 32 | + src: `http://{{hosts[alt][]}}:{{ports[http][0]}}/${frameCreatorPath}?b`, |
| 33 | + }, |
| 34 | + { |
| 35 | + src: `http://{{hosts[][www]}}:{{ports[http][0]}}/${frameCreatorPath}?c`, |
| 36 | + }, |
| 37 | + ]; |
| 38 | + |
| 39 | + // origins A, B, C |
| 40 | + iframeSources.forEach(f => { |
| 41 | + const url = new URL(f.src, location.origin); |
| 42 | + f.origin = url.origin; |
| 43 | + }) |
| 44 | + |
| 45 | + const A = iframeSources[0] |
| 46 | + const B = iframeSources[1]; |
| 47 | + const C = iframeSources[2]; |
| 48 | + |
| 49 | + const policy = { |
| 50 | + Default: "", |
| 51 | + NoRef: "no-referrer", |
| 52 | + Same: "same-origin" |
| 53 | + } |
| 54 | + |
| 55 | + const config = (name, f, referrerPolicy, sandbox = false) => ({ name: name, src: f.src, referrerPolicy: referrerPolicy, sandbox: sandbox }) |
| 56 | + |
| 57 | + // Remember the implicit top-level origin A |
| 58 | + const testCases = [ |
| 59 | + { // test no-referrer on iframe |
| 60 | + frames: [ |
| 61 | + config("B1", B, policy.NoRef) |
| 62 | + ], |
| 63 | + expected: [ |
| 64 | + ["null"] |
| 65 | + ] |
| 66 | + }, |
| 67 | + { // test all default referrer policy |
| 68 | + frames: [ |
| 69 | + config("B1", B, policy.Default), |
| 70 | + config("A2", A, policy.Default) |
| 71 | + ], |
| 72 | + expected: [ |
| 73 | + [A.origin], |
| 74 | + [B.origin, A.origin] |
| 75 | + ] |
| 76 | + }, |
| 77 | + { // test toggle of masked |
| 78 | + frames: [ |
| 79 | + config("B1", B, policy.Default), |
| 80 | + config("A2", A, policy.NoRef) |
| 81 | + ], |
| 82 | + expected: [ |
| 83 | + [A.origin], |
| 84 | + ["null", A.origin] |
| 85 | + ] |
| 86 | + }, |
| 87 | + { // another test of toggle of masked |
| 88 | + frames: [ |
| 89 | + config("B1", B, policy.Default), |
| 90 | + config("B2", B, policy.Default), |
| 91 | + config("A2", A, policy.NoRef) |
| 92 | + ], |
| 93 | + expected: [ |
| 94 | + [A.origin], |
| 95 | + [B.origin, A.origin], |
| 96 | + ["null", "null", A.origin] |
| 97 | + ] |
| 98 | + }, |
| 99 | + { // test sandboxed/opaque origin frames |
| 100 | + frames: [ |
| 101 | + config("C1", C, policy.Default, /* sandbox */ true), |
| 102 | + config("B2", B, policy.Default), |
| 103 | + ], |
| 104 | + expected: [ |
| 105 | + [A.origin], |
| 106 | + ["null", A.origin], |
| 107 | + ] |
| 108 | + }, |
| 109 | + { // 5 - Test same-origin referrer policy |
| 110 | + frames: [ |
| 111 | + config("B1", B, policy.Default), |
| 112 | + config("A2", A, policy.Default), |
| 113 | + config("B2", B, policy.Same) |
| 114 | + ], |
| 115 | + expected: [ |
| 116 | + [A.origin], |
| 117 | + [B.origin, A.origin], |
| 118 | + ["null", B.origin, A.origin] |
| 119 | + ] |
| 120 | + } |
| 121 | + ] |
| 122 | + |
| 123 | + testCases.forEach((cfg, index) => { |
| 124 | + promise_test(async (t) => { |
| 125 | + const iframeDetails = cfg.frames[0]; |
| 126 | + |
| 127 | + const topFrameAncestorOrigins = waitFor("sentOrigins", iframeDetails.name); |
| 128 | + const iframe = document.createElement("iframe"); |
| 129 | + await configAndNavigateIFrame(iframe, iframeDetails); |
| 130 | + const res = await topFrameAncestorOrigins; |
| 131 | + |
| 132 | + t.add_cleanup(() => { |
| 133 | + iframe.remove(); |
| 134 | + }); |
| 135 | + |
| 136 | + let results = [res]; |
| 137 | + |
| 138 | + for(let i = 1; i < cfg.frames.length; ++i) { |
| 139 | + // name of frame, that shall create a new frame |
| 140 | + const parentName = cfg.frames[i - 1].name; |
| 141 | + iframe.contentWindow.postMessage({ action: "addIframe", request: cfg.frames[i], name: parentName }, "*"); |
| 142 | + const res = await waitFor("sentOrigins", cfg.frames[i].name); |
| 143 | + results.push(res); |
| 144 | + } |
| 145 | + |
| 146 | + let i = 0; |
| 147 | + for(const { event, name, origins } of results) { |
| 148 | + assert_equals(origins.length, cfg.expected[i].length, `[test configuration ${index}, frame ${name}]: Unexpected length of ancestorOrigins list`); |
| 149 | + |
| 150 | + for(let j = 0; j < origins.length; ++j) { |
| 151 | + assert_equals(origins[j], cfg.expected[i][j], `[test configuration ${index}, frame ${name}]: Unexpected origin`) |
| 152 | + } |
| 153 | + i+=1; |
| 154 | + } |
| 155 | + }, `Location.ancestorOrigins configuration ${index}`); |
| 156 | + |
| 157 | + }) |
| 158 | + </script> |
| 159 | +</body> |
0 commit comments