xypriss 9.15.0 → 9.15.2

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/esm/index.js CHANGED
@@ -1144,7 +1144,7 @@ const DEFAULT_OPTIONS = {
1144
1144
  enabled: false,
1145
1145
  },
1146
1146
  cluster: {
1147
- enabled: false, // Disabled by default for single-process mode
1147
+ enabled: true, // Disabled by default for single-process mode
1148
1148
  workers: "auto",
1149
1149
  autoRespawn: true,
1150
1150
  mode: "synapx-dispatcher",
@@ -1910,6 +1910,8 @@ function canonicalizePath(filePath) {
1910
1910
  function isEngineCorePath(filePath) {
1911
1911
  if (!filePath || !ENGINE_CORE_DIR)
1912
1912
  return false;
1913
+ if (filePath === "<engine-core>")
1914
+ return true;
1913
1915
  const canonical = canonicalizePath(filePath);
1914
1916
  // Canonical match for ENGINE_CORE_DIR itself (with or without trailing slash)
1915
1917
  const normCore = canonicalizePath(ENGINE_CORE_DIR);
@@ -7384,7 +7386,9 @@ class FSBase extends PathApi {
7384
7386
  const baseRoot = runnerRoot || this.assignedRoot;
7385
7387
  const caller = getVerifiedCallerInfo();
7386
7388
  // If the verified application caller is genuine engine core, allow direct access
7387
- if (caller.callerFile && isEngineCorePath(caller.callerFile)) {
7389
+ if (caller.callerFile &&
7390
+ (caller.callerFile === "<engine-core>" ||
7391
+ isEngineCorePath(caller.callerFile))) {
7388
7392
  if (path__default.isAbsolute(targetPath)) {
7389
7393
  return path__default.resolve(targetPath);
7390
7394
  }
@@ -18381,6 +18385,181 @@ if (typeof globalThis !== "undefined") {
18381
18385
  /** Global singleton instance of the system. */
18382
18386
  const __sys__$1 = globalThis.__sys__;
18383
18387
 
18388
+ /**
18389
+ * XyPriss ProtoShield — Anti-Prototype Pollution Protection Engine
18390
+ *
18391
+ * Fortifies the V8 / JavaScriptCore runtime environment against Prototype Pollution attacks:
18392
+ * 1. Blocks unauthorized additions and mutations to `Object.prototype`, `Array.prototype`, and `Function.prototype`.
18393
+ * 2. Neutralizes mutable `__proto__` accessors exploited by recursive merge/patch vectors.
18394
+ * 3. Enforces strict boot-time configuration schema validation for `xypriss.config.json(c)`.
18395
+ *
18396
+ * @module server/utils/ProtoShield
18397
+ * @see https://xypriss.nehonix.com/docs/security/protoshield
18398
+ */
18399
+ class ProtoShield {
18400
+ static isInitialized = false;
18401
+ static activeConfig = null;
18402
+ /**
18403
+ * Validates raw JSON configuration from `xypriss.config.json(c)` at boot time.
18404
+ * Throws an explicit, descriptive error if any property is invalid.
18405
+ */
18406
+ static validateConfig(raw) {
18407
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
18408
+ throw new Error(`[XyPriss Validation Error] '$protoShield' must be a valid JSON object in xypriss.config.json(c). Received: ${typeof raw}`);
18409
+ }
18410
+ const allowedKeys = new Set([
18411
+ "enabled",
18412
+ "mode",
18413
+ "neutralizeProto",
18414
+ "freezePrototypes",
18415
+ "allowedProperties",
18416
+ ]);
18417
+ for (const key of Object.keys(raw)) {
18418
+ if (!allowedKeys.has(key)) {
18419
+ throw new Error(`[XyPriss Validation Error] Unknown property '$protoShield.${key}' in xypriss.config.json(c). Allowed properties: ${[...allowedKeys].map((k) => `'${k}'`).join(", ")}`);
18420
+ }
18421
+ }
18422
+ if (raw.enabled !== undefined && typeof raw.enabled !== "boolean") {
18423
+ throw new Error(`[XyPriss Validation Error] '$protoShield.enabled' must be a boolean. Received: ${typeof raw.enabled} (${JSON.stringify(raw.enabled)})`);
18424
+ }
18425
+ if (raw.mode !== undefined) {
18426
+ const acceptedModes = ["block", "warn", "freeze"];
18427
+ if (!acceptedModes.includes(raw.mode)) {
18428
+ throw new Error(`[XyPriss Validation Error] Invalid '$protoShield.mode': ${JSON.stringify(raw.mode)}. Accepted values are: 'block', 'warn', 'freeze'.`);
18429
+ }
18430
+ }
18431
+ if (raw.neutralizeProto !== undefined &&
18432
+ typeof raw.neutralizeProto !== "boolean") {
18433
+ throw new Error(`[XyPriss Validation Error] '$protoShield.neutralizeProto' must be a boolean. Received: ${typeof raw.neutralizeProto}`);
18434
+ }
18435
+ if (raw.freezePrototypes !== undefined &&
18436
+ typeof raw.freezePrototypes !== "boolean") {
18437
+ throw new Error(`[XyPriss Validation Error] '$protoShield.freezePrototypes' must be a boolean. Received: ${typeof raw.freezePrototypes}`);
18438
+ }
18439
+ if (raw.allowedProperties !== undefined) {
18440
+ if (!Array.isArray(raw.allowedProperties)) {
18441
+ throw new Error(`[XyPriss Validation Error] '$protoShield.allowedProperties' must be an array of strings. Received: ${typeof raw.allowedProperties}`);
18442
+ }
18443
+ for (let i = 0; i < raw.allowedProperties.length; i++) {
18444
+ const item = raw.allowedProperties[i];
18445
+ if (typeof item !== "string" || !item.trim()) {
18446
+ throw new Error(`[XyPriss Validation Error] '$protoShield.allowedProperties[${i}]' must be a non-empty string. Received: ${JSON.stringify(item)}`);
18447
+ }
18448
+ }
18449
+ }
18450
+ return {
18451
+ enabled: raw.enabled !== undefined ? raw.enabled : true,
18452
+ mode: raw.mode || "block",
18453
+ neutralizeProto: raw.neutralizeProto !== undefined ? raw.neutralizeProto : true,
18454
+ freezePrototypes: raw.freezePrototypes !== undefined
18455
+ ? raw.freezePrototypes
18456
+ : false,
18457
+ allowedProperties: Array.isArray(raw.allowedProperties)
18458
+ ? [...raw.allowedProperties]
18459
+ : [],
18460
+ };
18461
+ }
18462
+ /**
18463
+ * Bootstraps the ProtoShield defense according to the validated configuration.
18464
+ */
18465
+ static initialize(rawConfig) {
18466
+ if (this.isInitialized)
18467
+ return;
18468
+ const config = this.validateConfig(rawConfig);
18469
+ this.activeConfig = config;
18470
+ if (!config.enabled) {
18471
+ this.isInitialized = true;
18472
+ return;
18473
+ }
18474
+ const allowedSet = new Set([...config.allowedProperties, "__proto__"]);
18475
+ // 1. Neutralize mutable __proto__ accessor if enabled (done before strict interception)
18476
+ if (config.neutralizeProto) {
18477
+ this.applyNeutralizeProto(config.mode);
18478
+ }
18479
+ // 2. Intercept explicit property definitions on core prototypes
18480
+ this.applyPropertyInterception(allowedSet, config.mode);
18481
+ // 3. Prevent dynamic property expansion or enforce total immutability
18482
+ if (config.mode === "freeze" || config.freezePrototypes) {
18483
+ try {
18484
+ Object.freeze(Object.prototype);
18485
+ Object.freeze(Array.prototype);
18486
+ Object.freeze(Function.prototype);
18487
+ }
18488
+ catch { }
18489
+ }
18490
+ else if (config.mode === "block") {
18491
+ try {
18492
+ Object.preventExtensions(Object.prototype);
18493
+ }
18494
+ catch { }
18495
+ }
18496
+ this.isInitialized = true;
18497
+ }
18498
+ /**
18499
+ * Intercepts `Object.defineProperty` on base prototypes to thwart unauthorized extension.
18500
+ */
18501
+ static applyPropertyInterception(allowed, mode) {
18502
+ const origDefineProperty = Object.defineProperty;
18503
+ const targetPrototypes = [
18504
+ Object.prototype,
18505
+ Array.prototype,
18506
+ Function.prototype,
18507
+ ];
18508
+ Object.defineProperty = function (target, propertyKey, attributes) {
18509
+ if (targetPrototypes.includes(target)) {
18510
+ const propStr = String(propertyKey);
18511
+ if (!allowed.has(propStr)) {
18512
+ const message = `[XyPriss ProtoShield] Blocked Prototype Pollution attempt! Unauthorized property '${propStr}' on ${target?.constructor?.name || "Object"}.prototype`;
18513
+ if (mode === "warn") {
18514
+ console.warn(`\x1b[33m${message}\x1b[0m\n` +
18515
+ (new Error().stack || ""));
18516
+ }
18517
+ else {
18518
+ const err = new TypeError(message);
18519
+ console.error(`\x1b[31m${message}\x1b[0m\n` + (err.stack || ""));
18520
+ throw err;
18521
+ }
18522
+ }
18523
+ }
18524
+ return origDefineProperty.call(Object, target, propertyKey, attributes);
18525
+ };
18526
+ }
18527
+ /**
18528
+ * Intercepts and neutralizes the mutable `__proto__` setter on `Object.prototype`.
18529
+ */
18530
+ static applyNeutralizeProto(mode) {
18531
+ try {
18532
+ const origDesc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");
18533
+ if (origDesc && origDesc.set) {
18534
+ Object.defineProperty(Object.prototype, "__proto__", {
18535
+ get() {
18536
+ return Object.getPrototypeOf(this);
18537
+ },
18538
+ set(_newProto) {
18539
+ const message = `[XyPriss ProtoShield] Blocked Prototype Mutation attempt via '__proto__'!`;
18540
+ if (mode === "warn") {
18541
+ console.warn(`\x1b[33m${message}\x1b[0m\n` +
18542
+ (new Error().stack || ""));
18543
+ }
18544
+ else {
18545
+ const err = new TypeError(message);
18546
+ console.error(`\x1b[31m${message}\x1b[0m\n` +
18547
+ (err.stack || ""));
18548
+ throw err;
18549
+ }
18550
+ },
18551
+ configurable: false,
18552
+ enumerable: false,
18553
+ });
18554
+ }
18555
+ }
18556
+ catch { }
18557
+ }
18558
+ static getActiveConfig() {
18559
+ return this.activeConfig;
18560
+ }
18561
+ }
18562
+
18384
18563
  /**
18385
18564
  * XyPriss Meta Configuration Runner
18386
18565
  *
@@ -18421,7 +18600,10 @@ class MetaConfigRunner {
18421
18600
  */
18422
18601
  executeMetaConfig(searchDir) {
18423
18602
  const sys = getSysApi$1();
18424
- const root = searchDir || getCallerProjectRoot() || __sys__$1.__root__;
18603
+ let root = searchDir || getCallerProjectRoot();
18604
+ if (!root || root === ENGINE_CORE_DIR) {
18605
+ root = getPrimaryProjectRoot() || __sys__$1.__root__;
18606
+ }
18425
18607
  const metaFiles = searchDir
18426
18608
  ? [
18427
18609
  sys.path.join(root, "+xypriss.meta.ts"),
@@ -18474,7 +18656,10 @@ class ConfigLoader {
18474
18656
  * Load all xypriss.config.json files found in the project and apply configurations
18475
18657
  */
18476
18658
  loadAndApplySysConfig(targetRoot) {
18477
- const root = targetRoot || getCallerProjectRoot() || __sys__$1.__root__;
18659
+ let root = targetRoot || getCallerProjectRoot();
18660
+ if (!root || root === ENGINE_CORE_DIR) {
18661
+ root = getPrimaryProjectRoot() || __sys__$1.__root__;
18662
+ }
18478
18663
  if (!root || this.appliedRoots.has(root))
18479
18664
  return;
18480
18665
  this.appliedRoots.add(root);
@@ -18546,6 +18731,13 @@ class ConfigLoader {
18546
18731
  __sys__$1.vars.update(config.$vars);
18547
18732
  }
18548
18733
  }
18734
+ // Initialize and enforce ProtoShield protection if declared ($protoShield or protoShield)
18735
+ const protoConfig = config?.$protoShield !== undefined
18736
+ ? config.$protoShield
18737
+ : config?.protoShield;
18738
+ if (protoConfig !== undefined) {
18739
+ ProtoShield.initialize(protoConfig);
18740
+ }
18549
18741
  // Apply ServerOptions sections to Configs (centralized configuration management)
18550
18742
  const recognizedServerOptionKeys = [
18551
18743
  "server",
@@ -18602,8 +18794,7 @@ class ConfigLoader {
18602
18794
  if (config.__xfs__ && config.__xfs__.path) {
18603
18795
  const rawPath = config.__xfs__.path;
18604
18796
  const resolvedFsPath = this.resolvePath(rawPath, projectRoot, configDir);
18605
- if (resolvedFsPath &&
18606
- this.sys.fs.exist(resolvedFsPath)) {
18797
+ if (resolvedFsPath && this.sys.fs.exist(resolvedFsPath)) {
18607
18798
  const specializedFS = new XyPrissFS({
18608
18799
  __root__: resolvedFsPath,
18609
18800
  __mode__: __sys__$1.__env__.mode,
@@ -29475,8 +29666,12 @@ class LogProcessor {
29475
29666
  }
29476
29667
  }
29477
29668
 
29669
+ // S = Ox53 (dec: 5*16 + 3 = 83)
29670
+ // N = Ox4E (dec: 4*16 + 14 = 78)
29671
+ // P = Ox50 (dec: 5*16 + 0 = 80)
29672
+ // X = Ox58 (dec: 5*16 + 8 = 88)
29478
29673
  const MAGIC_HEADER = Buffer.from([0x53, 0x4e, 0x50, 0x58]); // "SNPX"
29479
- const CURRENT_VERSION = 0x0100;
29674
+ const CURRENT_VERSION = 0x0100; // 1
29480
29675
  const NONCE_SIZE = 12;
29481
29676
  const HEADER_SIZE = 8;
29482
29677
  /**
@@ -29549,12 +29744,14 @@ class EngineManager {
29549
29744
  uniquePluginPaths.length > 0
29550
29745
  ? ["--plugins", uniquePluginPaths.join(",")]
29551
29746
  : [];
29552
- // Access project root lazily via getSysApi() to break circular imports
29747
+ // Strict authoritative project root resolution (Fail-Closed)
29553
29748
  const sys = getSysApi$1();
29554
- const projectRoot = sys?.__root__ ||
29555
- getCallerProjectRoot() ||
29556
- identifyProjectRoot(process.cwd()) ||
29557
- process.cwd();
29749
+ const sysRoot = sys?.__root__;
29750
+ const projectRoot = getPrimaryProjectRoot() ||
29751
+ (sysRoot && sysRoot !== ENGINE_CORE_DIR ? sysRoot : "");
29752
+ if (!projectRoot || !isProjectRoot(projectRoot)) {
29753
+ throw new Error(`[XyPriss Security] Fail-Closed: Cannot resolve an authoritative project root for XHSC engine.`);
29754
+ }
29558
29755
  const entryPoint = process.argv[1] ||
29559
29756
  appConfigs?.cluster?.entryPoint ||
29560
29757
  path__default.join(projectRoot, "src", "server.ts");
@@ -41563,5 +41760,5 @@ function Router() {
41563
41760
  });
41564
41761
  }
41565
41762
 
41566
- export { ConfigurationManager as CM, ConfigurationManager as Configs, DateUtils, FileUploadAPI as FLA, FileUploadAPI, Plugin, PluginHookIds, Router, SecurityMiddleware, Send, Upload, XJsonResponseHandler, XServer, XStatic, XyGuard, XyPrissRouter, XyPrissXHSC, __cfg__, __const__, __sys__$1 as __sys__, createOptimalCache, createServer, getCallerProjectRoot, getInstanceId, getIp, getMime, getMimes, getSysApi, identifyProjectRoot, initializeFileUpload, mergeWithDefaults, mergeWithDefaults as mwdef, normalizeXtrsRules, parseDurationMs, parseXtrsRule, quickServer, uploadAny, uploadArray, uploadFields, uploadSingle, xems };
41763
+ export { ConfigurationManager as CM, ConfigurationManager as Configs, DateUtils, FileUploadAPI as FLA, FileUploadAPI, Plugin, PluginHookIds, ProtoShield, Router, SecurityMiddleware, Send, Upload, XJsonResponseHandler, XServer, XStatic, XyGuard, XyPrissRouter, XyPrissXHSC, __cfg__, __const__, __sys__$1 as __sys__, createOptimalCache, createServer, getCallerProjectRoot, getInstanceId, getIp, getMime, getMimes, getSysApi, identifyProjectRoot, initializeFileUpload, mergeWithDefaults, mergeWithDefaults as mwdef, normalizeXtrsRules, parseDurationMs, parseXtrsRule, quickServer, uploadAny, uploadArray, uploadFields, uploadSingle, xems };
41567
41764
  //# sourceMappingURL=index.js.map