60f5964c64
Front components run untrusted third-party React in a Web Worker. That
worker previously shared the host origin, so it could reach
origin-scoped storage (the metadata-store IndexedDB, the
`twenty-sign-out` BroadcastChannel), cookies, and same-origin resources.
This runs the worker inside a `sandbox="allow-scripts"` (no
`allow-same-origin`) iframe, giving it an opaque origin where the
browser denies localStorage, cookies, IndexedDB, and BroadcastChannel
outright. The worker is kept inside the iframe (rather than a bare
iframe) so untrusted code always runs off the main thread; the
remote-dom render path is unchanged.
- **Transport:** host ↔ iframe ↔ worker over a re-transferred
`MessagePort` (`ThreadMessagePort`); a small bootstrap script is inlined
into the iframe via `srcdoc` (bundled at build time by a prebuild step)
and relays the port to the worker it spawns. Messages across the
boundary use a typed discriminated union with a single parse/guard.
- **Network:** under the opaque origin, direct fetches to the Twenty API
would be `Origin: null`, so the component source and SDK modules are
fetched through an allowlisted, credential-omitting `hostFetch` bridge
and blobbed inside the worker. The allowlist is single-sourced on the
host (http(s) origins only) and carried in the render context. The
bridge is mandatory (rendering fails closed if it is missing), refuses
redirects except for GET/HEAD to the known file-storage URLs, and caps
response body size.
- **SDK loading:** SDK client modules now load inside the worker through
the bridge, replacing the host-side SDK-blob state/effect/provider with
a pure `getSdkClientUrls` URL builder.
- **Isolation tests:** a unit test locks the sandbox attribute
(`allow-scripts`, never `allow-same-origin`); a browser test asserts the
worker actually gets an opaque origin with storage denied, probing
cookies by writing one rather than reading an empty jar.
Also adds a "List Companies" seed front component that queries workspace
data via the SDK client (exercising the bridge end-to-end),
single-sources the command-menu confirmation-modal result event name and
detail type in `twenty-shared` (previously a hand-synced duplicate), and
decomposes the renderer (bridge, sandbox, worker orchestration) into
small single-purpose utils with unit tests.
## How it works
```mermaid
sequenceDiagram
autonumber
participant Host as Host window (twenty-front · host origin)
participant Frame as Sandboxed iframe (allow-scripts · opaque origin)
participant Worker as Worker (untrusted component · opaque origin)
participant API as Twenty API (host origin)
rect rgb(238,242,248)
Note over Host,Worker: 1 — Boot handshake
Host->>Frame: create iframe sandbox="allow-scripts", srcdoc = inlined bootstrap script
Host->>Host: MessageChannel + ThreadMessagePort(port1)<br/>exports = host API + hostFetch
Frame-->>Host: READY
Host->>Frame: INIT + transfer port2
Frame->>Worker: spawn inlined Worker + re-transfer port2
Worker->>Worker: ThreadMessagePort(port)<br/>exports = render / updateContext
Note over Host,Worker: Port now entangles Host ↔ Worker directly
end
rect rgb(246,240,248)
Note over Host,Worker: 2 — Render
Host->>Worker: render(connection, { componentUrl, sdkClientUrls, hostFetchOrigins, token })
Worker->>Worker: override globalThis.fetch<br/>(Twenty origins → hostFetch)
end
rect rgb(248,244,238)
Note over Worker,API: 3 — Network via hostFetch bridge (opaque Origin:null cannot reach the API directly)
Worker->>Host: hostFetch(componentUrl, Bearer)
Host->>Host: origin allowlist + credentials:'omit'
Host->>API: fetch(componentUrl)
API-->>Host: source
Host-->>Worker: { status, headers, body }
Worker->>Host: hostFetch(sdkClientUrls.core / .metadata)
Host-->>Worker: SDK module sources
Worker->>Worker: blob each source in its own opaque origin → import() → run untrusted React
end
rect rgb(238,248,242)
Note over Worker,Host: 4 — Render mirror
Worker->>Host: remote-dom mutations (RemoteConnection)
Host->>Host: RemoteReceiver → RemoteRootRenderer → host DOM
end
Note over Worker: Opaque origin ⇒ browser denies localStorage,<br/>cookies, IndexedDB, BroadcastChannel
```
151 lines
4.4 KiB
TypeScript
151 lines
4.4 KiB
TypeScript
import { v5 as uuidv5 } from 'uuid';
|
|
|
|
import { PAGE_LAYOUT_SEEDS } from 'src/engine/workspace-manager/dev-seeder/core/constants/page-layout-seeds.constant';
|
|
import { generateSeedId } from 'src/engine/workspace-manager/dev-seeder/core/utils/generate-seed-id.util';
|
|
|
|
const SEED_FRONT_COMPONENT_ID_NAMESPACE =
|
|
'e7a3b1c4-f5d6-4e8a-9b2c-3d4e5f6a7b8c';
|
|
|
|
export type SeedFrontComponentDefinition = {
|
|
id: string;
|
|
universalIdentifier: string;
|
|
name: string;
|
|
description: string;
|
|
componentName: string;
|
|
isHeadless: boolean;
|
|
usesSdkClient: boolean;
|
|
seedProjectSubdir: string;
|
|
};
|
|
|
|
export type SeedFrontComponentCommandMenuItemDefinition = {
|
|
universalIdentifier: string;
|
|
frontComponentId: string;
|
|
label: string;
|
|
icon: string;
|
|
position: number;
|
|
isPinned?: boolean;
|
|
pageLayoutId?: string | null;
|
|
};
|
|
|
|
export const getSeedFrontComponentIds = (workspaceId: string) => ({
|
|
helloWorldId: uuidv5(
|
|
`${workspaceId}:seed-front-component:hello-world`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
showNotificationId: uuidv5(
|
|
`${workspaceId}:seed-front-component:show-notification`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
listCompaniesId: uuidv5(
|
|
`${workspaceId}:seed-front-component:list-companies`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
});
|
|
|
|
export const getSeedFrontComponentDefinitions = (
|
|
workspaceId: string,
|
|
): SeedFrontComponentDefinition[] => {
|
|
const { helloWorldId, showNotificationId, listCompaniesId } =
|
|
getSeedFrontComponentIds(workspaceId);
|
|
|
|
return [
|
|
{
|
|
id: helloWorldId,
|
|
universalIdentifier: uuidv5(
|
|
`${workspaceId}:seed-front-component-uid:hello-world`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
name: 'Hello World',
|
|
description:
|
|
'A sample visual front component that displays execution context',
|
|
componentName: 'HelloWorld',
|
|
isHeadless: false,
|
|
usesSdkClient: false,
|
|
seedProjectSubdir: 'hello-world',
|
|
},
|
|
{
|
|
id: showNotificationId,
|
|
universalIdentifier: uuidv5(
|
|
`${workspaceId}:seed-front-component-uid:show-notification`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
name: 'Show Notification',
|
|
description:
|
|
'A sample headless front component that displays a notification',
|
|
componentName: 'ShowNotification',
|
|
isHeadless: true,
|
|
usesSdkClient: false,
|
|
seedProjectSubdir: 'show-notification',
|
|
},
|
|
{
|
|
id: listCompaniesId,
|
|
universalIdentifier: uuidv5(
|
|
`${workspaceId}:seed-front-component-uid:list-companies`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
name: 'List Companies',
|
|
description:
|
|
'A sample visual front component that queries companies through the SDK client',
|
|
componentName: 'ListCompanies',
|
|
isHeadless: false,
|
|
usesSdkClient: true,
|
|
seedProjectSubdir: 'list-companies',
|
|
},
|
|
];
|
|
};
|
|
|
|
export const getSeedFrontComponentCommandMenuItemDefinitions = (
|
|
workspaceId: string,
|
|
): SeedFrontComponentCommandMenuItemDefinition[] => {
|
|
const { helloWorldId, showNotificationId, listCompaniesId } =
|
|
getSeedFrontComponentIds(workspaceId);
|
|
|
|
return [
|
|
{
|
|
universalIdentifier: uuidv5(
|
|
`${workspaceId}:seed-front-component-command:hello-world`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
frontComponentId: helloWorldId,
|
|
label: 'Hello World',
|
|
icon: 'IconAppWindow',
|
|
position: 200,
|
|
},
|
|
{
|
|
universalIdentifier: uuidv5(
|
|
`${workspaceId}:seed-front-component-command:show-notification`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
frontComponentId: showNotificationId,
|
|
label: 'Show Notification',
|
|
icon: 'IconBell',
|
|
position: 201,
|
|
},
|
|
{
|
|
universalIdentifier: uuidv5(
|
|
`${workspaceId}:seed-front-component-command:standalone-page-show-notification`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
frontComponentId: showNotificationId,
|
|
label: 'Show Notification',
|
|
icon: 'IconStar',
|
|
position: 202,
|
|
isPinned: true,
|
|
pageLayoutId: generateSeedId(
|
|
workspaceId,
|
|
PAGE_LAYOUT_SEEDS.DOCUMENTATION_STANDALONE_PAGE,
|
|
),
|
|
},
|
|
{
|
|
universalIdentifier: uuidv5(
|
|
`${workspaceId}:seed-front-component-command:list-companies`,
|
|
SEED_FRONT_COMPONENT_ID_NAMESPACE,
|
|
),
|
|
frontComponentId: listCompaniesId,
|
|
label: 'List Companies',
|
|
icon: 'IconBuildingSkyscraper',
|
|
position: 203,
|
|
},
|
|
];
|
|
};
|