Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

fix(trueforge-ui): preserve sandbox artifact paths with parentheses (#1135)

Chirag Jain8f1a03c0f095
diff --git a/.changeset/balanced-artifact-download-paths.md b/.changeset/balanced-artifact-download-paths.md
new file mode 100644
index 000000000..8a6632ffa
--- /dev/null
+++ b/.changeset/balanced-artifact-download-paths.md
@@ -0,0 +1,5 @@
+---
+"@truefoundry/trueforge-ui": patch
+---
+
+Fix sandbox artifact download when the path contains parentheses.
diff --git a/packages/trueforge-ui/src/atoms/SandboxArtifactDownload.tsx b/packages/trueforge-ui/src/atoms/SandboxArtifactDownload.tsx
index d4f6af21f..475bfda0d 100644
--- a/packages/trueforge-ui/src/atoms/SandboxArtifactDownload.tsx
+++ b/packages/trueforge-ui/src/atoms/SandboxArtifactDownload.tsx
@@ -17,26 +17,121 @@ export type SandboxArtifactDownloadProps = {
export type SandboxArtifact = ChatFileDownloadFile;
const PREFIX = 'sandbox_artifacts';
-/** tfy / agent format: `[name](path)` */
-const PAIR_RE = /\[([^\]]*)\]\(([^)]*)\)/g;
+/** Delimiters that may appear escaped inside `[name](path)` entries. */
+const ESCAPABLE_DELIMITERS = new Set(['[', ']', '(', ')', '\\']);
+/** Same shape as server `validateNoPathTraversal` — keep in sync intentionally. */
+const PATH_TRAVERSAL_RE = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
+/** Matches server PATH_MAX usable length (NUL-terminated 4096). */
+const MAX_PATH_LENGTH = 4095;
+
+/**
+ * Model-authored paths feed download hrefs and API calls. Drop anything that is not a
+ * plain sandbox file path so `fileDownloadBaseUrl` cannot be steered via `..` or schemes.
+ */
+function isSafeArtifactPath(path: string): boolean {
+ if (path.length === 0 || path.length > MAX_PATH_LENGTH) return false;
+ if (path.includes('\0')) return false;
+ if (PATH_TRAVERSAL_RE.test(path)) return false;
+ // `javascript:…`, `https:…`, etc. — sandbox paths are filesystem, not URLs.
+ if (/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(path)) return false;
+ return true;
+}
+
+function toArtifact(name: string, path: string): SandboxArtifact | null {
+ const trimmedName = name.trim();
+ const trimmedPath = path.trim();
+ if (!trimmedName || !isSafeArtifactPath(trimmedPath)) return null;
+ return { name: trimmedName, path: trimmedPath };
+}
+
+/**
+ * Parse one `[name](path)` starting at `start` (`body[start] === '['`).
+ * Paths may contain balanced `(…)` so `/tmp/report(final).csv` stays intact.
+ * Returns null when the link is incomplete or crosses a newline.
+ */
+function parseMarkdownLink(body: string, start: number): { name: string; path: string; end: number } | null {
+ let cursor = start + 1;
+
+ let name = '';
+ let closedLabel = false;
+ while (cursor < body.length) {
+ const char = body[cursor];
+ if (char === '\n' || char === '\r') return null;
+ if (char === '\\' && ESCAPABLE_DELIMITERS.has(body[cursor + 1] ?? '')) {
+ name += body[cursor + 1] ?? '';
+ cursor += 2;
+ continue;
+ }
+ if (char === ']') {
+ closedLabel = true;
+ cursor++;
+ break;
+ }
+ name += char;
+ cursor++;
+ }
+ if (!closedLabel || body[cursor] !== '(') return null;
+
+ cursor++;
+ let path = '';
+ let depth = 0;
+ let closedPath = false;
+ while (cursor < body.length) {
+ const char = body[cursor];
+ if (char === '\n' || char === '\r') return null;
+ if (char === '\\' && ESCAPABLE_DELIMITERS.has(body[cursor + 1] ?? '')) {
+ path += body[cursor + 1] ?? '';
+ cursor += 2;
+ continue;
+ }
+ if (char === '(') {
+ depth++;
+ path += char;
+ cursor++;
+ continue;
+ }
+ if (char === ')') {
+ if (depth === 0) {
+ closedPath = true;
+ cursor++;
+ break;
+ }
+ depth--;
+ path += char;
+ cursor++;
+ continue;
+ }
+ path += char;
+ cursor++;
+ }
+ if (!closedPath) return null;
+
+ return { name, path, end: cursor };
+}
/**
* Parses a `sandbox_artifacts` fence body into `{ name, path }` entries.
- * Primary format matches tfy-web-components: `[file.js](/tmp/file.js)`.
- * Colon lines (`name: path`) are accepted as a fallback.
+ * Primary format: `[file.js](/tmp/file.js)`. Fallback: `name: path` per line.
*/
export function parseSandboxArtifacts(raw: string): SandboxArtifact[] {
const trimmed = raw.trim();
const body = trimmed.startsWith(PREFIX) ? trimmed.slice(PREFIX.length).trimStart() : trimmed;
const fromLinks: SandboxArtifact[] = [];
- PAIR_RE.lastIndex = 0;
- let match: RegExpExecArray | null = PAIR_RE.exec(body);
- while (match !== null) {
- const name = match[1]?.trim() ?? '';
- const path = match[2]?.trim() ?? '';
- if (name && path) fromLinks.push({ name, path });
- match = PAIR_RE.exec(body);
+ for (let start = 0; start < body.length; start++) {
+ // Skip an escaped `[` so it is not treated as a link opener.
+ if (body[start] === '\\' && ESCAPABLE_DELIMITERS.has(body[start + 1] ?? '')) {
+ start++;
+ continue;
+ }
+ if (body[start] !== '[') continue;
+
+ const link = parseMarkdownLink(body, start);
+ if (link == null) continue;
+
+ const artifact = toArtifact(link.name, link.path);
+ if (artifact != null) fromLinks.push(artifact);
+ start = link.end - 1;
}
if (fromLinks.length > 0) return fromLinks;
@@ -46,10 +141,10 @@ export function parseSandboxArtifacts(raw: string): SandboxArtifact[] {
if (idx === -1) return [];
const name = line.slice(0, idx).trim();
const path = line.slice(idx + 1).trim();
- if (!name || !path) return [];
// Skip leftover markdown link debris
if (name.startsWith('[') || path.startsWith('(')) return [];
- return [{ name, path }];
+ const artifact = toArtifact(name, path);
+ return artifact == null ? [] : [artifact];
});
}
diff --git a/packages/trueforge-ui/test/atoms/SandboxArtifactDownload.test.tsx b/packages/trueforge-ui/test/atoms/SandboxArtifactDownload.test.tsx
index fe7fa8b78..3cd17d855 100644
--- a/packages/trueforge-ui/test/atoms/SandboxArtifactDownload.test.tsx
+++ b/packages/trueforge-ui/test/atoms/SandboxArtifactDownload.test.tsx
@@ -15,6 +15,26 @@ describe('parseSandboxArtifacts', () => {
]);
});
+ it('keeps balanced parentheses in artifact paths', () => {
+ expect(parseSandboxArtifacts('[Report](/tmp/report(final).csv)\n[Nested](/tmp/a(b(c)d)e.txt)')).toEqual([
+ { name: 'Report', path: '/tmp/report(final).csv' },
+ { name: 'Nested', path: '/tmp/a(b(c)d)e.txt' },
+ ]);
+ });
+
+ it('unescapes link delimiters and keeps adjacent entries separate', () => {
+ expect(parseSandboxArtifacts('[report\\] final](/tmp/part\\)final\\(.csv)[notes](/tmp/notes.txt)')).toEqual([
+ { name: 'report] final', path: '/tmp/part)final(.csv' },
+ { name: 'notes', path: '/tmp/notes.txt' },
+ ]);
+ });
+
+ it('skips unbalanced links and continues with later valid entries', () => {
+ expect(parseSandboxArtifacts('[broken](/tmp/bad(unclosed\n[valid](/tmp/valid.csv)')).toEqual([
+ { name: 'valid', path: '/tmp/valid.csv' },
+ ]);
+ });
+
it('strips a leading sandbox_artifacts prefix', () => {
expect(parseSandboxArtifacts('sandbox_artifacts\n[a.py](/tmp/a.py)')).toEqual([
{ name: 'a.py', path: '/tmp/a.py' },
@@ -26,6 +46,21 @@ describe('parseSandboxArtifacts', () => {
{ name: 'report.pdf', path: '/sandbox/out/report.pdf' },
]);
});
+
+ it('drops path-traversal and scheme-like paths', () => {
+ expect(
+ parseSandboxArtifacts(
+ [
+ '[ok](/tmp/ok.txt)',
+ '[up](/tmp/../etc/passwd)',
+ '[dots](..\\secret)',
+ '[web](https://evil.example/x)',
+ '[js](javascript:alert(1))',
+ '[nul](/tmp/ok\0.txt)',
+ ].join('\n'),
+ ),
+ ).toEqual([{ name: 'ok', path: '/tmp/ok.txt' }]);
+ });
});
describe('SandboxArtifactDownload', () => {
@@ -36,6 +71,17 @@ describe('SandboxArtifactDownload', () => {
expect(screen.getByText('1 file generated')).toBeInTheDocument();
});
+ it('passes the full balanced-parentheses path to the download callback', async () => {
+ const onDownload = vi.fn(async () => {});
+ render(<SandboxArtifactDownload code={'[Report](/tmp/report(final).csv)'} onDownloadArtifact={onDownload} />);
+
+ await act(async () => {
+ screen.getByRole('link', { name: 'Download Report' }).click();
+ });
+
+ expect(onDownload).toHaveBeenCalledWith('/tmp/report(final).csv', 'Report');
+ });
+
it('shows a loader while the download is in flight', async () => {
let resolveDownload!: () => void;
const onDownload = vi.fn(