xypriss 9.15.0 → 9.15.1

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 CHANGED
@@ -1167,7 +1167,7 @@ const DEFAULT_OPTIONS = {
1167
1167
  enabled: false,
1168
1168
  },
1169
1169
  cluster: {
1170
- enabled: false, // Disabled by default for single-process mode
1170
+ enabled: true, // Disabled by default for single-process mode
1171
1171
  workers: "auto",
1172
1172
  autoRespawn: true,
1173
1173
  mode: "synapx-dispatcher",
@@ -1933,6 +1933,8 @@ function canonicalizePath(filePath) {
1933
1933
  function isEngineCorePath(filePath) {
1934
1934
  if (!filePath || !ENGINE_CORE_DIR)
1935
1935
  return false;
1936
+ if (filePath === "<engine-core>")
1937
+ return true;
1936
1938
  const canonical = canonicalizePath(filePath);
1937
1939
  // Canonical match for ENGINE_CORE_DIR itself (with or without trailing slash)
1938
1940
  const normCore = canonicalizePath(ENGINE_CORE_DIR);
@@ -7407,7 +7409,9 @@ class FSBase extends PathApi {
7407
7409
  const baseRoot = runnerRoot || this.assignedRoot;
7408
7410
  const caller = getVerifiedCallerInfo();
7409
7411
  // If the verified application caller is genuine engine core, allow direct access
7410
- if (caller.callerFile && isEngineCorePath(caller.callerFile)) {
7412
+ if (caller.callerFile &&
7413
+ (caller.callerFile === "<engine-core>" ||
7414
+ isEngineCorePath(caller.callerFile))) {
7411
7415
  if (path.isAbsolute(targetPath)) {
7412
7416
  return path.resolve(targetPath);
7413
7417
  }
@@ -18404,6 +18408,168 @@ if (typeof globalThis !== "undefined") {
18404
18408
  /** Global singleton instance of the system. */
18405
18409
  const __sys__$1 = globalThis.__sys__;
18406
18410
 
18411
+ /**
18412
+ * XyPriss ProtoShield — Anti-Prototype Pollution Protection Engine
18413
+ *
18414
+ * Fortifies the V8 / JavaScriptCore runtime environment against Prototype Pollution attacks:
18415
+ * 1. Blocks unauthorized additions and mutations to `Object.prototype`, `Array.prototype`, and `Function.prototype`.
18416
+ * 2. Neutralizes mutable `__proto__` accessors exploited by recursive merge/patch vectors.
18417
+ * 3. Enforces strict boot-time configuration schema validation for `xypriss.config.json(c)`.
18418
+ *
18419
+ * @module server/utils/ProtoShield
18420
+ * @see https://xypriss.nehonix.com/docs/security/protoshield
18421
+ */
18422
+ class ProtoShield {
18423
+ static isInitialized = false;
18424
+ static activeConfig = null;
18425
+ /**
18426
+ * Validates raw JSON configuration from `xypriss.config.json(c)` at boot time.
18427
+ * Throws an explicit, descriptive error if any property is invalid.
18428
+ */
18429
+ static validateConfig(raw) {
18430
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
18431
+ throw new Error(`[XyPriss Validation Error] '$protoShield' must be a valid JSON object in xypriss.config.json(c). Received: ${typeof raw}`);
18432
+ }
18433
+ const allowedKeys = new Set([
18434
+ "enabled",
18435
+ "mode",
18436
+ "neutralizeProto",
18437
+ "freezePrototypes",
18438
+ "allowedProperties",
18439
+ ]);
18440
+ for (const key of Object.keys(raw)) {
18441
+ if (!allowedKeys.has(key)) {
18442
+ throw new Error(`[XyPriss Validation Error] Unknown property '$protoShield.${key}' in xypriss.config.json(c). Allowed properties: ${[...allowedKeys].map((k) => `'${k}'`).join(", ")}`);
18443
+ }
18444
+ }
18445
+ if (raw.enabled !== undefined && typeof raw.enabled !== "boolean") {
18446
+ throw new Error(`[XyPriss Validation Error] '$protoShield.enabled' must be a boolean. Received: ${typeof raw.enabled} (${JSON.stringify(raw.enabled)})`);
18447
+ }
18448
+ if (raw.mode !== undefined) {
18449
+ const acceptedModes = ["block", "warn", "freeze"];
18450
+ if (!acceptedModes.includes(raw.mode)) {
18451
+ throw new Error(`[XyPriss Validation Error] Invalid '$protoShield.mode': ${JSON.stringify(raw.mode)}. Accepted values are: 'block', 'warn', 'freeze'.`);
18452
+ }
18453
+ }
18454
+ if (raw.neutralizeProto !== undefined && typeof raw.neutralizeProto !== "boolean") {
18455
+ throw new Error(`[XyPriss Validation Error] '$protoShield.neutralizeProto' must be a boolean. Received: ${typeof raw.neutralizeProto}`);
18456
+ }
18457
+ if (raw.freezePrototypes !== undefined && typeof raw.freezePrototypes !== "boolean") {
18458
+ throw new Error(`[XyPriss Validation Error] '$protoShield.freezePrototypes' must be a boolean. Received: ${typeof raw.freezePrototypes}`);
18459
+ }
18460
+ if (raw.allowedProperties !== undefined) {
18461
+ if (!Array.isArray(raw.allowedProperties)) {
18462
+ throw new Error(`[XyPriss Validation Error] '$protoShield.allowedProperties' must be an array of strings. Received: ${typeof raw.allowedProperties}`);
18463
+ }
18464
+ for (let i = 0; i < raw.allowedProperties.length; i++) {
18465
+ const item = raw.allowedProperties[i];
18466
+ if (typeof item !== "string" || !item.trim()) {
18467
+ throw new Error(`[XyPriss Validation Error] '$protoShield.allowedProperties[${i}]' must be a non-empty string. Received: ${JSON.stringify(item)}`);
18468
+ }
18469
+ }
18470
+ }
18471
+ return {
18472
+ enabled: raw.enabled !== undefined ? raw.enabled : true,
18473
+ mode: raw.mode || "block",
18474
+ neutralizeProto: raw.neutralizeProto !== undefined ? raw.neutralizeProto : true,
18475
+ freezePrototypes: raw.freezePrototypes !== undefined ? raw.freezePrototypes : false,
18476
+ allowedProperties: Array.isArray(raw.allowedProperties) ? [...raw.allowedProperties] : [],
18477
+ };
18478
+ }
18479
+ /**
18480
+ * Bootstraps the ProtoShield defense according to the validated configuration.
18481
+ */
18482
+ static initialize(rawConfig) {
18483
+ if (this.isInitialized)
18484
+ return;
18485
+ const config = this.validateConfig(rawConfig);
18486
+ this.activeConfig = config;
18487
+ if (!config.enabled) {
18488
+ this.isInitialized = true;
18489
+ return;
18490
+ }
18491
+ const allowedSet = new Set([...config.allowedProperties, "__proto__"]);
18492
+ // 1. Neutralize mutable __proto__ accessor if enabled (done before strict interception)
18493
+ if (config.neutralizeProto) {
18494
+ this.applyNeutralizeProto(config.mode);
18495
+ }
18496
+ // 2. Intercept explicit property definitions on core prototypes
18497
+ this.applyPropertyInterception(allowedSet, config.mode);
18498
+ // 3. Prevent dynamic property expansion or enforce total immutability
18499
+ if (config.mode === "freeze" || config.freezePrototypes) {
18500
+ try {
18501
+ Object.freeze(Object.prototype);
18502
+ Object.freeze(Array.prototype);
18503
+ Object.freeze(Function.prototype);
18504
+ }
18505
+ catch { }
18506
+ }
18507
+ else if (config.mode === "block") {
18508
+ try {
18509
+ Object.preventExtensions(Object.prototype);
18510
+ }
18511
+ catch { }
18512
+ }
18513
+ this.isInitialized = true;
18514
+ }
18515
+ /**
18516
+ * Intercepts `Object.defineProperty` on base prototypes to thwart unauthorized extension.
18517
+ */
18518
+ static applyPropertyInterception(allowed, mode) {
18519
+ const origDefineProperty = Object.defineProperty;
18520
+ const targetPrototypes = [Object.prototype, Array.prototype, Function.prototype];
18521
+ Object.defineProperty = function (target, propertyKey, attributes) {
18522
+ if (targetPrototypes.includes(target)) {
18523
+ const propStr = String(propertyKey);
18524
+ if (!allowed.has(propStr)) {
18525
+ const message = `[XyPriss ProtoShield] Blocked Prototype Pollution attempt! Unauthorized property '${propStr}' on ${target?.constructor?.name || "Object"}.prototype`;
18526
+ if (mode === "warn") {
18527
+ console.warn(`\x1b[33m${message}\x1b[0m\n` + (new Error().stack || ""));
18528
+ }
18529
+ else {
18530
+ const err = new TypeError(message);
18531
+ console.error(`\x1b[31m${message}\x1b[0m\n` + (err.stack || ""));
18532
+ throw err;
18533
+ }
18534
+ }
18535
+ }
18536
+ return origDefineProperty.call(Object, target, propertyKey, attributes);
18537
+ };
18538
+ }
18539
+ /**
18540
+ * Intercepts and neutralizes the mutable `__proto__` setter on `Object.prototype`.
18541
+ */
18542
+ static applyNeutralizeProto(mode) {
18543
+ try {
18544
+ const origDesc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");
18545
+ if (origDesc && origDesc.set) {
18546
+ Object.defineProperty(Object.prototype, "__proto__", {
18547
+ get() {
18548
+ return Object.getPrototypeOf(this);
18549
+ },
18550
+ set(_newProto) {
18551
+ const message = `[XyPriss ProtoShield] Blocked Prototype Mutation attempt via '__proto__'!`;
18552
+ if (mode === "warn") {
18553
+ console.warn(`\x1b[33m${message}\x1b[0m\n` + (new Error().stack || ""));
18554
+ }
18555
+ else {
18556
+ const err = new TypeError(message);
18557
+ console.error(`\x1b[31m${message}\x1b[0m\n` + (err.stack || ""));
18558
+ throw err;
18559
+ }
18560
+ },
18561
+ configurable: false,
18562
+ enumerable: false,
18563
+ });
18564
+ }
18565
+ }
18566
+ catch { }
18567
+ }
18568
+ static getActiveConfig() {
18569
+ return this.activeConfig;
18570
+ }
18571
+ }
18572
+
18407
18573
  /**
18408
18574
  * XyPriss Meta Configuration Runner
18409
18575
  *
@@ -18444,7 +18610,10 @@ class MetaConfigRunner {
18444
18610
  */
18445
18611
  executeMetaConfig(searchDir) {
18446
18612
  const sys = getSysApi$1();
18447
- const root = searchDir || getCallerProjectRoot() || __sys__$1.__root__;
18613
+ let root = searchDir || getCallerProjectRoot();
18614
+ if (!root || root === ENGINE_CORE_DIR) {
18615
+ root = getPrimaryProjectRoot() || __sys__$1.__root__;
18616
+ }
18448
18617
  const metaFiles = searchDir
18449
18618
  ? [
18450
18619
  sys.path.join(root, "+xypriss.meta.ts"),
@@ -18497,7 +18666,10 @@ class ConfigLoader {
18497
18666
  * Load all xypriss.config.json files found in the project and apply configurations
18498
18667
  */
18499
18668
  loadAndApplySysConfig(targetRoot) {
18500
- const root = targetRoot || getCallerProjectRoot() || __sys__$1.__root__;
18669
+ let root = targetRoot || getCallerProjectRoot();
18670
+ if (!root || root === ENGINE_CORE_DIR) {
18671
+ root = getPrimaryProjectRoot() || __sys__$1.__root__;
18672
+ }
18501
18673
  if (!root || this.appliedRoots.has(root))
18502
18674
  return;
18503
18675
  this.appliedRoots.add(root);
@@ -18569,6 +18741,13 @@ class ConfigLoader {
18569
18741
  __sys__$1.vars.update(config.$vars);
18570
18742
  }
18571
18743
  }
18744
+ // Initialize and enforce ProtoShield protection if declared ($protoShield or protoShield)
18745
+ const protoConfig = config?.$protoShield !== undefined
18746
+ ? config.$protoShield
18747
+ : config?.protoShield;
18748
+ if (protoConfig !== undefined) {
18749
+ ProtoShield.initialize(protoConfig);
18750
+ }
18572
18751
  // Apply ServerOptions sections to Configs (centralized configuration management)
18573
18752
  const recognizedServerOptionKeys = [
18574
18753
  "server",
@@ -18625,8 +18804,7 @@ class ConfigLoader {
18625
18804
  if (config.__xfs__ && config.__xfs__.path) {
18626
18805
  const rawPath = config.__xfs__.path;
18627
18806
  const resolvedFsPath = this.resolvePath(rawPath, projectRoot, configDir);
18628
- if (resolvedFsPath &&
18629
- this.sys.fs.exist(resolvedFsPath)) {
18807
+ if (resolvedFsPath && this.sys.fs.exist(resolvedFsPath)) {
18630
18808
  const specializedFS = new XyPrissFS({
18631
18809
  __root__: resolvedFsPath,
18632
18810
  __mode__: __sys__$1.__env__.mode,
@@ -29498,8 +29676,12 @@ class LogProcessor {
29498
29676
  }
29499
29677
  }
29500
29678
 
29679
+ // S = Ox53 (dec: 5*16 + 3 = 83)
29680
+ // N = Ox4E (dec: 4*16 + 14 = 78)
29681
+ // P = Ox50 (dec: 5*16 + 0 = 80)
29682
+ // X = Ox58 (dec: 5*16 + 8 = 88)
29501
29683
  const MAGIC_HEADER = Buffer.from([0x53, 0x4e, 0x50, 0x58]); // "SNPX"
29502
- const CURRENT_VERSION = 0x0100;
29684
+ const CURRENT_VERSION = 0x0100; // 1
29503
29685
  const NONCE_SIZE = 12;
29504
29686
  const HEADER_SIZE = 8;
29505
29687
  /**
@@ -29572,12 +29754,14 @@ class EngineManager {
29572
29754
  uniquePluginPaths.length > 0
29573
29755
  ? ["--plugins", uniquePluginPaths.join(",")]
29574
29756
  : [];
29575
- // Access project root lazily via getSysApi() to break circular imports
29757
+ // Strict authoritative project root resolution (Fail-Closed)
29576
29758
  const sys = getSysApi$1();
29577
- const projectRoot = sys?.__root__ ||
29578
- getCallerProjectRoot() ||
29579
- identifyProjectRoot(process.cwd()) ||
29580
- process.cwd();
29759
+ const sysRoot = sys?.__root__;
29760
+ const projectRoot = getPrimaryProjectRoot() ||
29761
+ (sysRoot && sysRoot !== ENGINE_CORE_DIR ? sysRoot : "");
29762
+ if (!projectRoot || !isProjectRoot(projectRoot)) {
29763
+ throw new Error(`[XyPriss Security] Fail-Closed: Cannot resolve an authoritative project root for XHSC engine.`);
29764
+ }
29581
29765
  const entryPoint = process.argv[1] ||
29582
29766
  appConfigs?.cluster?.entryPoint ||
29583
29767
  path.join(projectRoot, "src", "server.ts");
@@ -41593,6 +41777,7 @@ exports.FLA = FileUploadAPI;
41593
41777
  exports.FileUploadAPI = FileUploadAPI;
41594
41778
  exports.Plugin = Plugin;
41595
41779
  exports.PluginHookIds = PluginHookIds;
41780
+ exports.ProtoShield = ProtoShield;
41596
41781
  exports.Router = Router;
41597
41782
  exports.SecurityMiddleware = SecurityMiddleware;
41598
41783
  exports.Send = Send;