|
1 | 1 | import { useEffect, useRef, useState } from "react"; |
2 | 2 |
|
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 }) => { |
4 | 58 | const embeddedIframe = useRef(null); |
5 | 59 | const [elementWatchers, setElementWatchers] = useState({}); |
6 | 60 |
|
| 61 | + const { allowString, sandboxString } = constructPermissionStrings(permissions) |
| 62 | + |
7 | 63 | /* Retool passes up the list of elements to watch on page load */ |
8 | 64 | useEffect(() => { |
9 | 65 | for (const key in elementWatchers) { |
@@ -87,6 +143,8 @@ const Retool = ({ data, url, height, width, onData }) => { |
87 | 143 |
|
88 | 144 | return ( |
89 | 145 | <iframe |
| 146 | + allow={allowString} |
| 147 | + sandbox={sandboxString} |
90 | 148 | height={height ?? "100%"} |
91 | 149 | width={width ?? "100%"} |
92 | 150 | frameBorder="none" |
|
0 commit comments