Repository navigation
fix(filesystem): open files on a Windows mapped or subst drive #24243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
JahyunBaek
wants to merge
3
commits into
stablyai:main
from
JahyunBaek:fix/external-grant-native-realpath
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
41173fd
fix(filesystem): record the native realpath when granting an external…
JahyunBaek eb5e381
fix(filesystem): skip the native realpath when the JS one already failed
JahyunBaek 6b1a77e
test(filesystem): model the mapped drive with the shared containment …
JahyunBaek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/main/ipc/filesystem-auth-external-grant-native-realpath.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import type * as NodeFs from 'node:fs' | ||
| import type * as NodeFsPromises from 'node:fs/promises' | ||
| import { join, relative, resolve } from 'node:path' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import type { Store } from '../persistence' | ||
| import type { FolderWorkspace } from '../../shared/folder-workspace-types' | ||
| import type { ProjectGroup } from '../../shared/project-group-types' | ||
| import type { Repo } from '../../shared/repo-types' | ||
| import { | ||
| PATH_ACCESS_DENIED_MESSAGE, | ||
| authorizeExternalPath, | ||
| resolveAuthorizedPath | ||
| } from './filesystem-auth' | ||
| import { isDescendantOrEqual } from './filesystem-path-containment' | ||
|
|
||
| const fsMocks = vi.hoisted(() => ({ | ||
| jsRealpath: vi.fn((path: string) => path), | ||
| nativeRealpath: vi.fn((path: string) => path) | ||
| })) | ||
|
|
||
| vi.mock('node:fs', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof NodeFs>() | ||
| return { | ||
| ...actual, | ||
| realpathSync: Object.assign(fsMocks.jsRealpath, { native: fsMocks.nativeRealpath }) | ||
| } | ||
| }) | ||
| vi.mock('node:fs/promises', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof NodeFsPromises>() | ||
| return { ...actual, realpath: async (path: string) => fsMocks.nativeRealpath(path) } | ||
| }) | ||
| vi.mock('../repo-worktrees', () => ({ listRepoWorktreeGraph: vi.fn(), isRepoRoot: vi.fn() })) | ||
|
|
||
| // Why: a mapped (`net use`) or subst drive — the JS realpath keeps `X:\…`, the native one returns | ||
| // the share or target path. Modelled with plain roots so the case runs on every platform. | ||
| const mappedDrive = resolve('/mapped-drive') | ||
| const shareRoot = resolve('/nas-share') | ||
|
|
||
| function toShare(path: string): string { | ||
| return isDescendantOrEqual(path, mappedDrive) | ||
| ? join(shareRoot, relative(mappedDrive, path)) | ||
| : path | ||
| } | ||
| fsMocks.nativeRealpath.mockImplementation(toShare) | ||
|
|
||
| // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: Authorization reads only these catalog and settings methods; an empty catalog leaves session grants as the only way in. | ||
| const emptyStore = { | ||
| getRepos: (): Repo[] => [], | ||
| getProjectGroups: (): ProjectGroup[] => [], | ||
| getFolderWorkspaces: (): FolderWorkspace[] => [], | ||
| getSettings: (): { workspaceDir?: string } => ({}) | ||
| } as Store | ||
|
|
||
| describe('external path grants on a drive the native realpath resolves elsewhere', () => { | ||
| it('lets a granted file on a mapped drive pass the read check', async () => { | ||
| const file = join(mappedDrive, 'docs', 'page-1.png') | ||
| authorizeExternalPath(file) | ||
|
|
||
| await expect(resolveAuthorizedPath(file, emptyStore)).resolves.toBe( | ||
| join(shareRoot, 'docs', 'page-1.png') | ||
| ) | ||
| }) | ||
|
|
||
| it('lets files under a granted mapped-drive folder pass the read check', async () => { | ||
| authorizeExternalPath(join(mappedDrive, 'notes')) | ||
|
|
||
| await expect( | ||
| resolveAuthorizedPath(join(mappedDrive, 'notes', 'todo.md'), emptyStore) | ||
| ).resolves.toBe(join(shareRoot, 'notes', 'todo.md')) | ||
| }) | ||
|
|
||
| it('does not wait on the native realpath once the JS one has failed', () => { | ||
| fsMocks.jsRealpath.mockImplementationOnce(() => { | ||
| throw new Error('network path not found') | ||
| }) | ||
| fsMocks.nativeRealpath.mockClear() | ||
|
|
||
| authorizeExternalPath(join(mappedDrive, 'offline', 'report.pdf')) | ||
|
|
||
| expect(fsMocks.nativeRealpath).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not extend a file grant to its siblings', async () => { | ||
| authorizeExternalPath(join(mappedDrive, 'private', 'shared.png')) | ||
|
|
||
| await expect( | ||
| resolveAuthorizedPath(join(mappedDrive, 'private', 'secret.txt'), emptyStore) | ||
| ).rejects.toThrow(PATH_ACCESS_DENIED_MESSAGE) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.