| Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 1 | "use strict"; |
| 2 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 3 | define(function () { |
| Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 4 | |
| Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 5 | // Limit the supported sandbox permissions, especially |
| Marc Kupietz | 0d0dc6b | 2026-02-15 08:43:12 +0100 | [diff] [blame] | 6 | // to disallow 'same-origin' unless explicitly requested |
| 7 | // and the plugin is hosted on the same origin. |
| Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 8 | let allowed = { |
| 9 | "scripts" : 1, |
| 10 | "presentation" : 1, |
| 11 | "forms": 1, |
| 12 | "downloads-without-user-activation" : 1, |
| Akron | 8f1dbcf | 2022-12-21 12:09:39 +0100 | [diff] [blame] | 13 | "downloads" : 1, |
| Marc Kupietz | 0d0dc6b | 2026-02-15 08:43:12 +0100 | [diff] [blame] | 14 | "popups" : 1, |
| 15 | "same-origin" : 1 |
| 16 | }; |
| 17 | |
| 18 | /** |
| 19 | * Check if a URL is on the same origin as the current page. |
| 20 | */ |
| 21 | function _isSameOrigin (src) { |
| 22 | try { |
| 23 | const url = new URL(src, window.location.href); |
| 24 | return url.origin === window.location.origin; |
| 25 | } catch (e) { |
| 26 | return false; |
| 27 | } |
| Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 30 | return { |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 31 | create : function (data) { |
| 32 | return Object.create(this)._init(data); |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 33 | }, |
| 34 | |
| 35 | // Initialize service |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 36 | _init : function (data) { |
| 37 | if (!data || !data["name"] || !data["src"] || !data["id"]) |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 38 | throw Error("Service not well defined"); |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 39 | |
| 40 | this.name = data["name"]; |
| 41 | this.src = data["src"]; |
| 42 | this.id = data["id"]; |
| Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 43 | this.desc = data["desc"]; |
| Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 44 | let _perm = new Set(); |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 45 | let perm = data["permissions"]; |
| 46 | if (perm && Array.isArray(perm)) { |
| Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 47 | perm.forEach(function (p) { |
| 48 | if (p in allowed) { |
| 49 | _perm.add(p) |
| 50 | } |
| 51 | else { |
| 52 | KorAP.log(0, "Requested permission not allowed"); |
| 53 | } |
| 54 | }); |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 55 | }; |
| Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 56 | |
| 57 | this._perm = _perm; |
| Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 58 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 59 | // There is no close method defined yet |
| 60 | if (!this.close) { |
| 61 | this.close = function () { |
| 62 | this._closeIframe(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return this; |
| 67 | }, |
| 68 | |
| 69 | /** |
| 70 | * The element of the service as embedded in the panel |
| 71 | */ |
| 72 | load : function () { |
| 73 | if (this._load) |
| 74 | return this._load; |
| Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 75 | |
| 76 | if (window.location.protocol == 'https:' && |
| 77 | this.src.toLowerCase().indexOf('https:') != 0) { |
| 78 | KorAP.log(0, "Service endpoint is insecure"); |
| 79 | return; |
| 80 | }; |
| 81 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 82 | // Spawn new iframe |
| 83 | let e = document.createElement('iframe'); |
| 84 | e.setAttribute('allowTransparency',"true"); |
| 85 | e.setAttribute('frameborder', 0); |
| hebasta | 7891324 | 2020-03-30 13:39:20 +0200 | [diff] [blame] | 86 | // Allow forms in Plugins |
| Marc Kupietz | 1cd1569 | 2026-02-14 14:05:00 +0100 | [diff] [blame] | 87 | let permissions = Array.from(this._perm).sort().map(function(i){ return "allow-"+i }); |
| Marc Kupietz | 0d0dc6b | 2026-02-15 08:43:12 +0100 | [diff] [blame] | 88 | |
| 89 | // Only grant same-origin if plugin explicitly requested it |
| 90 | // AND is hosted on the same origin (security gate) |
| 91 | if (this._perm.has("same-origin") && !_isSameOrigin(this.src)) { |
| 92 | permissions = permissions.filter(function(p) { return p !== "allow-same-origin" }); |
| 93 | KorAP.log(0, "Ignoring same-origin permission for cross-origin plugin"); |
| 94 | }; |
| 95 | |
| Marc Kupietz | 1cd1569 | 2026-02-14 14:05:00 +0100 | [diff] [blame] | 96 | e.setAttribute('sandbox', permissions.join(" ")); |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 97 | e.style.height = '0px'; |
| 98 | e.setAttribute('name', this.id); |
| 99 | e.setAttribute('src', this.src); |
| 100 | |
| 101 | this._load = e; |
| 102 | return e; |
| 103 | }, |
| 104 | |
| Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 105 | /** |
| 106 | * Send a message to the embedded service. |
| 107 | */ |
| 108 | sendMsg : function (d) { |
| 109 | let iframe = this.load(); |
| Akron | b0ae841 | 2026-02-24 11:47:52 +0100 | [diff] [blame^] | 110 | if (iframe && iframe.contentWindow) { |
| 111 | iframe.contentWindow.postMessage( |
| 112 | d, |
| 113 | '*' |
| 114 | ); // TODO: Fix origin |
| 115 | }; |
| Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 116 | }, |
| 117 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 118 | // onClose : function () {}, |
| 119 | |
| 120 | /** |
| 121 | * Close the service iframe. |
| 122 | */ |
| 123 | _closeIframe : function () { |
| 124 | var e = this._load; |
| 125 | if (e && e.parentNode) { |
| 126 | e.parentNode.removeChild(e); |
| 127 | }; |
| 128 | this._load = null; |
| 129 | } |
| 130 | }; |
| 131 | }); |