|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js"; |
| 4 | + |
| 5 | +// Webpack 5 standalone bootstrap with an Electron external, as emitted for |
| 6 | +// `target: "electron-main"`. |
| 7 | +const bootstrap = (external: string, consumer: string, entry = "n(100);") => |
| 8 | + `(()=>{var m={45840:${external},100:(e,t,n)=>{"use strict";${consumer}}},c={};` + |
| 9 | + `function n(t){var r=c[t];if(void 0!==r)return r.exports;var o=c[t]={exports:{}};` + |
| 10 | + `return m[t](o,o.exports,n),o.exports}${entry}})();`; |
| 11 | + |
| 12 | +const EXTERNAL = 'e=>{"use strict";e.exports=require("electron")}'; |
| 13 | +const CONSUMER = |
| 14 | + 'const s=n(45840);new s.BrowserWindow({webPreferences:{preload:"./preload.js",contextIsolation:!1}});' + |
| 15 | + 's.ipcMain.handle("get-connections",async()=>[]);'; |
| 16 | + |
| 17 | +const electronOf = (source: string) => |
| 18 | + analyzeJavaScriptStaticSource(source).electron; |
| 19 | + |
| 20 | +describe("Electron identity through Webpack externals", () => { |
| 21 | + it("follows the loader passed to a module factory", () => { |
| 22 | + const electron = electronOf(bootstrap(EXTERNAL, CONSUMER)); |
| 23 | + expect(electron.browser_windows).toMatchObject([ |
| 24 | + { preload_path: "./preload.js" }, |
| 25 | + ]); |
| 26 | + expect(electron.ipc).toMatchObject([ |
| 27 | + { channel: "get-connections", side: "main" }, |
| 28 | + ]); |
| 29 | + }); |
| 30 | + |
| 31 | + it("follows a direct member read from the loader call", () => { |
| 32 | + expect( |
| 33 | + electronOf( |
| 34 | + bootstrap( |
| 35 | + EXTERNAL, |
| 36 | + 'new(n(45840).BrowserWindow)({webPreferences:{preload:"./p.js"}});', |
| 37 | + ), |
| 38 | + ).browser_windows, |
| 39 | + ).toMatchObject([{ preload_path: "./p.js" }]); |
| 40 | + }); |
| 41 | + |
| 42 | + it("follows the runtime loader called from the bootstrap body", () => { |
| 43 | + expect( |
| 44 | + electronOf( |
| 45 | + bootstrap( |
| 46 | + EXTERNAL, |
| 47 | + "", |
| 48 | + 'const s=n(45840);new s.BrowserWindow({webPreferences:{preload:"./p.js"}});', |
| 49 | + ), |
| 50 | + ).browser_windows, |
| 51 | + ).toMatchObject([{ preload_path: "./p.js" }]); |
| 52 | + }); |
| 53 | + |
| 54 | + it.each([ |
| 55 | + [ |
| 56 | + "an external overwritten after the require", |
| 57 | + bootstrap('e=>{e.exports=require("electron");e.exports=other}', CONSUMER), |
| 58 | + ], |
| 59 | + [ |
| 60 | + "a local module table entry", |
| 61 | + bootstrap("e=>{e.exports={BrowserWindow:class{}}}", CONSUMER), |
| 62 | + ], |
| 63 | + [ |
| 64 | + "a shadowed require inside the external", |
| 65 | + bootstrap( |
| 66 | + 'e=>{const require=x=>x;e.exports=require("electron")}', |
| 67 | + CONSUMER, |
| 68 | + ), |
| 69 | + ], |
| 70 | + [ |
| 71 | + "a reassigned loader parameter", |
| 72 | + bootstrap(EXTERNAL, `n=other;${CONSUMER}`), |
| 73 | + ], |
| 74 | + [ |
| 75 | + "a loader-shaped call outside a proven bootstrap", |
| 76 | + `var m={45840:${EXTERNAL}};function n(t){return other(t)}${CONSUMER}`, |
| 77 | + ], |
| 78 | + [ |
| 79 | + "a different table key", |
| 80 | + bootstrap(EXTERNAL, CONSUMER.replace("n(45840)", "n(45841)")), |
| 81 | + ], |
| 82 | + ])("keeps %s unresolved", (_name, source) => { |
| 83 | + const electron = electronOf(source); |
| 84 | + expect(electron.browser_windows).toEqual([]); |
| 85 | + // Only direct observations come from this identity; the separate |
| 86 | + // wrapper-substitution path keeps its own resolution basis. |
| 87 | + expect( |
| 88 | + electron.ipc.filter( |
| 89 | + ({ resolution_basis: basis }) => basis !== "wrapper-substitution", |
| 90 | + ), |
| 91 | + ).toEqual([]); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe("Webpack externals with a tampered runtime", () => { |
| 96 | + it.each([ |
| 97 | + [ |
| 98 | + "a replaced bootstrap loader", |
| 99 | + bootstrap( |
| 100 | + EXTERNAL, |
| 101 | + CONSUMER, |
| 102 | + "const o=n;n=k=>k===100?o(k):local;n(100);", |
| 103 | + ), |
| 104 | + ], |
| 105 | + [ |
| 106 | + "a generator external", |
| 107 | + bootstrap('function*(e){e.exports=require("electron")}', CONSUMER), |
| 108 | + ], |
| 109 | + [ |
| 110 | + "an async external", |
| 111 | + bootstrap('async e=>{e.exports=require("electron")}', CONSUMER), |
| 112 | + ], |
| 113 | + [ |
| 114 | + "a loader that returns something else", |
| 115 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 116 | + "return m[t](o,o.exports,n),o.exports}", |
| 117 | + "return m[t](o,o.exports,n),{BrowserWindow:class{}}}", |
| 118 | + ), |
| 119 | + ], |
| 120 | + [ |
| 121 | + "a table slot replaced before loading", |
| 122 | + bootstrap( |
| 123 | + EXTERNAL, |
| 124 | + CONSUMER, |
| 125 | + "m[45840]=e=>{e.exports={BrowserWindow:class{}}};n(100);", |
| 126 | + ), |
| 127 | + ], |
| 128 | + [ |
| 129 | + "a table slot replaced through an alias", |
| 130 | + bootstrap(EXTERNAL, CONSUMER, "const a=m;a[45840]=local;n(100);"), |
| 131 | + ], |
| 132 | + [ |
| 133 | + "a slot replaced through the runtime exposure", |
| 134 | + bootstrap(EXTERNAL, CONSUMER, "n.m=m;n.m[45840]=local;n(100);"), |
| 135 | + ], |
| 136 | + [ |
| 137 | + "a cache entry planted before loading", |
| 138 | + bootstrap(EXTERNAL, CONSUMER, "c[45840]={exports:local};n(100);"), |
| 139 | + ], |
| 140 | + [ |
| 141 | + "a computed table key that may collide", |
| 142 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 143 | + "c={};", |
| 144 | + "c={};m={...m,[key]:e=>{e.exports=local}};", |
| 145 | + ), |
| 146 | + ], |
| 147 | + [ |
| 148 | + "an unresolved computed key in the table", |
| 149 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 150 | + `45840:${EXTERNAL},`, |
| 151 | + `45840:${EXTERNAL},[key]:e=>{e.exports=local},`, |
| 152 | + ), |
| 153 | + ], |
| 154 | + ])("keeps %s unresolved", (_name, source) => { |
| 155 | + const electron = electronOf(source); |
| 156 | + expect(electron.browser_windows).toEqual([]); |
| 157 | + // Only direct observations come from this identity; the separate |
| 158 | + // wrapper-substitution path keeps its own resolution basis. |
| 159 | + expect( |
| 160 | + electron.ipc.filter( |
| 161 | + ({ resolution_basis: basis }) => basis !== "wrapper-substitution", |
| 162 | + ), |
| 163 | + ).toEqual([]); |
| 164 | + }); |
| 165 | +}); |
| 166 | + |
| 167 | +describe("Webpack externals with a tampered loader or module object", () => { |
| 168 | + it.each([ |
| 169 | + [ |
| 170 | + "cached exports rewritten through the runtime exposure", |
| 171 | + bootstrap( |
| 172 | + EXTERNAL, |
| 173 | + CONSUMER, |
| 174 | + "n.c=c;n(45840);n.c[45840].exports=local;n(100);", |
| 175 | + ), |
| 176 | + ], |
| 177 | + [ |
| 178 | + "a pre-populated cache", |
| 179 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 180 | + "c={};", |
| 181 | + "c={45840:{exports:local}};", |
| 182 | + ), |
| 183 | + ], |
| 184 | + [ |
| 185 | + "exports rewritten in the returned sequence", |
| 186 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 187 | + "return m[t](o,o.exports,n),o.exports}", |
| 188 | + "return m[t](o,o.exports,n),o.exports=local,o.exports}", |
| 189 | + ), |
| 190 | + ], |
| 191 | + [ |
| 192 | + "exports rewritten before the return", |
| 193 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 194 | + "return m[t](o,o.exports,n),o.exports}", |
| 195 | + "m[t](o,o.exports,n);o.exports=local;return o.exports}", |
| 196 | + ), |
| 197 | + ], |
| 198 | + [ |
| 199 | + "an alias of the exposed table", |
| 200 | + bootstrap(EXTERNAL, CONSUMER, "n.m=m;const a=n.m;a[45840]=local;n(100);"), |
| 201 | + ], |
| 202 | + [ |
| 203 | + "a rewritten module key", |
| 204 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 205 | + "function n(t){", |
| 206 | + "function n(t){if(t===45840)t=100;", |
| 207 | + ), |
| 208 | + ], |
| 209 | + [ |
| 210 | + "a dispatch behind a guard", |
| 211 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 212 | + "return m[t](o,o.exports,n),o.exports}", |
| 213 | + "if(t===100)m[t](o,o.exports,n);return o.exports}", |
| 214 | + ), |
| 215 | + ], |
| 216 | + [ |
| 217 | + "duplicate factory parameter names", |
| 218 | + bootstrap('function(e,e,n){e.exports=require("electron")}', CONSUMER), |
| 219 | + ], |
| 220 | + [ |
| 221 | + "an accessor for module exports", |
| 222 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 223 | + "var o=c[t]={exports:{}}", |
| 224 | + "var o=c[t]={get exports(){return local},set exports(v){}}", |
| 225 | + ), |
| 226 | + ], |
| 227 | + ])("keeps %s unresolved", (_name, source) => { |
| 228 | + const electron = electronOf(source); |
| 229 | + expect(electron.browser_windows).toEqual([]); |
| 230 | + // Only direct observations come from this identity; the separate |
| 231 | + // wrapper-substitution path keeps its own resolution basis. |
| 232 | + expect( |
| 233 | + electron.ipc.filter( |
| 234 | + ({ resolution_basis: basis }) => basis !== "wrapper-substitution", |
| 235 | + ), |
| 236 | + ).toEqual([]); |
| 237 | + }); |
| 238 | + |
| 239 | + it.each([ |
| 240 | + [ |
| 241 | + "a cache cleared when the factory throws", |
| 242 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 243 | + "return m[t](o,o.exports,n),o.exports}", |
| 244 | + "var i=!0;try{m[t].call(o.exports,o,o.exports,n),i=!1}finally{i&&delete c[t]}return o.exports}", |
| 245 | + ), |
| 246 | + ], |
| 247 | + [ |
| 248 | + "the runtime exposing its cache", |
| 249 | + bootstrap(EXTERNAL, CONSUMER, "n.c=c;n(100);"), |
| 250 | + ], |
| 251 | + [ |
| 252 | + "chunk installation through the runtime table", |
| 253 | + bootstrap( |
| 254 | + EXTERNAL, |
| 255 | + CONSUMER, |
| 256 | + "n.m=m;n.i=e=>{for(var k in e)n.m[k]=e[k]};n(100);", |
| 257 | + ), |
| 258 | + ], |
| 259 | + [ |
| 260 | + "a loaded flag set before returning", |
| 261 | + bootstrap(EXTERNAL, CONSUMER).replace( |
| 262 | + "return m[t](o,o.exports,n),o.exports}", |
| 263 | + "return m[t].call(o.exports,o,o.exports,n),o.loaded=!0,o.exports}", |
| 264 | + ), |
| 265 | + ], |
| 266 | + ])("still resolves with %s", (_name, source) => { |
| 267 | + expect(electronOf(source).browser_windows).toMatchObject([ |
| 268 | + { preload_path: "./preload.js" }, |
| 269 | + ]); |
| 270 | + }); |
| 271 | +}); |
0 commit comments