Skip to content
Open
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
48 changes: 35 additions & 13 deletions src/agents/agentBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,19 @@ export class AgentBus {
.sort();

for (const file of newFiles) {
const content = await fs.readFile(resolve(this.messagesPath, file), 'utf-8');
const message: AgentMessage = JSON.parse(content);
// One bad file must not wedge the queue: a parse or shape failure used
// to escape to the outer catch, abort this pass and, because the file
// was never marked processed, abort every later pass too.
let message: AgentMessage;
try {
const parsed: unknown = JSON.parse(await fs.readFile(resolve(this.messagesPath, file), 'utf-8'));
if (!isWellFormedMessage(parsed)) throw new Error('message is missing required fields');
message = parsed;
} catch (error) {
await this.quarantineMessageFile(file, error);
this.markProcessed(file);
continue;
}

// Invoke listeners
const callbacks = this.listeners.get(message.type);
Expand All @@ -452,16 +463,32 @@ export class AgentBus {
}
}

this.processedMessageIds.add(file);
if (this.processedMessageIds.size > MAX_RETAINED_MESSAGES) {
this.processedMessageIds.delete(this.processedMessageIds.values().next().value!);
}
this.markProcessed(file);
}
} catch {
// Ignore
}
}

private markProcessed(file: string): void {
this.processedMessageIds.add(file);
if (this.processedMessageIds.size > MAX_RETAINED_MESSAGES) {
this.processedMessageIds.delete(this.processedMessageIds.values().next().value!);
}
}

/**
* Move a malformed message file beside the messages dir (kept for inspection,
* never deleted) and log which file and why.
*/
private async quarantineMessageFile(file: string, reason: unknown): Promise<void> {
const quarantineDir = resolve(this.messagesPath, '..', QUARANTINE_DIRNAME);
const detail = reason instanceof Error ? reason.message : String(reason);
console.warn(`[AgentBus] Quarantined malformed message ${file} (execution ${this.executionId}): ${detail.slice(0, 200)}`);
await fs.mkdir(quarantineDir, { recursive: true }).catch(() => {});
await fs.rename(resolve(this.messagesPath, file), join(quarantineDir, file)).catch(() => {});
}

/**
* Get all messages
*/
Expand All @@ -478,15 +505,10 @@ export class AgentBus {
throw new Error('message is missing required fields');
}
messages.push(parsed);
} catch {
} catch (error) {
// Malformed history is quarantined beside the messages dir, so a
// corrupt payload cannot wedge the whole retrieval pass.
const quarantineDir = resolve(this.messagesPath, '..', QUARANTINE_DIRNAME);
await fs.mkdir(quarantineDir, { recursive: true }).catch(() => {});
await fs.rename(
resolve(this.messagesPath, file),
join(quarantineDir, file),
).catch(() => {});
await this.quarantineMessageFile(file, error);
}
}

Expand Down
36 changes: 35 additions & 1 deletion src/agents/artifactValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { AgentBus, BUS_DIR } from './agentBus.js';
import { parseAuditorOutput } from './auditor.js';
import { parseDocumenterOutput } from './documenter.js';
Expand Down Expand Up @@ -64,3 +64,37 @@ describe('auditor/documenter non-string output guard', () => {
expect(result.summary).toBe('Documenter output was not a string');
});
});

describe('agentBus polling skips malformed message files (AGT-3441)', () => {
it('quarantines a malformed file and still delivers the later valid message', async () => {
const executionId = `test-poll-${Date.now()}-${Math.random().toString(36).slice(2)}`;
const publisher = new AgentBus(executionId);
const subscriber = new AgentBus(executionId);
await publisher.init('workflow-123');
const messagesDir = resolve(BUS_DIR, executionId, 'messages');
// Sorted before every published file, so it is the first one polled.
await writeFile(join(messagesDir, '0000-corrupt.json'), '{ not json', 'utf-8');
await writeFile(join(messagesDir, '0001-wrong-shape.json'), JSON.stringify({ type: 'log' }), 'utf-8');

const seen: unknown[] = [];
subscriber.on('log', (msg) => { seen.push(msg.payload); });
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
try {
await publisher.publish('log', 'step-1', 'after the bad files');
await subscriber.pollOnce();
expect(seen).toEqual(['after the bad files']);

const quarantined = await readdir(resolve(messagesDir, '..', 'quarantine'));
expect(quarantined).toEqual(expect.arrayContaining(['0000-corrupt.json', '0001-wrong-shape.json']));
expect(warn).toHaveBeenCalledWith(expect.stringContaining('0000-corrupt.json'));

// A later poll sees the next message too; the bad files never come back.
await publisher.publish('log', 'step-2', 'second');
await subscriber.pollOnce();
expect(seen).toEqual(['after the bad files', 'second']);
} finally {
warn.mockRestore();
await publisher.cleanup();
}
});
});
53 changes: 53 additions & 0 deletions src/agents/guardPaths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { containedProjectPath, lexicallyContainedPath, partitionChangedPaths } from './guardPaths.js';

