增加测试循环

This commit is contained in:
IceCola 2024-05-26 16:12:19 +08:00
parent 79f35e91be
commit 11b971c0c2
2 changed files with 79 additions and 11 deletions

View File

@ -3,7 +3,7 @@ const FILE_URL = import.meta.url;
/** @typedef {any} Window */ /** @typedef {any} Window */
// 方便开关确定沙盒的问题喵 // 方便开关确定沙盒的问题喵
const SANDBOX_ENABLED = false; const SANDBOX_ENABLED = true;
// 暴露方法Symbol用于类之间通信 // 暴露方法Symbol用于类之间通信
const SandboxExposer = Symbol("Sandbox.Exposer"); // 实例暴露 const SandboxExposer = Symbol("Sandbox.Exposer"); // 实例暴露
@ -3269,15 +3269,16 @@ class Sandbox {
const executingScope = Sandbox.#executingScope[Sandbox.#executingScope.length - 1]; const executingScope = Sandbox.#executingScope[Sandbox.#executingScope.length - 1];
const scope = inheritScope && executingScope || thiz.#scope; const scope = inheritScope && executingScope || thiz.#scope;
const contextName = Sandbox.#makeName("__context_", scope); const contextName = Sandbox.#makeName("_", scope);
const argsName = Sandbox.#makeName("__args_", scope); const argsName = Sandbox.#makeName("_", scope);
const applyName = Sandbox.#makeName("_", scope);
const parameters = paramList const parameters = paramList
? paramList.join(", ") : ""; ? paramList.join(", ") : "";
const writeContextAction = { exists: 0, extend: 1, all: 2 }[writeContext] || 0; const writeContextAction = { exists: 0, extend: 1, all: 2 }[writeContext] || 0;
let argumentList; let argumentList;
const raw = new thiz.#domainFunction("_", `with(_){with(window){with(${contextName}){return(()=>{"use strict";return(function(${parameters}){\n// 沙盒代码起始\n${code}\n// 沙盒代码结束\n}).call(${contextName}.this,...${argsName})})()}}}`); const raw = new thiz.#domainFunction("_", `with(_){with(window){with(${contextName}){return(()=>{"use strict";return(${applyName}(function(${parameters}){\n// 沙盒代码起始\n${code}\n// 沙盒代码结束\n},${contextName}.this,${argsName}))})()}}}`);
const domain = thiz.#domain; const domain = thiz.#domain;
const domainWindow = thiz.#domainWindow; const domainWindow = thiz.#domainWindow;
@ -3295,6 +3296,8 @@ class Sandbox {
return marshalledContext; return marshalledContext;
case argsName: case argsName:
return argumentList; return argumentList;
case applyName:
return Reflect.apply;
} }
// 防止逃逸 // 防止逃逸

View File

@ -1,7 +1,12 @@
import { Sandbox, Domain, Marshal, Monitor, AccessAction, Rule, SANDBOX_ENABLED } from "./sandbox.js"; import { Sandbox, Domain, Marshal, Monitor, AccessAction, Rule, SANDBOX_ENABLED } from "./sandbox.js";
// 是否强制所有模式下使用沙盒 // 是否强制所有模式下使用沙盒
const SANDBOX_FORCED = false; const SANDBOX_FORCED = true;
// 是否启用自动测试
const SANDBOX_AUTOTEST = true;
// 是否禁用自动测试延迟
// 这将放弃渲染,在游戏结束前无响应
const SANDBOX_AUTOTEST_NODELAY = false;
const TRUSTED_IPS = Object.freeze([ const TRUSTED_IPS = Object.freeze([
"47.99.105.222", "47.99.105.222",
@ -289,8 +294,6 @@ function initSecurity({
}) { }) {
if (initialized) if (initialized)
throw "security 已经被初始化过了"; throw "security 已经被初始化过了";
if (!SANDBOX_ENABLED)
return;
topVariables.lib = lib; topVariables.lib = lib;
topVariables.game = game; topVariables.game = game;
@ -300,6 +303,9 @@ function initSecurity({
topVariables._status = _status; topVariables._status = _status;
topVariables.gnc = gnc; topVariables.gnc = gnc;
if (!SANDBOX_ENABLED)
return;
loadPolyfills(); loadPolyfills();
// @ts-ignore // @ts-ignore
@ -332,6 +338,14 @@ function initSecurity({
...Object.values(game.promises), ...Object.values(game.promises),
defaultEval, defaultEval,
window.require, window.require,
window.process,
window.module,
window.exports,
window.cordova,
// @ts-ignore
window.NonameAndroidBridge,
// @ts-ignore
window.noname_shijianInterfaces,
window, window,
]; ];
@ -399,6 +413,44 @@ function initSecurity({
// }) // })
// .start(); // .start();
if (SANDBOX_AUTOTEST) {
// 一个测试循环喵
if (SANDBOX_AUTOTEST_NODELAY) {
game.resume = () => { };
game.pause = () => { };
}
game.delay = game.delayx = () => { };
game.asyncDelay = game.asyncDelayx = async () => { };
Reflect.defineProperty(lib.element.GameEvent.prototype, "animate", {
get: () => undefined,
set() { },
enumerable: false,
configurable: false,
});
if (!lib.videos)
lib.videos = [];
game.over = function (...args) {
if (_status.over) return;
_status.over = true;
setTimeout(() => {
if (!_status.auto)
return;
const count = parseInt(localStorage.getItem("__sandboxTestCount") || "0");
localStorage.setItem("__sandboxTestCount", String(count + 1));
localStorage.setItem(
lib.configprefix + "directstart", "true");
game.reload();
}, SANDBOX_AUTOTEST_NODELAY ? 5000 : 1000);
};
lib.arenaReady.push(() => ui.click.auto());
}
initialized = true; initialized = true;
} }
@ -464,17 +516,30 @@ function getIsolateds(sandbox) {
* ``` * ```
*/ */
function loadPolyfills() { function loadPolyfills() {
function isNativeDescriptor(descriptor) {
if (typeof descriptor.value == "function"
&& !nativePattern.test(descriptor.value.toString()))
return false;
if (typeof descriptor.get == "function"
&& !nativePattern.test(descriptor.get.toString()))
return false;
if (typeof descriptor.set == "function"
&& !nativePattern.test(descriptor.set.toString()))
return false;
return true;
}
function copyDescriptors(top, box) { function copyDescriptors(top, box) {
for (const key of Reflect.ownKeys(top)) { for (const key of Reflect.ownKeys(top)) {
const descriptor = Reflect.getOwnPropertyDescriptor(top, key); const descriptor = Reflect.getOwnPropertyDescriptor(top, key);
if (!descriptor if (!descriptor
|| typeof descriptor.value !== "function") || (typeof descriptor.value !== "function"
&& !descriptor.get && !descriptor.set))
continue; continue;
const body = descriptor.value.toString(); if (isNativeDescriptor(descriptor))
if (nativePattern.test(body))
continue; continue;
box[key] = descriptor; box[key] = descriptor;