Skip to content

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

fix(evaluate): reject exposeFunctions in the utility world (#42860)

Dmitry Gozman67d98ef71554
diff --git a/docs/src/api/params.md b/docs/src/api/params.md
index c107b8660c..129cf4f206 100644
--- a/docs/src/api/params.md
+++ b/docs/src/api/params.md
@@ -594,19 +594,19 @@ Additional built-in types to preserve in the evaluation argument and return valu
* langs: js
- `exposeFunctions` <[boolean]>
-When set to `true`, functions passed inside [`param: arg`] are exposed in the page and can be called from the page function. Calling one returns a [Promise] of its result. Under the hood, each function is exposed via [`method: Page.exposeFunction`], so it is technically accessible from all frames and worlds of the page. Exposed functions are cleared upon the top-level navigation. Defaults to `false`, in which case functions are not serializable and passing one throws an error.
+When set to `true`, functions passed inside [`param: arg`] are exposed in the page and can be called from the page function. Calling one returns a [Promise] of its result. Under the hood, each function is exposed via [`method: Page.exposeFunction`], so it is technically accessible from all frames of the page. Exposed functions are cleared upon the top-level navigation. Defaults to `false`, in which case functions are not serializable and passing one throws an error.
## js-init-script-expose-functions
* langs: js
- `exposeFunctions` <[boolean]>
-When set to `true`, functions passed inside [`param: arg`] are exposed in the page and can be called from the init script. Calling one returns a [Promise] of its result. Under the hood, each function is exposed via [`method: Page.exposeFunction`], so it is technically accessible from all frames and worlds of the page. Unlike functions passed to [`method: Page.evaluate`], functions passed to an init script are exposed in every new document, so they survive navigations. Defaults to `false`, in which case functions are not serializable and are silently dropped.
+When set to `true`, functions passed inside [`param: arg`] are exposed in the page and can be called from the init script. Calling one returns a [Promise] of its result. Under the hood, each function is exposed via [`method: Page.exposeFunction`], so it is technically accessible from all frames of the page. Unlike functions passed to [`method: Page.evaluate`], functions passed to an init script are exposed in every new document, so they survive navigations. Defaults to `false`, in which case functions are not serializable and are silently dropped.
## js-evaluate-world
* langs: js
- `world` <[EvaluationWorld]<"main"|"utility">>
-The JavaScript world to evaluate the function in. `"main"` is the world where the page's own scripts run. `"utility"` is an isolated world that shares the DOM with the page, but has a separate JavaScript environment that the page's scripts cannot observe or tamper with. Defaults to `"main"`.
+The JavaScript world to evaluate the function in. `"main"` is the world where the page's own scripts run. `"utility"` is an isolated world that shares the DOM with the page, but has a separate JavaScript environment that the page's scripts cannot observe or tamper with. Defaults to `"main"`. Exposed functions are only available in the `"main"` world.
## js-evalonselector-pagefunction
* langs: js
diff --git a/packages/playwright-core/src/client/jsHandle.ts b/packages/playwright-core/src/client/jsHandle.ts
index 362652d908..bfcf903b63 100644
--- a/packages/playwright-core/src/client/jsHandle.ts
+++ b/packages/playwright-core/src/client/jsHandle.ts
@@ -151,4 +151,6 @@ export function assertEvaluateOptions(options: any) {
throw new Error('Too many arguments. If you need to pass more than 1 argument to the function wrap them in an object.');
if (options?.serialize !== undefined && (!Array.isArray(options.serialize) || options.serialize.some((type: unknown) => type !== 'Map' && type !== 'Set')))
throw new Error('serialize: expected an array of (Map|Set)');
+ if (options?.exposeFunctions && options?.world === 'utility')
+ throw new Error('Option "exposeFunctions" is not supported in the "utility" world.');
}
diff --git a/tests/page/page-evaluate-world.spec.ts b/tests/page/page-evaluate-world.spec.ts
index 4665b326b8..ed64695935 100644
--- a/tests/page/page-evaluate-world.spec.ts
+++ b/tests/page/page-evaluate-world.spec.ts
@@ -142,3 +142,9 @@ it('should isolate the utility world from main world tampering', async ({ page }
expect(await locator.evaluate(e => e.getAttribute('data-value'))).toBe('tampered');
expect(await locator.evaluate(e => e.getAttribute('data-value'), undefined, { world: 'utility' })).toBe('1');
});
+
+it('should reject exposeFunctions in the utility world', async ({ page }) => {
+ const error = await page.evaluate(async ({ cb }) => await cb(17), { cb: (x: number) => x * 2 }, { exposeFunctions: true, world: 'utility' }).catch(e => e);
+ expect(error.message).toContain('Option "exposeFunctions" is not supported in the "utility" world.');
+ expect(await page.evaluate(async ({ cb }) => await cb(17), { cb: (x: number) => x * 2 }, { exposeFunctions: true, world: 'main' })).toBe(34);
+});