Everyone on the net wants to disable puppeteer from enforcing the same-origin policy, I actually want to test that same-origin-policy enforcement works! but it doesn't and i don't know why, this is what I do:
import puppeteer from 'puppeteer';
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://example.com'); // <= THE ORIGIN
// Some error listeners
page.on("pageerror", function(err) {
theTempValue = err.toString();
console.log("Page error: " + theTempValue);
});
page.on("error", function (err) {
theTempValue = err.toString();
console.log("Error: " + theTempValue);
});
// THE EVALUATE
await page.evaluate(async (otherSiteURL, myOrigin) => {
console.log('the origin: '+ myOrigin); // prints https://example.com
console.log('otherSiteURL: '+ myOrigin); // prints https://some-other-site.com
let r = await fetch(otherSiteURL, {mode:'cors', method:"GET", headers: {Origin:myOrigin}});
r = await r.json();
// *** I have access to the response and no error has been triggered ***
console.log('Response is visible, while it shouldn't be due to cors: ' + JSON.stringify(r));
}, 'https://some-other-site.com', 'https://example.com');
await browser.close();
})();
As you can see I am in the context of page 'https://example.com'
and from evaluate function I am executing a fetch to some-other-site
url which i am the owner of and indeed I've checked that the
Access-Control-Allow-Origin header returned in the response is not *
and not https://example.com
, Hence this is 100% same-origin-policy violation and should result in the browser blocking access to the response, and reporting a CORS error
So Why on earth Puppeteer allows me to see the response and not throwing a CORS error ? what am I doing wrong - I WANT to see CORS error!