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): validate the session archive name in the @claude-flow/mcp dev helper

Dragan Spiridonov1cf16ede914b
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 d8e8f8ad81..bb0cd874a7 100644
--- a/v3/@claude-flow/cli/__tests__/session-helper-trust.test.ts
+++ b/v3/@claude-flow/cli/__tests__/session-helper-trust.test.ts
@@ -24,6 +24,7 @@ afterEach(() => {
const sources: ReadonlyArray<readonly [string, () => string | null, string | null]> = [
['package', () => null, resolve(here, '../.claude/helpers/session.cjs')],
['repository', () => null, resolve(here, '../../../../.claude/helpers/session.cjs')],
+ ['@claude-flow/mcp repository', () => null, resolve(here, '../../mcp/.claude/helpers/session.js')],
['generated', generateSessionManager, null],
['generated cross-platform', generateCrossPlatformSessionManager, null],
];
diff --git a/v3/@claude-flow/mcp/.claude/helpers/session.js b/v3/@claude-flow/mcp/.claude/helpers/session.js
index ab32233744..bdbb2ae0cf 100644
--- a/v3/@claude-flow/mcp/.claude/helpers/session.js
+++ b/v3/@claude-flow/mcp/.claude/helpers/session.js
@@ -6,10 +6,43 @@
const fs = require('fs');
const path = require('path');
+const crypto = require('crypto');
const SESSION_DIR = path.join(process.cwd(), '.claude-flow', 'sessions');
const SESSION_FILE = path.join(SESSION_DIR, 'current.json');
+// Archive names are generated by start(); anything else in current.json is
+// untrusted project content and must never pick a path.
+const SESSION_ID = /^session-\d+$/;
+
+// current.json lives in the opened project. A symlink there must never be read
+// or written through, so only a regular file counts as an active session.
+function hasSessionFile() {
+ try { return fs.lstatSync(SESSION_FILE).isFile(); } catch { return false; }
+}
+
+function archiveId(id) {
+ return SESSION_ID.test(String(id)) ? String(id) : `session-${Date.now()}`;
+}
+
+// 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));
+ if (rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
+ throw new Error('Session directory resolves outside the project');
+ }
+}
+
+// Exclusive temp file + rename: never writes through a planted symlink, and
+// rename() replaces a symlinked target instead of following it.
+function atomicWrite(file, data) {
+ assertSessionDirContained();
+ const tmp = `${file}.tmp.${process.pid}.${crypto.randomBytes(6).toString('hex')}`;
+ fs.writeFileSync(tmp, data, { flag: 'wx' });
+ fs.renameSync(tmp, file);
+}
+
const commands = {
start: () => {
const sessionId = `session-${Date.now()}`;
@@ -27,28 +60,28 @@ const commands = {
};
fs.mkdirSync(SESSION_DIR, { recursive: true });
- fs.writeFileSync(SESSION_FILE, JSON.stringify(session, null, 2));
+ atomicWrite(SESSION_FILE, JSON.stringify(session, null, 2));
console.log(`Session started: ${sessionId}`);
return session;
},
restore: () => {
- if (!fs.existsSync(SESSION_FILE)) {
+ if (!hasSessionFile()) {
console.log('No session to restore');
return null;
}
const session = JSON.parse(fs.readFileSync(SESSION_FILE, 'utf-8'));
session.restoredAt = new Date().toISOString();
- fs.writeFileSync(SESSION_FILE, JSON.stringify(session, null, 2));
+ atomicWrite(SESSION_FILE, JSON.stringify(session, null, 2));
console.log(`Session restored: ${session.id}`);
return session;
},
end: () => {
- if (!fs.existsSync(SESSION_FILE)) {
+ if (!hasSessionFile()) {
console.log('No active session');
return null;
}
@@ -57,12 +90,12 @@ const commands = {
session.endedAt = new Date().toISOString();
session.duration = Date.now() - new Date(session.startedAt).getTime();
- // Archive session
- const archivePath = path.join(SESSION_DIR, `${session.id}.json`);
- fs.writeFileSync(archivePath, JSON.stringify(session, null, 2));
+ // Archive session under a validated name; a crafted id cannot pick the path.
+ const id = archiveId(session.id);
+ atomicWrite(path.join(SESSION_DIR, `${id}.json`), JSON.stringify(session, null, 2));
fs.unlinkSync(SESSION_FILE);
- console.log(`Session ended: ${session.id}`);
+ console.log(`Session ended: ${id}`);
console.log(`Duration: ${Math.round(session.duration / 1000 / 60)} minutes`);
console.log(`Metrics: ${JSON.stringify(session.metrics)}`);
@@ -70,7 +103,7 @@ const commands = {
},
status: () => {
- if (!fs.existsSync(SESSION_FILE)) {
+ if (!hasSessionFile()) {
console.log('No active session');
return null;
}
@@ -87,7 +120,7 @@ const commands = {
},
update: (key, value) => {
- if (!fs.existsSync(SESSION_FILE)) {
+ if (!hasSessionFile()) {
console.log('No active session');
return null;
}
@@ -95,20 +128,20 @@ const commands = {
const session = JSON.parse(fs.readFileSync(SESSION_FILE, 'utf-8'));
session.context[key] = value;
session.updatedAt = new Date().toISOString();
- fs.writeFileSync(SESSION_FILE, JSON.stringify(session, null, 2));
+ atomicWrite(SESSION_FILE, JSON.stringify(session, null, 2));
return session;
},
metric: (name) => {
- if (!fs.existsSync(SESSION_FILE)) {
+ if (!hasSessionFile()) {
return null;
}
const session = JSON.parse(fs.readFileSync(SESSION_FILE, 'utf-8'));
if (session.metrics[name] !== undefined) {
session.metrics[name]++;
- fs.writeFileSync(SESSION_FILE, JSON.stringify(session, null, 2));
+ atomicWrite(SESSION_FILE, JSON.stringify(session, null, 2));
}
return session;