Skip to content

Commit

Permalink
feat: add security rules to IFrameBlock element
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Apr 18, 2024
1 parent 224ead0 commit cdbd1ff
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/editor/core/draw/particle/block/modules/IFrameBlock.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { IRowElement } from '../../../../../interface/Row'

export class IFrameBlock {
private static readonly sandbox = [
'allow-forms',
'allow-scripts',
'allow-same-origin',
'allow-popups'
]
private static readonly sandbox = ['allow-scripts', 'allow-same-origin']
private element: IRowElement

constructor(element: IRowElement) {
this.element = element
}

private _defineIframeProperties(iframeWindow: Window) {
Object.defineProperties(iframeWindow, {
// 禁止获取parent避免安全漏洞
parent: {
get: () => null
},
// 用于区分上下文
__POWERED_BY_CANVAS_EDITOR__: {
get: () => true
}
})
}

public render(blockItemContainer: HTMLDivElement) {
const block = this.element.block!
const iframe = document.createElement('iframe')
iframe.setAttribute('data-id', this.element.id!)
iframe.sandbox.add(...IFrameBlock.sandbox)
iframe.style.border = 'none'
iframe.style.width = '100%'
Expand All @@ -26,5 +35,7 @@ export class IFrameBlock {
iframe.srcdoc = block.iframeBlock.srcdoc
}
blockItemContainer.append(iframe)
// 重新定义iframe上属性
this._defineIframeProperties(iframe.contentWindow!)
}
}

0 comments on commit cdbd1ff

Please sign in to comment.