Commit bc8890e4 authored by Adithya Srinivasan's avatar Adithya Srinivasan Committed by Commit Bot

Portals: Move portal-host-exposure test off postMessage

In preparation for restricting cross-origin portal post messaging, this
CL changes portal-host-exposure.sub.html to use Stash to communicate
from a cross-origin portal to its embedder, instead of using
postMessage.

Bug: 1108793
Change-Id: Ib3dbb7cc56277cf28ac7a12ccf369a7ea9ce9f05
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2377323
Commit-Queue: Adithya Srinivasan <adithyas@chromium.org>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarRobert Ma <robertma@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802625}
parent 1a4026f8
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/stash-utils.sub.js"></script>
<script src="/common/utils.js"></script>
<body>
<script>
function openPortalAndReceiveMessage(portalSrc) {
function openPortal(portalSrc) {
assert_implements("HTMLPortalElement" in self);
let portal = document.createElement('portal');
const portal = document.createElement('portal');
portal.src = portalSrc;
let received = new Promise((resolve, reject) => {
portal.onmessage = resolve;
document.body.appendChild(portal);
});
return received;
return portal;
}
promise_test(() => {
return openPortalAndReceiveMessage("resources/portal-host.html");
async function openPortalAndReceiveMessage(portalSrc) {
const key = token();
const portal = openPortal(`${portalSrc}?key=${key}`);
document.body.appendChild(portal);
return StashUtils.takeValue(key);
}
promise_test(async () => {
const result = await openPortalAndReceiveMessage("resources/portal-host.html");
assert_equals(result, "passed");
}, "window.portalHost should be exposed in same-origin portal");
promise_test(() => {
return openPortalAndReceiveMessage(
promise_test(async () => {
const result = await openPortalAndReceiveMessage(
"http://{{hosts[alt][www]}}:{{ports[http][0]}}/portals/resources/portal-host.html");
assert_equals(result, "passed");
}, "window.portalHost should be exposed in cross-origin portal");
promise_test(() => {
return openPortalAndReceiveMessage(
promise_test(async () => {
const result = await openPortalAndReceiveMessage(
'resources/portal-host-cross-origin-navigate.sub.html');
assert_equals(result, "passed");
}, "window.portalHost should be exposed in portal after cross-origin navigation");
</script>
......
<!DOCTYPE html>
<body>
<script>
let channelName = new URL(location).searchParams.get('broadcastchannel');
window.location.href = `http://{{hosts[alt][www]}}:{{ports[http][0]}}/portals/resources/portal-host.html`;
let key = (new URLSearchParams(window.location.search)).get('key');
window.location.href = `http://{{hosts[alt][www]}}:{{ports[http][0]}}/portals/resources/portal-host.html?key=${key}`;
</script>
</body>
<!DOCTYPE html>
<script src="stash-utils.sub.js"></script>
<body>
<script>
if (window.portalHost)
window.portalHost.postMessage("has host", "*");
let queryParams = new URLSearchParams(window.location.search);
let key = queryParams.get('key');
if (key) {
StashUtils.putValue(key, window.portalHost ? "passed" : "failed");
}
</script>
</body>
const STASH_RESPONDER = "ws://{{host}}:{{ports[ws][0]}}/stash_responder_blocking";
class StashUtils {
/**
* Sends a request to store (|key|, |tuple|) in Stash
* (https://web-platform-tests.org/tools/wptserve/docs/stash.html).
* @param {string} key A UUID that acts as a key that can be used to retrieve |value| later.
* @param {string} value Value to be stored in Stash.
* @returns {Promise} Promise that resolves once the server responds.
*/
static putValue(key, value) {
return new Promise(resolve => {
const ws = new WebSocket(STASH_RESPONDER);
ws.onopen = () => {
ws.send(JSON.stringify({action: 'set', key: key, value: value}));
};
ws.onmessage = e => {
ws.close();
resolve();
};
});
}
/**
* Retrieves value associated with |key| in Stash. If no value has been
* associated with |key| yet, the method waits for putValue to be called with
* |key|, and a value to be associated, before resolving the return promise.
* @param {string} key A UUID that uniquely identifies the value to retrieve.
* @returns {Promise<string>} A promise that resolves with the value associated with |key|.
*/
static takeValue(key) {
return new Promise(resolve => {
const ws = new WebSocket(STASH_RESPONDER);
ws.onopen = () => {
ws.send(JSON.stringify({action: 'get', key: key}));
};
ws.onmessage = e => {
ws.close();
resolve(JSON.parse(e.data).value);
};
});
}
}
#!/usr/bin/python
import json
import threading
import wptserve.stash
from mod_pywebsocket import msgutil
address, authkey = wptserve.stash.load_env_config()
path = "/stash_responder_blocking"
stash = wptserve.stash.Stash(path, address=address, authkey=authkey)
cv = threading.Condition()
def handle_set(key, value):
with cv:
stash.put(key, value)
cv.notify_all()
def handle_get(key):
with cv:
while True:
value = stash.take(key)
if value is not None:
return value
cv.wait()
def web_socket_do_extra_handshake(request):
pass
def web_socket_transfer_data(request):
line = request.ws_stream.receive_message()
query = json.loads(line)
action = query["action"]
key = query["key"]
if action == "set":
value = query["value"]
handle_set(key, value)
response = {}
elif action == "get":
value = handle_get(key)
response = {"value": value}
else:
response = {}
msgutil.send_message(request, json.dumps(response))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment