|
| 1 | +// META: script=/common/get-host-info.sub.js |
| 2 | + |
| 3 | +const embedPath = new URL("resources/ancestororigins-embed.py", location.href).pathname, |
| 4 | + info = get_host_info(), |
| 5 | + localOrigin = info.HTTP_ORIGIN, |
| 6 | + localEmbed = localOrigin + embedPath, |
| 7 | + remoteOrigin = info.HTTP_REMOTE_ORIGIN, |
| 8 | + remoteEmbed = remoteOrigin + embedPath, |
| 9 | + remoteOrigin2 = info.HTTP_REMOTE_ORIGIN_WITH_DIFFERENT_PORT |
| 10 | + remoteEmbed2 = remoteOrigin2 + embedPath |
| 11 | +let id = 0 |
| 12 | + |
| 13 | +async_test(t => { |
| 14 | + const iframe = document.createElement("iframe"), |
| 15 | + localId = ++id |
| 16 | + iframe.sandbox = "allow-scripts" |
| 17 | + iframe.src = localEmbed + "?iframe=|" + remoteEmbed + "?id=" + localId |
| 18 | + document.body.appendChild(iframe) |
| 19 | + t.add_cleanup(() => iframe.remove()) |
| 20 | + |
| 21 | + |
| 22 | + self.addEventListener("message", t.step_func(e => { |
| 23 | + if(e.data.id === localId) { |
| 24 | + assert_array_equals(e.data.output, ["null", localOrigin]) |
| 25 | + t.done() |
| 26 | + } |
| 27 | + })) |
| 28 | +}, "Ensure sandboxed iframes show up as null") |
| 29 | + |
| 30 | +// The following code ends up generating multiple tests each with multiple nested |
| 31 | +// variables in the array below seed various scenarios, described by "desc". The capital letters |
| 32 | +// describe the |
| 33 | +// means it is same-origin. Some defaulting is used to avoid too much duplication. |
| 34 | +;[ |
| 35 | + { |
| 36 | + outerPolicy: true, |
| 37 | + desc: "A uses no-referrer -> B -> A", |
| 38 | + results: [remoteOrigin, "null"] |
| 39 | + }, |
| 40 | + { |
| 41 | + outerPolicy: true, |
| 42 | + innerEmbed: remoteEmbed2, |
| 43 | + desc: "A uses no-referrer -> B -> C", |
| 44 | + results: [remoteOrigin, "null"] |
| 45 | + }, |
| 46 | + { |
| 47 | + innerPolicy: true, |
| 48 | + desc: "A -> B uses no-referrer -> A", |
| 49 | + results: ["null", localOrigin] |
| 50 | + }, |
| 51 | + { |
| 52 | + innerPolicy: true, |
| 53 | + innerEmbed: remoteEmbed2, |
| 54 | + desc: "A -> B uses no-referrer -> C", |
| 55 | + results: ["null", localOrigin] |
| 56 | + }, |
| 57 | + { |
| 58 | + innerPolicy: true, |
| 59 | + outerEmbed: localEmbed, |
| 60 | + innerEmbed: remoteEmbed, |
| 61 | + desc: "A -> A uses no-referrer -> C", |
| 62 | + results: ["null", "null"], |
| 63 | + intermediateResults: [localOrigin] |
| 64 | + }, |
| 65 | + { |
| 66 | + innerPolicy: true, |
| 67 | + outerEmbed: localEmbed, |
| 68 | + desc: "A -> A uses no-referrer -> A", |
| 69 | + results: ["null", "null"], |
| 70 | + intermediateResults: [localOrigin] |
| 71 | + } |
| 72 | +].forEach(val => { |
| 73 | + async_test(t => { |
| 74 | + if(!val.intermediateResults) { |
| 75 | + val.intermediateResults = [val.results[1]] |
| 76 | + } |
| 77 | + if(!val.outerEmbed) { |
| 78 | + val.outerEmbed = remoteEmbed |
| 79 | + } |
| 80 | + if(!val.innerEmbed) { |
| 81 | + val.innerEmbed = localEmbed |
| 82 | + } |
| 83 | + |
| 84 | + const iframe = document.createElement("iframe"), |
| 85 | + innerId = ++id, |
| 86 | + innermostId = ++id |
| 87 | + if(val.outerPolicy) { |
| 88 | + iframe.referrerPolicy = "no-referrer" |
| 89 | + } |
| 90 | + let innerPolicy = "" |
| 91 | + if(val.innerPolicy) { |
| 92 | + innerPolicy = "no-referrer" |
| 93 | + } |
| 94 | + iframe.src = val.outerEmbed + "?id=" + innerId + "&iframe=" + innerPolicy + "|" + val.innerEmbed + "?id=" + innermostId |
| 95 | + document.body.appendChild(iframe) |
| 96 | + t.add_cleanup(() => iframe.remove()) |
| 97 | + |
| 98 | + let almostDone = false |
| 99 | + function localDone () { |
| 100 | + if(almostDone) { |
| 101 | + t.done() |
| 102 | + } |
| 103 | + almostDone = true |
| 104 | + } |
| 105 | + |
| 106 | + self.addEventListener("message", t.step_func(e => { |
| 107 | + if(e.data.id === innerId) { |
| 108 | + assert_array_equals(e.data.output, val.intermediateResults) |
| 109 | + localDone() |
| 110 | + } |
| 111 | + else if(e.data.id === innermostId) { |
| 112 | + assert_array_equals(e.data.output, val.results) |
| 113 | + localDone() |
| 114 | + } |
| 115 | + })) |
| 116 | + }, val.desc) |
| 117 | +}) |
| 118 | + |
| 119 | +;[ |
| 120 | + { |
| 121 | + desc: "A -> B -> B uses no-referrer -> A", |
| 122 | + innerinnerEmbed: localEmbed |
| 123 | + }, |
| 124 | + { |
| 125 | + desc: "A -> B -> B uses no-referrer -> B", |
| 126 | + innerinnerEmbed: remoteEmbed |
| 127 | + }, |
| 128 | + { |
| 129 | + desc: "A -> B -> B uses no-referrer -> C", |
| 130 | + innerinnerEmbed: remoteEmbed2 |
| 131 | + } |
| 132 | +].forEach(val => { |
| 133 | + async_test(t => { |
| 134 | + const iframe = document.createElement("iframe"), |
| 135 | + localId = ++id |
| 136 | + iframe.src = remoteEmbed + "?iframe=|" + remoteEmbed + "?iframe=no-referrer%257C" + val.innerinnerEmbed + "?id=" + localId |
| 137 | + document.body.appendChild(iframe) |
| 138 | + t.add_cleanup(() => iframe.remove()) |
| 139 | + |
| 140 | + self.addEventListener("message", t.step_func(e => { |
| 141 | + if(e.data.id === localId) { |
| 142 | + assert_array_equals(e.data.output, ["null", "null", localOrigin]) |
| 143 | + t.done() |
| 144 | + } |
| 145 | + })) |
| 146 | + }, val.desc) |
| 147 | +}) |
| 148 | + |
| 149 | +async_test(t => { |
| 150 | + const iframe = document.createElement("iframe"), |
| 151 | + localId = ++id |
| 152 | + document.body.appendChild(iframe) |
| 153 | + t.add_cleanup(() => iframe.remove()) |
| 154 | + |
| 155 | + const a = document.createElement("a") |
| 156 | + a.rel = "noreferrer" |
| 157 | + a.href = localEmbed + "?id=" + localId |
| 158 | + iframe.contentDocument.body.appendChild(a) |
| 159 | + a.click() |
| 160 | + |
| 161 | + self.addEventListener("message", t.step_func(e => { |
| 162 | + if(e.data.id === localId) { |
| 163 | + assert_array_equals(e.data.output, ["null"]) |
| 164 | + t.done() |
| 165 | + } |
| 166 | + })) |
| 167 | +}, "rel=noreferrer should redact") |
0 commit comments