Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(windows): expand ~\ and compare linked Claude config dirs case-insensitively

reviewee1c17b6c50e
diff --git a/src/core/claude-accounts-core.test.ts b/src/core/claude-accounts-core.test.ts
index d4aee72dd..0b3ab1bc6 100644
--- a/src/core/claude-accounts-core.test.ts
+++ b/src/core/claude-accounts-core.test.ts
@@ -14,7 +14,10 @@ import {
isSafeLocalTranscriptPath,
classifyClaudeConfigDir,
configDirFromTranscriptPath,
- normalizeLinkedConfigDir
+ normalizeLinkedConfigDir,
+ expandTildeLinkedDir,
+ sameLinkedConfigDir,
+ isUnderLinkedConfigDir
} from './claude-accounts-core'
import type { ClaudeAccount } from '../shared/types'
@@ -561,3 +564,38 @@ describe('isSafeLocalTranscriptPath — linked config dirs', () => {
).toBe(true)
})
})
+
+describe('linked config dir path helpers (link refusals on every dialect)', () => {
+ it('expands ~ and ~/ everywhere, ~\\ only in the win32 dialect', () => {
+ expect(expandTildeLinkedDir('~/.claude-2', '/home/u', 'posix')).toBe('/home/u/.claude-2')
+ expect(expandTildeLinkedDir('~', '/home/u', 'posix')).toBe('/home/u')
+ // POSIX: a backslash is filename text — left literal (then refused as relative).
+ expect(expandTildeLinkedDir('~\\.claude-2', '/home/u', 'posix')).toBe('~\\.claude-2')
+ expect(expandTildeLinkedDir('~\\.claude-2', 'C:\\Users\\X', 'win32')).toBe('C:\\Users\\X\\.claude-2')
+ expect(expandTildeLinkedDir('~/.claude-2', 'C:\\Users\\X', 'win32')).toBe('C:\\Users\\X\\.claude-2')
+ expect(expandTildeLinkedDir('/abs/~/x', '/home/u', 'posix')).toBe('/abs/~/x')
+ expect(expandTildeLinkedDir('~other/x', '/home/u', 'posix')).toBe('~other/x')
+ })
+
+ it('sameLinkedConfigDir is case-insensitive on win32 only', () => {
+ expect(sameLinkedConfigDir('C:\\Users\\X\\.claude', 'c:\\users\\x\\.CLAUDE\\')).toBe(true)
+ expect(sameLinkedConfigDir('C:/Users/X/.claude', 'c:\\users\\x\\.claude')).toBe(true)
+ expect(sameLinkedConfigDir('/home/u/.claude', '/home/u/.claude/')).toBe(true)
+ expect(sameLinkedConfigDir('/home/u/.claude', '/home/u/.CLAUDE')).toBe(false)
+ expect(sameLinkedConfigDir('/home/u/.claude', 'relative')).toBe(false)
+ expect(sameLinkedConfigDir(undefined, undefined)).toBe(false)
+ })
+
+ it('isUnderLinkedConfigDir is a strict, separator-bounded, dialect-correct prefix', () => {
+ const root = 'C:\\Users\\X\\AppData\\Roaming\\nodeterm\\claude-accounts'
+ expect(isUnderLinkedConfigDir('c:\\users\\x\\appdata\\roaming\\NODETERM\\claude-accounts\\abc', root)).toBe(
+ true
+ )
+ expect(isUnderLinkedConfigDir(root, root)).toBe(false)
+ expect(isUnderLinkedConfigDir(`${root}-evil\\abc`, root)).toBe(false)
+ expect(isUnderLinkedConfigDir('/data/claude-accounts/abc', '/data/claude-accounts')).toBe(true)
+ expect(isUnderLinkedConfigDir('/data/Claude-accounts/abc', '/data/claude-accounts')).toBe(false)
+ expect(isUnderLinkedConfigDir('/data/claude-accounts-x/abc', '/data/claude-accounts')).toBe(false)
+ expect(isUnderLinkedConfigDir('C:\\a\\b', '/a')).toBe(false)
+ })
+})
diff --git a/src/core/claude-accounts-core.ts b/src/core/claude-accounts-core.ts
index 8187559c9..6d91ed2a4 100644
--- a/src/core/claude-accounts-core.ts
+++ b/src/core/claude-accounts-core.ts
@@ -155,6 +155,43 @@ export function normalizeLinkedConfigDir(raw: unknown): string | null {
return segmentsOf(n, d).includes('..') ? null : n
}
+/**
+ * Expand a leading `~` in a config dir TYPED into Settings (no shell ever sees it, so nothing else
+ * would). `~` and `~/…` in every dialect; `~\…` too in the win32 dialect only — on POSIX a
+ * backslash is filename text, so `~\x` stays the literal (relative, hence refused) name it is.
+ */
+export function expandTildeLinkedDir(typed: string, home: string, dialect: PathDialect): string {
+ const tilde =
+ typed === '~' || typed.startsWith('~/') || (dialect === 'win32' && typed.startsWith('~\\'))
+ return tilde ? pathFor(dialect).join(home, typed.slice(1)) : typed
+}
+
+/** Two linked config dirs name the same directory: both re-validated, same dialect, compared by the
+ * owning filesystem's rule (case-insensitive on win32 — `C:\Users\X\.claude` is
+ * `c:\users\x\.CLAUDE\`). An invalid side is never "the same". */
+export function sameLinkedConfigDir(a: unknown, b: unknown): boolean {
+ const na = normalizeLinkedConfigDir(a)
+ const nb = normalizeLinkedConfigDir(b)
+ if (!na || !nb) return false
+ const d = pathDialectOf(na)
+ return d === pathDialectOf(nb) && sameDir(na, nb, d)
+}
+
+/** `child` lies strictly BELOW `root` (both re-validated, same dialect; case-insensitive on win32,
+ * where either separator is accepted by normalization). Equality is `sameLinkedConfigDir`. */
+export function isUnderLinkedConfigDir(child: unknown, root: unknown): boolean {
+ const c = normalizeLinkedConfigDir(child)
+ const r = normalizeLinkedConfigDir(root)
+ if (!c || !r) return false
+ const d = pathDialectOf(c)
+ if (d !== pathDialectOf(r)) return false
+ const sep = d === 'win32' ? '\\' : '/'
+ const prefix = r.endsWith(sep) ? r : r + sep
+ return d === 'win32'
+ ? c.length > prefix.length && c.toLowerCase().startsWith(prefix.toLowerCase())
+ : c.length > prefix.length && c.startsWith(prefix)
+}
+
/** The settings rows a linked-dir match may consult: local (a linked dir is local by definition —
* see §7 of the plan), settled, and carrying a value that survives re-validation. */
function linkedDirOf(acct: ClaudeAccount): string | null {
diff --git a/src/core/claude-accounts-service.ts b/src/core/claude-accounts-service.ts
index 808a3aeb6..ce4790bcb 100644
--- a/src/core/claude-accounts-service.ts
+++ b/src/core/claude-accounts-service.ts
@@ -16,9 +16,12 @@ import { promisify } from 'util'
import { IPC } from '../shared/ipc'
import {
accountConfigDir,
+ expandTildeLinkedDir,
+ isUnderLinkedConfigDir,
isSupportedClaudeVersion,
normalizeLinkedConfigDir,
- parseLoginCapture
+ parseLoginCapture,
+ sameLinkedConfigDir
} from './claude-accounts-core'
import {
claudeAccountsSnapshot,
@@ -199,8 +202,11 @@ export function registerClaudeAccountsIpc(deps: ClaudeAccountsDeps = {}): void {
// shell, so nothing else would ever expand it — and `~/.claude-2` is exactly how the user
// refers to the dir their own shell function exports.
const typed = rawDir.trim()
- const expanded =
- typed === '~' || typed.startsWith('~/') ? path.join(homedir(), typed.slice(1)) : typed
+ const expanded = expandTildeLinkedDir(
+ typed,
+ homedir(),
+ process.platform === 'win32' ? 'win32' : 'posix'
+ )
const configDir = normalizeLinkedConfigDir(expanded)
if (!configDir) {
throw new Error(`Not an absolute path: ${typed}`)
@@ -210,7 +216,9 @@ export function registerClaudeAccountsIpc(deps: ClaudeAccountsDeps = {}): void {
// path we should not be stat'ing, and refusing `~/.claude` needs to say so even on a machine
// where the dir has not been created yet ("no such directory" would be a misleading answer to
// "why can't I link my system account?").
- if (configDir === normalizeLinkedConfigDir(path.join(homedir(), '.claude'))) {
+ // Compared by the filesystem's own rule (case-insensitive on Windows), not `===` — else
+ // `c:\users\x\.CLAUDE` slips past every refusal below and one dir gets two ids.
+ if (sameLinkedConfigDir(configDir, path.join(homedir(), '.claude'))) {
throw new Error('That is the system Claude config dir — it is already the default account.')
}
const managedRoot = normalizeLinkedConfigDir(
@@ -219,13 +227,16 @@ export function registerClaudeAccountsIpc(deps: ClaudeAccountsDeps = {}): void {
// Under the managed root ⇒ it is an account nodeterm created. Linking it would give one dir two
// account ids, and the second one would be removable-without-delete while the first still
// `rm -rf`s the same directory.
- if (managedRoot && (configDir === managedRoot || configDir.startsWith(managedRoot + path.sep))) {
+ if (
+ managedRoot &&
+ (sameLinkedConfigDir(configDir, managedRoot) || isUnderLinkedConfigDir(configDir, managedRoot))
+ ) {
throw new Error('That is a nodeterm-managed account directory — add it with Add account.')
}
// Duplicate by NORMALIZED path, not by the string typed: `~/.claude-2/` and `/home/u/.claude-2`
// are the same dir, and two rows for one dir would show two chips for one identity.
for (const a of claudeAccountsSnapshot()) {
- if (!a.host && normalizeLinkedConfigDir(a.configDir) === configDir) {
+ if (!a.host && sameLinkedConfigDir(a.configDir, configDir)) {
throw new Error(`That config dir is already linked (${a.label || a.email || a.id}).`)
}
}