Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: block directory creation through an in-vault symlink escape in createNote (#9007)

[._.]/ Adam Eivy09c7b879d885
diff --git a/server/services/obsidian.js b/server/services/obsidian.js
index 6be320c665..b377e5ac70 100644
--- a/server/services/obsidian.js
+++ b/server/services/obsidian.js
@@ -130,6 +130,39 @@ function isWithinVaultLexically(vault, notePath) {
return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
}
+// Create `dirPath` (a directory under the vault) one path segment at a time,
+// refusing to create anything through an EXISTING symlink that resolves
+// outside the vault. A single recursive `ensureDir(dirPath)` call would hand
+// the whole string to one mkdir syscall, which the OS resolves following any
+// mid-path symlink — so a vault containing a legitimate symlinked folder that
+// happens to point outside the vault could otherwise have host directories
+// planted through it before `resolveVaultPath`'s realpath check ever runs.
+// Returns false (creating nothing further) the moment a segment escapes.
+async function ensureVaultDir(vault, dirPath) {
+ const rootResolved = resolve(vault.path);
+ const rootReal = (() => {
+ try { return realpathSync(rootResolved); } catch { return rootResolved; }
+ })();
+ const relDir = relative(rootResolved, resolve(dirPath));
+ if (relDir === '') return true; // the vault root itself always exists
+ if (relDir.startsWith('..') || isAbsolute(relDir)) return false;
+
+ let current = rootResolved;
+ for (const segment of relDir.split(/[\\/]+/).filter(Boolean)) {
+ current = join(current, segment);
+ if (existsSync(current)) {
+ const real = (() => {
+ try { return realpathSync(current); } catch { return current; }
+ })();
+ const rel = relative(rootReal, real);
+ if (rel !== '' && (rel.startsWith('..') || isAbsolute(rel))) return false;
+ } else {
+ await ensureDir(current);
+ }
+ }
+ return true;
+}
+
export async function getVaultById(id) {
const vaults = await getVaults();
return vaults.find(v => v.id === id) || null;
@@ -396,7 +429,10 @@ export async function createNote(vaultId, notePath, content = '') {
// For a new file the target doesn't exist yet — resolveVaultPath realpaths
// the parent directory (which must exist) to still catch symlink escapes.
- await ensureDir(dirname(join(vault.path, notePath)));
+ // ensureVaultDir creates it segment-by-segment so an existing symlink
+ // escape is refused before anything is created through it (#9007).
+ const dirOk = await ensureVaultDir(vault, dirname(join(vault.path, notePath)));
+ if (!dirOk) return { error: 'INVALID_PATH' };
const fullPath = resolveVaultPath(vault, notePath);
if (!fullPath) return { error: 'INVALID_PATH' };
if (existsSync(fullPath)) {
diff --git a/server/services/obsidian.test.js b/server/services/obsidian.test.js
index aa5b511069..19ec9ca2ee 100644
--- a/server/services/obsidian.test.js
+++ b/server/services/obsidian.test.js
@@ -9,7 +9,7 @@
*/
import { describe, it, expect, beforeEach, afterAll } from 'vitest';
-import { mkdirSync, rmSync, writeFileSync, readFileSync, existsSync } from 'fs';
+import { mkdirSync, rmSync, writeFileSync, readFileSync, existsSync, symlinkSync } from 'fs';
import { join } from 'path';
import { createTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
@@ -102,4 +102,17 @@ describe('obsidian note CRUD extension enforcement (#9007)', () => {
writeFileSync(join(VAULT_DIR, 'ok.md'), 'hello');
expect(await getNote(vaultId, 'ok.md')).toMatchObject({ content: 'hello' });
});
+
+ it('createNote refuses to create directories through an existing in-vault symlink that escapes it', async () => {
+ const outsideDir = join(tempRoot, 'outside');
+ mkdirSync(outsideDir, { recursive: true });
+ symlinkSync(outsideDir, join(VAULT_DIR, 'linked'));
+
+ const result = await createNote(vaultId, 'linked/newdir/escaped.md', 'x');
+ expect(result).toMatchObject({ error: 'INVALID_PATH' });
+ // The escaping segment must never have been created on the far side of
+ // the symlink — this is the directory-creation-through-a-mid-path-symlink
+ // case a single recursive `ensureDir(fullDirString)` cannot see (#9007).
+ expect(existsSync(join(outsideDir, 'newdir'))).toBe(false);
+ });
});