Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/host-app/src/HostApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', '') : ''}
/>
</div>
Expand Down
33 changes: 32 additions & 1 deletion e2e/tests/tests/check-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
34 changes: 29 additions & 5 deletions packages/uix-host-react/src/components/GuestUIFrame.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,45 @@ 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),
error: undefined,
});

const { container } = render(
<GuestUIFrame guestId="guest-1" src="page.html" allow="fullscreen" />
<GuestUIFrame
guestId="guest-1"
src="page.html"
allow="local-network-access 'none'; fullscreen"
/>
);

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(
<GuestUIFrame
guestId="guest-1"
src="page.html"
allow="microphone; autoplay"
/>
);

expect(container.querySelector("iframe")?.getAttribute("allow")).toBe(
"local-network-access; microphone; autoplay"
);
});

it("attaches the UI and invokes onConnect once connected", async () => {
Expand Down
22 changes: 19 additions & 3 deletions packages/uix-host-react/src/components/GuestUIFrame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +33,7 @@ type ReactIframeProps = IframeHTMLAttributes<HTMLIFrameElement>;
/**
* @public
*/
type FrameProps = Omit<ReactIframeProps, "sandbox">;
type FrameProps = Omit<ReactIframeProps, "sandbox" | "allow">;

/** @public */
export interface GuestUIProps extends FrameProps {
Expand All @@ -50,6 +58,12 @@ export interface GuestUIProps extends FrameProps {
* Additional sandbox attributes GuestUIFrame might need.
*/
sandbox?: AttrTokens<SandboxToken>;
/**
* 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.
*/
Expand Down Expand Up @@ -90,6 +104,7 @@ export const GuestUIFrame = ({
methods,
privateMethods,
sandbox = "",
allow = "",
style,
...customIFrameProps
}: GuestUIProps) => {
Expand Down Expand Up @@ -178,6 +193,7 @@ export const GuestUIFrame = ({
}
{...customIFrameProps}
{...requiredIframeProps}
allow={makeAllowAttrs(requiredIframeProps.allow, allow).join("; ")}
/>
);
};
Expand Down
31 changes: 30 additions & 1 deletion packages/uix-host/src/dom-utils/iframe-normalizers.test.ts
Original file line number Diff line number Diff line change
@@ -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", () =>
Expand All @@ -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);
Expand Down
41 changes: 40 additions & 1 deletion packages/uix-host/src/dom-utils/iframe-normalizers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ export type SandboxToken = `allow-${SandboxPermission}`;
export const makeSandboxAttrs = (...sandboxes: AttrTokens<SandboxToken>[]) =>
mergeAttrValues<SandboxToken>(...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<string, string>();
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
Expand All @@ -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("; ")
);
};
Loading