Skip to content

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

fix(firefox): do not dispatch OPTIONS request (#36503)

Yury Semikhatskyc22f1fc316d2
diff --git a/packages/playwright-core/src/server/firefox/ffNetworkManager.ts b/packages/playwright-core/src/server/firefox/ffNetworkManager.ts
index 5687ecdeb6..50bb3017a3 100644
--- a/packages/playwright-core/src/server/firefox/ffNetworkManager.ts
+++ b/packages/playwright-core/src/server/firefox/ffNetworkManager.ts
@@ -62,6 +62,9 @@ export class FFNetworkManager {
const frame = redirectedFrom ? redirectedFrom.request.frame() : (event.frameId ? this._page.frameManager.frame(event.frameId) : null);
if (!frame)
return;
+ // Align with Chromium and WebKit and not expose preflight OPTIONS requests to the client.
+ if (event.method === 'OPTIONS' && !event.isIntercepted)
+ return;
if (redirectedFrom)
this._requests.delete(redirectedFrom._id);
const request = new InterceptableRequest(frame, redirectedFrom, event);
diff --git a/tests/page/page-event-request.spec.ts b/tests/page/page-event-request.spec.ts
index 84717026a8..028ddcca54 100644
--- a/tests/page/page-event-request.spec.ts
+++ b/tests/page/page-event-request.spec.ts
@@ -286,3 +286,93 @@ it('<picture> resource should have type image', async ({ page }) => {
]);
expect(request.resourceType()).toBe('image');
});
+
+// Chromium: requestWillBeSentEvent.frameId is undefined for OPTIONS.
+// WebKit: no requestWillBeSent event in the protocol for OPTIONS (at least on Mac).
+// Firefox: OPTIONS request can be dispatched.
+it('should not expose preflight OPTIONS request', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36311' }
+}, async ({ page, server, browserName }) => {
+ const serverRequests = [];
+ server.setRoute('/cors', (req, res) => {
+ serverRequests.push(`${req.method} ${req.url}`);
+ if (req.method === 'OPTIONS') {
+ res.writeHead(204, {
+ 'Access-Control-Allow-Origin': '*',
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, OPTIONS',
+ 'Access-Control-Allow-Headers': '*',
+ });
+ res.end();
+ return;
+ }
+ res.writeHead(200, { 'Content-type': 'text/plain', 'Access-Control-Allow-Origin': '*' });
+ res.end('Hello there!');
+ });
+ const clientRequests = [];
+ page.on('request', request => {
+ clientRequests.push(`${request.method()} ${request.url()}`);
+ });
+ const response = await page.evaluate(async url => {
+ const response = await fetch(url, {
+ method: 'POST',
+ body: '',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-Custom-Header': 'test-value'
+ }
+ });
+ return await response.text();
+ }, server.CROSS_PROCESS_PREFIX + '/cors').catch(() => {});
+ expect(response).toBe('Hello there!');
+ expect(serverRequests).toEqual([
+ 'OPTIONS /cors',
+ 'POST /cors',
+ ]);
+ expect(clientRequests).toEqual([
+ `POST ${server.CROSS_PROCESS_PREFIX}/cors`,
+ ]);
+});
+
+it('should not expose preflight OPTIONS request with network interception', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36311' }
+}, async ({ page, server, browserName }) => {
+ const serverRequests = [];
+ server.setRoute('/cors', (req, res) => {
+ serverRequests.push(`${req.method} ${req.url}`);
+ if (req.method === 'OPTIONS') {
+ res.writeHead(204, {
+ 'Access-Control-Allow-Origin': '*',
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, OPTIONS',
+ 'Access-Control-Allow-Headers': '*',
+ });
+ res.end();
+ return;
+ }
+ res.writeHead(200, { 'Content-type': 'text/plain', 'Access-Control-Allow-Origin': '*' });
+ res.end('Hello there!');
+ });
+ await page.route('**/*', route => route.continue());
+ const clientRequests = [];
+ page.on('request', request => {
+ clientRequests.push(`${request.method()} ${request.url()}`);
+ });
+ const response = await page.evaluate(async url => {
+ const response = await fetch(url, {
+ method: 'POST',
+ body: '',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-Custom-Header': 'test-value'
+ }
+ });
+ return await response.text();
+ }, server.CROSS_PROCESS_PREFIX + '/cors').catch(() => {});
+ expect(response).toBe('Hello there!');
+ expect.soft(serverRequests).toEqual([
+ ...(browserName !== 'chromium' ? ['OPTIONS /cors'] : []),
+ 'POST /cors',
+ ]);
+ expect.soft(clientRequests).toEqual([
+ `POST ${server.CROSS_PROCESS_PREFIX}/cors`,
+ ]);
+});
diff --git a/tests/page/page-route.spec.ts b/tests/page/page-route.spec.ts
index 70461b679b..1d38c0c2e8 100644
--- a/tests/page/page-route.spec.ts
+++ b/tests/page/page-route.spec.ts
@@ -737,7 +737,7 @@ it('should respect cors overrides', async ({ page, server, browserName, isAndroi
}
});
-it('should not auto-intercept non-preflight OPTIONS', async ({ page, server, isAndroid, browserName }) => {
+it('should not auto-intercept non-preflight OPTIONS without network interception', async ({ page, server, isAndroid, browserName }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/20469' });
it.fixme(isAndroid);
@@ -775,6 +775,31 @@ it('should not auto-intercept non-preflight OPTIONS', async ({ page, server, isA
// Preflight for OPTIONS, then OPTIONS, then GET without preflight.
expect.soft(requests).toEqual(['OPTIONS:/something', 'OPTIONS:/something', 'GET:/something']);
}
+});
+
+// Make sure this runs in a new context as preflight results could be cached.
+it('should not auto-intercept non-preflight OPTIONS with network interception', async ({ page, server, isAndroid, browserName }) => {
+ it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/20469' });
+ it.fixme(isAndroid);
+
+ await page.goto(server.EMPTY_PAGE);
+
+ let requests = [];
+ server.setRoute('/something', (request, response) => {
+ requests.push(request.method + ':' + request.url);
+ if (request.method === 'OPTIONS') {
+ response.writeHead(200, {
+ 'Access-Control-Allow-Origin': '*',
+ 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, DELETE',
+ 'Access-Control-Allow-Headers': '*',
+ 'Cache-Control': 'no-cache'
+ });
+ response.end(`Hello`);
+ return;
+ }
+ response.writeHead(200, { 'Access-Control-Allow-Origin': '*' });
+ response.end('World');
+ });
// With interception.
{
@@ -791,10 +816,10 @@ it('should not auto-intercept non-preflight OPTIONS', async ({ page, server, isA
expect.soft(text1).toBe('Hello');
expect.soft(text2).toBe('World');
// Preflight for OPTIONS is auto-fulfilled, then OPTIONS, then GET without preflight.
- if (browserName === 'firefox')
- expect.soft(requests).toEqual(['OPTIONS:/something', 'OPTIONS:/something', 'GET:/something']);
- else
+ if (browserName === 'chromium')
expect.soft(requests).toEqual(['OPTIONS:/something', 'GET:/something']);
+ else
+ expect.soft(requests).toEqual(['OPTIONS:/something', 'OPTIONS:/something', 'GET:/something']);
}
});