From dfb0086f2093a1689a0b2a0d9f91d250cf68e2c9 Mon Sep 17 00:00:00 2001 From: abose Date: Mon, 13 Nov 2023 01:37:15 +0530 Subject: [PATCH] chore: fs lib overwrite protection --- src/phoenix/init_vfs.js | 15 +++++++++++++++ src/utils/ExtensionLoader.js | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/src/phoenix/init_vfs.js b/src/phoenix/init_vfs.js index 95c9ac669c..d079020127 100644 --- a/src/phoenix/init_vfs.js +++ b/src/phoenix/init_vfs.js @@ -288,12 +288,27 @@ const _createAppDirs = async function () { }; +const CORE_LIB_GUARD_INTERVAL = 5000; const _FS_ERROR_MESSAGE = 'Oops. Phoenix could not be started due to missing file system library.'; export default function initVFS() { if(!window.fs || !window.path || !window.Phoenix){ window.alert(_FS_ERROR_MESSAGE); throw new Error(_FS_ERROR_MESSAGE); } + const savedfs = window.fs, savedPath = window.path; + setInterval(()=>{ + if(window.fs !== savedfs){ + console.error("window.fs overwrite detected!! Some extension may have corrupted this." + + " attempting to revert to original lib."); + window.fs=savedfs; + } + if(window.path !== savedPath){ + console.error("window.path overwrite detected!! Some extension may have corrupted this." + + " attempting to revert to original lib."); + window.path=savedPath; + } + + }, CORE_LIB_GUARD_INTERVAL); _setupVFS(window.fs, window.path); window._phoenixfsAppDirsCreatePromise = _createAppDirs(); diff --git a/src/utils/ExtensionLoader.js b/src/utils/ExtensionLoader.js index 8f78356ded..6cf1fd4d2c 100644 --- a/src/utils/ExtensionLoader.js +++ b/src/utils/ExtensionLoader.js @@ -207,6 +207,7 @@ define(function (require, exports, module) { } throw new Error("Config can only be loaded from an http url, but got" + baseConfig.baseUrl); } + const savedFSlib = window.fs; /** * Loads the extension module that lives at baseUrl into its own Require.js context @@ -250,6 +251,13 @@ define(function (require, exports, module) { return extensionRequireDeferred.promise(); }).then(function (module) { // Extension loaded normally + if(savedFSlib !== window.fs) { + console.error("fslib overwrite detected while loading extension. This means that" + + " some extension tried to modify a core library. reverting to original lib.."); + // note that the extension name here may not be that actual extension that did the + // overwrite. So we dont log the extension name here. + window.fs = savedFSlib; + } var initPromise; _extensions[name] = module;