diff --git a/lib/Page.js b/lib/Page.js index e2aa3290780..ca7d37b9fc0 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -255,11 +255,20 @@ class Page extends EventEmitter { * @param {Array} cookies */ async setCookie(...cookies) { + const pageURL = this.url(); + const startsWithHTTP = pageURL.startsWith('http'); const items = cookies.map(cookie => { const item = Object.assign({}, cookie); - const pageURL = this.url(); - if (!item.url && pageURL.startsWith('http')) - item.url = this.url(); + if (!item.url && startsWithHTTP) + item.url = pageURL; + console.assert( + item.url !== 'about:blank', + `Blank page can not have cookie "${item.name}"` + ); + console.assert( + !String.prototype.startsWith.call(item.url || '', 'data:'), + `Data URL page can not have cookie "${item.name}"` + ); return item; }); await this.deleteCookie(...items); diff --git a/test/test.js b/test/test.js index 5f215de6a25..f495d888702 100644 --- a/test/test.js +++ b/test/test.js @@ -3126,6 +3126,61 @@ describe('Page', function() { expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie3=3'); }); + it('should not set a cookie on a blank page', async function({page}) { + let error = null; + await page.goto('about:blank'); + try { + await page.setCookie({name: 'example-cookie', value: 'best'}); + } catch (e) { + error = e; + } + expect(error).toBeTruthy(); + expect(error.message).toEqual('Protocol error (Network.deleteCookies): At least one of the url and domain needs to be specified undefined'); + }); + + it('should not set a cookie with blank page URL', async function({page, server}) { + let error = null; + await page.goto(server.PREFIX + '/grid.html'); + try { + await page.setCookie( + {name: 'example-cookie', value: 'best'}, + {url: 'about:blank', name: 'example-cookie-blank', value: 'best'} + ); + } catch (e) { + error = e; + } + expect(error).toBeTruthy(); + expect(error.message).toEqual( + `Blank page can not have cookie "example-cookie-blank"` + ); + }); + + it('should not set a cookie on a data URL page', async function({page}) { + let error = null; + await page.goto('data:,Hello%2C%20World!'); + try { + await page.setCookie({name: 'example-cookie', value: 'best'}); + } catch (e) { + error = e; + } + expect(error).toBeTruthy(); + expect(error.message).toEqual( + 'Protocol error (Network.deleteCookies): At least one of the url and domain needs to be specified undefined' + ); + }); + + it('should not set a cookie with blank page URL', async function({page, server}) { + let error = null; + await page.goto(server.PREFIX + '/grid.html'); + try { + await page.setCookie({name: 'example-cookie', value: 'best'}, {url: 'about:blank', name: 'example-cookie-blank', value: 'best'}); + } catch (e) { + error = e; + } + expect(error).toBeTruthy(); + expect(error.message).toEqual(`Blank page can not have cookie "example-cookie-blank"`); + }); + it('should set a cookie on a different domain', async({page, server}) => { await page.goto(server.PREFIX + '/grid.html'); await page.setCookie({name: 'example-cookie', value: 'best', url: 'https://www.example.com'});