Skip to content

Commit

Permalink
chore: fix: Avoid elements created through the createElementNS api fr…
Browse files Browse the repository at this point in the history
…om escaping the sandbox (#645)

Co-authored-by: zhouxiao.shaw <[email protected]>
  • Loading branch information
zhoushaw and zhouxiao.shaw authored Oct 19, 2023
1 parent 3116431 commit be01235
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 118 deletions.
3 changes: 2 additions & 1 deletion packages/browser-vm/src/dynamicNode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export function makeElInjector(sandboxConfig: SandboxOptions) {

if (typeof window.Element === 'function') {
// iframe can read html container this can't point to proxyDocument has Illegal invocation error
if (sandboxConfig.fixBaseUrl) safeWrapper(() => handleOwnerDocument());
if (sandboxConfig.fixBaseUrl || sandboxConfig.fixOwnerDocument)
safeWrapper(() => handleOwnerDocument());
const rewrite = (
methods: Array<string>,
builder: typeof injector | typeof injectorRemoveChild,
Expand Down
4 changes: 3 additions & 1 deletion packages/browser-vm/src/pluginify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ function createOptions(Garfish: interfaces.Garfish) {
fixStaticResourceBaseUrl: Boolean(
appInfo.sandbox?.fixStaticResourceBaseUrl,
),
fixOwnerDocument: Boolean(appInfo.sandbox?.fixOwnerDocument),
disableWith: Boolean(appInfo.sandbox?.disableWith),
disableElementtiming: Boolean(appInfo.sandbox?.disableElementtiming),
strictIsolation: Boolean(appInfo.sandbox?.strictIsolation),
// 缓存模式,不收集副作用
disableCollect: appInfo.cache === undefined ? true : Boolean(appInfo.cache),
disableCollect:
appInfo.cache === undefined ? true : Boolean(appInfo.cache),
el: () => appInstance.htmlNode,
styleScopeId: () => appInstance.appContainer.id,
protectVariable: () => appInfo.protectVariable || [],
Expand Down
6 changes: 6 additions & 0 deletions packages/browser-vm/src/proxyInterceptor/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export function createGetter(sandbox: Sandbox) {
const el = value.call(document, tagName, options);
return setSandboxRef(el);
};
}
if (p === 'createElementNS') {
return function (...args) {
const el = value.call(document, ...args);
return setSandboxRef(el);
};
} else if (p === 'createTextNode') {
return function (data) {
const el = value.call(document, data);
Expand Down
1 change: 1 addition & 0 deletions packages/browser-vm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface SandboxOptions {
namespace: string;
baseUrl?: string;
fixBaseUrl?: boolean;
fixOwnerDocument?: boolean;
fixStaticResourceBaseUrl?: boolean;
disableWith?: boolean;
strictIsolation?: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const createDefaultOptions = () => {
disableWith: false,
strictIsolation: false,
disableElementtiming: false,
fixOwnerDocument: false,
},
// global hooks
beforeLoad: () => {},
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export namespace interfaces {
disableWith?: boolean;
strictIsolation?: boolean;
disableElementtiming?: boolean;
fixOwnerDocument?: boolean;
}

export interface Config {
Expand Down
Loading

1 comment on commit be01235

@vercel
Copy link

@vercel vercel bot commented on be01235 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.