Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(helpers): check session dir containment before creating it

Dragan Spiridonovbc249a0a50d2
diff --git a/.claude/helpers/session.cjs b/.claude/helpers/session.cjs
index 3647afada0..4a63c3d977 100644
--- a/.claude/helpers/session.cjs
+++ b/.claude/helpers/session.cjs
@@ -50,7 +50,16 @@ function archiveId(id) {
function assertSessionDirContained() {
if (SESSION_DIR !== path.join(process.cwd(), '.claude-flow', 'sessions')) return;
const root = fs.realpathSync(process.cwd());
- const rel = path.relative(root, fs.realpathSync(SESSION_DIR));
+ // Resolve the deepest part of the path that exists (lstat: a symlink counts),
+ // so a symlinked .claude-flow is refused before anything is created under it.
+ let dir = SESSION_DIR;
+ for (;;) {
+ try { fs.lstatSync(dir); break; } catch { /* not created yet */ }
+ const parent = path.dirname(dir);
+ if (parent === dir) break;
+ dir = parent;
+ }
+ const rel = path.relative(root, fs.realpathSync(dir));
if (rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
throw new Error('Session directory resolves outside the project');
}
@@ -73,6 +82,7 @@ function ensureDir(dir) {
const commands = {
start: () => {
+ assertSessionDirContained();
ensureDir(SESSION_DIR);
const sessionId = `session-${Date.now()}`;
const session = {
diff --git a/v3/@claude-flow/cli/.claude/helpers/session.cjs b/v3/@claude-flow/cli/.claude/helpers/session.cjs
index 3f91aa523e..34c45c773a 100644
--- a/v3/@claude-flow/cli/.claude/helpers/session.cjs
+++ b/v3/@claude-flow/cli/.claude/helpers/session.cjs
@@ -27,7 +27,16 @@ function archiveId(id) {
// The session directory must resolve inside the project (no symlinked escape).
function assertSessionDirContained() {
const root = fs.realpathSync(process.cwd());
- const rel = path.relative(root, fs.realpathSync(SESSION_DIR));
+ // Resolve the deepest part of the path that exists (lstat: a symlink counts),
+ // so a symlinked .claude-flow is refused before anything is created under it.
+ let dir = SESSION_DIR;
+ for (;;) {
+ try { fs.lstatSync(dir); break; } catch { /* not created yet */ }
+ const parent = path.dirname(dir);
+ if (parent === dir) break;
+ dir = parent;
+ }
+ const rel = path.relative(root, fs.realpathSync(dir));
if (rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
throw new Error('Session directory resolves outside the project');
}
@@ -66,6 +75,7 @@ const commands = {
},
};
+ assertSessionDirContained();
fs.mkdirSync(SESSION_DIR, { recursive: true });
atomicWrite(SESSION_FILE, JSON.stringify(session, null, 2));
diff --git a/v3/@claude-flow/cli/__tests__/session-helper-trust.test.ts b/v3/@claude-flow/cli/__tests__/session-helper-trust.test.ts
index bb0cd874a7..bb7f91db2c 100644
--- a/v3/@claude-flow/cli/__tests__/session-helper-trust.test.ts
+++ b/v3/@claude-flow/cli/__tests__/session-helper-trust.test.ts
@@ -105,6 +105,15 @@ describe('session helper keeps writes inside the project session dir', () => {
expect(readdirSync(f.outside)).toEqual([]);
});
+ it('creates nothing outside the project when .claude-flow is a symlink', () => {
+ const f = fixture(generate, source);
+ symlinkSync(f.outside, join(f.project, '.claude-flow'), 'dir');
+
+ run(f, 'start');
+
+ expect(readdirSync(f.outside)).toEqual([]);
+ });
+
it('keeps the normal start / restore / end lifecycle working', () => {
const f = fixture(generate, source);
mkdirSync(join(f.project, '.claude-flow'), { recursive: true });
diff --git a/v3/@claude-flow/cli/src/init/helpers-generator.ts b/v3/@claude-flow/cli/src/init/helpers-generator.ts
index e7ee6972f4..6e3abb98b2 100644
--- a/v3/@claude-flow/cli/src/init/helpers-generator.ts
+++ b/v3/@claude-flow/cli/src/init/helpers-generator.ts
@@ -154,7 +154,16 @@ function archiveId(id) {
// The session directory must resolve inside the project (no symlinked escape).
function assertSessionDirContained() {
const root = fs.realpathSync(process.cwd());
- const rel = path.relative(root, fs.realpathSync(SESSION_DIR));
+ // Resolve the deepest part of the path that exists (lstat: a symlink counts),
+ // so a symlinked .claude-flow is refused before anything is created under it.
+ let dir = SESSION_DIR;
+ for (;;) {
+ try { fs.lstatSync(dir); break; } catch { /* not created yet */ }
+ const parent = path.dirname(dir);
+ if (parent === dir) break;
+ dir = parent;
+ }
+ const rel = path.relative(root, fs.realpathSync(dir));
if (rel === '..' || rel.startsWith(\`..\${path.sep}\`) || path.isAbsolute(rel)) {
throw new Error('Session directory resolves outside the project');
}
@@ -185,6 +194,7 @@ const commands = {
},
};
+ assertSessionDirContained();
fs.mkdirSync(SESSION_DIR, { recursive: true });
atomicWrite(SESSION_FILE, JSON.stringify(session, null, 2));
@@ -1501,7 +1511,16 @@ function archiveId(id) {
function assertSessionDirContained() {
if (SESSION_DIR !== path.join(process.cwd(), '.claude-flow', 'sessions')) return;
const root = fs.realpathSync(process.cwd());
- const rel = path.relative(root, fs.realpathSync(SESSION_DIR));
+ // Resolve the deepest part of the path that exists (lstat: a symlink counts),
+ // so a symlinked .claude-flow is refused before anything is created under it.
+ let dir = SESSION_DIR;
+ for (;;) {
+ try { fs.lstatSync(dir); break; } catch { /* not created yet */ }
+ const parent = path.dirname(dir);
+ if (parent === dir) break;
+ dir = parent;
+ }
+ const rel = path.relative(root, fs.realpathSync(dir));
if (rel === '..' || rel.startsWith(\`..\${path.sep}\`) || path.isAbsolute(rel)) {
throw new Error('Session directory resolves outside the project');
}
@@ -1525,6 +1544,7 @@ function ensureDir(dir) {
const commands = {
start: () => {
+ assertSessionDirContained();
ensureDir(SESSION_DIR);
const sessionId = \`session-\${Date.now()}\`;
const session = {
diff --git a/v3/@claude-flow/mcp/.claude/helpers/session.js b/v3/@claude-flow/mcp/.claude/helpers/session.js
index bdbb2ae0cf..080e30c342 100644
--- a/v3/@claude-flow/mcp/.claude/helpers/session.js
+++ b/v3/@claude-flow/mcp/.claude/helpers/session.js
@@ -28,7 +28,16 @@ function archiveId(id) {
// The session directory must resolve inside the project (no symlinked escape).
function assertSessionDirContained() {
const root = fs.realpathSync(process.cwd());
- const rel = path.relative(root, fs.realpathSync(SESSION_DIR));
+ // Resolve the deepest part of the path that exists (lstat: a symlink counts),
+ // so a symlinked .claude-flow is refused before anything is created under it.
+ let dir = SESSION_DIR;
+ for (;;) {
+ try { fs.lstatSync(dir); break; } catch { /* not created yet */ }
+ const parent = path.dirname(dir);
+ if (parent === dir) break;
+ dir = parent;
+ }
+ const rel = path.relative(root, fs.realpathSync(dir));
if (rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
throw new Error('Session directory resolves outside the project');
}
@@ -59,6 +68,7 @@ const commands = {
},
};
+ assertSessionDirContained();
fs.mkdirSync(SESSION_DIR, { recursive: true });
atomicWrite(SESSION_FILE, JSON.stringify(session, null, 2));