xypriss 9.14.29 → 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 +656 -232
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +654 -230
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.ts +76 -40
- 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
|
|
@@ -1823,25 +1824,61 @@ class XessShieldRequiredError extends Error {
|
|
|
1823
1824
|
}
|
|
1824
1825
|
}
|
|
1825
1826
|
|
|
1827
|
+
/**
|
|
1828
|
+
* Frozen JavaScript Intrinsics (Primordials)
|
|
1829
|
+
*
|
|
1830
|
+
* Secures string handling, path normalization, JSON serialization, and collections
|
|
1831
|
+
* against prototype pollution and monkey-patching attacks.
|
|
1832
|
+
*
|
|
1833
|
+
* @internal
|
|
1834
|
+
* @see XSA-XESS-015
|
|
1835
|
+
* @see XSA-XESS-020
|
|
1836
|
+
* @see XSA-XESS-021
|
|
1837
|
+
* @see XSA-XESS-022
|
|
1838
|
+
*/
|
|
1839
|
+
const _StringIncludes = Function.prototype.call.bind(String.prototype.includes);
|
|
1840
|
+
const _StringReplace = Function.prototype.call.bind(String.prototype.replace);
|
|
1841
|
+
const _StringStartsWith = Function.prototype.call.bind(String.prototype.startsWith);
|
|
1842
|
+
const _StringEndsWith = Function.prototype.call.bind(String.prototype.endsWith);
|
|
1843
|
+
const _StringSlice = Function.prototype.call.bind(String.prototype.slice);
|
|
1844
|
+
const _StringSubstring = Function.prototype.call.bind(String.prototype.substring);
|
|
1845
|
+
const _StringIndexOf = Function.prototype.call.bind(String.prototype.indexOf);
|
|
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);
|
|
1826
1856
|
/**
|
|
1827
1857
|
* Resolved canonical installation directory for the XyPriss engine core.
|
|
1828
1858
|
* Anchoring this path at module load time eliminates heuristic substring bypasses.
|
|
1829
1859
|
*
|
|
1830
1860
|
* @internal
|
|
1831
1861
|
* @see XSA-XESS-018
|
|
1862
|
+
* @see XSA-XESS-026
|
|
1832
1863
|
*/
|
|
1833
|
-
let
|
|
1864
|
+
let ENGINE_CORE_DIR = "";
|
|
1834
1865
|
try {
|
|
1866
|
+
let thisFile = "";
|
|
1835
1867
|
if (typeof ({ url: (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href)) }) !== "undefined" && (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href))) {
|
|
1836
|
-
|
|
1837
|
-
|
|
1868
|
+
thisFile = _StringReplace(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href))), /\\/g, "/");
|
|
1869
|
+
}
|
|
1870
|
+
else if (typeof __filename !== "undefined") {
|
|
1871
|
+
thisFile = _StringReplace(__filename, /\\/g, "/");
|
|
1872
|
+
}
|
|
1873
|
+
if (thisFile) {
|
|
1874
|
+
const srcIdx = _StringLastIndexOf(thisFile, "/src/");
|
|
1838
1875
|
if (srcIdx !== -1) {
|
|
1839
|
-
|
|
1876
|
+
ENGINE_CORE_DIR = _StringSlice(thisFile, 0, srcIdx + 1); // root directory with trailing '/'
|
|
1840
1877
|
}
|
|
1841
1878
|
else {
|
|
1842
|
-
const distIdx = thisFile
|
|
1879
|
+
const distIdx = _StringLastIndexOf(thisFile, "/dist/");
|
|
1843
1880
|
if (distIdx !== -1) {
|
|
1844
|
-
|
|
1881
|
+
ENGINE_CORE_DIR = _StringSlice(thisFile, 0, distIdx + 1); // root directory with trailing '/'
|
|
1845
1882
|
}
|
|
1846
1883
|
}
|
|
1847
1884
|
}
|
|
@@ -1853,18 +1890,27 @@ catch {
|
|
|
1853
1890
|
* Checks if a given file path belongs to the deep engine core.
|
|
1854
1891
|
* These files are skipped during stack analysis to find the user/plugin caller.
|
|
1855
1892
|
*
|
|
1856
|
-
* @see XSA-XESS-
|
|
1893
|
+
* @see XSA-XESS-015 - Immune to prototype pollution on String.prototype
|
|
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
|
|
1857
1896
|
*/
|
|
1858
1897
|
function isEngineCorePath(filePath) {
|
|
1859
1898
|
if (!filePath)
|
|
1860
1899
|
return false;
|
|
1861
|
-
const normalizedPath = filePath
|
|
1862
|
-
|
|
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
|
+
}
|
|
1909
|
+
if (ENGINE_CORE_DIR && _StringStartsWith(normalizedPath, ENGINE_CORE_DIR)) {
|
|
1863
1910
|
return true;
|
|
1864
1911
|
}
|
|
1865
|
-
if (normalizedPath
|
|
1866
|
-
normalizedPath
|
|
1867
|
-
normalizedPath.includes("/node_modules/xypriss/")) {
|
|
1912
|
+
if (_StringIncludes(normalizedPath, "/XyPriss/src/") ||
|
|
1913
|
+
_StringIncludes(normalizedPath, "/XyPriss/dist/")) {
|
|
1868
1914
|
return true;
|
|
1869
1915
|
}
|
|
1870
1916
|
return false;
|
|
@@ -1874,22 +1920,47 @@ function isEngineCorePath(filePath) {
|
|
|
1874
1920
|
* or if it comes from an external plugin / user space.
|
|
1875
1921
|
* This is used for SECURITY authorization.
|
|
1876
1922
|
*
|
|
1923
|
+
* @see XSA-XESS-015
|
|
1877
1924
|
* @see XSA-XESS-018
|
|
1925
|
+
* @see XSA-XESS-026
|
|
1878
1926
|
*/
|
|
1879
1927
|
function isCoreFrameworkPath(filePath) {
|
|
1880
1928
|
if (!filePath)
|
|
1881
1929
|
return false;
|
|
1882
|
-
const normalizedPath = filePath
|
|
1930
|
+
const normalizedPath = _StringReplace(filePath, /\\/g, "/");
|
|
1883
1931
|
// Authorize trusted internal mods within the framework repository
|
|
1884
|
-
if (normalizedPath
|
|
1932
|
+
if (_StringIncludes(normalizedPath, "/XyPriss/mods/"))
|
|
1885
1933
|
return true;
|
|
1886
1934
|
// Exclude other plugins even if they contain 'src' or are named 'xypriss-something'
|
|
1887
|
-
if (normalizedPath
|
|
1935
|
+
if (_StringIncludes(normalizedPath, "/mods/"))
|
|
1888
1936
|
return false;
|
|
1889
1937
|
// Engine Core is always trusted
|
|
1890
1938
|
return isEngineCorePath(filePath);
|
|
1891
1939
|
}
|
|
1892
1940
|
|
|
1941
|
+
/***************************************************************************
|
|
1942
|
+
* XyPrissJS - Fast And Secure
|
|
1943
|
+
*
|
|
1944
|
+
* @author Nehonix
|
|
1945
|
+
* @license Nehonix OSL (NOSL)
|
|
1946
|
+
*
|
|
1947
|
+
* Copyright (c) 2025 Nehonix. All rights reserved.
|
|
1948
|
+
*
|
|
1949
|
+
* This License governs the use, modification, and distribution of software
|
|
1950
|
+
* provided by NEHONIX under its open source projects.
|
|
1951
|
+
* NEHONIX is committed to fostering collaborative innovation while strictly
|
|
1952
|
+
* protecting its intellectual property rights.
|
|
1953
|
+
* Violation of any term of this License will result in immediate termination of all granted rights
|
|
1954
|
+
* and may subject the violator to legal action.
|
|
1955
|
+
*
|
|
1956
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
1957
|
+
* INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
|
|
1958
|
+
* AND NON-INFRINGEMENT.
|
|
1959
|
+
* IN NO EVENT SHALL NEHONIX BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
1960
|
+
* OR CONSEQUENTIAL DAMAGES ARISING FROM THE USE OR INABILITY TO USE THE SOFTWARE,
|
|
1961
|
+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
|
1962
|
+
*
|
|
1963
|
+
***************************************************************************** */
|
|
1893
1964
|
/**
|
|
1894
1965
|
* Frozen JavaScript Intrinsics (Primordials)
|
|
1895
1966
|
*
|
|
@@ -1900,12 +1971,44 @@ function isCoreFrameworkPath(filePath) {
|
|
|
1900
1971
|
* @see XSA-XESS-015
|
|
1901
1972
|
*/
|
|
1902
1973
|
const _ArrayIsArray = Array.isArray;
|
|
1903
|
-
|
|
1904
|
-
//
|
|
1905
|
-
|
|
1974
|
+
const _ObjectEntries = Object.entries;
|
|
1975
|
+
// Freeze CallSite accessors at early bootstrap to prevent prototype poisoning
|
|
1976
|
+
let _CallSiteGetFileName = null;
|
|
1977
|
+
let _CallSiteGetFunctionName = null;
|
|
1978
|
+
let _CallSiteIsEval = null;
|
|
1979
|
+
try {
|
|
1980
|
+
const origPrepare = Error.prepareStackTrace;
|
|
1981
|
+
Error.prepareStackTrace = (_err, sites) => {
|
|
1982
|
+
if (_ArrayIsArray(sites) && sites.length > 0) {
|
|
1983
|
+
const proto = Object.getPrototypeOf(sites[0]);
|
|
1984
|
+
if (proto) {
|
|
1985
|
+
if (typeof proto.getFileName === "function") {
|
|
1986
|
+
_CallSiteGetFileName = Function.prototype.call.bind(proto.getFileName);
|
|
1987
|
+
}
|
|
1988
|
+
if (typeof proto.getFunctionName === "function") {
|
|
1989
|
+
_CallSiteGetFunctionName = Function.prototype.call.bind(proto.getFunctionName);
|
|
1990
|
+
}
|
|
1991
|
+
if (typeof proto.isEval === "function") {
|
|
1992
|
+
_CallSiteIsEval = Function.prototype.call.bind(proto.isEval);
|
|
1993
|
+
}
|
|
1994
|
+
try {
|
|
1995
|
+
Object.freeze(proto);
|
|
1996
|
+
}
|
|
1997
|
+
catch { }
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
return "";
|
|
2001
|
+
};
|
|
2002
|
+
const e = new Error();
|
|
2003
|
+
void e.stack;
|
|
2004
|
+
Error.prepareStackTrace = origPrepare;
|
|
2005
|
+
}
|
|
2006
|
+
catch {
|
|
2007
|
+
// ignore
|
|
2008
|
+
}
|
|
1906
2009
|
// Capture native V8 C++ CallSite bindings at module bootstrap before any third-party code executes.
|
|
1907
2010
|
// `util.getCallSites` invokes v8::StackTrace::CurrentStackTrace directly in C++, completely
|
|
1908
|
-
// bypassing `Error.prepareStackTrace` overrides and ignoring `//# sourceURL` directives.
|
|
2011
|
+
// bypassing `Error.prepareStackTrace` overrides, CallSite prototype manipulation, and ignoring `//# sourceURL` directives.
|
|
1909
2012
|
const _nativeGetCallSites = typeof util.getCallSites === "function"
|
|
1910
2013
|
? util.getCallSites.bind(util)
|
|
1911
2014
|
: null;
|
|
@@ -1957,49 +2060,63 @@ function isProjectRoot(dir) {
|
|
|
1957
2060
|
}
|
|
1958
2061
|
/**
|
|
1959
2062
|
* Identifies the project or plugin root for a given caller path using xfpm metadata.
|
|
2063
|
+
*
|
|
2064
|
+
* @see XSA-XESS-015 - Immune to prototype pollution on String.prototype
|
|
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
|
|
1960
2068
|
*/
|
|
1961
2069
|
function identifyProjectRoot(filePath) {
|
|
1962
2070
|
if (!filePath)
|
|
1963
2071
|
return undefined;
|
|
1964
|
-
if (
|
|
2072
|
+
if (_StringIncludes(filePath, "[vm]") || _StringIncludes(filePath, "[eval]")) {
|
|
2073
|
+
return undefined;
|
|
2074
|
+
}
|
|
2075
|
+
if (!_PathIsAbsolute(filePath)) {
|
|
1965
2076
|
try {
|
|
1966
|
-
filePath =
|
|
2077
|
+
filePath = _PathResolve(filePath);
|
|
1967
2078
|
}
|
|
1968
2079
|
catch {
|
|
1969
2080
|
return undefined;
|
|
1970
2081
|
}
|
|
1971
2082
|
}
|
|
1972
|
-
const normalized = filePath
|
|
1973
|
-
const primaryRoot = (process.env.XYPRISS_PROJECT_ROOT || process.cwd()
|
|
2083
|
+
const normalized = _StringReplace(filePath, /\\/g, "/");
|
|
2084
|
+
const primaryRoot = _StringReplace(process.env.XYPRISS_PROJECT_ROOT || process.cwd(), /\\/g, "/");
|
|
1974
2085
|
// 1. Direct O(1) match against verified plugins registered by xfpm
|
|
1975
2086
|
const manifest = getPluginsManifest();
|
|
1976
2087
|
if (manifest?.roots) {
|
|
1977
|
-
|
|
1978
|
-
|
|
2088
|
+
const rootEntries = _ObjectEntries(manifest.roots);
|
|
2089
|
+
for (let i = 0; i < rootEntries.length; i++) {
|
|
2090
|
+
const [pluginRoot] = rootEntries[i];
|
|
2091
|
+
const normPluginRoot = _StringReplace(pluginRoot, /\\/g, "/");
|
|
1979
2092
|
if (normalized === normPluginRoot ||
|
|
1980
|
-
normalized
|
|
2093
|
+
_StringStartsWith(normalized, normPluginRoot + "/")) {
|
|
1981
2094
|
return pluginRoot;
|
|
1982
2095
|
}
|
|
1983
2096
|
}
|
|
1984
2097
|
}
|
|
1985
2098
|
// 2. Direct match against primary project root
|
|
1986
2099
|
if (normalized === primaryRoot ||
|
|
1987
|
-
normalized
|
|
2100
|
+
_StringStartsWith(normalized, primaryRoot + "/")) {
|
|
1988
2101
|
// If inside node_modules, it is an undeclared/untrusted dependency, NOT the application root!
|
|
1989
|
-
if (normalized
|
|
2102
|
+
if (_StringIncludes(normalized, "/node_modules/")) {
|
|
1990
2103
|
return undefined;
|
|
1991
2104
|
}
|
|
1992
2105
|
return primaryRoot;
|
|
1993
2106
|
}
|
|
1994
2107
|
// 3. Monorepo development mods (/mods/<name>/) fallback
|
|
1995
2108
|
// @see XSA-XESS-018: Must be strictly anchored to the primary project root or framework monorepo
|
|
1996
|
-
const modsIdx = normalized
|
|
1997
|
-
if (modsIdx !== -1 &&
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
const
|
|
2109
|
+
const modsIdx = _StringLastIndexOf(normalized, "/mods/");
|
|
2110
|
+
if (modsIdx !== -1 &&
|
|
2111
|
+
(_StringStartsWith(normalized, primaryRoot + "/") ||
|
|
2112
|
+
isEngineCorePath(normalized))) {
|
|
2113
|
+
const afterMods = _StringSubstring(normalized, modsIdx + 6);
|
|
2114
|
+
const nextSlash = _StringIndexOf(afterMods, "/");
|
|
2115
|
+
const modName = nextSlash === -1
|
|
2116
|
+
? afterMods
|
|
2117
|
+
: _StringSubstring(afterMods, 0, nextSlash);
|
|
2001
2118
|
if (modName) {
|
|
2002
|
-
return normalized
|
|
2119
|
+
return _StringSubstring(normalized, 0, modsIdx + 6 + modName.length);
|
|
2003
2120
|
}
|
|
2004
2121
|
}
|
|
2005
2122
|
return undefined;
|
|
@@ -2007,34 +2124,89 @@ function identifyProjectRoot(filePath) {
|
|
|
2007
2124
|
function normalizeScriptName(rawScript) {
|
|
2008
2125
|
if (!rawScript)
|
|
2009
2126
|
return "";
|
|
2010
|
-
if (rawScript
|
|
2127
|
+
if (_StringStartsWith(rawScript, "file://")) {
|
|
2011
2128
|
try {
|
|
2012
2129
|
return node_url.fileURLToPath(rawScript);
|
|
2013
2130
|
}
|
|
2014
2131
|
catch {
|
|
2015
|
-
return rawScript
|
|
2132
|
+
return _StringSlice(rawScript, 7);
|
|
2016
2133
|
}
|
|
2017
2134
|
}
|
|
2018
2135
|
return rawScript;
|
|
2019
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
|
+
*/
|
|
2020
2145
|
function isRuntimeLoaderPath(filePath) {
|
|
2021
|
-
const norm = filePath
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2146
|
+
const norm = _StringReplace(filePath, /\\/g, "/");
|
|
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/") ||
|
|
2154
|
+
_StringIncludes(norm, "/node_modules/ts-node/") ||
|
|
2155
|
+
_StringEndsWith(norm, "/node_modules/.bin/tsx") ||
|
|
2156
|
+
_StringEndsWith(norm, "/node_modules/.bin/ts-node")) {
|
|
2157
|
+
return true;
|
|
2158
|
+
}
|
|
2159
|
+
return false;
|
|
2025
2160
|
}
|
|
2026
2161
|
/**
|
|
2027
|
-
* Captures raw C++ V8/JSC CallSite frames
|
|
2028
|
-
* synchronously during stack generation. This neutralizes any user/attacker `Error.prepareStackTrace`
|
|
2029
|
-
* hijack and returns the full un-truncated call stack across both Node.js (V8) and Bun (JSC).
|
|
2162
|
+
* Captures raw C++ V8/JSC CallSite frames.
|
|
2030
2163
|
*
|
|
2031
|
-
*
|
|
2164
|
+
* Security Architecture (Fail-Closed & Anti-Spoofing):
|
|
2165
|
+
* 1. Prioritizes `util.getCallSites(64)` when running on Node.js/V8, directly invoking
|
|
2166
|
+
* the C++ v8::StackTrace without touching JavaScript prototype methods or `Error.prepareStackTrace`.
|
|
2167
|
+
* 2. Falls back to controlled `Error.prepareStackTrace` with frozen `CallSite.prototype` accessors
|
|
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]`).
|
|
2032
2170
|
*
|
|
2171
|
+
* @internal
|
|
2033
2172
|
* @see XSA-XESS-015
|
|
2173
|
+
* @see XSA-XESS-025
|
|
2034
2174
|
*/
|
|
2035
2175
|
function captureNativeFrames() {
|
|
2036
2176
|
const frames = [];
|
|
2037
|
-
// 1
|
|
2177
|
+
// Priority 1: Native C++ V8 CallSite frames (util.getCallSites)
|
|
2178
|
+
// Directly queries V8 engine in C++, immune to Error.prepareStackTrace overrides,
|
|
2179
|
+
// CallSite prototype manipulation, and ignoring `//# sourceURL` directives.
|
|
2180
|
+
if (_nativeGetCallSites) {
|
|
2181
|
+
try {
|
|
2182
|
+
const nativeSites = _nativeGetCallSites(64);
|
|
2183
|
+
const nativeLen = _ArrayIsArray(nativeSites)
|
|
2184
|
+
? nativeSites.length
|
|
2185
|
+
: 0;
|
|
2186
|
+
if (nativeLen > 0) {
|
|
2187
|
+
for (let i = 0; i < nativeLen; i++) {
|
|
2188
|
+
const s = nativeSites[i];
|
|
2189
|
+
if (!s)
|
|
2190
|
+
continue;
|
|
2191
|
+
const rawScript = s.scriptName || "";
|
|
2192
|
+
const fnName = s.functionName || "";
|
|
2193
|
+
const isSynthetic = !rawScript && (fnName === "eval" || fnName === "anonymous") ||
|
|
2194
|
+
_StringIncludes(rawScript, "[eval]") ||
|
|
2195
|
+
_StringIncludes(rawScript, "[vm]");
|
|
2196
|
+
frames[frames.length] = {
|
|
2197
|
+
filePath: normalizeScriptName(rawScript),
|
|
2198
|
+
fnName,
|
|
2199
|
+
isEval: isSynthetic,
|
|
2200
|
+
};
|
|
2201
|
+
}
|
|
2202
|
+
return frames;
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
catch {
|
|
2206
|
+
// fallback to Error.prepareStackTrace
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
// Priority 2: Controlled Error.prepareStackTrace override (Node.js fallback / Bun JSC)
|
|
2038
2210
|
const prevPrepare = Error.prepareStackTrace;
|
|
2039
2211
|
const prevLimit = Error.stackTraceLimit;
|
|
2040
2212
|
let rawSites = [];
|
|
@@ -2054,60 +2226,50 @@ function captureNativeFrames() {
|
|
|
2054
2226
|
Error.prepareStackTrace = prevPrepare;
|
|
2055
2227
|
Error.stackTraceLimit = prevLimit;
|
|
2056
2228
|
}
|
|
2057
|
-
// Use indexed iteration
|
|
2229
|
+
// Use indexed iteration and frozen CallSite accessors to defeat prototype pollution
|
|
2058
2230
|
for (let i = 0; i < rawSites.length; i++) {
|
|
2059
2231
|
const site = rawSites[i];
|
|
2060
2232
|
if (!site)
|
|
2061
2233
|
continue;
|
|
2062
2234
|
try {
|
|
2063
|
-
const rawFile =
|
|
2064
|
-
? site
|
|
2065
|
-
: ""
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2235
|
+
const rawFile = _CallSiteGetFileName
|
|
2236
|
+
? _CallSiteGetFileName(site) || ""
|
|
2237
|
+
: typeof site.getFileName === "function"
|
|
2238
|
+
? site.getFileName() || ""
|
|
2239
|
+
: "";
|
|
2240
|
+
const fnName = _CallSiteGetFunctionName
|
|
2241
|
+
? _CallSiteGetFunctionName(site) || ""
|
|
2242
|
+
: typeof site.getFunctionName === "function"
|
|
2243
|
+
? site.getFunctionName() || ""
|
|
2244
|
+
: "";
|
|
2245
|
+
const isEval = _CallSiteIsEval
|
|
2246
|
+
? Boolean(_CallSiteIsEval(site))
|
|
2247
|
+
: typeof site.isEval === "function"
|
|
2248
|
+
? Boolean(site.isEval())
|
|
2249
|
+
: false;
|
|
2250
|
+
const isSynthetic = isEval ||
|
|
2251
|
+
_StringIncludes(rawFile, "[eval]") ||
|
|
2252
|
+
_StringIncludes(rawFile, "[vm]");
|
|
2072
2253
|
frames[frames.length] = {
|
|
2073
2254
|
filePath: normalizeScriptName(rawFile),
|
|
2074
2255
|
fnName,
|
|
2075
|
-
isEval,
|
|
2256
|
+
isEval: isSynthetic,
|
|
2076
2257
|
};
|
|
2077
2258
|
}
|
|
2078
2259
|
catch {
|
|
2079
2260
|
// ignore
|
|
2080
2261
|
}
|
|
2081
2262
|
}
|
|
2082
|
-
// 2. Also merge frames from Node C++ util.getCallSites (which strips //# sourceURL on V8)
|
|
2083
|
-
if (_nativeGetCallSites) {
|
|
2084
|
-
try {
|
|
2085
|
-
const nativeSites = _nativeGetCallSites(64);
|
|
2086
|
-
const nativeLen = _ArrayIsArray(nativeSites) ? nativeSites.length : 0;
|
|
2087
|
-
for (let i = 0; i < nativeLen; i++) {
|
|
2088
|
-
const s = nativeSites[i];
|
|
2089
|
-
if (!s)
|
|
2090
|
-
continue;
|
|
2091
|
-
const rawScript = s.scriptName || "";
|
|
2092
|
-
const fnName = s.functionName || "";
|
|
2093
|
-
frames[frames.length] = {
|
|
2094
|
-
filePath: normalizeScriptName(rawScript),
|
|
2095
|
-
fnName,
|
|
2096
|
-
isEval: !rawScript &&
|
|
2097
|
-
(fnName === "eval" || fnName === "anonymous"),
|
|
2098
|
-
};
|
|
2099
|
-
}
|
|
2100
|
-
}
|
|
2101
|
-
catch {
|
|
2102
|
-
// ignore
|
|
2103
|
-
}
|
|
2104
|
-
}
|
|
2105
2263
|
return frames;
|
|
2106
2264
|
}
|
|
2107
2265
|
/**
|
|
2108
2266
|
* Verifies that an internal property accessor (such as `BaseApi.prototype.runner`)
|
|
2109
2267
|
* or internal engine method is being invoked directly by an internal XyPriss core module
|
|
2110
2268
|
* and not by userland or third-party `node_modules` code.
|
|
2269
|
+
*
|
|
2270
|
+
* @see XSA-XESS-015 - Immune to prototype pollution on String.prototype
|
|
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
|
|
2111
2273
|
*/
|
|
2112
2274
|
function isImmediateInternalEngineCall() {
|
|
2113
2275
|
const frames = captureNativeFrames();
|
|
@@ -2118,15 +2280,15 @@ function isImmediateInternalEngineCall() {
|
|
|
2118
2280
|
}
|
|
2119
2281
|
if (!filePath)
|
|
2120
2282
|
continue;
|
|
2121
|
-
if (filePath
|
|
2283
|
+
if (_StringIncludes(filePath, "[eval]") || _StringIncludes(filePath, "[vm]")) {
|
|
2122
2284
|
return false;
|
|
2123
2285
|
}
|
|
2124
|
-
if (filePath
|
|
2125
|
-
filePath
|
|
2286
|
+
if (_StringStartsWith(filePath, "node:") ||
|
|
2287
|
+
_StringStartsWith(filePath, "bun:") ||
|
|
2126
2288
|
filePath === "native") {
|
|
2127
2289
|
continue;
|
|
2128
2290
|
}
|
|
2129
|
-
const norm = filePath
|
|
2291
|
+
const norm = _StringReplace(filePath, /\\/g, "/");
|
|
2130
2292
|
// Skip stack-tracing plumbing and entrypoint caller wrappers inside the engine
|
|
2131
2293
|
if (isEngineCorePath(norm)) {
|
|
2132
2294
|
if (fnName === "captureNativeFrames" ||
|
|
@@ -2140,7 +2302,8 @@ function isImmediateInternalEngineCall() {
|
|
|
2140
2302
|
fnName === "call" ||
|
|
2141
2303
|
fnName === "callSync" ||
|
|
2142
2304
|
fnName === "callSyncSubprocess" ||
|
|
2143
|
-
fnName === "executeEnvCall"
|
|
2305
|
+
fnName === "executeEnvCall" ||
|
|
2306
|
+
fnName === "configureShield") {
|
|
2144
2307
|
continue;
|
|
2145
2308
|
}
|
|
2146
2309
|
}
|
|
@@ -2154,6 +2317,10 @@ function isImmediateInternalEngineCall() {
|
|
|
2154
2317
|
* Immune to `Error.prepareStackTrace` hijacking AND `//# sourceURL` spoofing:
|
|
2155
2318
|
* even if an attacker injects a fake frame via `new Function("//# sourceURL=...")`,
|
|
2156
2319
|
* the attacker's own `node_modules/...` frame remains on the call stack and is detected.
|
|
2320
|
+
*
|
|
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
|
|
2157
2324
|
*/
|
|
2158
2325
|
function getVerifiedCallerInfo() {
|
|
2159
2326
|
const frames = captureNativeFrames();
|
|
@@ -2161,7 +2328,9 @@ function getVerifiedCallerInfo() {
|
|
|
2161
2328
|
let immediateCallerFile = "";
|
|
2162
2329
|
for (let i = 0; i < frames.length; i++) {
|
|
2163
2330
|
const { filePath, isEval } = frames[i];
|
|
2164
|
-
if (isEval
|
|
2331
|
+
if (isEval ||
|
|
2332
|
+
_StringIncludes(filePath, "[eval]") ||
|
|
2333
|
+
_StringIncludes(filePath, "[vm]")) {
|
|
2165
2334
|
return {
|
|
2166
2335
|
root: undefined,
|
|
2167
2336
|
isUntrusted: true,
|
|
@@ -2170,15 +2339,8 @@ function getVerifiedCallerInfo() {
|
|
|
2170
2339
|
}
|
|
2171
2340
|
if (!filePath)
|
|
2172
2341
|
continue;
|
|
2173
|
-
if (filePath
|
|
2174
|
-
|
|
2175
|
-
root: undefined,
|
|
2176
|
-
isUntrusted: true,
|
|
2177
|
-
callerFile: filePath || "<eval>",
|
|
2178
|
-
};
|
|
2179
|
-
}
|
|
2180
|
-
if (filePath.startsWith("node:") ||
|
|
2181
|
-
filePath.startsWith("bun:") ||
|
|
2342
|
+
if (_StringStartsWith(filePath, "node:") ||
|
|
2343
|
+
_StringStartsWith(filePath, "bun:") ||
|
|
2182
2344
|
filePath === "native") {
|
|
2183
2345
|
continue;
|
|
2184
2346
|
}
|
|
@@ -2186,8 +2348,11 @@ function getVerifiedCallerInfo() {
|
|
|
2186
2348
|
if (isEngineCorePath(filePath)) {
|
|
2187
2349
|
continue;
|
|
2188
2350
|
}
|
|
2189
|
-
// Skip CLI TypeScript loaders (tsx / ts-node) at the bottom of the stack
|
|
2190
|
-
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)) {
|
|
2191
2356
|
continue;
|
|
2192
2357
|
}
|
|
2193
2358
|
const root = identifyProjectRoot(filePath);
|
|
@@ -2339,6 +2504,23 @@ function verifyPluginContract(pluginRoot, pluginName) {
|
|
|
2339
2504
|
* ```
|
|
2340
2505
|
*/
|
|
2341
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
|
+
}
|
|
2342
2524
|
/**
|
|
2343
2525
|
* Private constructor to enforce singleton pattern
|
|
2344
2526
|
* Initializes with default configuration
|
|
@@ -2346,16 +2528,24 @@ class ConfigurationManager {
|
|
|
2346
2528
|
constructor() {
|
|
2347
2529
|
this.initialized = false;
|
|
2348
2530
|
// Initialize with default configuration
|
|
2349
|
-
this.
|
|
2531
|
+
this._config = { ...DEFAULT_OPTIONS };
|
|
2532
|
+
}
|
|
2533
|
+
static getOrCreateInstance() {
|
|
2534
|
+
if (!ConfigurationManager.instance) {
|
|
2535
|
+
ConfigurationManager.instance = new ConfigurationManager();
|
|
2536
|
+
}
|
|
2537
|
+
return ConfigurationManager.instance;
|
|
2350
2538
|
}
|
|
2351
2539
|
/**
|
|
2352
2540
|
* Get the singleton instance
|
|
2541
|
+
* @see XSA-XESS-024 Caller validation for ConfigurationManager singleton access
|
|
2353
2542
|
*/
|
|
2354
2543
|
static getInstance() {
|
|
2355
|
-
|
|
2356
|
-
|
|
2544
|
+
const caller = getVerifiedCallerInfo();
|
|
2545
|
+
if (caller.isUntrusted) {
|
|
2546
|
+
return undefined;
|
|
2357
2547
|
}
|
|
2358
|
-
return ConfigurationManager.
|
|
2548
|
+
return ConfigurationManager.getOrCreateInstance();
|
|
2359
2549
|
}
|
|
2360
2550
|
/**
|
|
2361
2551
|
* Set the entire configuration
|
|
@@ -2367,11 +2557,11 @@ class ConfigurationManager {
|
|
|
2367
2557
|
if (caller.isUntrusted) {
|
|
2368
2558
|
return;
|
|
2369
2559
|
}
|
|
2370
|
-
const instance = ConfigurationManager.
|
|
2371
|
-
instance.
|
|
2560
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2561
|
+
instance._config = ConfigurationManager.deepMerge({ ...DEFAULT_OPTIONS }, config);
|
|
2372
2562
|
// Strict validation for XEMS if persistence is enabled
|
|
2373
|
-
ConfigurationManager.validateXemsConfig(instance.
|
|
2374
|
-
ConfigurationManager.validateGeneralConfig(instance.
|
|
2563
|
+
ConfigurationManager.validateXemsConfig(instance._config);
|
|
2564
|
+
ConfigurationManager.validateGeneralConfig(instance._config);
|
|
2375
2565
|
instance.initialized = true;
|
|
2376
2566
|
}
|
|
2377
2567
|
/**
|
|
@@ -2385,8 +2575,8 @@ class ConfigurationManager {
|
|
|
2385
2575
|
if (caller.isUntrusted) {
|
|
2386
2576
|
return undefined;
|
|
2387
2577
|
}
|
|
2388
|
-
const instance = ConfigurationManager.
|
|
2389
|
-
return instance.
|
|
2578
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2579
|
+
return instance._config[key];
|
|
2390
2580
|
}
|
|
2391
2581
|
/**
|
|
2392
2582
|
* Get the entire configuration object
|
|
@@ -2398,8 +2588,8 @@ class ConfigurationManager {
|
|
|
2398
2588
|
if (caller.isUntrusted) {
|
|
2399
2589
|
return {};
|
|
2400
2590
|
}
|
|
2401
|
-
const instance = ConfigurationManager.
|
|
2402
|
-
return { ...instance.
|
|
2591
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2592
|
+
return { ...instance._config };
|
|
2403
2593
|
}
|
|
2404
2594
|
/**
|
|
2405
2595
|
* Update a specific XyPriss configuration section (deep merge)
|
|
@@ -2412,24 +2602,24 @@ class ConfigurationManager {
|
|
|
2412
2602
|
if (caller.isUntrusted) {
|
|
2413
2603
|
return;
|
|
2414
2604
|
}
|
|
2415
|
-
const instance = ConfigurationManager.
|
|
2416
|
-
const currentValue = instance.
|
|
2605
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2606
|
+
const currentValue = instance._config[key];
|
|
2417
2607
|
// Deep merge the new value with the existing value
|
|
2418
2608
|
if (typeof currentValue === "object" &&
|
|
2419
2609
|
currentValue !== null &&
|
|
2420
2610
|
!Array.isArray(currentValue)) {
|
|
2421
|
-
instance.
|
|
2611
|
+
instance._config[key] = {
|
|
2422
2612
|
...currentValue,
|
|
2423
2613
|
...value,
|
|
2424
2614
|
};
|
|
2425
2615
|
}
|
|
2426
2616
|
else {
|
|
2427
2617
|
// For non-object values, just replace
|
|
2428
|
-
instance.
|
|
2618
|
+
instance._config[key] = value;
|
|
2429
2619
|
}
|
|
2430
2620
|
// Validate XEMS if it was part of the update
|
|
2431
2621
|
if (key === "server") {
|
|
2432
|
-
ConfigurationManager.validateXemsConfig(instance.
|
|
2622
|
+
ConfigurationManager.validateXemsConfig(instance._config);
|
|
2433
2623
|
}
|
|
2434
2624
|
}
|
|
2435
2625
|
/**
|
|
@@ -2498,17 +2688,21 @@ class ConfigurationManager {
|
|
|
2498
2688
|
* @param config - Partial configuration to merge
|
|
2499
2689
|
*/
|
|
2500
2690
|
static merge(config) {
|
|
2501
|
-
const
|
|
2691
|
+
const caller = getVerifiedCallerInfo();
|
|
2692
|
+
if (caller.isUntrusted) {
|
|
2693
|
+
return;
|
|
2694
|
+
}
|
|
2695
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2502
2696
|
// Check if the incoming config is marked as immutable
|
|
2503
2697
|
const shouldBeImmutable = config?.__isXyPrissImmutable;
|
|
2504
|
-
instance.
|
|
2698
|
+
instance._config = ConfigurationManager.deepMerge(instance._config, config);
|
|
2505
2699
|
// Strict validation for XEMS
|
|
2506
|
-
ConfigurationManager.validateXemsConfig(instance.
|
|
2507
|
-
ConfigurationManager.validateGeneralConfig(instance.
|
|
2700
|
+
ConfigurationManager.validateXemsConfig(instance._config);
|
|
2701
|
+
ConfigurationManager.validateGeneralConfig(instance._config);
|
|
2508
2702
|
// If it should be immutable, wrap it using the global __const__.vars.make
|
|
2509
2703
|
if (shouldBeImmutable &&
|
|
2510
2704
|
typeof globalThis.__const__ !== "undefined") {
|
|
2511
|
-
instance.
|
|
2705
|
+
instance._config = globalThis.__const__.vars.make(instance._config, "Configs");
|
|
2512
2706
|
}
|
|
2513
2707
|
instance.initialized = true;
|
|
2514
2708
|
}
|
|
@@ -2517,7 +2711,7 @@ class ConfigurationManager {
|
|
|
2517
2711
|
* @returns true if configuration has been set, false otherwise
|
|
2518
2712
|
*/
|
|
2519
2713
|
static isInitialized() {
|
|
2520
|
-
const instance = ConfigurationManager.
|
|
2714
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2521
2715
|
return instance.initialized;
|
|
2522
2716
|
}
|
|
2523
2717
|
/**
|
|
@@ -2525,36 +2719,54 @@ class ConfigurationManager {
|
|
|
2525
2719
|
* Useful for testing or reinitializing
|
|
2526
2720
|
*/
|
|
2527
2721
|
static reset() {
|
|
2528
|
-
const
|
|
2529
|
-
|
|
2722
|
+
const caller = getVerifiedCallerInfo();
|
|
2723
|
+
if (caller.isUntrusted) {
|
|
2724
|
+
return;
|
|
2725
|
+
}
|
|
2726
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2727
|
+
instance._config = { ...DEFAULT_OPTIONS };
|
|
2530
2728
|
instance.initialized = false;
|
|
2531
2729
|
}
|
|
2532
2730
|
/**
|
|
2533
2731
|
* Check if a specific configuration section exists
|
|
2732
|
+
* @see XSA-XESS-024 Caller validation for configuration section existence
|
|
2534
2733
|
* @param key - Configuration key to check
|
|
2535
2734
|
* @returns true if the configuration section exists
|
|
2536
2735
|
*/
|
|
2537
2736
|
static has(key) {
|
|
2538
|
-
const
|
|
2539
|
-
|
|
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;
|
|
2540
2743
|
}
|
|
2541
2744
|
/**
|
|
2542
2745
|
* Get configuration with a default value if not set
|
|
2746
|
+
* @see XSA-XESS-024 Caller validation for default config retrieval
|
|
2543
2747
|
* @param key - Configuration key
|
|
2544
2748
|
* @param defaultValue - Default value to return if key doesn't exist
|
|
2545
2749
|
* @returns Configuration value or default value
|
|
2546
2750
|
*/
|
|
2547
2751
|
static getOrDefault(key, defaultValue) {
|
|
2548
|
-
const
|
|
2549
|
-
|
|
2752
|
+
const caller = getVerifiedCallerInfo();
|
|
2753
|
+
if (caller.isUntrusted) {
|
|
2754
|
+
return defaultValue;
|
|
2755
|
+
}
|
|
2756
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2757
|
+
return instance._config[key] ?? defaultValue;
|
|
2550
2758
|
}
|
|
2551
2759
|
/**
|
|
2552
2760
|
* Delete a specific configuration section
|
|
2553
2761
|
* @param key - Configuration key to delete
|
|
2554
2762
|
*/
|
|
2555
2763
|
static delete(key) {
|
|
2556
|
-
const
|
|
2557
|
-
|
|
2764
|
+
const caller = getVerifiedCallerInfo();
|
|
2765
|
+
if (caller.isUntrusted) {
|
|
2766
|
+
return;
|
|
2767
|
+
}
|
|
2768
|
+
const instance = ConfigurationManager.getOrCreateInstance();
|
|
2769
|
+
delete instance._config[key];
|
|
2558
2770
|
}
|
|
2559
2771
|
/**
|
|
2560
2772
|
* Validates XEMS configuration strictly
|
|
@@ -4826,8 +5038,15 @@ class SynapxSyncBridge {
|
|
|
4826
5038
|
}
|
|
4827
5039
|
/**
|
|
4828
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
|
|
4829
5044
|
*/
|
|
4830
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
|
+
}
|
|
4831
5050
|
if (!this.isReady || !this.worker) {
|
|
4832
5051
|
// Lazy synchronous initialization if called before explicit init()
|
|
4833
5052
|
try {
|
|
@@ -4845,7 +5064,7 @@ class SynapxSyncBridge {
|
|
|
4845
5064
|
args: stringArgs,
|
|
4846
5065
|
opts: opts || {},
|
|
4847
5066
|
};
|
|
4848
|
-
const serialized = Buffer.from(
|
|
5067
|
+
const serialized = Buffer.from(_JSONStringify(payloadObj));
|
|
4849
5068
|
if (DATA_OFFSET + serialized.length > this.sab.byteLength) {
|
|
4850
5069
|
throw new Error(`libSynapx: Payload exceeds SharedArrayBuffer capacity (${this.sab.byteLength} bytes)`);
|
|
4851
5070
|
}
|
|
@@ -4869,7 +5088,7 @@ class SynapxSyncBridge {
|
|
|
4869
5088
|
Atomics.store(this.int32, IDX_STATUS, STATUS_IDLE);
|
|
4870
5089
|
let parsed;
|
|
4871
5090
|
try {
|
|
4872
|
-
parsed =
|
|
5091
|
+
parsed = _JSONParse(Buffer.from(respBytes).toString("utf8"));
|
|
4873
5092
|
}
|
|
4874
5093
|
catch (e) {
|
|
4875
5094
|
throw new Error(`libSynapx: Failed to decode response JSON: ${e.message}`);
|
|
@@ -4879,8 +5098,10 @@ class SynapxSyncBridge {
|
|
|
4879
5098
|
* Ref: XSA-SYNAPX-001 (SharedArrayBuffer Memory Scrubbing)
|
|
4880
5099
|
* Overwrite shared memory with zeros immediately following response consumption.
|
|
4881
5100
|
* Prevents plaintext credential retention across the 128 MB SharedArrayBuffer lifecycle.
|
|
5101
|
+
*
|
|
5102
|
+
* @see XSA-XESS-021 - Uses frozen _Uint8ArrayFill primordial to prevent interception
|
|
4882
5103
|
*/
|
|
4883
|
-
this.uint8
|
|
5104
|
+
_Uint8ArrayFill(this.uint8, 0, DATA_OFFSET, DATA_OFFSET + totalUsedLen);
|
|
4884
5105
|
Atomics.store(this.int32, IDX_REQ_LEN, 0);
|
|
4885
5106
|
Atomics.store(this.int32, IDX_RESP_LEN, 0);
|
|
4886
5107
|
}
|
|
@@ -5920,7 +6141,7 @@ class BaseApi {
|
|
|
5920
6141
|
* @param runner - The XyPrissRunner used to invoke native XHSC binaries.
|
|
5921
6142
|
*/
|
|
5922
6143
|
constructor(runner) {
|
|
5923
|
-
_runnerStore
|
|
6144
|
+
_WeakMapSet(_runnerStore, this, runner);
|
|
5924
6145
|
}
|
|
5925
6146
|
/**
|
|
5926
6147
|
* Protected accessor for internal engine classes only.
|
|
@@ -5930,7 +6151,7 @@ class BaseApi {
|
|
|
5930
6151
|
if (!isImmediateInternalEngineCall()) {
|
|
5931
6152
|
return undefined;
|
|
5932
6153
|
}
|
|
5933
|
-
return _runnerStore
|
|
6154
|
+
return _WeakMapGet(_runnerStore, this);
|
|
5934
6155
|
}
|
|
5935
6156
|
}
|
|
5936
6157
|
/**
|
|
@@ -6969,23 +7190,33 @@ class TempFileManager {
|
|
|
6969
7190
|
};
|
|
6970
7191
|
}
|
|
6971
7192
|
/**
|
|
6972
|
-
* 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.
|
|
6973
7197
|
*/
|
|
6974
7198
|
removeFile(filePath) {
|
|
6975
|
-
const sys = getSysApi$1();
|
|
6976
7199
|
const entry = this.trackedFiles.get(filePath);
|
|
6977
7200
|
if (entry?.timer) {
|
|
6978
7201
|
clearTimeout(entry.timer);
|
|
6979
7202
|
}
|
|
6980
7203
|
this.trackedFiles.delete(filePath);
|
|
6981
7204
|
try {
|
|
6982
|
-
if (
|
|
6983
|
-
|
|
7205
|
+
if (fs$1.existsSync(filePath)) {
|
|
7206
|
+
fs$1.unlinkSync(filePath);
|
|
6984
7207
|
logger.debug("fs", `Cleaned up temp file: ${filePath}`);
|
|
6985
7208
|
return true;
|
|
6986
7209
|
}
|
|
6987
7210
|
}
|
|
6988
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 { }
|
|
6989
7220
|
logger.warn("fs", `Failed to remove temp file ${filePath}: ${err?.message}`);
|
|
6990
7221
|
}
|
|
6991
7222
|
return false;
|
|
@@ -6994,14 +7225,13 @@ class TempFileManager {
|
|
|
6994
7225
|
* Clean up all active tracked temporary files.
|
|
6995
7226
|
*/
|
|
6996
7227
|
cleanupAll() {
|
|
6997
|
-
const sys = getSysApi$1();
|
|
6998
7228
|
let count = 0;
|
|
6999
7229
|
for (const [filePath, entry] of this.trackedFiles.entries()) {
|
|
7000
7230
|
if (entry.timer)
|
|
7001
7231
|
clearTimeout(entry.timer);
|
|
7002
7232
|
try {
|
|
7003
|
-
if (
|
|
7004
|
-
|
|
7233
|
+
if (fs$1.existsSync(filePath)) {
|
|
7234
|
+
fs$1.unlinkSync(filePath);
|
|
7005
7235
|
count++;
|
|
7006
7236
|
}
|
|
7007
7237
|
}
|
|
@@ -7018,12 +7248,11 @@ class TempFileManager {
|
|
|
7018
7248
|
return;
|
|
7019
7249
|
this.exitHooksRegistered = true;
|
|
7020
7250
|
const performExitCleanup = () => {
|
|
7021
|
-
const sys = getSysApi$1();
|
|
7022
7251
|
for (const [filePath, entry] of this.trackedFiles.entries()) {
|
|
7023
7252
|
if (entry.autoCleanupOnExit) {
|
|
7024
7253
|
try {
|
|
7025
|
-
if (
|
|
7026
|
-
|
|
7254
|
+
if (fs$1.existsSync(filePath)) {
|
|
7255
|
+
fs$1.unlinkSync(filePath);
|
|
7027
7256
|
}
|
|
7028
7257
|
}
|
|
7029
7258
|
catch { }
|
|
@@ -7082,8 +7311,16 @@ class FSCore extends FSBase {
|
|
|
7082
7311
|
* const content = __sys__.fs.readSync("/etc/hosts");
|
|
7083
7312
|
*/
|
|
7084
7313
|
this.readSync = (p, options = {}) => {
|
|
7085
|
-
|
|
7086
|
-
|
|
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
|
+
}
|
|
7087
7324
|
};
|
|
7088
7325
|
/**
|
|
7089
7326
|
* **High-Performance Read Stream**
|
|
@@ -8859,6 +9096,15 @@ class FSExtended extends FSWatch {
|
|
|
8859
9096
|
class FSApi extends FSExtended {
|
|
8860
9097
|
}
|
|
8861
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
|
+
}
|
|
8862
9108
|
/**
|
|
8863
9109
|
* **Dynamic Variables & Configuration API**
|
|
8864
9110
|
*
|
|
@@ -8866,14 +9112,15 @@ class FSApi extends FSExtended {
|
|
|
8866
9112
|
* Protected with Zero-Trust Caller Verification against unauthorized third-party dependencies.
|
|
8867
9113
|
*
|
|
8868
9114
|
* @see XSA-VARS-001
|
|
9115
|
+
* @see XSA-XESS-023
|
|
9116
|
+
* @see XSA-FS-002
|
|
8869
9117
|
*/
|
|
8870
9118
|
class VarsApi extends BaseApi {
|
|
8871
9119
|
get __root__() {
|
|
8872
|
-
return getCallerProjectRoot()
|
|
9120
|
+
return getCallerProjectRoot();
|
|
8873
9121
|
}
|
|
8874
9122
|
constructor(runner) {
|
|
8875
9123
|
super(runner);
|
|
8876
|
-
this.vars = new Map();
|
|
8877
9124
|
// Explicit typed properties for IntelliSense
|
|
8878
9125
|
this.__version__ = "0.0.0";
|
|
8879
9126
|
this.__author__ = "unknown";
|
|
@@ -8883,12 +9130,13 @@ class VarsApi extends BaseApi {
|
|
|
8883
9130
|
this.__alias__ = "app";
|
|
8884
9131
|
this.__port__ = 3000;
|
|
8885
9132
|
this.__PORT__ = 3000;
|
|
9133
|
+
_WeakMapSet(_varsStore, this, new Map());
|
|
8886
9134
|
// ==========================================
|
|
8887
9135
|
// ENTERPRISE IMMUTABILITY SHIELD
|
|
8888
9136
|
// ==========================================
|
|
8889
9137
|
// Lock __root__ so hackers cannot override it via Object.defineProperty
|
|
8890
9138
|
Object.defineProperty(this, "__root__", {
|
|
8891
|
-
get: () => getCallerProjectRoot()
|
|
9139
|
+
get: () => getCallerProjectRoot(),
|
|
8892
9140
|
enumerable: true,
|
|
8893
9141
|
configurable: false,
|
|
8894
9142
|
});
|
|
@@ -8928,7 +9176,8 @@ class VarsApi extends BaseApi {
|
|
|
8928
9176
|
].includes(key)) {
|
|
8929
9177
|
return this[key] ?? defaultValue;
|
|
8930
9178
|
}
|
|
8931
|
-
|
|
9179
|
+
const vars = getVarsMap(this);
|
|
9180
|
+
return vars.has(key) ? vars.get(key) : defaultValue;
|
|
8932
9181
|
}
|
|
8933
9182
|
/**
|
|
8934
9183
|
* **Set Variable**
|
|
@@ -8965,7 +9214,7 @@ class VarsApi extends BaseApi {
|
|
|
8965
9214
|
].includes(key)) {
|
|
8966
9215
|
this[key] = value;
|
|
8967
9216
|
}
|
|
8968
|
-
this.
|
|
9217
|
+
getVarsMap(this).set(key, value);
|
|
8969
9218
|
}
|
|
8970
9219
|
/**
|
|
8971
9220
|
* **Check Existence**
|
|
@@ -8979,7 +9228,7 @@ class VarsApi extends BaseApi {
|
|
|
8979
9228
|
if (caller.isUntrusted) {
|
|
8980
9229
|
return false;
|
|
8981
9230
|
}
|
|
8982
|
-
return key in this || this.
|
|
9231
|
+
return key in this || getVarsMap(this).has(key);
|
|
8983
9232
|
}
|
|
8984
9233
|
/**
|
|
8985
9234
|
* **Remove Variable**
|
|
@@ -8992,7 +9241,7 @@ class VarsApi extends BaseApi {
|
|
|
8992
9241
|
if (caller.isUntrusted) {
|
|
8993
9242
|
return;
|
|
8994
9243
|
}
|
|
8995
|
-
this.
|
|
9244
|
+
getVarsMap(this).delete(key);
|
|
8996
9245
|
}
|
|
8997
9246
|
/**
|
|
8998
9247
|
* **Update Multiple Variables**
|
|
@@ -9037,7 +9286,7 @@ class VarsApi extends BaseApi {
|
|
|
9037
9286
|
"__PORT__",
|
|
9038
9287
|
"__root__",
|
|
9039
9288
|
];
|
|
9040
|
-
return Array.from(new Set([...explicitKeys, ...this.
|
|
9289
|
+
return Array.from(new Set([...explicitKeys, ...getVarsMap(this).keys()]));
|
|
9041
9290
|
}
|
|
9042
9291
|
/**
|
|
9043
9292
|
* **Get All Variables**
|
|
@@ -9073,15 +9322,62 @@ class VarsApi extends BaseApi {
|
|
|
9073
9322
|
* **Clear All**
|
|
9074
9323
|
*/
|
|
9075
9324
|
clear() {
|
|
9076
|
-
this.
|
|
9325
|
+
getVarsMap(this).clear();
|
|
9077
9326
|
}
|
|
9078
9327
|
}
|
|
9079
9328
|
|
|
9080
9329
|
const _envRunnerStore = new WeakMap();
|
|
9081
9330
|
const _sealKeyStore = new WeakMap();
|
|
9331
|
+
const _whitelistStore = new WeakMap();
|
|
9332
|
+
const _whitelistedCacheStore = new WeakMap();
|
|
9082
9333
|
const _shieldedInstances = new WeakSet();
|
|
9083
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
|
+
}
|
|
9084
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
|
+
}
|
|
9085
9381
|
/**
|
|
9086
9382
|
* @param runner - The XyPrissRunner used to invoke native Go binaries.
|
|
9087
9383
|
* @param mode - The execution mode. Defaults to `"development"`.
|
|
@@ -9120,6 +9416,7 @@ class EnvApi {
|
|
|
9120
9416
|
"READABLE_STREAM",
|
|
9121
9417
|
"BUN_CONFIG_VERBOSE_FETCH",
|
|
9122
9418
|
"XYPRISS_ENV_SHIELD",
|
|
9419
|
+
"XYPRISS_SHIELD",
|
|
9123
9420
|
"BUN_DISABLE_DYNAMIC_CHUNK_SIZE",
|
|
9124
9421
|
// Node / Bun core HTTPS variables (read allowed for network clients, writes strictly guarded in set trap)
|
|
9125
9422
|
"NODE_TLS_REJECT_UNAUTHORIZED",
|
|
@@ -9134,10 +9431,11 @@ class EnvApi {
|
|
|
9134
9431
|
"TEMP",
|
|
9135
9432
|
"TMP",
|
|
9136
9433
|
]);
|
|
9137
|
-
this
|
|
9138
|
-
_envRunnerStore.set(this, runner);
|
|
9434
|
+
_WeakMapSet(_envRunnerStore, this, runner);
|
|
9139
9435
|
const sealKey = crypto$2.randomBytes(32).toString("hex");
|
|
9140
|
-
_sealKeyStore
|
|
9436
|
+
_WeakMapSet(_sealKeyStore, this, sealKey);
|
|
9437
|
+
_WeakMapSet(_whitelistStore, this, new Set(this.defaultWhitelist));
|
|
9438
|
+
_WeakMapSet(_whitelistedCacheStore, this, new Map());
|
|
9141
9439
|
try {
|
|
9142
9440
|
runner.runSync("env", "seal", [sealKey], { __seal__: sealKey, pid: process.pid });
|
|
9143
9441
|
}
|
|
@@ -9146,17 +9444,19 @@ class EnvApi {
|
|
|
9146
9444
|
this.isDynamic = isDynamic;
|
|
9147
9445
|
this.applyShield();
|
|
9148
9446
|
}
|
|
9447
|
+
/**
|
|
9448
|
+
* @see XSA-XESS-022 Prevents seal key theft from untrusted callers
|
|
9449
|
+
*/
|
|
9149
9450
|
getSealKey() {
|
|
9150
|
-
|
|
9151
|
-
if (!
|
|
9152
|
-
|
|
9153
|
-
_sealKeyStore.set(this, key);
|
|
9451
|
+
const caller = getVerifiedCallerInfo();
|
|
9452
|
+
if (caller.isUntrusted || !isImmediateInternalEngineCall()) {
|
|
9453
|
+
return undefined;
|
|
9154
9454
|
}
|
|
9155
|
-
return
|
|
9455
|
+
return getSealKey(this);
|
|
9156
9456
|
}
|
|
9157
9457
|
executeEnvCall(action, args = [], options = {}) {
|
|
9158
|
-
const sealKey =
|
|
9159
|
-
const runner = _envRunnerStore
|
|
9458
|
+
const sealKey = getSealKey(this);
|
|
9459
|
+
const runner = _WeakMapGet(_envRunnerStore, this);
|
|
9160
9460
|
if (!runner) {
|
|
9161
9461
|
throw new EnvStoreError();
|
|
9162
9462
|
}
|
|
@@ -9210,7 +9510,9 @@ class EnvApi {
|
|
|
9210
9510
|
// Keep process.env in sync strictly for declared whitelisted non-secret keys.
|
|
9211
9511
|
// @see XSA-XESS-017: Never mirror XEMS_*, ENC_*, DOTENV_*, XY_*, or __* into process.env,
|
|
9212
9512
|
// because Bun.env and import.meta.env bypass the Proxy shield and would expose plain secrets.
|
|
9213
|
-
|
|
9513
|
+
// @see XSA-XESS-019: Maintain in-memory whitelisted cache for O(1) process.env reads.
|
|
9514
|
+
if (getWhitelist(this).has(key)) {
|
|
9515
|
+
getWhitelistedCache(this).set(key, value);
|
|
9214
9516
|
try {
|
|
9215
9517
|
process.env[key] = value;
|
|
9216
9518
|
}
|
|
@@ -9237,7 +9539,9 @@ class EnvApi {
|
|
|
9237
9539
|
}
|
|
9238
9540
|
this.executeEnvCall("delete", [key], { root });
|
|
9239
9541
|
// @see XSA-XESS-017
|
|
9240
|
-
|
|
9542
|
+
// @see XSA-XESS-019: Invalidate whitelisted cache on delete.
|
|
9543
|
+
if (getWhitelist(this).has(key)) {
|
|
9544
|
+
getWhitelistedCache(this).delete(key);
|
|
9241
9545
|
try {
|
|
9242
9546
|
delete process.env[key];
|
|
9243
9547
|
}
|
|
@@ -9441,30 +9745,6 @@ class EnvApi {
|
|
|
9441
9745
|
is(envName) {
|
|
9442
9746
|
return this.mode === envName;
|
|
9443
9747
|
}
|
|
9444
|
-
/**
|
|
9445
|
-
* Configures the XESS (XyPriss Environment Security Shield) dynamically.
|
|
9446
|
-
*
|
|
9447
|
-
* **Security Enforcement:**
|
|
9448
|
-
* Only the host application and internal engine are authorized to invoke this method.
|
|
9449
|
-
* Invocations from third-party plugins in `node_modules` or foreign project roots
|
|
9450
|
-
* are strictly blocked to prevent malicious tampering with the `process.env` whitelist.
|
|
9451
|
-
*
|
|
9452
|
-
* @deprecated Consider declaring `$env: { whitelist: [...] }` in `xypriss.config.jsonc` instead.
|
|
9453
|
-
*/
|
|
9454
|
-
configureShield(config) {
|
|
9455
|
-
const callerRoot = getCallerProjectRoot();
|
|
9456
|
-
const runner = _envRunnerStore.get(this);
|
|
9457
|
-
const hostRoot = runner ? runner.getRoot() : "";
|
|
9458
|
-
/**
|
|
9459
|
-
* @see XSA-XESS-018: Strict Fail-Closed check. If callerRoot cannot be resolved
|
|
9460
|
-
* (e.g. called from third-party node_modules or unverified stack), block immediately.
|
|
9461
|
-
*/
|
|
9462
|
-
if (!callerRoot || (callerRoot !== hostRoot && !isCoreFrameworkPath(callerRoot))) {
|
|
9463
|
-
logger$1.warn("security", `Blocked unauthorized attempt by third-party caller at '${callerRoot || "<untrusted>"}' to configure Environment Security Shield.`);
|
|
9464
|
-
return;
|
|
9465
|
-
}
|
|
9466
|
-
this[XY_ENV_CONFIGURE_SHIELD](config);
|
|
9467
|
-
}
|
|
9468
9748
|
/**
|
|
9469
9749
|
* Configures the XESS (XyPriss Environment Security Shield) dynamically.
|
|
9470
9750
|
*
|
|
@@ -9475,6 +9755,9 @@ class EnvApi {
|
|
|
9475
9755
|
* and only accessible via this internal Symbol during early bootstrap.
|
|
9476
9756
|
*
|
|
9477
9757
|
* @internal
|
|
9758
|
+
* @see XSA-XESS-015
|
|
9759
|
+
* @see XSA-XESS-018
|
|
9760
|
+
* @see XSA-XESS-019
|
|
9478
9761
|
* @param config - XESS configuration options.
|
|
9479
9762
|
*/
|
|
9480
9763
|
[XY_ENV_CONFIGURE_SHIELD](config) {
|
|
@@ -9486,13 +9769,55 @@ class EnvApi {
|
|
|
9486
9769
|
return;
|
|
9487
9770
|
if (config.whitelist !== undefined) {
|
|
9488
9771
|
if (config.replaceDefaultWhitelist) {
|
|
9489
|
-
this
|
|
9772
|
+
setWhitelist(this, new Set(config.whitelist));
|
|
9490
9773
|
}
|
|
9491
9774
|
else {
|
|
9492
|
-
this
|
|
9775
|
+
setWhitelist(this, new Set([
|
|
9493
9776
|
...this.defaultWhitelist,
|
|
9494
9777
|
...config.whitelist,
|
|
9495
|
-
]);
|
|
9778
|
+
]));
|
|
9779
|
+
}
|
|
9780
|
+
}
|
|
9781
|
+
// Cache all whitelisted values from the vault during trusted bootstrap
|
|
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();
|
|
9794
|
+
}
|
|
9795
|
+
/**
|
|
9796
|
+
* Pre-populates the in-memory cache of whitelisted variables from the secure vault
|
|
9797
|
+
* during trusted engine bootstrap.
|
|
9798
|
+
*
|
|
9799
|
+
* This provides O(1) synchronous lookups for `process.env` without invoking `runner.runSync`,
|
|
9800
|
+
* preventing any conflict with Zero-Trust caller verification on restricted modules.
|
|
9801
|
+
*
|
|
9802
|
+
* @internal
|
|
9803
|
+
* @see XSA-XESS-019
|
|
9804
|
+
*/
|
|
9805
|
+
_doRefreshWhitelistedCache() {
|
|
9806
|
+
const runner = _WeakMapGet(_envRunnerStore, this);
|
|
9807
|
+
const hostRoot = runner ? runner.getRoot() : "";
|
|
9808
|
+
const whitelist = getWhitelist(this);
|
|
9809
|
+
const cache = getWhitelistedCache(this);
|
|
9810
|
+
for (const key of whitelist) {
|
|
9811
|
+
if (!this.defaultWhitelist.has(key) || key === "PORT") {
|
|
9812
|
+
try {
|
|
9813
|
+
const res = this.executeEnvCall("get", [key], { root: hostRoot });
|
|
9814
|
+
if (res?.found && res.value !== undefined) {
|
|
9815
|
+
cache.set(key, res.value);
|
|
9816
|
+
}
|
|
9817
|
+
}
|
|
9818
|
+
catch {
|
|
9819
|
+
// Vault might not be initialized or key not present yet
|
|
9820
|
+
}
|
|
9496
9821
|
}
|
|
9497
9822
|
}
|
|
9498
9823
|
}
|
|
@@ -9515,7 +9840,7 @@ class EnvApi {
|
|
|
9515
9840
|
*/
|
|
9516
9841
|
user() {
|
|
9517
9842
|
try {
|
|
9518
|
-
const runner = _envRunnerStore
|
|
9843
|
+
const runner = _WeakMapGet(_envRunnerStore, this);
|
|
9519
9844
|
if (!runner)
|
|
9520
9845
|
return "";
|
|
9521
9846
|
const result = runner.runSync("sys", "user");
|
|
@@ -9549,7 +9874,7 @@ class EnvApi {
|
|
|
9549
9874
|
if (this.isDynamic && callerInfo.root) {
|
|
9550
9875
|
return callerInfo.root;
|
|
9551
9876
|
}
|
|
9552
|
-
const runner = _envRunnerStore
|
|
9877
|
+
const runner = _WeakMapGet(_envRunnerStore, this);
|
|
9553
9878
|
const hostRoot = runner ? runner.getRoot() : null;
|
|
9554
9879
|
return hostRoot || callerInfo.root || null;
|
|
9555
9880
|
}
|
|
@@ -9667,6 +9992,8 @@ class EnvApi {
|
|
|
9667
9992
|
/**
|
|
9668
9993
|
* Verifies whether the caller that directly triggered a process.env Proxy trap
|
|
9669
9994
|
* belongs to the internal XyPriss engine core or to external user/plugin code.
|
|
9995
|
+
*
|
|
9996
|
+
* @see XSA-XESS-015 - Immune to prototype pollution on String.prototype
|
|
9670
9997
|
*/
|
|
9671
9998
|
function isProxyCallerEngineCore() {
|
|
9672
9999
|
const frames = captureNativeFrames();
|
|
@@ -9675,10 +10002,10 @@ class EnvApi {
|
|
|
9675
10002
|
const { filePath, fnName } = frames[i];
|
|
9676
10003
|
if (!filePath)
|
|
9677
10004
|
continue;
|
|
9678
|
-
if (filePath
|
|
9679
|
-
filePath
|
|
10005
|
+
if (_StringStartsWith(filePath, "node:") ||
|
|
10006
|
+
_StringStartsWith(filePath, "bun:") ||
|
|
9680
10007
|
filePath === "native" ||
|
|
9681
|
-
filePath
|
|
10008
|
+
_StringIncludes(filePath, "[eval]")) {
|
|
9682
10009
|
continue;
|
|
9683
10010
|
}
|
|
9684
10011
|
if (fnName === "captureNativeFrames" ||
|
|
@@ -9691,7 +10018,7 @@ class EnvApi {
|
|
|
9691
10018
|
continue;
|
|
9692
10019
|
}
|
|
9693
10020
|
// The very first frame after the Proxy trap is the consumer that accessed process.env!
|
|
9694
|
-
return isEngineCorePath(filePath
|
|
10021
|
+
return isEngineCorePath(_StringReplace(filePath, /\\/g, "/"));
|
|
9695
10022
|
}
|
|
9696
10023
|
return false;
|
|
9697
10024
|
}
|
|
@@ -9703,21 +10030,30 @@ class EnvApi {
|
|
|
9703
10030
|
if (typeof prop !== "string") {
|
|
9704
10031
|
return Reflect.get(target, prop, receiver);
|
|
9705
10032
|
}
|
|
9706
|
-
|
|
10033
|
+
/**
|
|
10034
|
+
* Fast bypass for internal engine shield checks to prevent proxy re-entrancy.
|
|
10035
|
+
* @see XSA-XESS-018
|
|
10036
|
+
* @see XSA-XESS-019
|
|
10037
|
+
*/
|
|
10038
|
+
if (isInsideShieldCheck ||
|
|
10039
|
+
prop === "XYPRISS_PROJECT_ROOT" ||
|
|
10040
|
+
prop === "XYPRISS_ENV_SHIELD" ||
|
|
10041
|
+
prop === "XYPRISS_SHIELD") {
|
|
9707
10042
|
return Reflect.get(target, prop, receiver);
|
|
9708
10043
|
}
|
|
9709
|
-
|
|
9710
|
-
|
|
9711
|
-
|
|
9712
|
-
|
|
9713
|
-
|
|
9714
|
-
|
|
9715
|
-
|
|
9716
|
-
|
|
9717
|
-
|
|
9718
|
-
|
|
9719
|
-
|
|
9720
|
-
|
|
10044
|
+
/**
|
|
10045
|
+
* Whitelisted environment fields resolution:
|
|
10046
|
+
* Resolves in O(1) from the secure in-memory cache populated from the Vault.
|
|
10047
|
+
* Third-party packages in node_modules can read declared whitelisted keys safely
|
|
10048
|
+
* without triggering runner.runSync Zero-Trust caller rejection.
|
|
10049
|
+
*
|
|
10050
|
+
* @see XSA-XESS-019
|
|
10051
|
+
*/
|
|
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);
|
|
9721
10057
|
}
|
|
9722
10058
|
return Reflect.get(target, prop, receiver);
|
|
9723
10059
|
}
|
|
@@ -9735,12 +10071,15 @@ class EnvApi {
|
|
|
9735
10071
|
// All other keys (including ENC_*, DOTENV_*, __*, and application secrets):
|
|
9736
10072
|
// direct process.env reads by untrusted/external callers are strictly intercepted and blocked!
|
|
9737
10073
|
if (!warnedKeys.has(prop)) {
|
|
9738
|
-
|
|
10074
|
+
/**
|
|
10075
|
+
* Check silent mode flags directly from the backing target object
|
|
10076
|
+
* to permanently prevent recursive proxy invocations.
|
|
10077
|
+
*
|
|
10078
|
+
* @see XSA-XESS-018
|
|
10079
|
+
* @see XSA-XESS-019
|
|
10080
|
+
*/
|
|
9739
10081
|
const isSilent = target["XYPRISS_ENV_SHIELD"] === "silent" ||
|
|
9740
|
-
target["XYPRISS_SHIELD"] === "silent"
|
|
9741
|
-
(typeof process !== "undefined" &&
|
|
9742
|
-
(process.env?.XYPRISS_SHIELD === "silent" ||
|
|
9743
|
-
process.env?.XYPRISS_ENV_SHIELD === "silent"));
|
|
10082
|
+
target["XYPRISS_SHIELD"] === "silent";
|
|
9744
10083
|
if (!isSilent) {
|
|
9745
10084
|
process.stderr.write(`\x1b[33m[SECURITY]\x1b[0m ` +
|
|
9746
10085
|
`Direct access to process.env["${prop}"] is blocked. ` +
|
|
@@ -9797,7 +10136,7 @@ class EnvApi {
|
|
|
9797
10136
|
}
|
|
9798
10137
|
}
|
|
9799
10138
|
// Allowed whitelisted non-critical keys
|
|
9800
|
-
if (self.
|
|
10139
|
+
if (getWhitelist(self).has(key)) {
|
|
9801
10140
|
try {
|
|
9802
10141
|
target[key] = String(value);
|
|
9803
10142
|
}
|
|
@@ -9857,7 +10196,7 @@ class EnvApi {
|
|
|
9857
10196
|
}
|
|
9858
10197
|
return true;
|
|
9859
10198
|
}
|
|
9860
|
-
if (self.
|
|
10199
|
+
if (getWhitelist(self).has(prop)) {
|
|
9861
10200
|
try {
|
|
9862
10201
|
delete target[prop];
|
|
9863
10202
|
}
|
|
@@ -9885,13 +10224,13 @@ class EnvApi {
|
|
|
9885
10224
|
finally {
|
|
9886
10225
|
isInsideShieldCheck = false;
|
|
9887
10226
|
}
|
|
9888
|
-
return [...self
|
|
10227
|
+
return [...getWhitelist(self)];
|
|
9889
10228
|
},
|
|
9890
10229
|
// Align `in` operator behaviour with the restricted key set.
|
|
9891
10230
|
has(target, prop) {
|
|
9892
10231
|
if (typeof prop !== "string")
|
|
9893
10232
|
return false;
|
|
9894
|
-
if (self.
|
|
10233
|
+
if (getWhitelist(self).has(prop))
|
|
9895
10234
|
return true;
|
|
9896
10235
|
if (prop.startsWith("XYPRISS_") ||
|
|
9897
10236
|
prop.startsWith("LIBXESS_") ||
|
|
@@ -16237,9 +16576,15 @@ class XyPrissFS {
|
|
|
16237
16576
|
*
|
|
16238
16577
|
* The absolute path to the project root directory. This is the primary
|
|
16239
16578
|
* resolution anchor for all system operations.
|
|
16579
|
+
*
|
|
16580
|
+
* @see XSA-FS-002 Fail-closed project root resolution for untrusted callers
|
|
16240
16581
|
*/
|
|
16241
16582
|
get __root__() {
|
|
16242
|
-
|
|
16583
|
+
const callerRoot = getCallerProjectRoot();
|
|
16584
|
+
if (!callerRoot) {
|
|
16585
|
+
return undefined;
|
|
16586
|
+
}
|
|
16587
|
+
return callerRoot;
|
|
16243
16588
|
}
|
|
16244
16589
|
/**
|
|
16245
16590
|
* **Initialize System API**
|
|
@@ -16383,6 +16728,76 @@ function createSecurityShield(target, label = "__sys__") {
|
|
|
16383
16728
|
return createProxy(target, label);
|
|
16384
16729
|
}
|
|
16385
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();
|
|
16386
16801
|
/**
|
|
16387
16802
|
* **XyPriss System Variables (`__sys__`)**
|
|
16388
16803
|
*
|
|
@@ -16399,8 +16814,15 @@ class XyPrissXHSC extends XyPrissFS {
|
|
|
16399
16814
|
[XY_XHSC_REGISTER_FS](pluginId, instance) {
|
|
16400
16815
|
this._pluginMap.set(pluginId, instance);
|
|
16401
16816
|
}
|
|
16817
|
+
/**
|
|
16818
|
+
* @see XSA-FS-002 Fail-closed project root resolution for untrusted callers
|
|
16819
|
+
*/
|
|
16402
16820
|
get __root__() {
|
|
16403
|
-
|
|
16821
|
+
const callerRoot = getCallerProjectRoot();
|
|
16822
|
+
if (!callerRoot) {
|
|
16823
|
+
return undefined;
|
|
16824
|
+
}
|
|
16825
|
+
return callerRoot;
|
|
16404
16826
|
}
|
|
16405
16827
|
constructor(data = {}) {
|
|
16406
16828
|
const root = data.__root__ || process.cwd();
|
|
@@ -35065,6 +35487,12 @@ class XemsError extends Error {
|
|
|
35065
35487
|
* XEMS Runner (Long-running process manager)
|
|
35066
35488
|
* Manages the persistent Rust process for in-memory storage.
|
|
35067
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();
|
|
35068
35496
|
class XemsRunner {
|
|
35069
35497
|
constructor(options = {}) {
|
|
35070
35498
|
this.process = null;
|
|
@@ -35086,7 +35514,7 @@ class XemsRunner {
|
|
|
35086
35514
|
*/
|
|
35087
35515
|
static getInstance(pathStr, options = {}) {
|
|
35088
35516
|
const absPath = path.resolve(process.cwd(), pathStr);
|
|
35089
|
-
const existing =
|
|
35517
|
+
const existing = _runnersByPathStore.get(absPath);
|
|
35090
35518
|
if (existing) {
|
|
35091
35519
|
// Merge options if provided
|
|
35092
35520
|
if (options.secret)
|
|
@@ -35094,7 +35522,7 @@ class XemsRunner {
|
|
|
35094
35522
|
return existing;
|
|
35095
35523
|
}
|
|
35096
35524
|
const runner = new XemsRunner({ ...options, persistPath: pathStr });
|
|
35097
|
-
|
|
35525
|
+
_runnersByPathStore.set(absPath, runner);
|
|
35098
35526
|
return runner;
|
|
35099
35527
|
}
|
|
35100
35528
|
enablePersistence(pathStr, secret, resources) {
|
|
@@ -35112,13 +35540,13 @@ class XemsRunner {
|
|
|
35112
35540
|
}
|
|
35113
35541
|
// Register this runner in the global registry for this path
|
|
35114
35542
|
const absPath = path.resolve(process.cwd(), pathStr);
|
|
35115
|
-
const existing =
|
|
35543
|
+
const existing = _runnersByPathStore.get(absPath);
|
|
35116
35544
|
if (existing && existing !== this) {
|
|
35117
35545
|
this.logger.warn("xems", `Path ${pathStr} is already being managed by another runner. Switching to shared mode.`);
|
|
35118
35546
|
// Transitions this instance to delegate to the existing one if needed?
|
|
35119
35547
|
// Actually, for simplicity, we just ensure WE don't spawn if another one is already there.
|
|
35120
35548
|
}
|
|
35121
|
-
|
|
35549
|
+
_runnersByPathStore.set(absPath, this);
|
|
35122
35550
|
// Avoid redundant restarts if path is the same
|
|
35123
35551
|
if (this.options.persistPath === pathStr && this.process) {
|
|
35124
35552
|
return;
|
|
@@ -35516,10 +35944,6 @@ class XemsRunner {
|
|
|
35516
35944
|
this.initPromise = null;
|
|
35517
35945
|
}
|
|
35518
35946
|
}
|
|
35519
|
-
/**
|
|
35520
|
-
* Enables hardware-bound persistent storage for XEMS.
|
|
35521
|
-
*/
|
|
35522
|
-
XemsRunner.runnersByPath = new Map();
|
|
35523
35947
|
/**
|
|
35524
35948
|
* Contextual wrapper for XEMS operations within a specific sandbox.
|
|
35525
35949
|
* Provides a cleaner, fluent API for advanced operations.
|