diff --git a/.github/workflows/native-api-sdk.yml b/.github/workflows/native-api-sdk.yml index 0a5172f4b..98e2318b2 100644 --- a/.github/workflows/native-api-sdk.yml +++ b/.github/workflows/native-api-sdk.yml @@ -72,6 +72,7 @@ jobs: npm run test:editor:block-start-hints 2>&1 | tee "$RUNNER_TEMP/document-markdown-hints.log" npm run test:editor:markdown 2>&1 | tee "$RUNNER_TEMP/document-markdown-roundtrip.log" npm run test:collaboration:location-client 2>&1 | tee "$RUNNER_TEMP/document-location-client.log" + npm run test:workspaces:path-alias-observations 2>&1 | tee "$RUNNER_TEMP/document-path-alias-observations.log" - name: Opened document resource policy run: | set -euo pipefail @@ -178,6 +179,7 @@ jobs: if-no-files-found: error path: | ${{ runner.temp }}/source-projection-lock.log + ${{ runner.temp }}/document-path-alias-observations.log ${{ runner.temp }}/document-retention-policy.log ${{ runner.temp }}/document-live-tabs.log ${{ runner.temp }}/document-tab-open.log diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b150206d..c623a6d35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ This project uses calendar-style release versions, currently `YYYY.M.D.N`. - Nothing yet. +### Fixed + +- Refresh contradictory filesystem observations once during read-only path checks. A brief canonical file or directory replacement no longer reports a false path-alias conflict; symlink, case-alias and mutation checks retain their protections. + ## [2026.11.8.2] - 2026-11-08 ### Added diff --git a/app/lib/workspaces/path-guard.ts b/app/lib/workspaces/path-guard.ts index e40fe4224..dae989496 100644 --- a/app/lib/workspaces/path-guard.ts +++ b/app/lib/workspaces/path-guard.ts @@ -85,14 +85,23 @@ export async function assertWorkspacePathHasNoAliases( : await ensureWorkspaceRoot(workspace); for (const segment of segments) { const candidate = path.join(parent, segment); - let stat: Awaited>; - try { stat = await fs.lstat(candidate); } - catch (error) { - if (error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT') return; - throw error; + const observations = options.readOnly ? 2 : 1; + for (let attempt = 0; attempt < observations; attempt++) { + let stat: Awaited>; + try { stat = await fs.lstat(candidate); } + catch (error) { + if (error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT') return; + throw error; + } + if (stat.isSymbolicLink()) throw new WorkspacePathAliasError(); + if ((await fs.readdir(parent)).includes(segment)) { + parent = candidate; + break; + } + // A read may observe a changed entry between lstat and readdir. Refresh + // both observations once; mutation checks retain their fenced rejection. + if (attempt + 1 === observations) throw new WorkspacePathAliasError(); } - if (stat.isSymbolicLink() || !(await fs.readdir(parent)).includes(segment)) throw new WorkspacePathAliasError(); - parent = candidate; } } diff --git a/package.json b/package.json index b68c4931d..9c82a2d62 100644 --- a/package.json +++ b/package.json @@ -549,6 +549,7 @@ "test:auth:setup": "tsx --conditions react-server scripts/auth-setup-test.ts", "test:workspaces:bootstrap": "tsx --conditions react-server scripts/workspace-bootstrap-idempotency-test.ts", "test:workspaces:bootstrap:native": "NODE_ENV=test CANVAS_SHARED_CONNECTION_POSTGRES_TEST=1 tsx --conditions react-server scripts/workspace-bootstrap-idempotency-test.ts --native", + "test:workspaces:path-alias-observations": "NODE_ENV=test tsx --conditions react-server --test scripts/workspace-path-alias-observation-test.ts", "test:auth:seat-limit": "tsx --conditions react-server scripts/auth-seat-limit-test.ts", "test:mobile:compatibility": "tsx --conditions react-server scripts/mobile-compatibility-test.ts && tsx --conditions react-server scripts/mobile-compatibility-route-test.ts && tsx --conditions react-server scripts/mobile-app-promotion-test.ts && tsx --conditions react-server scripts/mobile-app-promotion-postgres-test.ts && tsx scripts/mobile-setup-link-test.ts && tsx scripts/mobile-setup-card-ui-test.tsx", "test:mobile:bootstrap": "tsx --conditions react-server scripts/mobile-bootstrap-test.ts && tsx --conditions react-server scripts/mobile-bootstrap-route-test.ts", @@ -1110,7 +1111,7 @@ "test:api-keys:proxy": "NODE_ENV=test tsx --conditions react-server scripts/api-key-workspace-proxy-test.ts", "test:api-keys:errors": "NODE_ENV=test tsx --conditions react-server scripts/api-key-error-result-test.ts", "test:all:runner": "node --test scripts/test-all-orchestration-test.mjs", - "test:collaboration:regular:contracts": "tsx --test scripts/regular-collaboration-profile-test.mjs scripts/owned-fixture-files-test.mjs scripts/recovered-room-read-test.mjs scripts/completed-native-rename-test.ts", + "test:collaboration:regular:contracts": "tsx --test scripts/regular-collaboration-profile-test.mjs scripts/owned-fixture-files-test.mjs scripts/recovered-room-read-test.mjs scripts/completed-native-rename-test.ts && npm run test:workspaces:path-alias-observations", "test:e2e:collaboration:regular": "E2E_EXTERNAL_SERVER=1 COLLABORATION_E2E=1 CANVAS_REGULAR_COLLABORATION_E2E=1 playwright test --config tests/playwright/playwright.regular-collaboration.config.ts --workers=1 --max-failures=0" }, "dependencies": { diff --git a/scripts/workspace-path-alias-observation-test.ts b/scripts/workspace-path-alias-observation-test.ts new file mode 100644 index 000000000..f3fc7e141 --- /dev/null +++ b/scripts/workspace-path-alias-observation-test.ts @@ -0,0 +1,192 @@ +import assert from 'node:assert/strict'; +import { promises as fs } from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import test from 'node:test'; + +import { assertWorkspacePathHasNoAliases } from '../app/lib/workspaces/path-guard'; +import type { WorkspaceContext } from '../app/lib/workspaces/types'; + +const originalReaddir = fs.readdir; +const contents = Buffer.from('[Document](nested/document.md)\n'); +type Fixture = { root: string; file: string; workspace: WorkspaceContext }; + +async function withFixture(run: (fixture: Fixture) => Promise): Promise { + const root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), 'canvas-path-observation-'))); + await fs.chmod(root, 0o700); + const file = path.join(root, 'Verweise.md'); + const workspace: WorkspaceContext = { + workspaceId: 'owned-path-observation', workspaceType: 'personal', rootPath: root, + organizationId: null, ownerUserId: 'owned-path-observation', legacy: false, + permissions: { canRead: true, canWrite: true, canDelete: true, canCreatePublicLinks: true, + canManageWorkspace: true, canRunAgent: true }, + }; + try { + await fs.writeFile(file, contents); + await run({ root, file, workspace }); + } finally { + Object.defineProperty(fs, 'readdir', { value: originalReaddir }); + await fs.rm(root, { recursive: true, force: true }); + } +} + +async function duringDirectoryObservation( + root: string, + observe: (readActualDirectory: () => Promise) => Promise, + run: () => Promise, +): Promise { + // Interpose real rename/symlink operations between the guard's real lstat + // and real readdir; never invent directory entries or replace lstat results. + Object.defineProperty(fs, 'readdir', { value: async (parent: unknown, ...options: unknown[]) => { + if (String(parent) === root && options.length === 0) return observe(() => originalReaddir(root)); + return Reflect.apply(originalReaddir, fs, [parent, ...options]); + } }); + try { await run(); } + finally { Object.defineProperty(fs, 'readdir', { value: originalReaddir }); } +} + +test('canonical paths retain their exact bytes for read and mutation checks', async () => { + await withFixture(async ({ file, workspace }) => { + await assertWorkspacePathHasNoAliases(workspace, 'Verweise.md', { readOnly: true }); + await assertWorkspacePathHasNoAliases(workspace, 'Verweise.md'); + assert.deepEqual(await fs.readFile(file), contents); + }); +}); + +test('a configured root symlink remains supported', async () => { + await withFixture(async ({ root, file, workspace }) => { + const configuredRoot = path.join(root, 'configured-root'); + await fs.symlink(root, configuredRoot, 'dir'); + await assertWorkspacePathHasNoAliases({ ...workspace, rootPath: configuredRoot }, 'Verweise.md', { readOnly: true }); + assert.deepEqual(await fs.readFile(file), contents); + }); +}); + +test('real file and directory symlinks remain rejected for reads and mutations', async () => { + await withFixture(async ({ root, file, workspace }) => { + await fs.symlink('Verweise.md', path.join(root, 'alias.md')); + await fs.symlink(root, path.join(root, 'directory-alias'), 'dir'); + for (const alias of ['alias.md', 'directory-alias/Verweise.md']) { + for (const options of [{ readOnly: true }, {}]) { + await assert.rejects(assertWorkspacePathHasNoAliases(workspace, alias, options), { code: 'WORKSPACE_PATH_ALIAS' }); + } + } + assert.deepEqual(await fs.readFile(file), contents); + }); +}); + +test('case-folding aliases stay rejected; distinct-case missing paths stay pristine', async (context) => { + await withFixture(async ({ root, workspace }) => { + const caseFolded = await fs.lstat(path.join(root, 'verweise.md')).catch(() => null); + for (const options of [{ readOnly: true }, {}]) { + if (caseFolded) { + await assert.rejects(assertWorkspacePathHasNoAliases(workspace, 'verweise.md', options), { code: 'WORKSPACE_PATH_ALIAS' }); + } else { + await assertWorkspacePathHasNoAliases(workspace, 'verweise.md', options); + await assert.rejects(fs.lstat(path.join(root, 'verweise.md')), { code: 'ENOENT' }); + } + } + context.diagnostic(caseFolded ? 'Actual case-folding filesystem alias checked.' : 'Actual case-sensitive missing-name contract checked.'); + }); +}); + +test('a read refreshes a directory observation after a canonical file disappears and returns', async () => { + await withFixture(async ({ root, file, workspace }) => { + let moved = false; + await duringDirectoryObservation(root, async read => { + if (moved) return read(); + moved = true; + const staged = path.join(root, '.owned-transition'); + await fs.rename(file, staged); + try { return await read(); } + finally { await fs.rename(staged, file); } + }, () => assertWorkspacePathHasNoAliases(workspace, 'Verweise.md', { readOnly: true })); + assert(moved); + assert((await fs.lstat(file)).isFile()); + assert.deepEqual(await fs.readFile(file), contents); + }); +}); + +test('a read refreshes a changed parent-directory observation before descending', async () => { + await withFixture(async ({ root, workspace }) => { + const directory = path.join(root, 'folder'), staged = path.join(root, '.owned-directory'); + await fs.mkdir(directory); + await fs.writeFile(path.join(directory, 'note.md'), contents); + let moved = false; + await duringDirectoryObservation(root, async read => { + if (moved) return read(); + moved = true; + await fs.rename(directory, staged); + try { return await read(); } + finally { await fs.rename(staged, directory); } + }, () => assertWorkspacePathHasNoAliases(workspace, 'folder/note.md', { readOnly: true })); + assert(moved); + assert.deepEqual(await fs.readFile(path.join(directory, 'note.md')), contents); + }); +}); + +test('a newly missing suffix remains missing and never becomes an alias or recreated file', async () => { + await withFixture(async ({ root, file, workspace }) => { + const staged = path.join(root, '.owned-removed-entry'); + let moved = false; + await duringDirectoryObservation(root, async read => { + if (!moved) { moved = true; await fs.rename(file, staged); } + return read(); + }, () => assertWorkspacePathHasNoAliases(workspace, 'Verweise.md', { readOnly: true })); + assert(moved); + await assert.rejects(fs.lstat(file), { code: 'ENOENT' }); + assert.deepEqual(await fs.readFile(staged), contents); + }); +}); + +test('a symlink appearing after the first observation is rejected on the fresh check', async () => { + await withFixture(async ({ root, file, workspace }) => { + const staged = path.join(root, '.owned-symlink-target'); + let moved = false; + await duringDirectoryObservation(root, async read => { + if (moved) return read(); + moved = true; + await fs.rename(file, staged); + const entries = await read(); + await fs.symlink('.owned-symlink-target', file); + return entries; + }, () => assert.rejects(assertWorkspacePathHasNoAliases(workspace, 'Verweise.md', { readOnly: true }), { code: 'WORKSPACE_PATH_ALIAS' })); + assert(moved); + assert((await fs.lstat(file)).isSymbolicLink()); + assert.deepEqual(await fs.readFile(staged), contents); + }); +}); + +test('mutation checks still reject inconsistent directory evidence without retrying a write', async () => { + await withFixture(async ({ root, file, workspace }) => { + await duringDirectoryObservation(root, async read => { + const staged = path.join(root, '.owned-write-transition'); + await fs.rename(file, staged); + try { return await read(); } + finally { await fs.rename(staged, file); } + }, () => assert.rejects(assertWorkspacePathHasNoAliases(workspace, 'Verweise.md'), { code: 'WORKSPACE_PATH_ALIAS' })); + assert.deepEqual(await fs.readFile(file), contents); + }); +}); + +test('continuously inconsistent observations remain bounded and fail closed', async () => { + await withFixture(async ({ root, file, workspace }) => { + let observations = 0; + await duringDirectoryObservation(root, async read => { + assert(++observations <= 3, 'A read must not spin on a changing directory.'); + const staged = path.join(root, '.owned-continuous-transition'); + await fs.rename(file, staged); + try { return await read(); } + finally { await fs.rename(staged, file); } + }, () => assert.rejects(assertWorkspacePathHasNoAliases(workspace, 'Verweise.md', { readOnly: true }), { code: 'WORKSPACE_PATH_ALIAS' })); + assert.deepEqual(await fs.readFile(file), contents); + }); +}); + +test('read-only checks never create a missing workspace root', async () => { + await withFixture(async ({ root, workspace }) => { + const missingRoot = path.join(root, 'missing-root'); + await assert.rejects(assertWorkspacePathHasNoAliases({ ...workspace, rootPath: missingRoot }, 'future.md', { readOnly: true }), { code: 'ENOENT' }); + await assert.rejects(fs.lstat(missingRoot), { code: 'ENOENT' }); + }); +}); diff --git a/tests/studio-bulk-experimental.spec.ts b/tests/studio-bulk-experimental.spec.ts index b6852fcc9..ba89fb339 100644 --- a/tests/studio-bulk-experimental.spec.ts +++ b/tests/studio-bulk-experimental.spec.ts @@ -318,7 +318,14 @@ async function toggleFromSettings(page: Page, enabled: boolean): Promise { await expect(switchControl).toBeChecked({ checked: enabled }); } +async function waitForStudioWorkspace(page: Page): Promise { + // Workspace resolution replaces the initial Studio header subtree. + await expect(page.getByRole('banner').locator('[data-testid="workspace-switcher"]:visible')) + .toHaveAttribute('data-active-workspace-id', /\S+/); +} + async function assertLauncherBulk(page: Page, enabled: boolean): Promise { + await waitForStudioWorkspace(page); console.info(`[studio-bulk-e2e] launcher ${enabled ? 'enabled' : 'disabled'}: open apps at ${new URL(page.url()).pathname}`); await page.getByRole('button', { name: launcherLabel }).click(); console.info('[studio-bulk-e2e] launcher: open Studio actions'); @@ -336,6 +343,7 @@ async function assertLauncherBulk(page: Page, enabled: boolean): Promise { } async function assertMobileNavigation(page: Page, enabled: boolean): Promise { + await waitForStudioWorkspace(page); await page.getByRole('button', { name: 'Studio', exact: true }).click(); const menu = page.getByRole('menu'); await expect(menu.getByRole('menuitem', { name: /^(?:Models|Modelle)$/ })).toBeVisible();