Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

test: add inline scripts to csp tests (#38295)

Yury Semikhatskya99b3e4d870e
diff --git a/tests/assets/csp.html b/tests/assets/csp.html
index 34fc1fc1a5..378187491d 100644
--- a/tests/assets/csp.html
+++ b/tests/assets/csp.html
@@ -1 +1,2 @@
<meta http-equiv="Content-Security-Policy" content="default-src 'self'">
+<script type='text/javascript'>window.__inlineScriptValue = 42;</script>";
\ No newline at end of file
diff --git a/tests/electron/electron-app.spec.ts b/tests/electron/electron-app.spec.ts
index f1d8e351f9..ea4d6f4b07 100644
--- a/tests/electron/electron-app.spec.ts
+++ b/tests/electron/electron-app.spec.ts
@@ -213,6 +213,7 @@ test('should bypass csp', async ({ launchElectronApp, server }) => {
await page.goto(server.PREFIX + '/csp.html');
await page.addScriptTag({ content: 'window["__injected"] = 42;' });
expect(await page.evaluate('window["__injected"]')).toBe(42);
+ expect(await page.evaluate('window["__inlineScriptValue"]')).toBe(42);
});
test('should create page for browser view', async ({ launchElectronApp }) => {
diff --git a/tests/library/browsercontext-csp.spec.ts b/tests/library/browsercontext-csp.spec.ts
index 9e155bef88..1317b9e9d5 100644
--- a/tests/library/browsercontext-csp.spec.ts
+++ b/tests/library/browsercontext-csp.spec.ts
@@ -24,6 +24,7 @@ it('should bypass CSP meta tag @smoke', async ({ browser, server }) => {
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(server.PREFIX + '/csp.html');
+ expect(await page.evaluate('window["__inlineScriptValue"]')).toBe(undefined);
await page.addScriptTag({ content: 'window["__injected"] = 42;' }).catch(e => void e);
expect(await page.evaluate('window["__injected"]')).toBe(undefined);
await context.close();
@@ -34,6 +35,7 @@ it('should bypass CSP meta tag @smoke', async ({ browser, server }) => {
const context = await browser.newContext({ bypassCSP: true });
const page = await context.newPage();
await page.goto(server.PREFIX + '/csp.html');
+ expect(await page.evaluate('window["__inlineScriptValue"]')).toBe(42);
await page.addScriptTag({ content: 'window["__injected"] = 42;' });
expect(await page.evaluate('window["__injected"]')).toBe(42);
await context.close();
@@ -42,12 +44,16 @@ it('should bypass CSP meta tag @smoke', async ({ browser, server }) => {
it('should bypass CSP header', async ({ browser, server }) => {
// Make sure CSP prohibits addScriptTag.
- server.setCSP('/empty.html', 'default-src "self"');
-
+ server.setRoute('/empty.html', (req, res) => {
+ res.setHeader('Content-Security-Policy', 'default-src "self"');
+ res.setHeader('Content-Type', 'text/html');
+ res.end(`<script type='text/javascript'>window.__inlineScriptValue = 42;</script>`);
+ });
{
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
+ expect(await page.evaluate('window["__inlineScriptValue"]')).toBe(undefined);
await page.addScriptTag({ content: 'window["__injected"] = 42;' }).catch(e => void e);
expect(await page.evaluate('window["__injected"]')).toBe(undefined);
await context.close();
@@ -58,6 +64,7 @@ it('should bypass CSP header', async ({ browser, server }) => {
const context = await browser.newContext({ bypassCSP: true });
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
+ expect(await page.evaluate('window["__inlineScriptValue"]')).toBe(42);
await page.addScriptTag({ content: 'window["__injected"] = 42;' });
expect(await page.evaluate('window["__injected"]')).toBe(42);
await context.close();
@@ -68,10 +75,12 @@ it('should bypass after cross-process navigation', async ({ browser, server }) =
const context = await browser.newContext({ bypassCSP: true });
const page = await context.newPage();
await page.goto(server.PREFIX + '/csp.html');
+ expect(await page.evaluate('window["__inlineScriptValue"]')).toBe(42);
await page.addScriptTag({ content: 'window["__injected"] = 42;' });
expect(await page.evaluate('window["__injected"]')).toBe(42);
await page.goto(server.CROSS_PROCESS_PREFIX + '/csp.html');
+ expect(await page.evaluate('window["__inlineScriptValue"]')).toBe(42);
await page.addScriptTag({ content: 'window["__injected"] = 42;' });
expect(await page.evaluate('window["__injected"]')).toBe(42);
await context.close();
@@ -84,6 +93,7 @@ it('should bypass CSP in iframes as well', async ({ browser, server }) => {
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
const frame = await attachFrame(page, 'frame1', server.PREFIX + '/csp.html');
+ expect(await frame.evaluate('window["__inlineScriptValue"]')).toBe(undefined);
await frame.addScriptTag({ content: 'window["__injected"] = 42;' }).catch(e => void e);
expect(await frame.evaluate('window["__injected"]')).toBe(undefined);
await context.close();
@@ -95,6 +105,7 @@ it('should bypass CSP in iframes as well', async ({ browser, server }) => {
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
const frame = await attachFrame(page, 'frame1', server.PREFIX + '/csp.html');
+ expect(await frame.evaluate('window["__inlineScriptValue"]')).toBe(42);
await frame.addScriptTag({ content: 'window["__injected"] = 42;' }).catch(e => void e);
expect(await frame.evaluate('window["__injected"]')).toBe(42);
await context.close();
diff --git a/tests/library/defaultbrowsercontext-1.spec.ts b/tests/library/defaultbrowsercontext-1.spec.ts
index 05ba78626f..23dfe84ba1 100644
--- a/tests/library/defaultbrowsercontext-1.spec.ts
+++ b/tests/library/defaultbrowsercontext-1.spec.ts
@@ -113,6 +113,7 @@ it('should support bypassCSP option', async ({ server, launchPersistent }) => {
await page.goto(server.PREFIX + '/csp.html');
await page.addScriptTag({ content: 'window["__injected"] = 42;' });
expect(await page.evaluate('__injected')).toBe(42);
+ expect(await page.evaluate('window["__inlineScriptValue"]')).toBe(42);
});
it('should support javascriptEnabled option', async ({ launchPersistent, browserName }) => {