feat(Page): Page.addScriptTag should throw when blocked by CSP (#2320)

This patch teaches Page.addScriptTag and Page.addStyleTag to throw
an error when blocked by CSP.

References #1229.
This commit is contained in:
Andrey Lushnikov 2018-04-06 13:17:55 -07:00 committed by GitHub
parent 4663b43a62
commit 846c0800b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 6 deletions

View File

@ -470,11 +470,12 @@ class Frame {
script.src = url; script.src = url;
if (type) if (type)
script.type = type; script.type = type;
document.head.appendChild(script); const promise = new Promise((res, rej) => {
await new Promise((res, rej) => {
script.onload = res; script.onload = res;
script.onerror = rej; script.onerror = rej;
}); });
document.head.appendChild(script);
await promise;
return script; return script;
} }
@ -487,7 +488,11 @@ class Frame {
const script = document.createElement('script'); const script = document.createElement('script');
script.type = type; script.type = type;
script.text = content; script.text = content;
let error = null;
script.onerror = e => error = e;
document.head.appendChild(script); document.head.appendChild(script);
if (error)
throw error;
return script; return script;
} }
} }
@ -529,23 +534,29 @@ class Frame {
const link = document.createElement('link'); const link = document.createElement('link');
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.href = url; link.href = url;
document.head.appendChild(link); const promise = new Promise((res, rej) => {
await new Promise((res, rej) => {
link.onload = res; link.onload = res;
link.onerror = rej; link.onerror = rej;
}); });
document.head.appendChild(link);
await promise;
return link; return link;
} }
/** /**
* @param {string} content * @param {string} content
* @return {!HTMLElement} * @return {!Promise<!HTMLElement>}
*/ */
function addStyleContent(content) { async function addStyleContent(content) {
const style = document.createElement('style'); const style = document.createElement('style');
style.type = 'text/css'; style.type = 'text/css';
style.appendChild(document.createTextNode(content)); style.appendChild(document.createTextNode(content));
const promise = new Promise((res, rej) => {
style.onload = res;
style.onerror = rej;
});
document.head.appendChild(style); document.head.appendChild(style);
await promise;
return style; return style;
} }
} }

View File

@ -1336,6 +1336,20 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
expect(scriptHandle.asElement()).not.toBeNull(); expect(scriptHandle.asElement()).not.toBeNull();
expect(await page.evaluate(() => __injected)).toBe(35); expect(await page.evaluate(() => __injected)).toBe(35);
}); });
it('should throw when added with content to the CSP page', async({page, server}) => {
await page.goto(server.PREFIX + '/csp.html');
let error = null;
await page.addScriptTag({ content: 'window.__injected = 35;' }).catch(e => error = e);
expect(error).toBeTruthy();
});
it('should throw when added with URL to the CSP page', async({page, server}) => {
await page.goto(server.PREFIX + '/csp.html');
let error = null;
await page.addScriptTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedfile.js' }).catch(e => error = e);
expect(error).toBeTruthy();
});
}); });
describe('Page.addStyleTag', function() { describe('Page.addStyleTag', function() {
@ -1388,6 +1402,20 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
expect(styleHandle.asElement()).not.toBeNull(); expect(styleHandle.asElement()).not.toBeNull();
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(0, 128, 0)'); expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(0, 128, 0)');
}); });
it('should throw when added with content to the CSP page', async({page, server}) => {
await page.goto(server.PREFIX + '/csp.html');
let error = null;
await page.addStyleTag({ content: 'body { background-color: green; }' }).catch(e => error = e);
expect(error).toBeTruthy();
});
it('should throw when added with URL to the CSP page', async({page, server}) => {
await page.goto(server.PREFIX + '/csp.html');
let error = null;
await page.addStyleTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedstyle.css' }).catch(e => error = e);
expect(error).toBeTruthy();
});
}); });
describe('Page.url', function() { describe('Page.url', function() {