Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/shared/config/ConfigurationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
});
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/configurationManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down