describe('guard path containment (AGT-3441)', () => {
let root: string;
let project: string;
let outside: string;

beforeEach(() => {
root = mkdtempSync(join(tmpdir(), 'openswarm-guardpaths-'));
project = join(root, 'project');
outside = join(root, 'outside');
mkdirSync(join(project, 'src'), { recursive: true });
mkdirSync(outside, { recursive: true });
writeFileSync(join(project, 'src', 'a.ts'), 'export const a = 1;\n');
writeFileSync(join(outside, 'secret.ts'), 'export const secret = 1;\n');
symlinkSync(outside, join(project, 'linked-dir'));
symlinkSync(join(outside, 'secret.ts'), join(project, 'src', 'linked-file.ts'));
});

afterEach(() => rmSync(root, { recursive: true, force: true }));

it('accepts project-relative paths, including absolute ones inside the project and new files', () => {
expect(containedProjectPath(project, 'src/a.ts')).toBe('src/a.ts');
expect(containedProjectPath(project, './src/a.ts')).toBe('src/a.ts');
expect(containedProjectPath(project, join(project, 'src', 'a.ts'))).toBe('src/a.ts');
expect(containedProjectPath(project, 'src/new/deep/file.ts')).toBe('src/new/deep/file.ts');
});

it('rejects traversal, absolute outside paths, empty and NUL paths lexically', () => {
for (const bad of ['../outside/secret.ts', 'src/../../outside/secret.ts', join(outside, 'secret.ts'), '', '.', 'src/a.ts\0x', '..']) {
expect(lexicallyContainedPath(project, bad)).toBeNull();
expect(containedProjectPath(project, bad)).toBeNull();
}
});

it('rejects paths that leave the project through a symlinked directory or file', () => {
expect(lexicallyContainedPath(project, 'linked-dir/secret.ts')).toBe('linked-dir/secret.ts');
expect(containedProjectPath(project, 'linked-dir/secret.ts')).toBeNull();
expect(containedProjectPath(project, 'linked-dir/not-yet.ts')).toBeNull();
expect(containedProjectPath(project, 'src/linked-file.ts')).toBeNull();
});

it('partitions a changed-file list', () => {
expect(partitionChangedPaths(project, ['src/a.ts', '../x.ts', 'linked-dir/secret.ts'])).toEqual({
contained: ['src/a.ts'],
escaped: ['../x.ts', 'linked-dir/secret.ts'],
});
});
});
75 changes: 75 additions & 0 deletions src/agents/guardPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// ============================================
// OpenSwarm - Pipeline guard path containment (AGT-3441)
// Changed-file paths come from the worker's report. Before a guard opens one,
// it must name a file inside the project: not absolute, no `..` escape, and no
// symlinked ancestor (or the file itself) that resolves outside the project.
// ============================================

import { realpathSync } from 'node:fs';
import { dirname, isAbsolute, normalize, relative, resolve } from 'node:path';

function realpathOrNull(path: string): string | null {
try {
return realpathSync(path);
} catch {
return null;
}
}

function within(root: string, candidate: string): boolean {
const rel = relative(root, candidate);
return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
}

