From 97d679bdac9d55b65710b2036b5f11053fc1a4d1 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Sat, 16 Dec 2023 05:03:52 -0500 Subject: [PATCH] MISC: Protect against reassigning certain globals (#972) --- jest.config.js | 9 +++++---- src/engine.tsx | 1 + src/utils/Protections.ts | 9 +++++++++ test/__mocks__/{monacoMock.js => NullMock.js} | 0 test/__mocks__/rawLoader.js | 0 test/__mocks__/styleMock.js | 0 6 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 src/utils/Protections.ts rename test/__mocks__/{monacoMock.js => NullMock.js} (100%) delete mode 100644 test/__mocks__/rawLoader.js delete mode 100644 test/__mocks__/styleMock.js diff --git a/jest.config.js b/jest.config.js index 1478db196..b27249a09 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,12 +10,13 @@ module.exports = { moduleNameMapper: { "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "/test/__mocks__/fileMock.js", - "\\.(css|less)$": "/test/__mocks__/styleMock.js", - "\\!!raw-loader!.*$": "/test/__mocks__/rawLoader.js", + "\\.(css|less)$": "/test/__mocks__/NullMock.js", + "\\!!raw-loader!.*$": "/test/__mocks__/NullMock.js", "@player": "/src/Player", "@enums": "/src/Enums", "@nsdefs": "/src/ScriptEditor/NetscriptDefinitions", - "^monaco-editor$": "/test/__mocks__/monacoMock.js", - "^monaco-vim$": "/test/__mocks__/monacoMock.js", + "^monaco-editor$": "/test/__mocks__/NullMock.js", + "^monaco-vim$": "/test/__mocks__/NullMock.js", + "/utils/Protections$": "/test/__mocks__/NullMock.js", }, }; diff --git a/src/engine.tsx b/src/engine.tsx index de8d1a5a5..ac67c018c 100644 --- a/src/engine.tsx +++ b/src/engine.tsx @@ -9,6 +9,7 @@ import { staneksGift } from "./CotMG/Helper"; import { processPassiveFactionRepGain, inviteToFaction } from "./Faction/FactionHelpers"; import { Router } from "./ui/GameRoot"; import { Page } from "./ui/Router"; +import "./utils/Protections"; // Side-effect: Protect against certain unrecoverable errors import "./PersonObjects/Player/PlayerObject"; // For side-effect of creating Player import { diff --git a/src/utils/Protections.ts b/src/utils/Protections.ts new file mode 100644 index 000000000..1ff968576 --- /dev/null +++ b/src/utils/Protections.ts @@ -0,0 +1,9 @@ +// This file is imported for side effects only. +/* Prevent inadvertantly redefining certain window properties, + which are known to cause unrecoverable game errors when redefined. + The player is able to redefine these properties as writable if desired. */ +Object.defineProperties(window, { + Number: { writable: false }, + Object: { writable: false }, + String: { writable: false }, +}); diff --git a/test/__mocks__/monacoMock.js b/test/__mocks__/NullMock.js similarity index 100% rename from test/__mocks__/monacoMock.js rename to test/__mocks__/NullMock.js diff --git a/test/__mocks__/rawLoader.js b/test/__mocks__/rawLoader.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/__mocks__/styleMock.js b/test/__mocks__/styleMock.js deleted file mode 100644 index e69de29bb..000000000