From 29dd33880b50adf2b76d997ab9f5c068ef04450b Mon Sep 17 00:00:00 2001 From: Levy Tate <78818969+iLevyTate@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:07:36 -0500 Subject: [PATCH] Confine configuration overrides to own safe properties --- src/shared/config/ConfigurationManager.js | 31 ++++++++++++++++++----- test/configurationManager.test.js | 11 ++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/shared/config/ConfigurationManager.js b/src/shared/config/ConfigurationManager.js index cc816259..81978787 100644 --- a/src/shared/config/ConfigurationManager.js +++ b/src/shared/config/ConfigurationManager.js @@ -20,7 +20,6 @@ const { parseEnvValue, getEnvVar, validateValue } = require('./configValidation' // Path segments override() must never walk into or assign: they reach the // object prototype instead of a config value. -const UNSAFE_PATH_KEYS = new Set(['__proto__', 'constructor', 'prototype']); /** * Configuration Manager Class @@ -267,7 +266,9 @@ class ConfigurationManager { } const parts = path.split('.'); - if (parts.some((part) => UNSAFE_PATH_KEYS.has(part))) { + if ( + parts.some((part) => part === '__proto__' || part === 'constructor' || part === 'prototype') + ) { this._validationErrors.push({ path, value, reason: 'unsafe_path' }); return; } @@ -294,13 +295,31 @@ class ConfigurationManager { let current = this._config; for (let i = 0; i < parts.length - 1; i++) { - if (!current[parts[i]]) { - current[parts[i]] = {}; + const key = parts[i]; + if (key === '__proto__' || key === 'constructor' || key === 'prototype') return; + if ( + !Object.hasOwn(current, key) || + typeof current[key] !== 'object' || + current[key] === null + ) { + Object.defineProperty(current, key, { + value: {}, + writable: true, + enumerable: true, + configurable: true + }); } - current = current[parts[i]]; + current = current[key]; } - current[parts[parts.length - 1]] = value; + const key = parts[parts.length - 1]; + if (key === '__proto__' || key === 'constructor' || key === 'prototype') return; + Object.defineProperty(current, key, { + value, + writable: true, + enumerable: true, + configurable: true + }); } /** diff --git a/test/configurationManager.test.js b/test/configurationManager.test.js index 25374518..88ab3681 100644 --- a/test/configurationManager.test.js +++ b/test/configurationManager.test.js @@ -104,6 +104,17 @@ describe('ConfigurationManager', () => { } ); + test('override creates own containers without mutating inherited objects', () => { + const mgr = new ConfigurationManager(); + mgr.load(); + mgr.override('CUSTOM.toString.flag', true); + expect(Object.prototype.toString).not.toHaveProperty('flag'); + expect(mgr.get('CUSTOM.toString.flag')).toBe(true); + mgr.override('CUSTOM.runtime', 1); + mgr.override('CUSTOM.runtime.flag', true); + expect(mgr.get('CUSTOM.runtime.flag')).toBe(true); + }); + test('override writes unknown nested paths and reset restores defaults', () => { const mgr = new ConfigurationManager(); mgr.load();