History

merged 0.80.0-kasen into 0.81.0-kasen, 0.81.0-kasen source ready for build.

various changes, idk, added a whitelist filter to ent script filter

Revofire committed 7 years ago

Commit 47af4061 parent 576f41f7 files · +89 −32Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 7 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+56−7
@@ -252,7 +252,7 @@ QString ScriptEngine::getContext() const { · 251 unmodified lines
252252 return "unknown";
253253 }
254254
255−bool ScriptEngine::isDebugMode() const {
255+bool ScriptEngine::isDebugMode() const {
256256 #if defined(DEBUG)
257257 return true;
258258 #else
@@ -769,6 +769,11 @@ void ScriptEngine::init() { · 510 unmodified lines
769769 #if DEV_BUILD || PR_BUILD
770770 registerGlobalObject("StackTest", new StackTestScriptingInterface(this));
771771 #endif
772+
773+ globalObject().setProperty("KALILA", "isWaifu");
774+ globalObject().setProperty("Kute", newFunction([](QScriptContext* context, QScriptEngine* engine) -> QScriptValue {
775+ return context->argument(0).toString().toLower() == "kalila" ? true : false;
776+ }));
772777 }
773778
774779 void ScriptEngine::registerValue(const QString& valueName, QScriptValue value) {
@@ -2252,6 +2257,7 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co · 1477 unmodified lines
22522257 BaseScriptEngine sandbox;
22532258 sandbox.setProcessEventsInterval(SANDBOX_TIMEOUT);
22542259 QScriptValue testConstructor, exception;
2260+ if (atoi(getenv("UNSAFE_ENTITY_SCRIPTS") ? getenv("UNSAFE_ENTITY_SCRIPTS") : "0"))
22552261 {
22562262 QTimer timeout;
22572263 timeout.setSingleShot(true);
@@ -2274,13 +2280,57 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co · 16 unmodified lines
22742280 exception = testConstructor;
22752281 }
22762282 }
2283+ else {
2284+ // IF YOU FUCK UP, DELETE FROM HERE TO...
2285+ QList<QString> safeURLS = { "https://animedeck.com/", "http://animedeck.com/" };
2286+ safeURLS += qEnvironmentVariable("EXTRA_WHITELIST").split(QRegExp("\\s*,\\s*"), QString::SkipEmptyParts);
2287+
2288+ bool isInWhitelist = false; // assume unsafe
2289+ for (const auto& str : safeURLS) {
2290+ // qDebug() << "CHECKING" << entityID.toString() << scriptOrURL << "AGAINST" << str;
2291+ qDebug() << "SCRIPTOURL STARTSWITH" << scriptOrURL << "TESTING AGAINST" << str << "RESULTS IN"
2292+ << scriptOrURL.startsWith(str);
2293+ if (scriptOrURL.startsWith(str)) {
2294+ isInWhitelist = true;
2295+ break; // bail early since we found a match
2296+ }
2297+ }
2298+ if (!isInWhitelist) {
2299+ qDebug() << "(disabled entity script)" << entityID.toString() << scriptOrURL;
2300+ exception = makeError("UNSAFE_ENTITY_SCRIPTS == 0");
2301+ } else {
2302+ QTimer timeout;
2303+ timeout.setSingleShot(true);
2304+ timeout.start(SANDBOX_TIMEOUT);
2305+ connect(&timeout, &QTimer::timeout, [=, &sandbox] {
2306+ qCDebug(scriptengine) << "ScriptEngine::entityScriptContentAvailable timeout";
2307+
2308+ // Guard against infinite loops and non-performant code
2309+ sandbox.raiseException(
2310+ sandbox.makeError(QString("Timed out (entity constructors are limited to %1ms)").arg(SANDBOX_TIMEOUT)));
2311+ });
2312+
2313+ testConstructor = sandbox.evaluate(program);
2314+
2315+ if (sandbox.hasUncaughtException()) {
2316+ exception = sandbox.cloneUncaughtException(QString("(preflight %1)").arg(entityID.toString()));
2317+ sandbox.clearExceptions();
2318+ } else if (testConstructor.isError()) {
2319+ exception = testConstructor;
2320+ }
2321+ }
2322+ // DELETE UP TO HERE, THEN UNCOMMENT BELOW.
2323+
2324+ // qDebug() << "(disabled entity script)" << entityID.toString() << scriptOrURL;
2325+ // exception = makeError("UNSAFE_ENTITY_SCRIPTS == 0");
2326+ }
22772327
22782328 if (exception.isError()) {
2279− // create a local copy using makeError to decouple from the sandbox engine
2280− exception = makeError(exception);
2281− setError(formatException(exception, _enableExtendedJSExceptions.get()), EntityScriptStatus::ERROR_RUNNING_SCRIPT);
2282− emit unhandledException(exception);
2283− return;
2329+ // create a local copy using makeError to decouple from the sandbox engine
2330+ exception = makeError(exception);
2331+ setError(formatException(exception, _enableExtendedJSExceptions.get()), EntityScriptStatus::ERROR_RUNNING_SCRIPT);
2332+ emit unhandledException(exception);
2333+ return;
22842334 }
22852335
22862336 // CONSTRUCTOR VIABILITY
@@ -2648,4 +2698,3 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 361 unmodified lines
26482698 }
26492699 }
26502700 }
2651−