/**
* Lexical check: a project-relative path with no absolute prefix, no `..`
* escape and no NUL byte. Returns the normalized relative path, or null.
*/
export function lexicallyContainedPath(projectPath: string, filePath: string): string | null {
if (typeof filePath !== 'string' || filePath.length === 0 || filePath.includes('\0')) return null;
const cleaned = normalize(filePath).replace(/\\/g, '/').replace(/^\.\//, '');
const absolute = isAbsolute(cleaned) ? cleaned : resolve(projectPath, cleaned);
const rel = normalize(relative(projectPath, absolute)).replace(/\\/g, '/');
if (rel === '' || rel === '.' || rel === '..' || rel.startsWith('../') || isAbsolute(rel)) return null;
return rel;
}

/**
* Physical check: the nearest existing path on the way to `rel` (the file
* itself when it exists) must resolve inside the real project root, so a
* symlinked directory or file cannot redirect a guard read outside it.
*/
export function physicallyContained(projectPath: string, rel: string): boolean {
const root = realpathOrNull(projectPath);
if (!root) return false;
let probe = resolve(projectPath, rel);
for (;;) {
const real = realpathOrNull(probe);
if (real) return within(root, real);
const parent = dirname(probe);
if (parent === probe || !within(resolve(projectPath), parent)) return false;
probe = parent;
}
}

/** Lexical + physical containment; returns the relative path or null. */
export function containedProjectPath(projectPath: string, filePath: string): string | null {
const rel = lexicallyContainedPath(projectPath, filePath);
return rel && physicallyContained(projectPath, rel) ? rel : null;
}

/** Split worker-reported changed files into contained and escaping paths. */
export function partitionChangedPaths(
projectPath: string,
files: readonly string[],
): { contained: string[]; escaped: string[] } {
const contained: string[] = [];
const escaped: string[] = [];
for (const file of files) {
const rel = containedProjectPath(projectPath, file);
if (rel) contained.push(rel);
else escaped.push(String(file));
}
return { contained, escaped };
}

39 changes: 39 additions & 0 deletions src/agents/pipelineGuards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,42 @@ describe('pipelineGuards — INT-2388 deterministic guards', () => {
});
});
});

describe('pipelineGuards — changed paths that escape the project (AGT-3441)', () => {
let root: string;
let repo: string;

beforeEach(() => {
root = mkdtempSync(join(tmpdir(), 'openswarm-guards-escape-'));
repo = join(root, 'repo');
mkdirSync(repo, { recursive: true });
mkdirSync(join(root, 'outside'), { recursive: true });
writeFileSync(join(root, 'outside', 'evil.ts'), 'const password = "hunter2"; // TODO fake\n');
execFileSync('git', ['init', '-q', '-b', 'main'], { cwd: repo });
});

afterEach(() => rmSync(root, { recursive: true, force: true }));

it('reports escaping paths and never hands them to file-reading guards', async () => {
const { symlinkSync } = await import('node:fs');
symlinkSync(join(root, 'outside'), join(repo, 'link'));
writeFileSync(join(repo, 'ok.ts'), 'export const ok = 1;\n');
const result = await runGuards(
mockWorker(['ok.ts', '../outside/evil.ts', join(root, 'outside', 'evil.ts'), 'link/evil.ts']),
repo,
{ bsDetector: true },
);
const containment = result.results.find(r => r.guard === 'pathContainment');
expect(containment?.blocking).toBe(false);
expect(containment?.passed).toBe(false);
expect(containment?.issues).toHaveLength(3);
// bsDetector only ever saw ok.ts
expect(guardIssues(result, 'bsDetector').every(issue => issue.includes('ok.ts'))).toBe(true);
});

it('adds no containment result when every path is inside the project', async () => {
writeFileSync(join(repo, 'ok.ts'), 'export const ok = 1;\n');
const result = await runGuards(mockWorker(['ok.ts']), repo, { bsDetector: true });
expect(result.results.find(r => r.guard === 'pathContainment')).toBeUndefined();
});
});
26 changes: 10 additions & 16 deletions src/agents/pipelineGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import { execFile } from 'node:child_process';
import { readFile } from 'node:fs/promises';
import { isAbsolute, join, normalize, relative, resolve } from 'node:path';
import { join } from 'node:path';
import { containedProjectPath, partitionChangedPaths } from './guardPaths.js';
import { promisify } from 'node:util';
import type { WorkerResult } from './agentPair.js';
import type { PipelineGuardsConfig } from '../core/types.js';
Expand Down Expand Up @@ -379,7 +380,7 @@ export async function getAddedLinesForFile(projectPath: string, filePath: string
// Fall through to untracked-new handling below.
}

