Add function to enable/disable routing

Wraps the toX() functions of the Router to add a check to only run the
function when it is allowed.

The check is used in the import page to prevent user scripts from
redirecting away to a crime or something else.
This commit is contained in:
Martin Fournier
2022-01-22 10:53:16 -05:00
parent d386528627
commit a47eae8b25
3 changed files with 56 additions and 12 deletions
+30 -1
View File
@@ -84,6 +84,9 @@ import { ThemeBrowser } from "../Themes/ui/ThemeBrowser";
import { ImportSaveRoot } from "./React/ImportSaveRoot";
import { BypassWrapper } from "./React/BypassWrapper";
import _wrap from "lodash/wrap";
import _functions from "lodash/functions";
const htmlLocation = location;
interface IProps {
@@ -111,6 +114,9 @@ export let Router: IRouter = {
page: () => {
throw new Error("Router called before initialization");
},
allowRouting: () => {
throw new Error("Router called before initialization");
},
toActiveScripts: () => {
throw new Error("Router called before initialization");
},
@@ -238,6 +244,8 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
if (importString === undefined && page === Page.ImportSave)
throw new Error("Trying to go to a page without the proper setup");
const [allowRoutingCalls, setAllowRoutingCalls] = useState(true);
function resetErrorBoundary(): void {
setErrorBoundaryKey(errorBoundaryKey + 1);
}
@@ -259,6 +267,7 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
Router = {
page: () => page,
allowRouting: (value: boolean) => setAllowRoutingCalls(value),
toActiveScripts: () => setPage(Page.ActiveScripts),
toAugmentations: () => setPage(Page.Augmentations),
toBladeburner: () => setPage(Page.Bladeburner),
@@ -333,6 +342,26 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
},
};
useEffect(() => {
// Wrap Router navigate functions to be able to disable the execution
_functions(Router).
filter((fnName) => fnName.startsWith('to')).
forEach((fnName) => {
// @ts-ignore - tslint does not like this, couldn't find a way to make it cooperate
Router[fnName] = _wrap(Router[fnName], (func, ...args) => {
if (!allowRoutingCalls) {
// Let's just log to console.
console.log(`Routing is currently disabled - Attempted router.${fnName}()`);
return;
}
// Call the function normally
return func(...args);
});
});
});
useEffect(() => {
if (page !== Page.Terminal) window.scrollTo(0, 0);
});
@@ -539,7 +568,7 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
<ImportSaveRoot
importString={importString}
automatic={importAutomatic}
onReturning={() => Router.toTerminal()}
router={Router}
/>
);
withSidebar = false;