Skip to content

The agent that grows with you

fix(desktop): large text pastes attach from HERMES_HOME/composer-pastes when the chat cwd is elsewhere

teknium160dd9778c625
diff --git a/agent/context_references.py b/agent/context_references.py
index 233896906a9..2920522d973 100644
--- a/agent/context_references.py
+++ b/agent/context_references.py
@@ -447,12 +447,28 @@ def _is_under(path: Path, root: Path) -> bool:
return True
+# Desktop persists a large plain-text paste as a `.txt` under this Hermes-managed
+# directory (apps/desktop/electron/composer-paste.ts) and attaches it as `@file:`.
+# The chat's cwd is rarely an ancestor of it, so it is the one anchored root the
+# workspace guard admits besides `allowed_root` itself.
+COMPOSER_PASTES_DIRNAME = "composer-pastes"
+
+
+def _composer_paste_roots() -> list[Path]:
+ from agent.file_safety import _hermes_dirs
+ return [hermes_dir / COMPOSER_PASTES_DIRNAME for hermes_dir in _hermes_dirs()]
+
+
def _resolve_path(cwd: Path, target: str, *, allowed_root: Path | None = None) -> Path:
from agent.file_safety import is_nt_namespace_path
if is_nt_namespace_path(target): # raw-string check: resolving such a path is the NTLM-leak trigger
raise ValueError("path uses a Windows NT/device namespace prefix and cannot be attached")
resolved = (cwd / Path(os.path.expanduser(target))).resolve() # `/` keeps an absolute target as-is
- if allowed_root is not None and not _is_under(resolved, allowed_root):
+ if (
+ allowed_root is not None
+ and not _is_under(resolved, allowed_root)
+ and not any(_is_under(resolved, root) for root in _composer_paste_roots())
+ ):
raise ValueError("path is outside the allowed workspace")
return resolved
diff --git a/apps/desktop/electron/composer-paste.test.ts b/apps/desktop/electron/composer-paste.test.ts
new file mode 100644
index 00000000000..8bb0c153284
--- /dev/null
+++ b/apps/desktop/electron/composer-paste.test.ts
@@ -0,0 +1,27 @@
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { COMPOSER_PASTES_DIRNAME, writeComposerPaste } from './composer-paste'
+
+const scratch: string[] = []
+
+afterEach(() => {
+ for (const dir of scratch.splice(0)) {
+ fs.rmSync(dir, { force: true, recursive: true })
+ }
+})
+
+describe('writeComposerPaste', () => {
+ it('lands the paste directly under <HERMES_HOME>/composer-pastes so the backend admits it', async () => {
+ const hermesHome = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-home-'))
+ scratch.push(hermesHome)
+
+ const filePath = await writeComposerPaste(hermesHome, 'pasted body')
+
+ expect(path.dirname(filePath)).toBe(path.join(hermesHome, COMPOSER_PASTES_DIRNAME))
+ expect(fs.readFileSync(filePath, 'utf8')).toBe('pasted body')
+ })
+})
diff --git a/apps/desktop/electron/composer-paste.ts b/apps/desktop/electron/composer-paste.ts
index df26f41e4b0..582fc34d11c 100644
--- a/apps/desktop/electron/composer-paste.ts
+++ b/apps/desktop/electron/composer-paste.ts
@@ -2,14 +2,21 @@ import crypto from 'node:crypto'
import fs from 'node:fs'
import path from 'node:path'
+export const COMPOSER_PASTES_DIRNAME = 'composer-pastes'
+
/**
* Persist a large plain-text paste as a `.txt` file the composer can attach
* as a chip instead of flooding the input. The renderer never chooses the
* path: the file lands in a Desktop-managed directory with a generated name,
* mirroring how `writeComposerImage` handles pasted images.
+ *
+ * `hermesHome` must be the HERMES_HOME root, not Electron's userData dir: the
+ * backend admits `@file:` attachments outside the chat's cwd only from
+ * `<HERMES_HOME>/composer-pastes` (agent/context_references.py::_resolve_path),
+ * and on Linux/macOS userData is a different tree (#117149).
*/
-export async function writeComposerPaste(userDataDir: string, text: string): Promise<string> {
- const dir = path.join(userDataDir, 'composer-pastes')
+export async function writeComposerPaste(hermesHome: string, text: string): Promise<string> {
+ const dir = path.join(hermesHome, COMPOSER_PASTES_DIRNAME)
await fs.promises.mkdir(dir, { recursive: true })
const stamp = new Date().toISOString().replace(/[:.]/g, '-').replace('T', '_').replace('Z', '')
const random = crypto.randomBytes(3).toString('hex')
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index ce1c54512fd..553d2492af3 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -17280,7 +17280,7 @@ ipcMain.handle('hermes:savePastedText', async (_event, payload) => {
throw new Error('savePastedText: missing text')
}
- return writeComposerPaste(app.getPath('userData'), text)
+ return writeComposerPaste(HERMES_HOME, text)
})
ipcMain.handle('hermes:saveClipboardImage', async () => {
diff --git a/tests/agent/test_context_references.py b/tests/agent/test_context_references.py
index 44aa5e742c6..b7f42f4f785 100644
--- a/tests/agent/test_context_references.py
+++ b/tests/agent/test_context_references.py
@@ -614,3 +614,35 @@ async def test_side_thread_expansion_guards_the_served_profile_home(tmp_path: Pa
assert "HUB-CACHE-BODY" not in result.message
assert any("internal Hermes path" in w for w in result.warnings)
+
+
+async def test_composer_paste_outside_workspace_is_attached_but_sibling_dir_is_not(tmp_path, monkeypatch):
+ """Desktop saves a large paste under <HERMES_HOME>/composer-pastes and attaches it
+ as `@file:`; the chat cwd is almost never an ancestor of that directory, so the
+ workspace guard must admit exactly that anchored root (#117149) — and nothing
+ that merely contains the substring next to it."""
+ from agent.context_references import preprocess_context_references_async
+
+ monkeypatch.setenv("HOME", str(tmp_path))
+ hermes_home = tmp_path / ".hermes"
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+ paste = hermes_home / "composer-pastes" / "pasted_content_1.txt"
+ paste.parent.mkdir(parents=True)
+ paste.write_text("PASTED-BODY-MARKER\n", encoding="utf-8")
+ lookalike = hermes_home / "my-composer-pastes-backup" / "secret.txt"
+ lookalike.parent.mkdir(parents=True)
+ lookalike.write_text("LOOKALIKE-SECRET\n", encoding="utf-8")
+ workspace = tmp_path / "project"
+ workspace.mkdir()
+
+ result = await preprocess_context_references_async(
+ f"see @file:{paste} and @file:{lookalike}",
+ cwd=workspace,
+ allowed_root=workspace,
+ context_length=100_000,
+ )
+
+ assert "PASTED-BODY-MARKER" in result.expanded
+ assert "LOOKALIKE-SECRET" not in result.expanded
+ assert "outside the allowed workspace" in result.expanded