Skip to content

Commit f55d3e1

Browse files
author
liufeichun
committed
fix(runtime-dom): ensure iframe sandbox is handled as an attribute to prevent unintended behavior
1 parent 5a8aa0b commit f55d3e1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packages/runtime-dom/__tests__/patchAttrs.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,29 @@ describe('runtime-dom: attrs patching', () => {
8888
expect(el2.dataset.test).toBe(undefined)
8989
expect(testvalue).toBe(obj)
9090
})
91+
92+
// #13946
93+
test('sandbox attribute should always be handled as attribute', () => {
94+
const iframe = document.createElement('iframe')
95+
96+
// Verify sandbox is treated as attribute, not property
97+
patchProp(iframe, 'sandbox', null, 'allow-scripts')
98+
expect(iframe.getAttribute('sandbox')).toBe('allow-scripts')
99+
100+
// Setting to null should remove the attribute
101+
patchProp(iframe, 'sandbox', 'allow-scripts', null)
102+
expect(iframe.hasAttribute('sandbox')).toBe(false)
103+
expect(iframe.getAttribute('sandbox')).toBe(null)
104+
105+
// Setting to undefined should also remove the attribute
106+
patchProp(iframe, 'sandbox', null, 'allow-forms')
107+
expect(iframe.getAttribute('sandbox')).toBe('allow-forms')
108+
patchProp(iframe, 'sandbox', 'allow-forms', undefined)
109+
expect(iframe.hasAttribute('sandbox')).toBe(false)
110+
111+
// Empty string should set empty attribute (most restrictive sandbox)
112+
patchProp(iframe, 'sandbox', null, '')
113+
expect(iframe.getAttribute('sandbox')).toBe('')
114+
expect(iframe.hasAttribute('sandbox')).toBe(true)
115+
})
91116
})

packages/runtime-dom/src/patchProp.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ function shouldSetAsProp(
111111
return false
112112
}
113113

114+
// #13946 iframe.sandbox should always be set as attribute since setting
115+
// the property to null results in 'null' string, and setting to empty string
116+
// enables the most restrictive sandbox mode instead of no sandboxing.
117+
if (key === 'sandbox') {
118+
return false
119+
}
120+
114121
// #1787, #2840 form property on form elements is readonly and must be set as
115122
// attribute.
116123
if (key === 'form') {

0 commit comments

Comments
 (0)