fix(Code Node): Upgrade pyodide, sandbox it, and prevent JS sandbox escape (#14356)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2025-04-23 14:41:48 +02:00
committed by GitHub
parent 9021e195fa
commit 6c9c720ae9
6 changed files with 42 additions and 59 deletions

View File

@@ -1,4 +1,5 @@
import { dirname } from 'node:path';
import { createContext, runInContext } from 'node:vm';
import type { PyodideInterface } from 'pyodide';
let pyodideInstance: PyodideInterface | undefined;
@@ -6,8 +7,22 @@ let pyodideInstance: PyodideInterface | undefined;
export async function LoadPyodide(packageCacheDir: string): Promise<PyodideInterface> {
if (pyodideInstance === undefined) {
const { loadPyodide } = await import('pyodide');
const { XMLHttpRequest } = await import('xmlhttprequest-ssl');
const indexURL = dirname(require.resolve('pyodide'));
pyodideInstance = await loadPyodide({ indexURL, packageCacheDir });
const context = createContext({
loadPyodide,
indexURL,
packageCacheDir,
jsglobals: {
Object,
console,
XMLHttpRequest,
},
});
pyodideInstance = (await runInContext(
'loadPyodide({ indexURL, packageCacheDir, jsglobals })',
context,
)) as PyodideInterface;
await pyodideInstance.runPythonAsync(`
from _pyodide_core import jsproxy_typedict