Skip to content

Commit 8e4019b

Browse files
committed
Add iframe permissions
1 parent 38442ea commit 8e4019b

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

src/components/Retool.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,65 @@
11
import { useEffect, useRef, useState } from "react";
22

3-
const Retool = ({ data, url, height, width, onData }) => {
3+
const IFramePermissionKeys = set(['downloads', 'forms', 'modals', 'popups', 'fullscreen', 'camera', 'microphone', 'geolocation', 'payment', 'topNavigation', 'sameOrigin'])
4+
5+
const constructPermissionStrings = (permissions) => {
6+
if (!permissions || permissions.length === 0) {
7+
return { allowString: undefined, sandboxString: undefined }
8+
}
9+
10+
const allowArray = []
11+
const sandboxArray = []
12+
13+
permissions.filter(permission => IFramePermissionKeys.has(permission)).forEach(permission => {
14+
switch (permission) {
15+
case 'downloads':
16+
sandboxArray.push('allow-downloads')
17+
break
18+
case 'forms':
19+
sandboxArray.push('allow-forms')
20+
break
21+
case 'modals':
22+
sandboxArray.push('allow-modals')
23+
break
24+
case 'popups':
25+
sandboxArray.push('allow-popups', 'allow-popups-to-escape-sandbox')
26+
break
27+
case 'fullscreen':
28+
allowArray.push('fullscreen')
29+
break
30+
case 'camera':
31+
allowArray.push('camera')
32+
break
33+
case 'microphone':
34+
allowArray.push('microphone')
35+
break
36+
case 'geolocation':
37+
allowArray.push('geolocation')
38+
break
39+
case 'payment':
40+
allowArray.push('payment')
41+
break
42+
case 'topNavigation':
43+
sandboxArray.push('allow-top-navigation', 'allow-top-navigation-by-user-activation')
44+
break
45+
case 'sameOrigin':
46+
sandboxArray.push('allow-same-origin')
47+
break
48+
}
49+
})
50+
51+
const allowString = allowArray.length > 0 ? allowArray.join(';') : undefined
52+
const sandboxString = sandboxArray.length > 0 ? sandboxArray.join(' ') : undefined
53+
54+
return { allowString, sandboxString }
55+
}
56+
57+
const Retool = ({ data, url, height, width, onData, permissions }) => {
458
const embeddedIframe = useRef(null);
559
const [elementWatchers, setElementWatchers] = useState({});
660

61+
const { allowString, sandboxString } = constructPermissionStrings(permissions)
62+
763
/* Retool passes up the list of elements to watch on page load */
864
useEffect(() => {
965
for (const key in elementWatchers) {
@@ -87,6 +143,8 @@ const Retool = ({ data, url, height, width, onData }) => {
87143

88144
return (
89145
<iframe
146+
allow={allowString}
147+
sandbox={sandboxString}
90148
height={height ?? "100%"}
91149
width={width ?? "100%"}
92150
frameBorder="none"

0 commit comments

Comments
 (0)