Skip to content

Commit 515488d

Browse files
authoredNov 21, 2024··
fix: replace eval with globalThis to comply with CSP policies (#1232)
**Issue:** Sandpack currently uses `eval` to obtain the global object, which triggers CSP `unsafe-eval` violations. This poses security risks and limits the ability to use Sandpack in environments with strict CSP policies. **Solution:** Replaced the `eval`-based approach with an IIFE that sequentially checks for `globalThis`, `self`, `window`, and `global` to securely access the global object without violating CSP policies. **Testing:** - Tested in a local development environment with CSP enforced to ensure no violations occur. **Related Issue:** - [1221](#1221)
1 parent 1cc5222 commit 515488d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed
 

‎sandpack-react/src/components/Console/utils/transformers.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
/* eslint-disable @typescript-eslint/explicit-function-return-type */
44
/* eslint-disable @typescript-eslint/no-explicit-any */
55
// Const
6-
const GLOBAL = (function getGlobal() {
7-
// NOTE: see http://www.ecma-international.org/ecma-262/6.0/index.html#sec-performeval step 10
8-
const savedEval = eval;
6+
const GLOBAL = (function getGlobal(): any {
7+
if (typeof globalThis !== "undefined") return globalThis; // modern standard
98

10-
return savedEval("this");
9+
if (typeof window !== "undefined") return window; // browser
10+
11+
if (typeof global !== "undefined") return global; // Node.js
12+
13+
// eslint-disable-next-line no-restricted-globals
14+
if (typeof self !== "undefined") return self; // Web Worker
15+
16+
throw Error("Unable to locate global object");
1117
})();
1218

1319
const ARRAY_BUFFER_SUPPORTED = typeof ArrayBuffer === "function";

0 commit comments

Comments
 (0)
Please sign in to comment.