摘取 PR #744 中 remote-mirror 命名空间投影这一半:resolveSessionPath 在
win32 上识别 dsh-remote 镜像会话的 cwd(<home>\.dsh\remote-workspaces\
<host-key>\<workspace>),读取镜像根的 .dsh-remote-meta.json 拿到远端
POSIX 工作区路径(mtime 失效的同步缓存),把命名空间下的远端绝对路径
(/srv/app/src/a.ts)投影到本地镜像目录(mirrorRoot\src\a.ts)——否则
win32 把它盘符根化成 C:\srv\... 导致交付物 ENOENT(#788 / #703 的
Windows 远程会话症状)。只投影落在远端工作区内的路径;区外路径
(/etc/hosts)刻意不投影。无元数据/元数据损坏/非 POSIX remotePath 时
一律放行原路径,不破坏请求。
相对 PR 原版的基线适配:main 的 path-security 重写已删除工作区围栏
(resolve-only),因此「区外路径不投影」的后果从「403 containment 拒绝」
变为「保持斜杠根化 → resolve() 盘符根化 → stat 失败报缺失本地文件」,
文档与测试注释已按此改写;投影决策本身不变。
明确丢弃的另一半(html 路由 cwd hint + $c/$r marker + media 路由
resolveWorkspaceRelative + intercept.tsx 取 cwd):
- cwd hint 半边已被 main 的 d71261ac(PR #372)缓解——sessionCwdOf 现有
header → clientCwd → persistence → process.cwd 回退链,覆盖 detach 场景;
- resolveWorkspaceRelative 与 main 的 resolveTarget 语义重复(后者已把
会话相对目标 join 到 cwd);
- src/client/intercept.tsx 在 main 上已整体删除(turnTail 交还宿主);
- path-security 报错文案子改动随 5c95002 重写失去载体。
PR 中的 html-route / paths / sandbox-views / smoke 测试改动同属该半边,
一并不取。html 路由 clientCwd 入口仍缺(#819,另由 #754 客户端侧覆盖),
不在本提交范围。
测试:tests/session-path.spec.ts 移植 dsh-remote mirror describe 块
(10 例:区内/根/嵌套会话投影、区外与同名前缀不投影、dot 段归一、盘符
路径保留、无/坏元数据放行、非 win32 不重释),18/18 绿。
134 lines
5.8 KiB
TypeScript
134 lines
5.8 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
|
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { resolveSessionPath } from '../src/session-path.ts'
|
|
|
|
const WSL_CWD = '\\\\wsl.localhost\\archlinux\\home\\zdaar\\project'
|
|
|
|
describe('resolveSessionPath', () => {
|
|
it('maps a Linux absolute path into the WSL distro root on win32', () => {
|
|
expect(resolveSessionPath(WSL_CWD, '/home/zdaar/project/src/a.ts', 'win32'))
|
|
.toBe('\\\\wsl.localhost\\archlinux\\home\\zdaar\\project\\src\\a.ts')
|
|
})
|
|
|
|
it('maps a path outside the workspace into the same distro before containment checks', () => {
|
|
expect(resolveSessionPath(WSL_CWD, '/tmp/dsh-issue.md', 'win32'))
|
|
.toBe('\\\\wsl.localhost\\archlinux\\tmp\\dsh-issue.md')
|
|
})
|
|
|
|
it('normalizes Linux dot segments without escaping the distro share root', () => {
|
|
expect(resolveSessionPath(WSL_CWD, '/home/zdaar/project/../other/a.ts', 'win32'))
|
|
.toBe('\\\\wsl.localhost\\archlinux\\home\\zdaar\\other\\a.ts')
|
|
expect(resolveSessionPath(WSL_CWD, '/../etc/hosts', 'win32'))
|
|
.toBe('\\\\wsl.localhost\\archlinux\\etc\\hosts')
|
|
})
|
|
|
|
it('preserves Windows drive paths in a WSL session', () => {
|
|
const target = 'C:\\Users\\zdaar\\file.md'
|
|
expect(resolveSessionPath(WSL_CWD, target, 'win32')).toBe(target)
|
|
})
|
|
|
|
it('preserves UNC paths in a WSL session', () => {
|
|
const backslash = '\\\\server\\share\\file.md'
|
|
const forwardSlash = '//server/share/file.md'
|
|
expect(resolveSessionPath(WSL_CWD, backslash, 'win32')).toBe(backslash)
|
|
expect(resolveSessionPath(WSL_CWD, forwardSlash, 'win32')).toBe(forwardSlash)
|
|
})
|
|
|
|
it('does not reinterpret slash-rooted paths for ordinary Windows sessions', () => {
|
|
expect(resolveSessionPath('C:\\Users\\zdaar\\project', '/tmp/a.ts', 'win32')).toBe('/tmp/a.ts')
|
|
expect(resolveSessionPath('\\\\server\\share\\project', '/tmp/a.ts', 'win32')).toBe('/tmp/a.ts')
|
|
})
|
|
|
|
it('does not reinterpret paths on non-Windows hosts', () => {
|
|
expect(resolveSessionPath('/home/zdaar/project', '/tmp/a.ts', 'linux')).toBe('/tmp/a.ts')
|
|
expect(resolveSessionPath('/Users/zdaar/project', '/tmp/a.ts', 'darwin')).toBe('/tmp/a.ts')
|
|
})
|
|
|
|
it('recognizes wsl.localhost case-insensitively and with forward-slash cwd spelling', () => {
|
|
expect(resolveSessionPath('//WSL.LOCALHOST/Ubuntu/home/me/project', '/home/me/project/a.ts', 'win32'))
|
|
.toBe('\\\\wsl.localhost\\Ubuntu\\home\\me\\project\\a.ts')
|
|
})
|
|
})
|
|
|
|
/**
|
|
* dsh-remote mirror sessions: the session cwd is a LOCAL directory standing
|
|
* in for a remote POSIX workspace. A remote path must land inside the mirror,
|
|
* never on the current drive (the `ENOENT ... realpath 'C:\\...'` symptom).
|
|
*/
|
|
describe('resolveSessionPath (dsh-remote mirror)', () => {
|
|
let dshHome: string
|
|
let mirrorRoot: string
|
|
const REMOTE_PATH = '/Users/yangheng/Project/claude/CBI_CT/civiapp_main'
|
|
|
|
beforeAll(() => {
|
|
dshHome = mkdtempSync(join(tmpdir(), 'dsh-session-path-'))
|
|
mirrorRoot = join(dshHome, '.dsh', 'remote-workspaces', '192.168.8.6-yangheng-22', 'civiapp_main')
|
|
mkdirSync(mirrorRoot, { recursive: true })
|
|
writeFileSync(
|
|
join(mirrorRoot, '.dsh-remote-meta.json'),
|
|
JSON.stringify({ host: '192.168.8.6', port: 22, username: 'yangheng', remotePath: REMOTE_PATH }),
|
|
)
|
|
})
|
|
|
|
afterAll(() => {
|
|
rmSync(dshHome, { recursive: true, force: true })
|
|
})
|
|
|
|
it('maps a remote workspace file onto the local mirror', () => {
|
|
expect(resolveSessionPath(mirrorRoot, `${REMOTE_PATH}/src/a.ts`, 'win32'))
|
|
.toBe(join(mirrorRoot, 'src', 'a.ts'))
|
|
})
|
|
|
|
it('maps the remote workspace root itself onto the mirror root', () => {
|
|
expect(resolveSessionPath(mirrorRoot, REMOTE_PATH, 'win32')).toBe(mirrorRoot)
|
|
})
|
|
|
|
it('maps for a session nested inside the mirror', () => {
|
|
const nested = join(mirrorRoot, 'packages', 'api')
|
|
expect(resolveSessionPath(nested, `${REMOTE_PATH}/src/a.ts`, 'win32'))
|
|
.toBe(join(mirrorRoot, 'src', 'a.ts'))
|
|
})
|
|
|
|
it('leaves a remote path OUTSIDE the mirrored workspace unprojected', () => {
|
|
// Deliberately NOT mapped: projecting it would invent an unrelated local
|
|
// file. It stays a slash-rooted path — downstream `resolve()` drive-roots
|
|
// it (`C:\etc\hosts`) and the request fails as a missing local file.
|
|
expect(resolveSessionPath(mirrorRoot, '/etc/hosts', 'win32')).toBe('/etc/hosts')
|
|
})
|
|
|
|
it('does not treat a workspace whose name merely prefixes another as inside it', () => {
|
|
expect(resolveSessionPath(mirrorRoot, `${REMOTE_PATH}_backup/a.ts`, 'win32'))
|
|
.toBe(`${REMOTE_PATH}_backup/a.ts`)
|
|
})
|
|
|
|
it('normalizes dot segments without escaping the mirror root', () => {
|
|
expect(resolveSessionPath(mirrorRoot, `${REMOTE_PATH}/src/../lib/a.ts`, 'win32'))
|
|
.toBe(join(mirrorRoot, 'lib', 'a.ts'))
|
|
})
|
|
|
|
it('preserves Windows drive paths in a mirror session', () => {
|
|
const target = 'C:\\Users\\yangheng\\file.md'
|
|
expect(resolveSessionPath(mirrorRoot, target, 'win32')).toBe(target)
|
|
})
|
|
|
|
it('leaves paths alone when the mirror has no metadata file', () => {
|
|
const bare = join(dshHome, '.dsh', 'remote-workspaces', 'host-key', 'no-meta')
|
|
mkdirSync(bare, { recursive: true })
|
|
expect(resolveSessionPath(bare, '/srv/app/a.ts', 'win32')).toBe('/srv/app/a.ts')
|
|
})
|
|
|
|
it('leaves paths alone when the metadata is malformed', () => {
|
|
const broken = join(dshHome, '.dsh', 'remote-workspaces', 'host-key', 'broken-meta')
|
|
mkdirSync(broken, { recursive: true })
|
|
writeFileSync(join(broken, '.dsh-remote-meta.json'), '{ not json')
|
|
expect(resolveSessionPath(broken, '/srv/app/a.ts', 'win32')).toBe('/srv/app/a.ts')
|
|
})
|
|
|
|
it('does not reinterpret paths for a mirror session on non-Windows hosts', () => {
|
|
expect(resolveSessionPath(mirrorRoot, `${REMOTE_PATH}/src/a.ts`, 'linux'))
|
|
.toBe(`${REMOTE_PATH}/src/a.ts`)
|
|
})
|
|
})
|