Skip to content

Commit

Permalink
chore: fs lib overwrite protection
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Nov 12, 2023
1 parent 49b486a commit dfb0086
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/phoenix/init_vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions src/utils/ExtensionLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit dfb0086

Please sign in to comment.