xypriss 9.14.30 → 9.14.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +411 -130
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +409 -128
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.ts +33 -19
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -15,10 +15,11 @@ var node_worker_threads = require('node:worker_threads');
|
|
|
15
15
|
var os = require('node:os');
|
|
16
16
|
var crypto$2 = require('node:crypto');
|
|
17
17
|
var nehoid = require('nehoid');
|
|
18
|
+
var vm = require('node:vm');
|
|
18
19
|
var net$1 = require('net');
|
|
19
20
|
var reliantType = require('reliant-type');
|
|
20
21
|
var events = require('events');
|
|
21
|
-
var vm = require('vm');
|
|
22
|
+
var vm$1 = require('vm');
|
|
22
23
|
var compression = require('xypriss-compression');
|
|
23
24
|
var strulink = require('strulink');
|
|
24
25
|
var promises = require('node:fs/promises');
|
|
@@ -50,7 +51,7 @@ var fs__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
|
50
51
|
var path__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(path$1);
|
|
51
52
|
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs$1);
|
|
52
53
|
var net__namespace = /*#__PURE__*/_interopNamespaceDefault(net);
|
|
53
|
-
var vm__namespace = /*#__PURE__*/_interopNamespaceDefault(vm);
|
|
54
|
+
var vm__namespace = /*#__PURE__*/_interopNamespaceDefault(vm$1);
|
|
54
55
|
|
|
55
56
|
/**
|
|
56
57
|
* Type definitions for Console Interception System
|
|
@@ -1826,25 +1827,39 @@ class XessShieldRequiredError extends Error {
|
|
|
1826
1827
|
/**
|
|
1827
1828
|
* Frozen JavaScript Intrinsics (Primordials)
|
|
1828
1829
|
*
|
|
1829
|
-
* Secures string handling
|
|
1830
|
-
*
|
|
1830
|
+
* Secures string handling, path normalization, JSON serialization, and collections
|
|
1831
|
+
* against prototype pollution and monkey-patching attacks.
|
|
1831
1832
|
*
|
|
1832
1833
|
* @internal
|
|
1833
1834
|
* @see XSA-XESS-015
|
|
1835
|
+
* @see XSA-XESS-020
|
|
1836
|
+
* @see XSA-XESS-021
|
|
1837
|
+
* @see XSA-XESS-022
|
|
1834
1838
|
*/
|
|
1835
1839
|
const _StringIncludes = Function.prototype.call.bind(String.prototype.includes);
|
|
1836
1840
|
const _StringReplace = Function.prototype.call.bind(String.prototype.replace);
|
|
1837
1841
|
const _StringStartsWith = Function.prototype.call.bind(String.prototype.startsWith);
|
|
1842
|
+
const _StringEndsWith = Function.prototype.call.bind(String.prototype.endsWith);
|
|
1838
1843
|
const _StringSlice = Function.prototype.call.bind(String.prototype.slice);
|
|
1839
1844
|
const _StringSubstring = Function.prototype.call.bind(String.prototype.substring);
|
|
1840
1845
|
const _StringIndexOf = Function.prototype.call.bind(String.prototype.indexOf);
|
|
1841
1846
|
const _StringLastIndexOf = Function.prototype.call.bind(String.prototype.lastIndexOf);
|
|
1847
|
+
const _PathResolve = path.resolve.bind(path);
|
|
1848
|
+
const _PathIsAbsolute = path.isAbsolute.bind(path);
|
|
1849
|
+
const _JSONParse = JSON.parse;
|
|
1850
|
+
const _JSONStringify = JSON.stringify;
|
|
1851
|
+
const _Uint8ArrayFill = Function.prototype.call.bind(Uint8Array.prototype.fill);
|
|
1852
|
+
const _WeakMapGet = Function.prototype.call.bind(WeakMap.prototype.get);
|
|
1853
|
+
const _WeakMapSet = Function.prototype.call.bind(WeakMap.prototype.set);
|
|
1854
|
+
Function.prototype.call.bind(WeakMap.prototype.has);
|
|
1855
|
+
Function.prototype.call.bind(WeakMap.prototype.delete);
|
|
1842
1856
|
/**
|
|
1843
1857
|
* Resolved canonical installation directory for the XyPriss engine core.
|
|
1844
1858
|
* Anchoring this path at module load time eliminates heuristic substring bypasses.
|
|
1845
1859
|
*
|
|
1846
1860
|
* @internal
|
|
1847
1861
|
* @see XSA-XESS-018
|
|
1862
|
+
* @see XSA-XESS-026
|
|
1848
1863
|
*/
|
|
1849
1864
|
let ENGINE_CORE_DIR = "";
|
|
1850
1865
|
try {
|
|
@@ -1877,17 +1892,25 @@ catch {
|
|
|
1877
1892
|
*
|
|
1878
1893
|
* @see XSA-XESS-015 - Immune to prototype pollution on String.prototype
|
|
1879
1894
|
* @see XSA-XESS-018 - Heuristic path evaluation hardening anchored on ENGINE_CORE_DIR
|
|
1895
|
+
* @see XSA-XESS-026 - Rejects any node_modules subpath not strictly starting with ENGINE_CORE_DIR
|
|
1880
1896
|
*/
|
|
1881
1897
|
function isEngineCorePath(filePath) {
|
|
1882
1898
|
if (!filePath)
|
|
1883
1899
|
return false;
|
|
1884
1900
|
const normalizedPath = _StringReplace(filePath, /\\/g, "/");
|
|
1901
|
+
// Any path inside node_modules that is NOT inside our resolved ENGINE_CORE_DIR
|
|
1902
|
+
// is an untrusted third-party package and CANNOT be the engine core.
|
|
1903
|
+
if (_StringIncludes(normalizedPath, "/node_modules/")) {
|
|
1904
|
+
if (ENGINE_CORE_DIR && _StringStartsWith(normalizedPath, ENGINE_CORE_DIR)) {
|
|
1905
|
+
return true;
|
|
1906
|
+
}
|
|
1907
|
+
return false;
|
|
1908
|
+
}
|
|
1885
1909
|
if (ENGINE_CORE_DIR && _StringStartsWith(normalizedPath, ENGINE_CORE_DIR)) {
|
|
1886
1910
|
return true;
|
|
1887
1911
|
}
|
|
1888
1912
|
if (_StringIncludes(normalizedPath, "/XyPriss/src/") ||
|
|
1889
|
-
_StringIncludes(normalizedPath, "/XyPriss/dist/")
|
|
1890
|
-
_StringIncludes(normalizedPath, "/node_modules/xypriss/")) {
|
|
1913
|
+
_StringIncludes(normalizedPath, "/XyPriss/dist/")) {
|
|
1891
1914
|
return true;
|
|
1892
1915
|
}
|
|
1893
1916
|
return false;
|
|
@@ -1899,6 +1922,7 @@ function isEngineCorePath(filePath) {
|
|
|
1899
1922
|
*
|
|
1900
1923
|
* @see XSA-XESS-015
|
|
1901
1924
|
* @see XSA-XESS-018
|
|
1925
|
+
* @see XSA-XESS-026
|
|
1902
1926
|
*/
|
|
1903
1927
|
function isCoreFrameworkPath(filePath) {
|
|
1904
1928
|
if (!filePath)
|
|
@@ -2039,13 +2063,18 @@ function isProjectRoot(dir) {
|
|
|
2039
2063
|
*
|
|
2040
2064
|
* @see XSA-XESS-015 - Immune to prototype pollution on String.prototype
|
|
2041
2065
|
* @see XSA-XESS-018 - Anchored path verification
|
|
2066
|
+
* @see XSA-XESS-020 - Immune to path.resolve / path.isAbsolute monkey-patching
|
|
2067
|
+
* @see XSA-XESS-025 - Rejects synthetic VM / eval script paths
|
|
2042
2068
|
*/
|
|
2043
2069
|
function identifyProjectRoot(filePath) {
|
|
2044
2070
|
if (!filePath)
|
|
2045
2071
|
return undefined;
|
|
2046
|
-
if (
|
|
2072
|
+
if (_StringIncludes(filePath, "[vm]") || _StringIncludes(filePath, "[eval]")) {
|
|
2073
|
+
return undefined;
|
|
2074
|
+
}
|
|
2075
|
+
if (!_PathIsAbsolute(filePath)) {
|
|
2047
2076
|
try {
|
|
2048
|
-
filePath =
|
|
2077
|
+
filePath = _PathResolve(filePath);
|
|
2049
2078
|
}
|
|
2050
2079
|
catch {
|
|
2051
2080
|
return undefined;
|
|
@@ -2105,11 +2134,29 @@ function normalizeScriptName(rawScript) {
|
|
|
2105
2134
|
}
|
|
2106
2135
|
return rawScript;
|
|
2107
2136
|
}
|
|
2137
|
+
/**
|
|
2138
|
+
* Checks whether a stack frame belongs to a top-level CLI TypeScript runtime loader (tsx / ts-node).
|
|
2139
|
+
* Strict hierarchy verification prevents nested dependency packages (e.g. evil-pkg/node_modules/tsx)
|
|
2140
|
+
* or arbitrary helper binaries (node_modules/.bin/evil.js) from masquerading as system loaders.
|
|
2141
|
+
*
|
|
2142
|
+
* @internal
|
|
2143
|
+
* @see XSA-XESS-026
|
|
2144
|
+
*/
|
|
2108
2145
|
function isRuntimeLoaderPath(filePath) {
|
|
2109
2146
|
const norm = _StringReplace(filePath, /\\/g, "/");
|
|
2110
|
-
|
|
2147
|
+
// Nested node_modules (e.g. node_modules/evil-pkg/node_modules/...) is ALWAYS an untrusted dependency!
|
|
2148
|
+
const firstNm = _StringIndexOf(norm, "/node_modules/");
|
|
2149
|
+
const lastNm = _StringLastIndexOf(norm, "/node_modules/");
|
|
2150
|
+
if (firstNm !== lastNm) {
|
|
2151
|
+
return false;
|
|
2152
|
+
}
|
|
2153
|
+
if (_StringIncludes(norm, "/node_modules/tsx/") ||
|
|
2111
2154
|
_StringIncludes(norm, "/node_modules/ts-node/") ||
|
|
2112
|
-
|
|
2155
|
+
_StringEndsWith(norm, "/node_modules/.bin/tsx") ||
|
|
2156
|
+
_StringEndsWith(norm, "/node_modules/.bin/ts-node")) {
|
|
2157
|
+
return true;
|
|
2158
|
+
}
|
|
2159
|
+
return false;
|
|
2113
2160
|
}
|
|
2114
2161
|
/**
|
|
2115
2162
|
* Captures raw C++ V8/JSC CallSite frames.
|
|
@@ -2119,9 +2166,11 @@ function isRuntimeLoaderPath(filePath) {
|
|
|
2119
2166
|
* the C++ v8::StackTrace without touching JavaScript prototype methods or `Error.prepareStackTrace`.
|
|
2120
2167
|
* 2. Falls back to controlled `Error.prepareStackTrace` with frozen `CallSite.prototype` accessors
|
|
2121
2168
|
* and indexed array iteration, immune to `Array.prototype[Symbol.iterator]` prototype pollution.
|
|
2169
|
+
* 3. Identifies and flags synthetic VM/eval script origins (`[vm]`, `[eval]`).
|
|
2122
2170
|
*
|
|
2123
2171
|
* @internal
|
|
2124
2172
|
* @see XSA-XESS-015
|
|
2173
|
+
* @see XSA-XESS-025
|
|
2125
2174
|
*/
|
|
2126
2175
|
function captureNativeFrames() {
|
|
2127
2176
|
const frames = [];
|
|
@@ -2141,11 +2190,13 @@ function captureNativeFrames() {
|
|
|
2141
2190
|
continue;
|
|
2142
2191
|
const rawScript = s.scriptName || "";
|
|
2143
2192
|
const fnName = s.functionName || "";
|
|
2193
|
+
const isSynthetic = !rawScript && (fnName === "eval" || fnName === "anonymous") ||
|
|
2194
|
+
_StringIncludes(rawScript, "[eval]") ||
|
|
2195
|
+
_StringIncludes(rawScript, "[vm]");
|
|
2144
2196
|
frames[frames.length] = {
|
|
2145
2197
|
filePath: normalizeScriptName(rawScript),
|
|
2146
2198
|
fnName,
|
|
2147
|
-
isEval:
|
|
2148
|
-
(fnName === "eval" || fnName === "anonymous"),
|
|
2199
|
+
isEval: isSynthetic,
|
|
2149
2200
|
};
|
|
2150
2201
|
}
|
|
2151
2202
|
return frames;
|
|
@@ -2196,10 +2247,13 @@ function captureNativeFrames() {
|
|
|
2196
2247
|
: typeof site.isEval === "function"
|
|
2197
2248
|
? Boolean(site.isEval())
|
|
2198
2249
|
: false;
|
|
2250
|
+
const isSynthetic = isEval ||
|
|
2251
|
+
_StringIncludes(rawFile, "[eval]") ||
|
|
2252
|
+
_StringIncludes(rawFile, "[vm]");
|
|
2199
2253
|
frames[frames.length] = {
|
|
2200
2254
|
filePath: normalizeScriptName(rawFile),
|
|
2201
2255
|
fnName,
|
|
2202
|
-
isEval,
|
|
2256
|
+
isEval: isSynthetic,
|
|
2203
2257
|
};
|
|
2204
2258
|
}
|
|
2205
2259
|
catch {
|
|
@@ -2215,6 +2269,7 @@ function captureNativeFrames() {
|
|
|
2215
2269
|
*
|
|
2216
2270
|
* @see XSA-XESS-015 - Immune to prototype pollution on String.prototype
|
|
2217
2271
|
* @see XSA-XESS-018 - Excludes internal configuration proxies from bypassing caller checks
|
|
2272
|
+
* @see XSA-XESS-025 - Strictly blocks synthetic VM and eval execution frames
|
|
2218
2273
|
*/
|
|
2219
2274
|
function isImmediateInternalEngineCall() {
|
|
2220
2275
|
const frames = captureNativeFrames();
|
|
@@ -2225,7 +2280,7 @@ function isImmediateInternalEngineCall() {
|
|
|
2225
2280
|
}
|
|
2226
2281
|
if (!filePath)
|
|
2227
2282
|
continue;
|
|
2228
|
-
if (_StringIncludes(filePath, "[eval]")) {
|
|
2283
|
+
if (_StringIncludes(filePath, "[eval]") || _StringIncludes(filePath, "[vm]")) {
|
|
2229
2284
|
return false;
|
|
2230
2285
|
}
|
|
2231
2286
|
if (_StringStartsWith(filePath, "node:") ||
|
|
@@ -2264,6 +2319,8 @@ function isImmediateInternalEngineCall() {
|
|
|
2264
2319
|
* the attacker's own `node_modules/...` frame remains on the call stack and is detected.
|
|
2265
2320
|
*
|
|
2266
2321
|
* @see XSA-XESS-015 - Immune to prototype pollution on String.prototype and CallSite.prototype
|
|
2322
|
+
* @see XSA-XESS-025 - Detects synthetic node:vm and eval script origins
|
|
2323
|
+
* @see XSA-XESS-026 - Prevents direct impersonation of CLI runtime loaders
|
|
2267
2324
|
*/
|
|
2268
2325
|
function getVerifiedCallerInfo() {
|
|
2269
2326
|
const frames = captureNativeFrames();
|
|
@@ -2271,7 +2328,9 @@ function getVerifiedCallerInfo() {
|
|
|
2271
2328
|
let immediateCallerFile = "";
|
|
2272
2329
|
for (let i = 0; i < frames.length; i++) {
|
|
2273
2330
|
const { filePath, isEval } = frames[i];
|
|
2274
|
-
if (isEval
|
|
2331
|
+
if (isEval ||
|
|
2332
|
+
_StringIncludes(filePath, "[eval]") ||
|
|
2333
|
+
_StringIncludes(filePath, "[vm]")) {
|
|
2275
2334
|
return {
|
|
2276
2335
|
root: undefined,
|
|
2277
2336
|
isUntrusted: true,
|
|
@@ -2280,13 +2339,6 @@ function getVerifiedCallerInfo() {
|
|
|
2280
2339
|
}
|
|
2281
2340
|
if (!filePath)
|
|
2282
2341
|
continue;
|
|
2283
|
-
if (_StringIncludes(filePath, "[eval]")) {
|
|
2284
|
-
return {
|
|
2285
|
-
root: undefined,
|
|
2286
|
-
isUntrusted: true,
|
|
2287
|
-
callerFile: filePath || "<eval>",
|
|
2288
|
-
};
|
|
2289
|
-
}
|
|
2290
2342
|
if (_StringStartsWith(filePath, "node:") ||
|
|
2291
2343
|
_StringStartsWith(filePath, "bun:") ||
|
|
2292
2344
|
filePath === "native") {
|
|
@@ -2296,8 +2348,11 @@ function getVerifiedCallerInfo() {
|
|
|
2296
2348
|
if (isEngineCorePath(filePath)) {
|
|
2297
2349
|
continue;
|
|
2298
2350
|
}
|
|
2299
|
-
// Skip CLI TypeScript loaders (tsx / ts-node) at the bottom of the stack
|
|
2300
|
-
if
|
|
2351
|
+
// Skip genuine CLI TypeScript loaders (tsx / ts-node) at the bottom of the stack
|
|
2352
|
+
// ONLY if an application caller has already been established above in the call chain.
|
|
2353
|
+
// A loader can never be the direct caller of restricted APIs!
|
|
2354
|
+
// @see XSA-XESS-026
|
|
2355
|
+
if (immediateCallerRoot && isRuntimeLoaderPath(filePath)) {
|
|
2301
2356
|
continue;
|
|
2302
2357
|
}
|
|
2303
2358
|
const root = identifyProjectRoot(filePath);
|
|
@@ -2449,6 +2504,23 @@ function verifyPluginContract(pluginRoot, pluginName) {
|
|
|
2449
2504
|
* ```
|
|
2450
2505
|
*/
|
|
2451
2506
|
class ConfigurationManager {
|
|
2507
|
+
/**
|
|
2508
|
+
* @see XSA-XESS-024 Prevents direct access to server configuration object
|
|
2509
|
+
*/
|
|
2510
|
+
get config() {
|
|
2511
|
+
const caller = getVerifiedCallerInfo();
|
|
2512
|
+
if (caller.isUntrusted) {
|
|
2513
|
+
return {};
|
|
2514
|
+
}
|
|
2515
|
+
return this._config;
|
|
2516
|
+
}
|
|
2517
|
+
set config(val) {
|
|
2518
|
+
const caller = getVerifiedCallerInfo();
|
|
2519
|
+
if (caller.isUntrusted) {
|
|
2520
|
+
return;
|
|
2521
|
+
}
|
|
2522
|
+
this._config = val;
|
|
2523
|
+
}
|
|
2452
2524
|
/**
|
|
2453
2525
|
* Private constructor to enforce singleton pattern
|
|
2454
2526
|
* Initializes with default configuration
|
|
@@ -2456,16 +2528,24 @@ class ConfigurationManager {
|
|
|
2456
2528
|
constructor() {
|
|
2457
2529
|
this.initialized = false;
|
|
2458
2530
|
// Initialize with default configuration
|
|
2459
|
-
this.
|
|
2531
|
+
this._config = { ...DEFAULT_OPTIONS };
|
|
2532
|
+
}
|
|
2533
|
+
static getOrCreateInstance() {
|
|
2534
|
+
if (!ConfigurationManager.instance) {
|
|
2535
|
+
ConfigurationManager.instance = new ConfigurationManager();
|
|
2536
|
+
}
|
|
2537
|
+
return ConfigurationManager.instance;
|
|
2460
2538
|
}
|
|
2461
2539
|
/**
|
|
2462
2540
|
* Get the singleton instance
|
|
2541
|
+
* @see XSA-XESS-024 Caller validation for ConfigurationManager singleton access
|
|
2463
2542
|
*/
|
|
2464
2543
|
static getInstance() {
|
|
2465
|
-
|
|
2466
|
-
|
|
2544
|
+
const caller = getVerifiedCallerInfo();
|
|
2545
|
+
if (caller.isUntrusted) {
|
|
2546
|
+
return undefined;
|
|
2467
2547
|
}
|
|
2468
|
-
return ConfigurationManager.
|
|
2548
|
+
return ConfigurationManager.getOrCreateInstance();
|
|
2469
2549
|
}
|
|
2470
2550
|
/**
|
|
2471
2551
|
* Set the entire configuration
|
|
@@ -2477,11 +2557,11 @@ class ConfigurationManager {
|
|
|
2477
2557
|
if (caller.isUntrusted) {
|
|
2478
2558
|
return;
|
|
2479
2559
|
}
|
|
2480
|
-
const instance = ConfigurationManager.
|
|
2481
|
-
instance.
|
|
2560
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2561
|
+
instance._config = ConfigurationManager.deepMerge({ ...DEFAULT_OPTIONS }, config);
|
|
2482
2562
|
// Strict validation for XEMS if persistence is enabled
|
|
2483
|
-
ConfigurationManager.validateXemsConfig(instance.
|
|
2484
|
-
ConfigurationManager.validateGeneralConfig(instance.
|
|
2563
|
+
ConfigurationManager.validateXemsConfig(instance._config);
|
|
2564
|
+
ConfigurationManager.validateGeneralConfig(instance._config);
|
|
2485
2565
|
instance.initialized = true;
|
|
2486
2566
|
}
|
|
2487
2567
|
/**
|
|
@@ -2495,8 +2575,8 @@ class ConfigurationManager {
|
|
|
2495
2575
|
if (caller.isUntrusted) {
|
|
2496
2576
|
return undefined;
|
|
2497
2577
|
}
|
|
2498
|
-
const instance = ConfigurationManager.
|
|
2499
|
-
return instance.
|
|
2578
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2579
|
+
return instance._config[key];
|
|
2500
2580
|
}
|
|
2501
2581
|
/**
|
|
2502
2582
|
* Get the entire configuration object
|
|
@@ -2508,8 +2588,8 @@ class ConfigurationManager {
|
|
|
2508
2588
|
if (caller.isUntrusted) {
|
|
2509
2589
|
return {};
|
|
2510
2590
|
}
|
|
2511
|
-
const instance = ConfigurationManager.
|
|
2512
|
-
return { ...instance.
|
|
2591
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2592
|
+
return { ...instance._config };
|
|
2513
2593
|
}
|
|
2514
2594
|
/**
|
|
2515
2595
|
* Update a specific XyPriss configuration section (deep merge)
|
|
@@ -2522,24 +2602,24 @@ class ConfigurationManager {
|
|
|
2522
2602
|
if (caller.isUntrusted) {
|
|
2523
2603
|
return;
|
|
2524
2604
|
}
|
|
2525
|
-
const instance = ConfigurationManager.
|
|
2526
|
-
const currentValue = instance.
|
|
2605
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2606
|
+
const currentValue = instance._config[key];
|
|
2527
2607
|
// Deep merge the new value with the existing value
|
|
2528
2608
|
if (typeof currentValue === "object" &&
|
|
2529
2609
|
currentValue !== null &&
|
|
2530
2610
|
!Array.isArray(currentValue)) {
|
|
2531
|
-
instance.
|
|
2611
|
+
instance._config[key] = {
|
|
2532
2612
|
...currentValue,
|
|
2533
2613
|
...value,
|
|
2534
2614
|
};
|
|
2535
2615
|
}
|
|
2536
2616
|
else {
|
|
2537
2617
|
// For non-object values, just replace
|
|
2538
|
-
instance.
|
|
2618
|
+
instance._config[key] = value;
|
|
2539
2619
|
}
|
|
2540
2620
|
// Validate XEMS if it was part of the update
|
|
2541
2621
|
if (key === "server") {
|
|
2542
|
-
ConfigurationManager.validateXemsConfig(instance.
|
|
2622
|
+
ConfigurationManager.validateXemsConfig(instance._config);
|
|
2543
2623
|
}
|
|
2544
2624
|
}
|
|
2545
2625
|
/**
|
|
@@ -2608,17 +2688,21 @@ class ConfigurationManager {
|
|
|
2608
2688
|
* @param config - Partial configuration to merge
|
|
2609
2689
|
*/
|
|
2610
2690
|
static merge(config) {
|
|
2611
|
-
const
|
|
2691
|
+
const caller = getVerifiedCallerInfo();
|
|
2692
|
+
if (caller.isUntrusted) {
|
|
2693
|
+
return;
|
|
2694
|
+
}
|
|
2695
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2612
2696
|
// Check if the incoming config is marked as immutable
|
|
2613
2697
|
const shouldBeImmutable = config?.__isXyPrissImmutable;
|
|
2614
|
-
instance.
|
|
2698
|
+
instance._config = ConfigurationManager.deepMerge(instance._config, config);
|
|
2615
2699
|
// Strict validation for XEMS
|
|
2616
|
-
ConfigurationManager.validateXemsConfig(instance.
|
|
2617
|
-
ConfigurationManager.validateGeneralConfig(instance.
|
|
2700
|
+
ConfigurationManager.validateXemsConfig(instance._config);
|
|
2701
|
+
ConfigurationManager.validateGeneralConfig(instance._config);
|
|
2618
2702
|
// If it should be immutable, wrap it using the global __const__.vars.make
|
|
2619
2703
|
if (shouldBeImmutable &&
|
|
2620
2704
|
typeof globalThis.__const__ !== "undefined") {
|
|
2621
|
-
instance.
|
|
2705
|
+
instance._config = globalThis.__const__.vars.make(instance._config, "Configs");
|
|
2622
2706
|
}
|
|
2623
2707
|
instance.initialized = true;
|
|
2624
2708
|
}
|
|
@@ -2627,7 +2711,7 @@ class ConfigurationManager {
|
|
|
2627
2711
|
* @returns true if configuration has been set, false otherwise
|
|
2628
2712
|
*/
|
|
2629
2713
|
static isInitialized() {
|
|
2630
|
-
const instance = ConfigurationManager.
|
|
2714
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2631
2715
|
return instance.initialized;
|
|
2632
2716
|
}
|
|
2633
2717
|
/**
|
|
@@ -2635,36 +2719,54 @@ class ConfigurationManager {
|
|
|
2635
2719
|
* Useful for testing or reinitializing
|
|
2636
2720
|
*/
|
|
2637
2721
|
static reset() {
|
|
2638
|
-
const
|
|
2639
|
-
|
|
2722
|
+
const caller = getVerifiedCallerInfo();
|
|
2723
|
+
if (caller.isUntrusted) {
|
|
2724
|
+
return;
|
|
2725
|
+
}
|
|
2726
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2727
|
+
instance._config = { ...DEFAULT_OPTIONS };
|
|
2640
2728
|
instance.initialized = false;
|
|
2641
2729
|
}
|
|
2642
2730
|
/**
|
|
2643
2731
|
* Check if a specific configuration section exists
|
|
2732
|
+
* @see XSA-XESS-024 Caller validation for configuration section existence
|
|
2644
2733
|
* @param key - Configuration key to check
|
|
2645
2734
|
* @returns true if the configuration section exists
|
|
2646
2735
|
*/
|
|
2647
2736
|
static has(key) {
|
|
2648
|
-
const
|
|
2649
|
-
|
|
2737
|
+
const caller = getVerifiedCallerInfo();
|
|
2738
|
+
if (caller.isUntrusted) {
|
|
2739
|
+
return false;
|
|
2740
|
+
}
|
|
2741
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2742
|
+
return key in instance._config && instance._config[key] !== undefined;
|
|
2650
2743
|
}
|
|
2651
2744
|
/**
|
|
2652
2745
|
* Get configuration with a default value if not set
|
|
2746
|
+
* @see XSA-XESS-024 Caller validation for default config retrieval
|
|
2653
2747
|
* @param key - Configuration key
|
|
2654
2748
|
* @param defaultValue - Default value to return if key doesn't exist
|
|
2655
2749
|
* @returns Configuration value or default value
|
|
2656
2750
|
*/
|
|
2657
2751
|
static getOrDefault(key, defaultValue) {
|
|
2658
|
-
const
|
|
2659
|
-
|
|
2752
|
+
const caller = getVerifiedCallerInfo();
|
|
2753
|
+
if (caller.isUntrusted) {
|
|
2754
|
+
return defaultValue;
|
|
2755
|
+
}
|
|
2756
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2757
|
+
return instance._config[key] ?? defaultValue;
|
|
2660
2758
|
}
|
|
2661
2759
|
/**
|
|
2662
2760
|
* Delete a specific configuration section
|
|
2663
2761
|
* @param key - Configuration key to delete
|
|
2664
2762
|
*/
|
|
2665
2763
|
static delete(key) {
|
|
2666
|
-
const
|
|
2667
|
-
|
|
2764
|
+
const caller = getVerifiedCallerInfo();
|
|
2765
|
+
if (caller.isUntrusted) {
|
|
2766
|
+
return;
|
|
2767
|
+
}
|
|
2768
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2769
|
+
delete instance._config[key];
|
|
2668
2770
|
}
|
|
2669
2771
|
/**
|
|
2670
2772
|
* Validates XEMS configuration strictly
|
|
@@ -4936,8 +5038,15 @@ class SynapxSyncBridge {
|
|
|
4936
5038
|
}
|
|
4937
5039
|
/**
|
|
4938
5040
|
* Executes a synchronous RPC call over shared memory and kernel futex.
|
|
5041
|
+
*
|
|
5042
|
+
* @see XSA-XESS-021 In-transit secret protection via frozen JSON/Uint8 primordials
|
|
5043
|
+
* @see XSA-XESS-022 Caller verification to prevent raw SynapxSyncBridge RPC from untrusted callers
|
|
4939
5044
|
*/
|
|
4940
5045
|
callSync(module, action, args = [], opts = {}, timeoutMs = 5000) {
|
|
5046
|
+
const callerInfo = getVerifiedCallerInfo();
|
|
5047
|
+
if (callerInfo.isUntrusted || !isImmediateInternalEngineCall()) {
|
|
5048
|
+
throw new Error("libSynapx: Unauthorized caller for bridge");
|
|
5049
|
+
}
|
|
4941
5050
|
if (!this.isReady || !this.worker) {
|
|
4942
5051
|
// Lazy synchronous initialization if called before explicit init()
|
|
4943
5052
|
try {
|
|
@@ -4955,7 +5064,7 @@ class SynapxSyncBridge {
|
|
|
4955
5064
|
args: stringArgs,
|
|
4956
5065
|
opts: opts || {},
|
|
4957
5066
|
};
|
|
4958
|
-
const serialized = Buffer.from(
|
|
5067
|
+
const serialized = Buffer.from(_JSONStringify(payloadObj));
|
|
4959
5068
|
if (DATA_OFFSET + serialized.length > this.sab.byteLength) {
|
|
4960
5069
|
throw new Error(`libSynapx: Payload exceeds SharedArrayBuffer capacity (${this.sab.byteLength} bytes)`);
|
|
4961
5070
|
}
|
|
@@ -4979,7 +5088,7 @@ class SynapxSyncBridge {
|
|
|
4979
5088
|
Atomics.store(this.int32, IDX_STATUS, STATUS_IDLE);
|
|
4980
5089
|
let parsed;
|
|
4981
5090
|
try {
|
|
4982
|
-
parsed =
|
|
5091
|
+
parsed = _JSONParse(Buffer.from(respBytes).toString("utf8"));
|
|
4983
5092
|
}
|
|
4984
5093
|
catch (e) {
|
|
4985
5094
|
throw new Error(`libSynapx: Failed to decode response JSON: ${e.message}`);
|
|
@@ -4989,8 +5098,10 @@ class SynapxSyncBridge {
|
|
|
4989
5098
|
* Ref: XSA-SYNAPX-001 (SharedArrayBuffer Memory Scrubbing)
|
|
4990
5099
|
* Overwrite shared memory with zeros immediately following response consumption.
|
|
4991
5100
|
* Prevents plaintext credential retention across the 128 MB SharedArrayBuffer lifecycle.
|
|
5101
|
+
*
|
|
5102
|
+
* @see XSA-XESS-021 - Uses frozen _Uint8ArrayFill primordial to prevent interception
|
|
4992
5103
|
*/
|
|
4993
|
-
this.uint8
|
|
5104
|
+
_Uint8ArrayFill(this.uint8, 0, DATA_OFFSET, DATA_OFFSET + totalUsedLen);
|
|
4994
5105
|
Atomics.store(this.int32, IDX_REQ_LEN, 0);
|
|
4995
5106
|
Atomics.store(this.int32, IDX_RESP_LEN, 0);
|
|
4996
5107
|
}
|
|
@@ -6030,7 +6141,7 @@ class BaseApi {
|
|
|
6030
6141
|
* @param runner - The XyPrissRunner used to invoke native XHSC binaries.
|
|
6031
6142
|
*/
|
|
6032
6143
|
constructor(runner) {
|
|
6033
|
-
_runnerStore
|
|
6144
|
+
_WeakMapSet(_runnerStore, this, runner);
|
|
6034
6145
|
}
|
|
6035
6146
|
/**
|
|
6036
6147
|
* Protected accessor for internal engine classes only.
|
|
@@ -6040,7 +6151,7 @@ class BaseApi {
|
|
|
6040
6151
|
if (!isImmediateInternalEngineCall()) {
|
|
6041
6152
|
return undefined;
|
|
6042
6153
|
}
|
|
6043
|
-
return _runnerStore
|
|
6154
|
+
return _WeakMapGet(_runnerStore, this);
|
|
6044
6155
|
}
|
|
6045
6156
|
}
|
|
6046
6157
|
/**
|
|
@@ -7079,23 +7190,33 @@ class TempFileManager {
|
|
|
7079
7190
|
};
|
|
7080
7191
|
}
|
|
7081
7192
|
/**
|
|
7082
|
-
* Remove a single tracked temporary file safely
|
|
7193
|
+
* Remove a single tracked temporary file safely.
|
|
7194
|
+
*
|
|
7195
|
+
* @see XSA-FS-002 Uses direct filesystem deletion to ensure timer-based TTL cleanup succeeds
|
|
7196
|
+
* even when asynchronous event-loop callbacks lack caller stack context.
|
|
7083
7197
|
*/
|
|
7084
7198
|
removeFile(filePath) {
|
|
7085
|
-
const sys = getSysApi$1();
|
|
7086
7199
|
const entry = this.trackedFiles.get(filePath);
|
|
7087
7200
|
if (entry?.timer) {
|
|
7088
7201
|
clearTimeout(entry.timer);
|
|
7089
7202
|
}
|
|
7090
7203
|
this.trackedFiles.delete(filePath);
|
|
7091
7204
|
try {
|
|
7092
|
-
if (
|
|
7093
|
-
|
|
7205
|
+
if (fs$1.existsSync(filePath)) {
|
|
7206
|
+
fs$1.unlinkSync(filePath);
|
|
7094
7207
|
logger.debug("fs", `Cleaned up temp file: ${filePath}`);
|
|
7095
7208
|
return true;
|
|
7096
7209
|
}
|
|
7097
7210
|
}
|
|
7098
7211
|
catch (err) {
|
|
7212
|
+
const sys = getSysApi$1();
|
|
7213
|
+
try {
|
|
7214
|
+
if (sys?.fs.exist(filePath)) {
|
|
7215
|
+
sys.fs.rm(filePath, { force: true });
|
|
7216
|
+
return true;
|
|
7217
|
+
}
|
|
7218
|
+
}
|
|
7219
|
+
catch { }
|
|
7099
7220
|
logger.warn("fs", `Failed to remove temp file ${filePath}: ${err?.message}`);
|
|
7100
7221
|
}
|
|
7101
7222
|
return false;
|
|
@@ -7104,14 +7225,13 @@ class TempFileManager {
|
|
|
7104
7225
|
* Clean up all active tracked temporary files.
|
|
7105
7226
|
*/
|
|
7106
7227
|
cleanupAll() {
|
|
7107
|
-
const sys = getSysApi$1();
|
|
7108
7228
|
let count = 0;
|
|
7109
7229
|
for (const [filePath, entry] of this.trackedFiles.entries()) {
|
|
7110
7230
|
if (entry.timer)
|
|
7111
7231
|
clearTimeout(entry.timer);
|
|
7112
7232
|
try {
|
|
7113
|
-
if (
|
|
7114
|
-
|
|
7233
|
+
if (fs$1.existsSync(filePath)) {
|
|
7234
|
+
fs$1.unlinkSync(filePath);
|
|
7115
7235
|
count++;
|
|
7116
7236
|
}
|
|
7117
7237
|
}
|
|
@@ -7128,12 +7248,11 @@ class TempFileManager {
|
|
|
7128
7248
|
return;
|
|
7129
7249
|
this.exitHooksRegistered = true;
|
|
7130
7250
|
const performExitCleanup = () => {
|
|
7131
|
-
const sys = getSysApi$1();
|
|
7132
7251
|
for (const [filePath, entry] of this.trackedFiles.entries()) {
|
|
7133
7252
|
if (entry.autoCleanupOnExit) {
|
|
7134
7253
|
try {
|
|
7135
|
-
if (
|
|
7136
|
-
|
|
7254
|
+
if (fs$1.existsSync(filePath)) {
|
|
7255
|
+
fs$1.unlinkSync(filePath);
|
|
7137
7256
|
}
|
|
7138
7257
|
}
|
|
7139
7258
|
catch { }
|
|
@@ -7192,8 +7311,16 @@ class FSCore extends FSBase {
|
|
|
7192
7311
|
* const content = __sys__.fs.readSync("/etc/hosts");
|
|
7193
7312
|
*/
|
|
7194
7313
|
this.readSync = (p, options = {}) => {
|
|
7195
|
-
|
|
7196
|
-
|
|
7314
|
+
try {
|
|
7315
|
+
const res = this.runner.runSync("fs", "read", [p, String(Boolean(options.bytes))], options);
|
|
7316
|
+
return res?.data !== undefined ? res.data : res;
|
|
7317
|
+
}
|
|
7318
|
+
catch (err) {
|
|
7319
|
+
if (err?.message?.includes("Access denied: untrusted caller")) {
|
|
7320
|
+
return "";
|
|
7321
|
+
}
|
|
7322
|
+
throw err;
|
|
7323
|
+
}
|
|
7197
7324
|
};
|
|
7198
7325
|
/**
|
|
7199
7326
|
* **High-Performance Read Stream**
|
|
@@ -8969,6 +9096,15 @@ class FSExtended extends FSWatch {
|
|
|
8969
9096
|
class FSApi extends FSExtended {
|
|
8970
9097
|
}
|
|
8971
9098
|
|
|
9099
|
+
const _varsStore = new WeakMap();
|
|
9100
|
+
function getVarsMap(api) {
|
|
9101
|
+
let map = _WeakMapGet(_varsStore, api);
|
|
9102
|
+
if (!map) {
|
|
9103
|
+
map = new Map();
|
|
9104
|
+
_WeakMapSet(_varsStore, api, map);
|
|
9105
|
+
}
|
|
9106
|
+
return map;
|
|
9107
|
+
}
|
|
8972
9108
|
/**
|
|
8973
9109
|
* **Dynamic Variables & Configuration API**
|
|
8974
9110
|
*
|
|
@@ -8976,14 +9112,15 @@ class FSApi extends FSExtended {
|
|
|
8976
9112
|
* Protected with Zero-Trust Caller Verification against unauthorized third-party dependencies.
|
|
8977
9113
|
*
|
|
8978
9114
|
* @see XSA-VARS-001
|
|
9115
|
+
* @see XSA-XESS-023
|
|
9116
|
+
* @see XSA-FS-002
|
|
8979
9117
|
*/
|
|
8980
9118
|
class VarsApi extends BaseApi {
|
|
8981
9119
|
get __root__() {
|
|
8982
|
-
return getCallerProjectRoot()
|
|
9120
|
+
return getCallerProjectRoot();
|
|
8983
9121
|
}
|
|
8984
9122
|
constructor(runner) {
|
|
8985
9123
|
super(runner);
|
|
8986
|
-
this.vars = new Map();
|
|
8987
9124
|
// Explicit typed properties for IntelliSense
|
|
8988
9125
|
this.__version__ = "0.0.0";
|
|
8989
9126
|
this.__author__ = "unknown";
|
|
@@ -8993,12 +9130,13 @@ class VarsApi extends BaseApi {
|
|
|
8993
9130
|
this.__alias__ = "app";
|
|
8994
9131
|
this.__port__ = 3000;
|
|
8995
9132
|
this.__PORT__ = 3000;
|
|
9133
|
+
_WeakMapSet(_varsStore, this, new Map());
|
|
8996
9134
|
// ==========================================
|
|
8997
9135
|
// ENTERPRISE IMMUTABILITY SHIELD
|
|
8998
9136
|
// ==========================================
|
|
8999
9137
|
// Lock __root__ so hackers cannot override it via Object.defineProperty
|
|
9000
9138
|
Object.defineProperty(this, "__root__", {
|
|
9001
|
-
get: () => getCallerProjectRoot()
|
|
9139
|
+
get: () => getCallerProjectRoot(),
|
|
9002
9140
|
enumerable: true,
|
|
9003
9141
|
configurable: false,
|
|
9004
9142
|
});
|
|
@@ -9038,7 +9176,8 @@ class VarsApi extends BaseApi {
|
|
|
9038
9176
|
].includes(key)) {
|
|
9039
9177
|
return this[key] ?? defaultValue;
|
|
9040
9178
|
}
|
|
9041
|
-
|
|
9179
|
+
const vars = getVarsMap(this);
|
|
9180
|
+
return vars.has(key) ? vars.get(key) : defaultValue;
|
|
9042
9181
|
}
|
|
9043
9182
|
/**
|
|
9044
9183
|
* **Set Variable**
|
|
@@ -9075,7 +9214,7 @@ class VarsApi extends BaseApi {
|
|
|
9075
9214
|
].includes(key)) {
|
|
9076
9215
|
this[key] = value;
|
|
9077
9216
|
}
|
|
9078
|
-
this.
|
|
9217
|
+
getVarsMap(this).set(key, value);
|
|
9079
9218
|
}
|
|
9080
9219
|
/**
|
|
9081
9220
|
* **Check Existence**
|
|
@@ -9089,7 +9228,7 @@ class VarsApi extends BaseApi {
|
|
|
9089
9228
|
if (caller.isUntrusted) {
|
|
9090
9229
|
return false;
|
|
9091
9230
|
}
|
|
9092
|
-
return key in this || this.
|
|
9231
|
+
return key in this || getVarsMap(this).has(key);
|
|
9093
9232
|
}
|
|
9094
9233
|
/**
|
|
9095
9234
|
* **Remove Variable**
|
|
@@ -9102,7 +9241,7 @@ class VarsApi extends BaseApi {
|
|
|
9102
9241
|
if (caller.isUntrusted) {
|
|
9103
9242
|
return;
|
|
9104
9243
|
}
|
|
9105
|
-
this.
|
|
9244
|
+
getVarsMap(this).delete(key);
|
|
9106
9245
|
}
|
|
9107
9246
|
/**
|
|
9108
9247
|
* **Update Multiple Variables**
|
|
@@ -9147,7 +9286,7 @@ class VarsApi extends BaseApi {
|
|
|
9147
9286
|
"__PORT__",
|
|
9148
9287
|
"__root__",
|
|
9149
9288
|
];
|
|
9150
|
-
return Array.from(new Set([...explicitKeys, ...this.
|
|
9289
|
+
return Array.from(new Set([...explicitKeys, ...getVarsMap(this).keys()]));
|
|
9151
9290
|
}
|
|
9152
9291
|
/**
|
|
9153
9292
|
* **Get All Variables**
|
|
@@ -9183,15 +9322,62 @@ class VarsApi extends BaseApi {
|
|
|
9183
9322
|
* **Clear All**
|
|
9184
9323
|
*/
|
|
9185
9324
|
clear() {
|
|
9186
|
-
this.
|
|
9325
|
+
getVarsMap(this).clear();
|
|
9187
9326
|
}
|
|
9188
9327
|
}
|
|
9189
9328
|
|
|
9190
9329
|
const _envRunnerStore = new WeakMap();
|
|
9191
9330
|
const _sealKeyStore = new WeakMap();
|
|
9331
|
+
const _whitelistStore = new WeakMap();
|
|
9332
|
+
const _whitelistedCacheStore = new WeakMap();
|
|
9192
9333
|
const _shieldedInstances = new WeakSet();
|
|
9193
9334
|
const SHIELD_MARKER = Symbol("xypriss.internal.env.shielded");
|
|
9335
|
+
/**
|
|
9336
|
+
* @see XSA-XESS-022 Prevents prototype exposure of capability seal key
|
|
9337
|
+
*/
|
|
9338
|
+
function getSealKey(api) {
|
|
9339
|
+
let key = _WeakMapGet(_sealKeyStore, api);
|
|
9340
|
+
if (!key) {
|
|
9341
|
+
key = crypto$2.randomBytes(32).toString("hex");
|
|
9342
|
+
_WeakMapSet(_sealKeyStore, api, key);
|
|
9343
|
+
}
|
|
9344
|
+
return key;
|
|
9345
|
+
}
|
|
9346
|
+
/**
|
|
9347
|
+
* @see XSA-XESS-023 Secures whitelisted fields against direct prototype / object property tampering
|
|
9348
|
+
*/
|
|
9349
|
+
function getWhitelist(api) {
|
|
9350
|
+
let wl = _WeakMapGet(_whitelistStore, api);
|
|
9351
|
+
if (!wl) {
|
|
9352
|
+
wl = new Set(api.defaultWhitelist);
|
|
9353
|
+
_WeakMapSet(_whitelistStore, api, wl);
|
|
9354
|
+
}
|
|
9355
|
+
return wl;
|
|
9356
|
+
}
|
|
9357
|
+
function setWhitelist(api, wl) {
|
|
9358
|
+
_WeakMapSet(_whitelistStore, api, wl);
|
|
9359
|
+
}
|
|
9360
|
+
function getWhitelistedCache(api) {
|
|
9361
|
+
let cache = _WeakMapGet(_whitelistedCacheStore, api);
|
|
9362
|
+
if (!cache) {
|
|
9363
|
+
cache = new Map();
|
|
9364
|
+
_WeakMapSet(_whitelistedCacheStore, api, cache);
|
|
9365
|
+
}
|
|
9366
|
+
return cache;
|
|
9367
|
+
}
|
|
9194
9368
|
class EnvApi {
|
|
9369
|
+
/**
|
|
9370
|
+
* Keys that may pass through the Shield to the real `process.env`.
|
|
9371
|
+
*
|
|
9372
|
+
* @see XSA-XESS-023 Prevents external mutation of whitelisted fields Set from node_modules
|
|
9373
|
+
*/
|
|
9374
|
+
get whitelistedFields() {
|
|
9375
|
+
const caller = getVerifiedCallerInfo();
|
|
9376
|
+
if (caller.isUntrusted || !isImmediateInternalEngineCall()) {
|
|
9377
|
+
return new Set();
|
|
9378
|
+
}
|
|
9379
|
+
return getWhitelist(this);
|
|
9380
|
+
}
|
|
9195
9381
|
/**
|
|
9196
9382
|
* @param runner - The XyPrissRunner used to invoke native Go binaries.
|
|
9197
9383
|
* @param mode - The execution mode. Defaults to `"development"`.
|
|
@@ -9245,18 +9431,11 @@ class EnvApi {
|
|
|
9245
9431
|
"TEMP",
|
|
9246
9432
|
"TMP",
|
|
9247
9433
|
]);
|
|
9248
|
-
this
|
|
9249
|
-
/**
|
|
9250
|
-
* In-memory cache for whitelisted environment variables.
|
|
9251
|
-
* Populated directly from the Vault during engine bootstrap, enabling O(1)
|
|
9252
|
-
* synchronous reads from process.env without IPC or caller validation conflicts.
|
|
9253
|
-
*
|
|
9254
|
-
* @see XSA-XESS-019
|
|
9255
|
-
*/
|
|
9256
|
-
this._whitelistedCache = new Map();
|
|
9257
|
-
_envRunnerStore.set(this, runner);
|
|
9434
|
+
_WeakMapSet(_envRunnerStore, this, runner);
|
|
9258
9435
|
const sealKey = crypto$2.randomBytes(32).toString("hex");
|
|
9259
|
-
_sealKeyStore
|
|
9436
|
+
_WeakMapSet(_sealKeyStore, this, sealKey);
|
|
9437
|
+
_WeakMapSet(_whitelistStore, this, new Set(this.defaultWhitelist));
|
|
9438
|
+
_WeakMapSet(_whitelistedCacheStore, this, new Map());
|
|
9260
9439
|
try {
|
|
9261
9440
|
runner.runSync("env", "seal", [sealKey], { __seal__: sealKey, pid: process.pid });
|
|
9262
9441
|
}
|
|
@@ -9265,17 +9444,19 @@ class EnvApi {
|
|
|
9265
9444
|
this.isDynamic = isDynamic;
|
|
9266
9445
|
this.applyShield();
|
|
9267
9446
|
}
|
|
9447
|
+
/**
|
|
9448
|
+
* @see XSA-XESS-022 Prevents seal key theft from untrusted callers
|
|
9449
|
+
*/
|
|
9268
9450
|
getSealKey() {
|
|
9269
|
-
|
|
9270
|
-
if (!
|
|
9271
|
-
|
|
9272
|
-
_sealKeyStore.set(this, key);
|
|
9451
|
+
const caller = getVerifiedCallerInfo();
|
|
9452
|
+
if (caller.isUntrusted || !isImmediateInternalEngineCall()) {
|
|
9453
|
+
return undefined;
|
|
9273
9454
|
}
|
|
9274
|
-
return
|
|
9455
|
+
return getSealKey(this);
|
|
9275
9456
|
}
|
|
9276
9457
|
executeEnvCall(action, args = [], options = {}) {
|
|
9277
|
-
const sealKey =
|
|
9278
|
-
const runner = _envRunnerStore
|
|
9458
|
+
const sealKey = getSealKey(this);
|
|
9459
|
+
const runner = _WeakMapGet(_envRunnerStore, this);
|
|
9279
9460
|
if (!runner) {
|
|
9280
9461
|
throw new EnvStoreError();
|
|
9281
9462
|
}
|
|
@@ -9330,8 +9511,8 @@ class EnvApi {
|
|
|
9330
9511
|
// @see XSA-XESS-017: Never mirror XEMS_*, ENC_*, DOTENV_*, XY_*, or __* into process.env,
|
|
9331
9512
|
// because Bun.env and import.meta.env bypass the Proxy shield and would expose plain secrets.
|
|
9332
9513
|
// @see XSA-XESS-019: Maintain in-memory whitelisted cache for O(1) process.env reads.
|
|
9333
|
-
if (this.
|
|
9334
|
-
this.
|
|
9514
|
+
if (getWhitelist(this).has(key)) {
|
|
9515
|
+
getWhitelistedCache(this).set(key, value);
|
|
9335
9516
|
try {
|
|
9336
9517
|
process.env[key] = value;
|
|
9337
9518
|
}
|
|
@@ -9359,8 +9540,8 @@ class EnvApi {
|
|
|
9359
9540
|
this.executeEnvCall("delete", [key], { root });
|
|
9360
9541
|
// @see XSA-XESS-017
|
|
9361
9542
|
// @see XSA-XESS-019: Invalidate whitelisted cache on delete.
|
|
9362
|
-
if (this.
|
|
9363
|
-
this.
|
|
9543
|
+
if (getWhitelist(this).has(key)) {
|
|
9544
|
+
getWhitelistedCache(this).delete(key);
|
|
9364
9545
|
try {
|
|
9365
9546
|
delete process.env[key];
|
|
9366
9547
|
}
|
|
@@ -9588,17 +9769,28 @@ class EnvApi {
|
|
|
9588
9769
|
return;
|
|
9589
9770
|
if (config.whitelist !== undefined) {
|
|
9590
9771
|
if (config.replaceDefaultWhitelist) {
|
|
9591
|
-
this
|
|
9772
|
+
setWhitelist(this, new Set(config.whitelist));
|
|
9592
9773
|
}
|
|
9593
9774
|
else {
|
|
9594
|
-
this
|
|
9775
|
+
setWhitelist(this, new Set([
|
|
9595
9776
|
...this.defaultWhitelist,
|
|
9596
9777
|
...config.whitelist,
|
|
9597
|
-
]);
|
|
9778
|
+
]));
|
|
9598
9779
|
}
|
|
9599
9780
|
}
|
|
9600
9781
|
// Cache all whitelisted values from the vault during trusted bootstrap
|
|
9601
|
-
this.
|
|
9782
|
+
this._doRefreshWhitelistedCache();
|
|
9783
|
+
}
|
|
9784
|
+
/**
|
|
9785
|
+
* @see XSA-XESS-019
|
|
9786
|
+
* @see XSA-XESS-023 Guarded against untrusted execution
|
|
9787
|
+
*/
|
|
9788
|
+
refreshWhitelistedCache() {
|
|
9789
|
+
const caller = getVerifiedCallerInfo();
|
|
9790
|
+
if (caller.isUntrusted || !isImmediateInternalEngineCall()) {
|
|
9791
|
+
return;
|
|
9792
|
+
}
|
|
9793
|
+
this._doRefreshWhitelistedCache();
|
|
9602
9794
|
}
|
|
9603
9795
|
/**
|
|
9604
9796
|
* Pre-populates the in-memory cache of whitelisted variables from the secure vault
|
|
@@ -9610,15 +9802,17 @@ class EnvApi {
|
|
|
9610
9802
|
* @internal
|
|
9611
9803
|
* @see XSA-XESS-019
|
|
9612
9804
|
*/
|
|
9613
|
-
|
|
9614
|
-
const runner = _envRunnerStore
|
|
9805
|
+
_doRefreshWhitelistedCache() {
|
|
9806
|
+
const runner = _WeakMapGet(_envRunnerStore, this);
|
|
9615
9807
|
const hostRoot = runner ? runner.getRoot() : "";
|
|
9616
|
-
|
|
9808
|
+
const whitelist = getWhitelist(this);
|
|
9809
|
+
const cache = getWhitelistedCache(this);
|
|
9810
|
+
for (const key of whitelist) {
|
|
9617
9811
|
if (!this.defaultWhitelist.has(key) || key === "PORT") {
|
|
9618
9812
|
try {
|
|
9619
9813
|
const res = this.executeEnvCall("get", [key], { root: hostRoot });
|
|
9620
9814
|
if (res?.found && res.value !== undefined) {
|
|
9621
|
-
|
|
9815
|
+
cache.set(key, res.value);
|
|
9622
9816
|
}
|
|
9623
9817
|
}
|
|
9624
9818
|
catch {
|
|
@@ -9646,7 +9840,7 @@ class EnvApi {
|
|
|
9646
9840
|
*/
|
|
9647
9841
|
user() {
|
|
9648
9842
|
try {
|
|
9649
|
-
const runner = _envRunnerStore
|
|
9843
|
+
const runner = _WeakMapGet(_envRunnerStore, this);
|
|
9650
9844
|
if (!runner)
|
|
9651
9845
|
return "";
|
|
9652
9846
|
const result = runner.runSync("sys", "user");
|
|
@@ -9680,7 +9874,7 @@ class EnvApi {
|
|
|
9680
9874
|
if (this.isDynamic && callerInfo.root) {
|
|
9681
9875
|
return callerInfo.root;
|
|
9682
9876
|
}
|
|
9683
|
-
const runner = _envRunnerStore
|
|
9877
|
+
const runner = _WeakMapGet(_envRunnerStore, this);
|
|
9684
9878
|
const hostRoot = runner ? runner.getRoot() : null;
|
|
9685
9879
|
return hostRoot || callerInfo.root || null;
|
|
9686
9880
|
}
|
|
@@ -9855,9 +10049,11 @@ class EnvApi {
|
|
|
9855
10049
|
*
|
|
9856
10050
|
* @see XSA-XESS-019
|
|
9857
10051
|
*/
|
|
9858
|
-
|
|
9859
|
-
|
|
9860
|
-
|
|
10052
|
+
const whitelist = getWhitelist(self);
|
|
10053
|
+
const cache = getWhitelistedCache(self);
|
|
10054
|
+
if (whitelist.has(prop)) {
|
|
10055
|
+
if (cache.has(prop)) {
|
|
10056
|
+
return cache.get(prop);
|
|
9861
10057
|
}
|
|
9862
10058
|
return Reflect.get(target, prop, receiver);
|
|
9863
10059
|
}
|
|
@@ -9940,7 +10136,7 @@ class EnvApi {
|
|
|
9940
10136
|
}
|
|
9941
10137
|
}
|
|
9942
10138
|
// Allowed whitelisted non-critical keys
|
|
9943
|
-
if (self.
|
|
10139
|
+
if (getWhitelist(self).has(key)) {
|
|
9944
10140
|
try {
|
|
9945
10141
|
target[key] = String(value);
|
|
9946
10142
|
}
|
|
@@ -10000,7 +10196,7 @@ class EnvApi {
|
|
|
10000
10196
|
}
|
|
10001
10197
|
return true;
|
|
10002
10198
|
}
|
|
10003
|
-
if (self.
|
|
10199
|
+
if (getWhitelist(self).has(prop)) {
|
|
10004
10200
|
try {
|
|
10005
10201
|
delete target[prop];
|
|
10006
10202
|
}
|
|
@@ -10028,13 +10224,13 @@ class EnvApi {
|
|
|
10028
10224
|
finally {
|
|
10029
10225
|
isInsideShieldCheck = false;
|
|
10030
10226
|
}
|
|
10031
|
-
return [...self
|
|
10227
|
+
return [...getWhitelist(self)];
|
|
10032
10228
|
},
|
|
10033
10229
|
// Align `in` operator behaviour with the restricted key set.
|
|
10034
10230
|
has(target, prop) {
|
|
10035
10231
|
if (typeof prop !== "string")
|
|
10036
10232
|
return false;
|
|
10037
|
-
if (self.
|
|
10233
|
+
if (getWhitelist(self).has(prop))
|
|
10038
10234
|
return true;
|
|
10039
10235
|
if (prop.startsWith("XYPRISS_") ||
|
|
10040
10236
|
prop.startsWith("LIBXESS_") ||
|
|
@@ -16380,9 +16576,15 @@ class XyPrissFS {
|
|
|
16380
16576
|
*
|
|
16381
16577
|
* The absolute path to the project root directory. This is the primary
|
|
16382
16578
|
* resolution anchor for all system operations.
|
|
16579
|
+
*
|
|
16580
|
+
* @see XSA-FS-002 Fail-closed project root resolution for untrusted callers
|
|
16383
16581
|
*/
|
|
16384
16582
|
get __root__() {
|
|
16385
|
-
|
|
16583
|
+
const callerRoot = getCallerProjectRoot();
|
|
16584
|
+
if (!callerRoot) {
|
|
16585
|
+
return undefined;
|
|
16586
|
+
}
|
|
16587
|
+
return callerRoot;
|
|
16386
16588
|
}
|
|
16387
16589
|
/**
|
|
16388
16590
|
* **Initialize System API**
|
|
@@ -16526,6 +16728,76 @@ function createSecurityShield(target, label = "__sys__") {
|
|
|
16526
16728
|
return createProxy(target, label);
|
|
16527
16729
|
}
|
|
16528
16730
|
|
|
16731
|
+
let _vmHardened = false;
|
|
16732
|
+
/**
|
|
16733
|
+
* Hardens `node:vm` to prevent asynchronous stack decoupling and script origin spoofing.
|
|
16734
|
+
*
|
|
16735
|
+
* Any script compiled through `node:vm` APIs (such as `runInThisContext`, `createScript`, or `new vm.Script`)
|
|
16736
|
+
* has its filename/script origin deterministically tagged with `[vm]`.
|
|
16737
|
+
* This enables the zero-trust stack inspector (`getVerifiedCallerInfo`) to detect synthetic VM frames
|
|
16738
|
+
* even after event loop timers have cleared the origin caller frames.
|
|
16739
|
+
*
|
|
16740
|
+
* @internal
|
|
16741
|
+
* @see XSA-XESS-025
|
|
16742
|
+
*/
|
|
16743
|
+
function installVmHardening() {
|
|
16744
|
+
if (_vmHardened)
|
|
16745
|
+
return;
|
|
16746
|
+
_vmHardened = true;
|
|
16747
|
+
try {
|
|
16748
|
+
const wrapOptions = (opts) => {
|
|
16749
|
+
if (typeof opts === "string") {
|
|
16750
|
+
return "[vm] " + opts;
|
|
16751
|
+
}
|
|
16752
|
+
if (opts && typeof opts === "object") {
|
|
16753
|
+
const fname = opts.filename || opts.name;
|
|
16754
|
+
if (fname) {
|
|
16755
|
+
return { ...opts, filename: `[vm] ${fname}` };
|
|
16756
|
+
}
|
|
16757
|
+
return { ...opts, filename: "[vm] <vm-script>" };
|
|
16758
|
+
}
|
|
16759
|
+
return { filename: "[vm] <vm-script>" };
|
|
16760
|
+
};
|
|
16761
|
+
const origRunInThisContext = vm.runInThisContext;
|
|
16762
|
+
if (typeof origRunInThisContext === "function") {
|
|
16763
|
+
vm.runInThisContext = function (code, options) {
|
|
16764
|
+
return origRunInThisContext.call(this, code, wrapOptions(options));
|
|
16765
|
+
};
|
|
16766
|
+
}
|
|
16767
|
+
const origRunInContext = vm.runInContext;
|
|
16768
|
+
if (typeof origRunInContext === "function") {
|
|
16769
|
+
vm.runInContext = function (code, contextifiedObject, options) {
|
|
16770
|
+
return origRunInContext.call(this, code, contextifiedObject, wrapOptions(options));
|
|
16771
|
+
};
|
|
16772
|
+
}
|
|
16773
|
+
const origRunInNewContext = vm.runInNewContext;
|
|
16774
|
+
if (typeof origRunInNewContext === "function") {
|
|
16775
|
+
vm.runInNewContext = function (code, contextObject, options) {
|
|
16776
|
+
return origRunInNewContext.call(this, code, contextObject, wrapOptions(options));
|
|
16777
|
+
};
|
|
16778
|
+
}
|
|
16779
|
+
const origCreateScript = vm.createScript;
|
|
16780
|
+
if (typeof origCreateScript === "function") {
|
|
16781
|
+
vm.createScript = function (code, options) {
|
|
16782
|
+
return origCreateScript.call(this, code, wrapOptions(options));
|
|
16783
|
+
};
|
|
16784
|
+
}
|
|
16785
|
+
const origScript = vm.Script;
|
|
16786
|
+
if (typeof origScript === "function") {
|
|
16787
|
+
const PatchedScript = function (code, options) {
|
|
16788
|
+
return new origScript(code, wrapOptions(options));
|
|
16789
|
+
};
|
|
16790
|
+
PatchedScript.prototype = origScript.prototype;
|
|
16791
|
+
vm.Script = PatchedScript;
|
|
16792
|
+
}
|
|
16793
|
+
}
|
|
16794
|
+
catch {
|
|
16795
|
+
// ignore if vm is not available in environment
|
|
16796
|
+
}
|
|
16797
|
+
}
|
|
16798
|
+
|
|
16799
|
+
// Install VM hardening at engine bootstrap to prevent async stack decoupling
|
|
16800
|
+
installVmHardening();
|
|
16529
16801
|
/**
|
|
16530
16802
|
* **XyPriss System Variables (`__sys__`)**
|
|
16531
16803
|
*
|
|
@@ -16542,8 +16814,15 @@ class XyPrissXHSC extends XyPrissFS {
|
|
|
16542
16814
|
[XY_XHSC_REGISTER_FS](pluginId, instance) {
|
|
16543
16815
|
this._pluginMap.set(pluginId, instance);
|
|
16544
16816
|
}
|
|
16817
|
+
/**
|
|
16818
|
+
* @see XSA-FS-002 Fail-closed project root resolution for untrusted callers
|
|
16819
|
+
*/
|
|
16545
16820
|
get __root__() {
|
|
16546
|
-
|
|
16821
|
+
const callerRoot = getCallerProjectRoot();
|
|
16822
|
+
if (!callerRoot) {
|
|
16823
|
+
return undefined;
|
|
16824
|
+
}
|
|
16825
|
+
return callerRoot;
|
|
16547
16826
|
}
|
|
16548
16827
|
constructor(data = {}) {
|
|
16549
16828
|
const root = data.__root__ || process.cwd();
|
|
@@ -35208,6 +35487,12 @@ class XemsError extends Error {
|
|
|
35208
35487
|
* XEMS Runner (Long-running process manager)
|
|
35209
35488
|
* Manages the persistent Rust process for in-memory storage.
|
|
35210
35489
|
*/
|
|
35490
|
+
/**
|
|
35491
|
+
* Module-level storage for XEMS runners by path.
|
|
35492
|
+
*
|
|
35493
|
+
* @see XSA-XESS-024 Prevents exposing runners and secrets via static class reflection
|
|
35494
|
+
*/
|
|
35495
|
+
const _runnersByPathStore = new Map();
|
|
35211
35496
|
class XemsRunner {
|
|
35212
35497
|
constructor(options = {}) {
|
|
35213
35498
|
this.process = null;
|
|
@@ -35229,7 +35514,7 @@ class XemsRunner {
|
|
|
35229
35514
|
*/
|
|
35230
35515
|
static getInstance(pathStr, options = {}) {
|
|
35231
35516
|
const absPath = path.resolve(process.cwd(), pathStr);
|
|
35232
|
-
const existing =
|
|
35517
|
+
const existing = _runnersByPathStore.get(absPath);
|
|
35233
35518
|
if (existing) {
|
|
35234
35519
|
// Merge options if provided
|
|
35235
35520
|
if (options.secret)
|
|
@@ -35237,7 +35522,7 @@ class XemsRunner {
|
|
|
35237
35522
|
return existing;
|
|
35238
35523
|
}
|
|
35239
35524
|
const runner = new XemsRunner({ ...options, persistPath: pathStr });
|
|
35240
|
-
|
|
35525
|
+
_runnersByPathStore.set(absPath, runner);
|
|
35241
35526
|
return runner;
|
|
35242
35527
|
}
|
|
35243
35528
|
enablePersistence(pathStr, secret, resources) {
|
|
@@ -35255,13 +35540,13 @@ class XemsRunner {
|
|
|
35255
35540
|
}
|
|
35256
35541
|
// Register this runner in the global registry for this path
|
|
35257
35542
|
const absPath = path.resolve(process.cwd(), pathStr);
|
|
35258
|
-
const existing =
|
|
35543
|
+
const existing = _runnersByPathStore.get(absPath);
|
|
35259
35544
|
if (existing && existing !== this) {
|
|
35260
35545
|
this.logger.warn("xems", `Path ${pathStr} is already being managed by another runner. Switching to shared mode.`);
|
|
35261
35546
|
// Transitions this instance to delegate to the existing one if needed?
|
|
35262
35547
|
// Actually, for simplicity, we just ensure WE don't spawn if another one is already there.
|
|
35263
35548
|
}
|
|
35264
|
-
|
|
35549
|
+
_runnersByPathStore.set(absPath, this);
|
|
35265
35550
|
// Avoid redundant restarts if path is the same
|
|
35266
35551
|
if (this.options.persistPath === pathStr && this.process) {
|
|
35267
35552
|
return;
|
|
@@ -35659,10 +35944,6 @@ class XemsRunner {
|
|
|
35659
35944
|
this.initPromise = null;
|
|
35660
35945
|
}
|
|
35661
35946
|
}
|
|
35662
|
-
/**
|
|
35663
|
-
* Enables hardware-bound persistent storage for XEMS.
|
|
35664
|
-
*/
|
|
35665
|
-
XemsRunner.runnersByPath = new Map();
|
|
35666
35947
|
/**
|
|
35667
35948
|
* Contextual wrapper for XEMS operations within a specific sandbox.
|
|
35668
35949
|
* Provides a cleaner, fluent API for advanced operations.
|