Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 18 additions & 10 deletions lib/FrameManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,12 @@ class Frame {
script.src = url;
if (type)
script.type = type;
document.head.appendChild(script);
await new Promise((res, rej) => {
script.onload = res;
const promise = new Promise((res, rej) => {
script.onload = res.bind(null, script);
script.onerror = rej;
});
return script;
document.head.appendChild(script);
return promise;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

await promise;
return script;

Makes this a bit easier to read. I missed that we were returning the script at first.

}

/**
Expand All @@ -487,7 +487,11 @@ class Frame {
const script = document.createElement('script');
script.type = type;
script.text = content;
let error = null;
script.onerror = e => error = e;
document.head.appendChild(script);
if (error)
throw error;
return script;
}
}
Expand Down Expand Up @@ -529,24 +533,28 @@ class Frame {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
document.head.appendChild(link);
await new Promise((res, rej) => {
link.onload = res;
const promise = new Promise((res, rej) => {
link.onload = res.bind(null, link);
link.onerror = rej;
});
return link;
document.head.appendChild(link);
return promise;
}

/**
* @param {string} content
* @return {!HTMLElement}
* @return {!Promise<!HTMLElement>}
*/
function addStyleContent(content) {
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(content));
const promise = new Promise((res, rej) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above.

style.onload = res.bind(null, style);
style.onerror = rej;
});
document.head.appendChild(style);
return style;
return promise;
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,20 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
expect(scriptHandle.asElement()).not.toBeNull();
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() {
Expand Down Expand Up @@ -1388,6 +1402,20 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
expect(styleHandle.asElement()).not.toBeNull();
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() {
Expand Down