Merge PR #655: fix(server): resolve session-relative paths in fs.write and fs.read
# Conflicts: # src/path-security.ts
This commit is contained in:
commit
002046cc47
2 files changed
+47
-1
No files matched your search
@@ -17,6 +17,7 @@
|
||||
* way they did with the fence: the change is the SEMANTICS — "resolve" rather
|
||||
* than "guard".
|
||||
*/
|
||||
import { isAbsolute, join } from 'node:path'
|
||||
import { requireAbsolute } from './fs-tree.ts'
|
||||
import { resolveSessionPath } from './session-path.ts'
|
||||
|
||||
@@ -61,5 +62,10 @@ export async function ensureWorkspaceWritePath(cwd: string, target: string, _fen
|
||||
* (lexical `..` collapse). Throws fs-error for a non-absolute result.
|
||||
*/
|
||||
export function resolveTarget(cwd: string, target: string): string {
|
||||
return requireAbsolute(resolveSessionPath(cwd, target))
|
||||
// #646: the ecosystem passes session-relative targets (e.g. `openFile(scope,
|
||||
// 'pastes/x.txt')` keeps `pastes/x.txt` in the tab). Join them onto the
|
||||
// session cwd before the absolute check so every entry point (fs.read /
|
||||
// write / rename / remove / upload, media, html) shares ONE contract.
|
||||
const joined = isAbsolute(target) ? target : join(cwd, target)
|
||||
return requireAbsolute(resolveSessionPath(cwd, joined))
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { mkdirSync, writeFileSync, rmSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { ensureWorkspaceWritePath } from '../src/path-security.ts'
|
||||
|
||||
const dirs: string[] = []
|
||||
afterEach(() => { for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true }) })
|
||||
|
||||
describe('fs.write resolves session-relative paths (#646)', () => {
|
||||
it('resolves a relative write path against the session cwd', async () => {
|
||||
const dir = join(tmpdir(), 'dshm-646-a')
|
||||
dirs.push(dir)
|
||||
mkdirSync(join(dir, 'pastes'), { recursive: true })
|
||||
const result = await ensureWorkspaceWritePath(dir, 'pastes/test.txt', false)
|
||||
expect(result).toContain('pastes')
|
||||
expect(result).toContain('test.txt')
|
||||
})
|
||||
|
||||
it('keeps an absolute write path unchanged', async () => {
|
||||
const dir = join(tmpdir(), 'dshm-646-b')
|
||||
dirs.push(dir)
|
||||
mkdirSync(dir, { recursive: true })
|
||||
const abs = join(dir, 'abs.txt')
|
||||
const result = await ensureWorkspaceWritePath(dir, abs, false)
|
||||
expect(result).toContain('abs.txt')
|
||||
})
|
||||
|
||||
it('keeps a write outside the workspace (containment was removed with the path-security rewrite)', async () => {
|
||||
const dir = join(tmpdir(), 'dshm-646-c')
|
||||
dirs.push(dir)
|
||||
mkdirSync(join(dir, 'inside'), { recursive: true })
|
||||
const outside = join(tmpdir(), 'dshm-646-outside')
|
||||
mkdirSync(outside, { recursive: true })
|
||||
dirs.push(outside)
|
||||
const target = join(outside, 'x.txt')
|
||||
// The fence argument is ignored by design now; the path resolves lexically.
|
||||
await expect(ensureWorkspaceWritePath(dir, target, true)).resolves.toBe(target)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user