diff --git a/e2e/host-app/src/HostApp.jsx b/e2e/host-app/src/HostApp.jsx index 7844185..dea1d57 100644 --- a/e2e/host-app/src/HostApp.jsx +++ b/e2e/host-app/src/HostApp.jsx @@ -59,6 +59,7 @@ export default function Component() { id="iframe-for-guest" key={Math.random()} guestId={extensions[0].id} + allow="microphone; autoplay" src={extensions[0].url.href ? extensions[0].url.href.replace('register', '') : ''} /> diff --git a/e2e/tests/tests/check-button.js b/e2e/tests/tests/check-button.js index f326830..43f4c1f 100644 --- a/e2e/tests/tests/check-button.js +++ b/e2e/tests/tests/check-button.js @@ -13,9 +13,40 @@ test("Check if guest is loaded", async (t) => { .expect(iframe.getAttribute("src")) .contains("http://localhost:3002", "Iframe src should point to guest app", { timeout: 10000 }); + // HostApp passes allow="microphone; autoplay"; it must be merged with, not + // replaced by, the required directive -- including after attachUI() runs + // normalizeIframe() on the mounted frame. await t .expect(iframe.getAttribute("allow")) - .eql("local-network-access", "Guest UI iframe should allow local-network-access", { timeout: 10000 }); + .eql( + "local-network-access; microphone; autoplay", + "Guest UI iframe should merge custom allow directives with local-network-access", + { timeout: 10000 } + ); +}); + +test("Custom allow directives are granted inside the guest UI frame", async (t) => { + await t + .expect(Selector(iframeSelector).exists) + .ok("Iframe should exist", { timeout: 10000 }); + await t.switchToIframe(iframeSelector); + + const allowsFeature = (feature) => + t.eval( + () => { + const policy = document.permissionsPolicy || document.featurePolicy; + return policy ? policy.allowsFeature(feature) : null; + }, + { dependencies: { feature } } + ); + + await t + .expect(await allowsFeature("microphone")) + .ok("Cross-origin guest UI frame should be granted microphone") + .expect(await allowsFeature("autoplay")) + .ok("Cross-origin guest UI frame should be granted autoplay") + .expect(await allowsFeature("camera")) + .notOk("Features not listed in allow must stay blocked"); }); test("Guest server iframe allows local-network-access", async (t) => { diff --git a/packages/uix-host-react/src/components/GuestUIFrame.test.tsx b/packages/uix-host-react/src/components/GuestUIFrame.test.tsx index 6b37241..6e36f4a 100644 --- a/packages/uix-host-react/src/components/GuestUIFrame.test.tsx +++ b/packages/uix-host-react/src/components/GuestUIFrame.test.tsx @@ -106,7 +106,7 @@ describe("GuestUIFrame", () => { expect(iframe?.getAttribute("name")).toBe("uix-guest-guest-1"); }); - it('always sets allow="local-network-access", even if a custom allow prop is passed', () => { + it("keeps the required local-network-access directive even if a custom allow prop overrides it", () => { const guest = createGuest("guest-1"); mockedUseHost.mockReturnValue({ host: mockHostWithGuest(guest), @@ -114,13 +114,37 @@ describe("GuestUIFrame", () => { }); const { container } = render( - + ); const iframe = container.querySelector("iframe"); - // required props win over any custom `allow`, same as the other - // required iframe props (data-uix-guest, role, referrerPolicy) - expect(iframe?.getAttribute("allow")).toBe("local-network-access"); + expect(iframe?.getAttribute("allow")).toBe( + "local-network-access; fullscreen" + ); + }); + + it("merges a custom allow prop with the required allow directives", () => { + const guest = createGuest("guest-1"); + mockedUseHost.mockReturnValue({ + host: mockHostWithGuest(guest), + error: undefined, + }); + + const { container } = render( + + ); + + expect(container.querySelector("iframe")?.getAttribute("allow")).toBe( + "local-network-access; microphone; autoplay" + ); }); it("attaches the UI and invokes onConnect once connected", async () => { diff --git a/packages/uix-host-react/src/components/GuestUIFrame.tsx b/packages/uix-host-react/src/components/GuestUIFrame.tsx index 4778fc0..54aeff6 100644 --- a/packages/uix-host-react/src/components/GuestUIFrame.tsx +++ b/packages/uix-host-react/src/components/GuestUIFrame.tsx @@ -14,8 +14,16 @@ import { CrossRealmObject, UIFrameRect, VirtualApi } from "@adobe/uix-core"; import React, { useEffect, useRef } from "react"; import type { IframeHTMLAttributes } from "react"; import { useHost } from "../hooks/useHost.js"; -import type { AttrTokens, SandboxToken } from "@adobe/uix-host"; -import { makeSandboxAttrs, requiredIframeProps } from "@adobe/uix-host"; +import type { + AllowDirectives, + AttrTokens, + SandboxToken, +} from "@adobe/uix-host"; +import { + makeAllowAttrs, + makeSandboxAttrs, + requiredIframeProps, +} from "@adobe/uix-host"; /** * @internal @@ -25,7 +33,7 @@ type ReactIframeProps = IframeHTMLAttributes; /** * @public */ -type FrameProps = Omit; +type FrameProps = Omit; /** @public */ export interface GuestUIProps extends FrameProps { @@ -50,6 +58,12 @@ export interface GuestUIProps extends FrameProps { * Additional sandbox attributes GuestUIFrame might need. */ sandbox?: AttrTokens; + /** + * Additional Permissions Policy directives for the iframe "allow=" attribute + * (e.g. `"microphone; autoplay"` or `["microphone", "autoplay"]`). Merged + * with the directives the SDK requires. + */ + allow?: AllowDirectives; /** * Optional custom URL or path. */ @@ -90,6 +104,7 @@ export const GuestUIFrame = ({ methods, privateMethods, sandbox = "", + allow = "", style, ...customIFrameProps }: GuestUIProps) => { @@ -178,6 +193,7 @@ export const GuestUIFrame = ({ } {...customIFrameProps} {...requiredIframeProps} + allow={makeAllowAttrs(requiredIframeProps.allow, allow).join("; ")} /> ); }; diff --git a/packages/uix-host/src/dom-utils/iframe-normalizers.test.ts b/packages/uix-host/src/dom-utils/iframe-normalizers.test.ts index d1a0547..64cf95f 100644 --- a/packages/uix-host/src/dom-utils/iframe-normalizers.test.ts +++ b/packages/uix-host/src/dom-utils/iframe-normalizers.test.ts @@ -1,4 +1,8 @@ -import { makeSandboxAttrs, normalizeIframe } from "./iframe-normalizers"; +import { + makeAllowAttrs, + makeSandboxAttrs, + normalizeIframe, +} from "./iframe-normalizers"; describe("makeSandboxAttrs", () => { it("merges and dedupes sandbox attributes", () => @@ -13,7 +17,32 @@ describe("makeSandboxAttrs", () => { )); }); +describe("makeAllowAttrs", () => { + it("merges directives from strings and arrays, first feature wins", () => + expect( + makeAllowAttrs( + "local-network-access", + " microphone ; autoplay 'self' ;", + ["Microphone 'none'", "camera https://example.com"] + ) + ).toEqual([ + "local-network-access", + "microphone", + "autoplay 'self'", + "camera https://example.com", + ])); +}); + describe("normalizeIframe", () => { + it("merges required allow directives with existing ones", () => { + const frame = document.createElement("iframe"); + frame.setAttribute("allow", "microphone; autoplay"); + normalizeIframe(frame); + expect(frame.getAttribute("allow")).toBe( + "local-network-access; microphone; autoplay" + ); + }); + it("applies required attributes and default sandbox to iframe", () => { const frame = document.createElement("iframe"); normalizeIframe(frame); diff --git a/packages/uix-host/src/dom-utils/iframe-normalizers.ts b/packages/uix-host/src/dom-utils/iframe-normalizers.ts index 5821dd4..7f94f33 100644 --- a/packages/uix-host/src/dom-utils/iframe-normalizers.ts +++ b/packages/uix-host/src/dom-utils/iframe-normalizers.ts @@ -41,6 +41,38 @@ export type SandboxToken = `allow-${SandboxPermission}`; export const makeSandboxAttrs = (...sandboxes: AttrTokens[]) => mergeAttrValues(...sandboxes); +/** + * Permissions Policy directives for an iframe "allow=" attribute. Either a + * semicolon-separated string (e.g. `"microphone; autoplay 'self'"`) or an + * array of individual directives (e.g. `["microphone", "autoplay 'self'"]`). + * @public + */ +export type AllowDirectives = string | string[]; + +const tokenizeAllowDirectives = (directives: AllowDirectives) => + (typeof directives === "string" ? directives.split(";") : directives) + .map((directive) => directive.trim().replace(/\s+/g, " ")) + .filter(Boolean); + +/** + * Merge and deduplicate Permissions Policy directives for an iframe "allow=" + * attribute. Directives are keyed by feature name; when the same feature + * appears more than once, the first occurrence wins. + * @internal + */ +export const makeAllowAttrs = (...allowLists: AllowDirectives[]) => { + const byFeature = new Map(); + for (const allowList of allowLists) { + for (const directive of tokenizeAllowDirectives(allowList)) { + const feature = directive.split(" ")[0].toLowerCase(); + if (!byFeature.has(feature)) { + byFeature.set(feature, directive); + } + } + } + return [...byFeature.values()]; +}; + /** * Limit provided set of "sandbox" attributes to a list of legal ones. * @internal @@ -57,14 +89,21 @@ export const requiredIframeProps = { allow: "local-network-access", }; -const requiredIframeAttrEntries = Object.entries(requiredIframeProps); +const { allow: requiredAllow, ...requiredIframeAttrs } = requiredIframeProps; +const requiredIframeAttrEntries = Object.entries(requiredIframeAttrs); /** * Set and/or verify the required properties for use with uix-sdk on an iframe. + * Required "allow=" directives are merged into any existing ones rather than + * replacing them, so host-granted permissions are preserved. * @internal */ export const normalizeIframe = (iframe: HTMLIFrameElement) => { for (const [attr, value] of requiredIframeAttrEntries) { iframe.setAttribute(attr, value); } + iframe.setAttribute( + "allow", + makeAllowAttrs(requiredAllow, iframe.getAttribute("allow") || "").join("; ") + ); };