mirror of
https://github.com/gnh1201/welsonjs.git
synced 2026-04-18 18:18:42 +00:00
Rename global store to __WJS__ and adjust serialization
Rename the global store identifier from __WJ_STORE to __WJS__ throughout JsSerializer.cs (store access in load/unload/set/get and EnsureStore initialization). Also change value retrieval to only JSON.stringify objects and use String(v) for primitives to avoid wrapping non-objects in JSON. Preserve the Object.create(null) store initialization to prevent prototype pollution.
This commit is contained in:
parent
362367f473
commit
c16603af42
|
|
@ -44,7 +44,7 @@ namespace WelsonJS.Launcher
|
||||||
// Create slot and parse
|
// Create slot and parse
|
||||||
// Using Object.create(null) for a clean dictionary without prototype.
|
// Using Object.create(null) for a clean dictionary without prototype.
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append("(function(){var S=globalThis.__WJ_STORE;");
|
sb.Append("(function(){var S=globalThis.__WJS__;");
|
||||||
sb.Append("S[").Append(id.ToString(CultureInfo.InvariantCulture)).Append("]=JSON.parse(").Append(Q(json)).Append(");");
|
sb.Append("S[").Append(id.ToString(CultureInfo.InvariantCulture)).Append("]=JSON.parse(").Append(Q(json)).Append(");");
|
||||||
sb.Append("return '1';})()");
|
sb.Append("return '1';})()");
|
||||||
string r = _core.EvaluateToString(sb.ToString());
|
string r = _core.EvaluateToString(sb.ToString());
|
||||||
|
|
@ -59,7 +59,7 @@ namespace WelsonJS.Launcher
|
||||||
public void Unload(int id)
|
public void Unload(int id)
|
||||||
{
|
{
|
||||||
EnsureStore();
|
EnsureStore();
|
||||||
string script = "(function(){var S=globalThis.__WJ_STORE; delete S[" + id.ToString(CultureInfo.InvariantCulture) + "]; return '1';})()";
|
string script = "(function(){var S=globalThis.__WJS__; delete S[" + id.ToString(CultureInfo.InvariantCulture) + "]; return '1';})()";
|
||||||
_core.EvaluateToString(script);
|
_core.EvaluateToString(script);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,7 +70,7 @@ namespace WelsonJS.Launcher
|
||||||
{
|
{
|
||||||
if (json == null) throw new ArgumentNullException("json");
|
if (json == null) throw new ArgumentNullException("json");
|
||||||
EnsureStore();
|
EnsureStore();
|
||||||
string script = "(function(){var S=globalThis.__WJ_STORE; S[" + id.ToString(CultureInfo.InvariantCulture) + "]=JSON.parse(" + Q(json) + "); return '1';})()";
|
string script = "(function(){var S=globalThis.__WJS__; S[" + id.ToString(CultureInfo.InvariantCulture) + "]=JSON.parse(" + Q(json) + "); return '1';})()";
|
||||||
_core.EvaluateToString(script);
|
_core.EvaluateToString(script);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,7 +81,7 @@ namespace WelsonJS.Launcher
|
||||||
{
|
{
|
||||||
EnsureStore();
|
EnsureStore();
|
||||||
space = Clamp(space, 0, 10);
|
space = Clamp(space, 0, 10);
|
||||||
string script = "(function(){var v=globalThis.__WJ_STORE[" + id.ToString(CultureInfo.InvariantCulture) + "]; return JSON.stringify(v,null," + space.ToString(CultureInfo.InvariantCulture) + ");})()";
|
string script = "(function(){var v=globalThis.__WJS__[" + id.ToString(CultureInfo.InvariantCulture) + "]; return JSON.stringify(v,null," + space.ToString(CultureInfo.InvariantCulture) + ");})()";
|
||||||
return _core.EvaluateToString(script);
|
return _core.EvaluateToString(script);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,13 +96,13 @@ namespace WelsonJS.Launcher
|
||||||
string jsPath = BuildJsPath(path);
|
string jsPath = BuildJsPath(path);
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append("(function(){var v=globalThis.__WJ_STORE[")
|
sb.Append("(function(){var v=globalThis.__WJS__[")
|
||||||
.Append(id.ToString(CultureInfo.InvariantCulture))
|
.Append(id.ToString(CultureInfo.InvariantCulture))
|
||||||
.Append("];var p=").Append(jsPath).Append(";");
|
.Append("];var p=").Append(jsPath).Append(";");
|
||||||
sb.Append("for(var i=0;i<p.length;i++){var k=p[i];");
|
sb.Append("for(var i=0;i<p.length;i++){var k=p[i];");
|
||||||
sb.Append("if(Array.isArray(v) && typeof k==='number'){ v=v[k]; }");
|
sb.Append("if(Array.isArray(v) && typeof k==='number'){ v=v[k]; }");
|
||||||
sb.Append("else { v=(v==null?null:v[k]); }}");
|
sb.Append("else { v=(v==null?null:v[k]); }}");
|
||||||
sb.Append("return JSON.stringify(v);})()");
|
sb.Append("return (typeof v==='object'?JSON.stringify(v):String(v));})()");
|
||||||
return _core.EvaluateToString(sb.ToString());
|
return _core.EvaluateToString(sb.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -110,11 +110,11 @@ namespace WelsonJS.Launcher
|
||||||
private void EnsureStore()
|
private void EnsureStore()
|
||||||
{
|
{
|
||||||
if (_storeReady) return;
|
if (_storeReady) return;
|
||||||
// Create a single global dictionary: globalThis.__WJ_STORE
|
// Create a single global dictionary: globalThis.__WJS__
|
||||||
// Object.create(null) prevents prototype pollution and accidental hits on built-ins.
|
// Object.create(null) prevents prototype pollution and accidental hits on built-ins.
|
||||||
string script =
|
string script =
|
||||||
"(function(){var g=globalThis||this;" +
|
"(function(){var g=globalThis||this;" +
|
||||||
"if(!g.__WJ_STORE){Object.defineProperty(g,'__WJ_STORE',{value:Object.create(null),writable:false,enumerable:false,configurable:false});}" +
|
"if(!g.__WJS__){Object.defineProperty(g,'__WJS__',{value:Object.create(null),writable:false,enumerable:false,configurable:false});}" +
|
||||||
"return '1';})()";
|
"return '1';})()";
|
||||||
string r = _core.EvaluateToString(script);
|
string r = _core.EvaluateToString(script);
|
||||||
_storeReady = (r == "1");
|
_storeReady = (r == "1");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user