|
| 1 | +## 什么是沙箱 |
| 2 | + |
| 3 | +### 沙箱的应用场景 |
| 4 | + |
| 5 | +### 沙箱的实现方式 |
| 6 | + |
| 7 | +## 简单实现一个沙箱 |
| 8 | + |
| 9 | +### 基于快照实现一个沙箱 |
| 10 | + |
| 11 | +支持单应用的代理沙箱-极简版 |
| 12 | + |
| 13 | +```js |
| 14 | +class SnapshotSandbox { |
| 15 | + constructor() { |
| 16 | + this.proxy = window; |
| 17 | + this.sandboxSnapshot = {}; // 记录在沙箱上的所有属性快照 |
| 18 | + this.windowSnapshot = {}; // 存储window上的所有属性快照 |
| 19 | + this.active(); |
| 20 | + } |
| 21 | + // 激活沙箱,在沙箱里进行操作 |
| 22 | + active() { |
| 23 | + for (const prop in window) { |
| 24 | + if (window.hasOwnProperty(prop)) { |
| 25 | + // 激活时,先把window上的属性保存在 windowSnapshot |
| 26 | + this.windowSnapshot[prop] = window[prop]; |
| 27 | + } |
| 28 | + } |
| 29 | + // 再把沙箱之前的属性重新赋值给 window |
| 30 | + Object.keys(this.sandboxSnapshot).forEach((p) => { |
| 31 | + window[p] = this.sandboxSnapshot[p]; |
| 32 | + }); |
| 33 | + } |
| 34 | + //失活 |
| 35 | + inActive() { |
| 36 | + for (const prop in window) { |
| 37 | + if (window.hasOwnProperty(prop)) { |
| 38 | + // 比较现在的window 和之前的 window对比有啥区别 |
| 39 | + if (window[prop] !== this.windowSnapshot[prop]) { |
| 40 | + // 如果不一样,就说明有变化,需要保存变化 |
| 41 | + // 把沙箱的属性保存在sandboxSnapshot |
| 42 | + this.sandboxSnapshot[prop] = window[prop]; |
| 43 | + // 需要将window上之间的属性 windowSnapshot 再赋值给window |
| 44 | + window[prop] = this.windowSnapshot[prop]; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +let sandBox = new SnapshotSandbox(); |
| 52 | +// 应用的运行从开始到结束,切换后不会影响全局 |
| 53 | +((window) => { |
| 54 | + window.a = 1; |
| 55 | + window.b = 2; |
| 56 | + console.log(window.a, window.b); // 1, 2 |
| 57 | + // 失活 |
| 58 | + sandBox.inActive(); |
| 59 | + console.log(window.a, window.b); // undefined undefined |
| 60 | + // 激活还原 |
| 61 | + sandBox.active(); |
| 62 | + console.log(window.a, window.b); // 1, 2 |
| 63 | +})(sandBox.proxy); // sandBox.proxy就是window |
| 64 | +``` |
| 65 | + |
| 66 | +存在的问题 |
| 67 | + |
| 68 | +### 基于 proxy 实现一个沙箱 |
| 69 | + |
| 70 | +```js |
| 71 | +class LegacySandBox { |
| 72 | + addedPropsMapInSandbox = new Map(); |
| 73 | + modifiedPropsOriginalValueMapInSandbox = new Map(); |
| 74 | + currentUpdatedPropsValueMap = new Map(); |
| 75 | + proxyWindow; |
| 76 | + setWindowProp(prop, value, toDelete = false) { |
| 77 | + if (value === undefined && toDelete) { |
| 78 | + delete window[prop]; |
| 79 | + } else { |
| 80 | + window[prop] = value; |
| 81 | + } |
| 82 | + } |
| 83 | + active() { |
| 84 | + this.currentUpdatedPropsValueMap.forEach((value, prop) => this.setWindowProp(prop, value)); |
| 85 | + } |
| 86 | + inactive() { |
| 87 | + this.modifiedPropsOriginalValueMapInSandbox.forEach((value, prop) => |
| 88 | + this.setWindowProp(prop, value) |
| 89 | + ); |
| 90 | + this.addedPropsMapInSandbox.forEach((_, prop) => this.setWindowProp(prop, undefined, true)); |
| 91 | + } |
| 92 | + constructor() { |
| 93 | + const fakeWindow = Object.create(null); |
| 94 | + this.proxyWindow = new Proxy(fakeWindow, { |
| 95 | + set: (target, prop, value, receiver) => { |
| 96 | + const originalVal = window[prop]; |
| 97 | + if (!window.hasOwnProperty(prop)) { |
| 98 | + this.addedPropsMapInSandbox.set(prop, value); |
| 99 | + } else if (!this.modifiedPropsOriginalValueMapInSandbox.has(prop)) { |
| 100 | + this.modifiedPropsOriginalValueMapInSandbox.set(prop, originalVal); |
| 101 | + } |
| 102 | + this.currentUpdatedPropsValueMap.set(prop, value); |
| 103 | + window[prop] = value; |
| 104 | + }, |
| 105 | + get: (target, prop, receiver) => { |
| 106 | + return target[prop]; |
| 107 | + }, |
| 108 | + }); |
| 109 | + } |
| 110 | +} |
| 111 | +// 验证: |
| 112 | +let legacySandBox = new LegacySandBox(); |
| 113 | +legacySandBox.active(); |
| 114 | +legacySandBox.proxyWindow.city = 'Beijing'; |
| 115 | +console.log('window.city-01:', window.city); |
| 116 | +legacySandBox.inactive(); |
| 117 | +console.log('window.city-02:', window.city); |
| 118 | +legacySandBox.active(); |
| 119 | +console.log('window.city-03:', window.city); |
| 120 | +legacySandBox.inactive(); |
| 121 | +// 输出: |
| 122 | +// window.city-01: Beijing |
| 123 | +// window.city-02: undefined |
| 124 | +// window.city-03: Beijing |
| 125 | +``` |
| 126 | + |
| 127 | +支持多应用的代理沙箱-极简版 |
| 128 | + |
| 129 | +```js |
| 130 | +class ProxySandBox { |
| 131 | + constructor() { |
| 132 | + const fakeWindow = Object.create(null); |
| 133 | + this.proxyWindow = new Proxy(fakeWindow, { |
| 134 | + set: (target, prop, value, receiver) => { |
| 135 | + if (this.isRunning) { |
| 136 | + target[prop] = value; |
| 137 | + } |
| 138 | + }, |
| 139 | + get: (target, prop, receiver) => { |
| 140 | + return prop in target ? target[prop] : window[prop]; |
| 141 | + }, |
| 142 | + }); |
| 143 | + } |
| 144 | + proxyWindow; |
| 145 | + isRunning = false; |
| 146 | + active() { |
| 147 | + this.isRunning = true; |
| 148 | + } |
| 149 | + inactive() { |
| 150 | + this.isRunning = false; |
| 151 | + } |
| 152 | +} |
| 153 | +// 验证: |
| 154 | +let proxySandBox1 = new ProxySandBox(); |
| 155 | +let proxySandBox2 = new ProxySandBox(); |
| 156 | +proxySandBox1.active(); |
| 157 | +proxySandBox2.active(); |
| 158 | +proxySandBox1.proxyWindow.city = 'Beijing'; |
| 159 | +proxySandBox2.proxyWindow.city = 'Shanghai'; |
| 160 | +console.log('active:proxySandBox1:window.city:', proxySandBox1.proxyWindow.city); |
| 161 | +console.log('active:proxySandBox2:window.city:', proxySandBox2.proxyWindow.city); |
| 162 | +console.log('window:window.city:', window.city); |
| 163 | +proxySandBox1.inactive(); |
| 164 | +proxySandBox2.inactive(); |
| 165 | +console.log('inactive:proxySandBox1:window.city:', proxySandBox1.proxyWindow.city); |
| 166 | +console.log('inactive:proxySandBox2:window.city:', proxySandBox2.proxyWindow.city); |
| 167 | +console.log('window:window.city:', window.city); |
| 168 | +// 输出: |
| 169 | +// active:proxySandBox1:window.city: Beijing |
| 170 | +// active:proxySandBox2:window.city: Shanghai |
| 171 | +// window:window.city: undefined |
| 172 | +// inactive:proxySandBox1:window.city: Beijing |
| 173 | +// inactive:proxySandBox2:window.city: Shanghai |
| 174 | +// window:window.city: undefined |
| 175 | +``` |
| 176 | + |
| 177 | +## 参考 |
| 178 | + |
| 179 | +- [微前端 01 : 乾坤的 Js 隔离机制(快照沙箱、两种代理沙箱)](https://www.scanonly.com/article/298077) |
0 commit comments