if (!isNew) return '';
if (!isNew || !containedProjectPath(projectPath, filePath)) return '';
try {
return await readFile(join(projectPath, filePath), 'utf8');
} catch {
Expand Down Expand Up @@ -533,14 +534,6 @@ async function literalExistsInHeadSource(projectPath: string, literal: string):
}
}

function normalizeEvidencePath(projectPath: string, filePath: string): string | null {
const cleaned = normalize(filePath).replace(/\\/g, '/').replace(/^\.\//, '');
const absolute = isAbsolute(cleaned) ? cleaned : resolve(projectPath, cleaned);
const rel = normalize(relative(projectPath, absolute)).replace(/\\/g, '/');
if (rel.startsWith('../') || rel === '..' || isAbsolute(rel)) return null;
return rel;
}

async function hasExternalContractEvidence(
workerResult: WorkerResult,
projectPath: string,
Expand All @@ -551,7 +544,7 @@ async function hasExternalContractEvidence(
const fileRefs = new Map<string, Set<number>>();
let match: RegExpExecArray | null;
while ((match = EVIDENCE_FILE_REF_RE.exec(text))) {
const normalized = normalizeEvidencePath(projectPath, match[1]);
const normalized = containedProjectPath(projectPath, match[1]);
if (!normalized) continue;
const lineNo = parseInt(match[2], 10);
const lines = fileRefs.get(normalized) ?? new Set<number>();
Expand Down Expand Up @@ -928,11 +921,12 @@ export async function runGuards(
): Promise<GuardsRunResult> {
const results: GuardResult[] = [];
// Keep the original result for reporting/publication, but never feed
// generated pytest/venv artifacts into guards that open changed files.
const guardWorkerResult = {
...workerResult,
filesChanged: workerResult.filesChanged.filter((file) => !isEphemeralWorktreeArtifact(file)),
};
// generated pytest/venv artifacts — or paths that escape the project
// (absolute, `..`, symlinked out) — into guards that open changed files.
// Reported, not blocking: a tracked symlink pointing outside is legitimate.
const { contained, escaped } = partitionChangedPaths(projectPath, workerResult.filesChanged.filter((file) => !isEphemeralWorktreeArtifact(file)));
const guardWorkerResult = { ...workerResult, filesChanged: contained };
if (escaped.length > 0) results.push({ passed: false, guard: 'pathContainment', blocking: false, issues: escaped.slice(0, 20).map((file) => `Changed path escapes the project: ${file}`) });

if (config.qualityGate) {
console.warn('[Guard:qualityGate] Deprecated: use autonomous.verify baseline-diff verification instead.');
Expand Down
30 changes: 30 additions & 0 deletions src/agents/tester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,33 @@ describe('parseTesterOutput fake-pass guard (INT-2521)', () => {
expect(parseTesterOutput('2 failed, 3 passed').success).toBe(false);
});
});

describe('parseTesterOutput validates result field types (AGT-3441)', () => {
const wrap = (obj: unknown) => JSON.stringify({ type: 'result', result: '```json\n' + JSON.stringify(obj) + '\n```' });

it('does not coerce a string "false" into success', () => {
const r = parseTesterOutput(wrap({ success: 'false', testsPassed: 1, testsFailed: 2 }));
expect(r.success).toBe(false);
});

it('keeps only string list entries, clipped and capped', () => {
const r = parseTesterOutput(wrap({
success: false,
testsFailed: 1,
failedTests: ['a.test.ts > case', 42, null, { x: 1 }, '', 'z'.repeat(2000)],
suggestions: [{ nested: true }],
}));
expect(r.failedTests?.[0]).toBe('a.test.ts > case');
expect(r.failedTests).toHaveLength(2);
expect(r.failedTests?.[1].length).toBe(500);
expect(r.suggestions).toBeUndefined();
});

it('drops non-finite or negative counts and non-string errors', () => {
const r = parseTesterOutput(wrap({ success: false, testsPassed: -3, testsFailed: '7', coverage: 'high', error: { code: 1 } }));
expect(r.testsPassed).toBe(0);
expect(r.testsFailed).toBe(0);
expect(r.coverage).toBeUndefined();
expect(r.error).toBeUndefined();
});
});
Loading
Loading