mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-25 06:36:49 +09:00
Merge PR #3459: fix(config): prevent prototype traversal through dotted keys
This commit is contained in:
commit
64eb6eecbc
2 files changed
+54
-2
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { ConfigFileManager } from '../src/services/config-file-manager.js';
|
||||
|
||||
let cwd: string;
|
||||
let file: string;
|
||||
beforeEach(() => {
|
||||
cwd = mkdtempSync(join(tmpdir(), 'config-prototype-'));
|
||||
file = join(cwd, 'claude-flow.config.json');
|
||||
writeFileSync(file, '{"custom":{"keep":true}}');
|
||||
vi.stubEnv('CLAUDE_FLOW_CONFIG', '');
|
||||
});
|
||||
afterEach(() => {
|
||||
delete (Object.prototype as any).configPolluted;
|
||||
vi.unstubAllEnvs();
|
||||
rmSync(cwd, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe('configuration property traversal', () => {
|
||||
it.each(['__proto__.configPolluted', 'custom.__proto__.configPolluted', 'constructor.prototype.configPolluted', 'custom.constructor.prototype.configPolluted', 'custom.prototype.configPolluted', '__proto__'])('rejects unsafe key %s before mutating memory or disk', (key) => {
|
||||
const manager = new ConfigFileManager();
|
||||
manager.load(cwd);
|
||||
const before = readFileSync(file, 'utf8');
|
||||
let error: unknown;
|
||||
try { manager.set(cwd, key, 'tainted'); } catch (caught) { error = caught; }
|
||||
expect(({} as any).configPolluted).toBeUndefined();
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(String(error)).toMatch(/unsafe.*key/i);
|
||||
expect(readFileSync(file, 'utf8')).toBe(before);
|
||||
expect(manager.get(cwd, 'custom.keep')).toBe(true);
|
||||
});
|
||||
|
||||
it('does not read inherited properties as configuration values', () => {
|
||||
const manager = new ConfigFileManager();
|
||||
expect(manager.get(cwd, 'toString')).toBeUndefined();
|
||||
expect(manager.get(cwd, 'custom.constructor')).toBeUndefined();
|
||||
expect(manager.get(cwd, 'custom.__proto__')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('continues to write ordinary nested own properties', () => {
|
||||
const manager = new ConfigFileManager();
|
||||
manager.set(cwd, 'custom.enabled', false);
|
||||
manager.set(cwd, 'custom.count', 0);
|
||||
expect(JSON.parse(readFileSync(file, 'utf8'))).toEqual({ custom: { keep: true, enabled: false, count: 0 } });
|
||||
});
|
||||
});
|
||||
@@ -121,6 +121,10 @@ export class ConfigFileManager {
|
||||
|
||||
/** Set a nested config value by dot-separated key */
|
||||
set(cwd: string, key: string, value: unknown): void {
|
||||
// Reject the entire path before touching cached config or disk.
|
||||
if (key.split('.').some(part => part === '__proto__' || part === 'constructor' || part === 'prototype')) {
|
||||
throw new Error(`Unsafe configuration key: ${key}`);
|
||||
}
|
||||
// A targeted update must not persist unrelated defaults (notably the
|
||||
// default memory path, which would relocate an existing memory store).
|
||||
const config = this.config ?? this.load(cwd) ?? {};
|
||||
@@ -213,7 +217,7 @@ function getNestedValue(obj: Record<string, unknown>, key: string): unknown {
|
||||
const parts = key.split('.');
|
||||
let current: unknown = obj;
|
||||
for (const part of parts) {
|
||||
if (current === null || current === undefined || typeof current !== 'object') {
|
||||
if (current === null || current === undefined || typeof current !== 'object' || !Object.hasOwn(current, part)) {
|
||||
return undefined;
|
||||
}
|
||||
current = (current as Record<string, unknown>)[part];
|
||||
@@ -227,7 +231,7 @@ function setNestedValue(obj: Record<string, unknown>, key: string, value: unknow
|
||||
let current: Record<string, unknown> = obj;
|
||||
for (let i = 0; i < parts.length - 1; i++) {
|
||||
const part = parts[i];
|
||||
if (!(part in current) || typeof current[part] !== 'object' || current[part] === null) {
|
||||
if (!Object.hasOwn(current, part) || typeof current[part] !== 'object' || current[part] === null) {
|
||||
current[part] = {};
|
||||
}
|
||||
current = current[part] as Record<string, unknown>;
|
||||
|
||||
Reference in new issue
Block a user