Skip to content

Commit

Permalink
cookie-store: add tests for cross-origin
Browse files Browse the repository at this point in the history
Ensure cookieStore changes propogate across origins

Bug: 729800
Change-Id: Ife37521a548fc35711e5925621ea27ca44036559
Reviewed-on: https://chromium-review.googlesource.com/1212147
Commit-Queue: Jarryd Goodman <[email protected]>
Reviewed-by: Victor Costan <[email protected]>
Cr-Commit-Position: refs/heads/master@{#590847}
  • Loading branch information
jarryd999 authored and Commit Bot committed Sep 12, 2018
1 parent 90d11af commit 5250ac1
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!doctype html>
<meta charset='utf-8'>
<title>Async Cookies: cookieStore basic API across frames</title>
<link rel='help' href='https://github.com/WICG/cookie-store'>
<link rel='author' href='[email protected]' title='Jarryd Goodman'>
<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>
<style>iframe { display: none; }</style>
<iframe id='iframe'></iframe>
<script>
'use strict';

promise_test(async t => {
const iframe = document.getElementById('iframe');
const frameCookieStore = iframe.contentWindow.cookieStore;

const oldCookie = await frameCookieStore.get('cookie-name');
assert_equals(oldCookie, null);

await cookieStore.set('cookie-name', 'cookie-value');
t.add_cleanup(() => cookieStore.delete('cookie-name'));

const frameCookie = await frameCookieStore.get('cookie-name');
assert_equals(frameCookie.value, 'cookie-value');
}, 'cookieStore.get() sees cookieStore.set() in frame');

promise_test(async t => {
const iframe = document.getElementById('iframe');
const frameCookieStore = iframe.contentWindow.cookieStore;

const oldCookie = await frameCookieStore.get('cookie-name');
assert_equals(oldCookie, null);

await frameCookieStore.set('cookie-name', 'cookie-value');
t.add_cleanup(() => frameCookieStore.delete('cookie-name'));

const cookie = await cookieStore.get('cookie-name');
assert_equals(cookie.value, 'cookie-value');
}, 'cookieStore.get() in frame sees cookieStore.set()')
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!doctype html>
<meta charset='utf-8'>
<title>Async Cookies: cookieStore basic API across origins</title>
<link rel='help' href='https://github.com/WICG/cookie-store'>
<link rel='author' href='[email protected]' title='Jarryd Goodman'>
<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>
<script src='resources/helpers.js'></script>
<style>iframe { display: none; }</style>

<script>
'use strict';

const kPath = '/cookie-store/resources/helper_iframe.sub.html';
const kCorsBase = `https://{{domains[www1]}}:{{ports[https][0]}}`;
const kCorsUrl = `${kCorsBase}${kPath}`;

promise_test(async t => {
const iframe = await createIframe(kCorsUrl, t);
assert_true(iframe != null);

iframe.contentWindow.postMessage({
opname: 'set-cookie',
name: 'cookie-name',
value: 'cookie-value',
}, kCorsBase);
t.add_cleanup(() => {
cookieStore.delete('cookie-name');
});

await waitForMessage();

const cookies = await cookieStore.getAll();
assert_equals(cookies.length, 1);
assert_equals(cookies[0].name, 'cookie-name');
assert_equals(cookies[0].value, 'cookie-value');
}, 'cookieStore.get() sees cookieStore.set() in cross-origin frame');

promise_test(async t => {
const iframe = await createIframe(kCorsUrl, t);
assert_true(iframe != null);

await cookieStore.set('cookie-name', 'cookie-value');

const cookie = await cookieStore.get('cookie-name');
assert_equals(cookie.value, 'cookie-value');

iframe.contentWindow.postMessage({
opname: 'get-cookie',
name: 'cookie-name',
}, kCorsBase);
t.add_cleanup(() => {
cookieStore.delete('cookie-name');
})

const message = await waitForMessage();

const { frameCookie } = message;
assert_not_equals(frameCookie, null);
assert_equals(frameCookie.name, 'cookie-name');
assert_equals(frameCookie.value, 'cookie-value');
}, 'cookieStore.get() in cross-origin frame sees cookieStore.set()');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<meta charset='utf-8'>
<link rel='author' href='[email protected]' title='Jarryd Goodman'>
<script>
'use strict';

// Writing a cookie:
// Input: { cookieToSet: { name: 'cookie-name', value: 'cookie-value' } }
// Response: "Cookie has been set"
//
// Read a cookie.
// Command: { existingCookieName: 'cookie-name' }
// Response: Result of cookieStore.get():
// { frameCookie: { name: 'cookie-name', value: 'cookie-value' } }
window.addEventListener('message', async function (event) {
const { opname } = event.data;
if (opname === 'set-cookie') {
const { name, value } = event.data
await cookieStore.set({
name,
value,
domain: '{{host}}',
});
event.source.postMessage('Cookie has been set', event.origin);
} else if (opname === 'get-cookie') {
const { name } = event.data
const frameCookie = await cookieStore.get(name);
event.source.postMessage({frameCookie}, event.origin);
}
});
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Promise based helper function who's return promise will resolve
* once the iframe src has been loaded
* @param {string} url the url to set the iframe src
* @param {test} t a test object to add a cleanup function to
* @return {Promise} when resolved, will return the iframe
*/
self.createIframe = (url, t) => new Promise(resolve => {
const iframe = document.createElement('iframe');
iframe.addEventListener('load', () => {resolve(iframe);}, {once: true});
iframe.src = url;
document.documentElement.appendChild(iframe);
t.add_cleanup(() => iframe.remove());
});

/**
* Function that will return a promise that resolves when a message event
* is fired. Returns a promise that resolves to the message that was received
*/
self.waitForMessage = () => new Promise(resolve => {
window.addEventListener('message', event => {
resolve(event.data);
}, {once: true});
});

0 comments on commit 5250ac1

Please sign in to comment.