mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
* feat(repo): add graphical folder picker for adding local repositories
Previously, adding a local repository or scanning a folder in the
"Add Repository" dialog required typing an absolute filesystem path by
hand. This adds a navigable directory browser so users can select a
folder graphically instead.
Backend
- New shared Zod schemas (DirectoryEntry, BrowseDirectoryResponse,
BrowseDirectoryRequest) exported via shared/types and shared/schemas.
- New env var REPO_BROWSE_ROOT plus getBrowseRootPath() helper. It is
intentionally NOT defaulted to WORKSPACE_PATH: folder browsing is
opt-in and only works when an administrator explicitly configures a
root directory.
- New filesystem service (browseDirectory) that lists only directories
(hidden entries filtered), flags Git repositories, and confines all
access to the configured root:
- 501 when REPO_BROWSE_ROOT is not configured
- 403 on path traversal outside the root
- 404 for missing directories, 400 when the target is a file
- New authenticated route GET /api/filesystem/browse, registered under
the protected API group.
Frontend
- New api client browseDirectory() (uses fetchWrapper, so auth cookies
are sent).
- New DirectoryPickerDialog: navigate into subfolders, go up a level,
Git repos marked with an icon, and a "Select This Folder" action.
Retries are disabled so a configuration error is not polled
repeatedly.
- When browsing is disabled (501), the dialog shows a clear, actionable
message telling the user to set REPO_BROWSE_ROOT and to enter the
path manually in the meantime, instead of a generic error.
- AddRepoDialog gains a "Browse" button next to both the Local and
Folder path inputs; the chosen path is written back into the active
field.
Tests
- Service and route tests covering listing, Git-repo detection, hidden
filtering, parent navigation, and the 400/403/404/501 error paths.
Security note: because there is no default root, the picker is inert
until REPO_BROWSE_ROOT is set, and it can never escape that root.
* fix(fs): reject symlink escapes in directory browse
Canonicalize root and requested path with fs.realpath() and gate on
path.relative() so symlinks inside REPO_BROWSE_ROOT pointing outside
are rejected with 403 before fs.stat/fs.readdir follow them. Handles
root '/' correctly. Adds regression test for symlink escape.
* fix(ui): make directory picker fill dialog height
Replace grid content-start packing with explicit grid-rows tracks so
the directory list grows to fill the dialog and scrolls while the
header, path, and footer stay fixed. Drops the ineffective flex-1
from the grid child on the scroll container.
---------
Co-authored-by: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com>
114 lines
3.9 KiB
TypeScript
114 lines
3.9 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import { promises as fs } from 'node:fs'
|
|
import path from 'node:path'
|
|
import os from 'node:os'
|
|
import { browseDirectory } from './filesystem'
|
|
|
|
let tmpRoot: string
|
|
const originalBrowseRoot = process.env.REPO_BROWSE_ROOT
|
|
|
|
beforeEach(async () => {
|
|
tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'ocm-browse-'))
|
|
process.env.REPO_BROWSE_ROOT = tmpRoot
|
|
})
|
|
|
|
afterEach(async () => {
|
|
if (originalBrowseRoot === undefined) {
|
|
delete process.env.REPO_BROWSE_ROOT
|
|
} else {
|
|
process.env.REPO_BROWSE_ROOT = originalBrowseRoot
|
|
}
|
|
await fs.rm(tmpRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
describe('browseDirectory', () => {
|
|
it('lists only directories at the browse root and marks the root', async () => {
|
|
await fs.mkdir(path.join(tmpRoot, 'projects'))
|
|
await fs.mkdir(path.join(tmpRoot, 'archive'))
|
|
await fs.writeFile(path.join(tmpRoot, 'readme.txt'), 'ignored file')
|
|
|
|
const result = await browseDirectory()
|
|
|
|
expect(result.isRoot).toBe(true)
|
|
expect(result.parentPath).toBeNull()
|
|
expect(result.path).toBe(path.resolve(tmpRoot))
|
|
expect(result.entries.map((e) => e.name)).toEqual(['archive', 'projects'])
|
|
})
|
|
|
|
it('marks git repositories', async () => {
|
|
const repoDir = path.join(tmpRoot, 'my-repo')
|
|
await fs.mkdir(repoDir)
|
|
await fs.mkdir(path.join(repoDir, '.git'))
|
|
await fs.mkdir(path.join(tmpRoot, 'plain'))
|
|
|
|
const result = await browseDirectory()
|
|
|
|
const repo = result.entries.find((e) => e.name === 'my-repo')
|
|
const plain = result.entries.find((e) => e.name === 'plain')
|
|
expect(repo?.isGitRepo).toBe(true)
|
|
expect(plain?.isGitRepo).toBe(false)
|
|
})
|
|
|
|
it('ignores hidden directories', async () => {
|
|
await fs.mkdir(path.join(tmpRoot, '.hidden'))
|
|
await fs.mkdir(path.join(tmpRoot, 'visible'))
|
|
|
|
const result = await browseDirectory()
|
|
|
|
expect(result.entries.map((e) => e.name)).toEqual(['visible'])
|
|
})
|
|
|
|
it('navigates into a subdirectory and exposes the parent path', async () => {
|
|
const sub = path.join(tmpRoot, 'level1')
|
|
await fs.mkdir(sub)
|
|
await fs.mkdir(path.join(sub, 'level2'))
|
|
|
|
const result = await browseDirectory(sub)
|
|
|
|
expect(result.isRoot).toBe(false)
|
|
expect(result.parentPath).toBe(path.resolve(tmpRoot))
|
|
expect(result.entries.map((e) => e.name)).toEqual(['level2'])
|
|
})
|
|
|
|
it('rejects paths outside the browse root with 403', async () => {
|
|
const outside = await fs.mkdtemp(path.join(os.tmpdir(), 'ocm-outside-'))
|
|
try {
|
|
await expect(browseDirectory(outside)).rejects.toMatchObject({ statusCode: 403 })
|
|
} finally {
|
|
await fs.rm(outside, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('rejects traversal above the root with 403', async () => {
|
|
await expect(browseDirectory(path.join(tmpRoot, '..'))).rejects.toMatchObject({ statusCode: 403 })
|
|
})
|
|
|
|
it('rejects a symlink inside the root that escapes the root with 403', async () => {
|
|
const outside = await fs.mkdtemp(path.join(os.tmpdir(), 'ocm-outside-'))
|
|
try {
|
|
const linkPath = path.join(tmpRoot, 'escape-link')
|
|
await fs.symlink(outside, linkPath)
|
|
|
|
await expect(browseDirectory(linkPath)).rejects.toMatchObject({ statusCode: 403 })
|
|
} finally {
|
|
await fs.rm(outside, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('returns 404 for a non-existent directory', async () => {
|
|
await expect(browseDirectory(path.join(tmpRoot, 'nope'))).rejects.toMatchObject({ statusCode: 404 })
|
|
})
|
|
|
|
it('returns 400 when the path is a file', async () => {
|
|
const filePath = path.join(tmpRoot, 'file.txt')
|
|
await fs.writeFile(filePath, 'data')
|
|
|
|
await expect(browseDirectory(filePath)).rejects.toMatchObject({ statusCode: 400 })
|
|
})
|
|
|
|
it('returns 501 when REPO_BROWSE_ROOT is not configured', async () => {
|
|
delete process.env.REPO_BROWSE_ROOT
|
|
await expect(browseDirectory()).rejects.toMatchObject({ statusCode: 501 })
|
|
})
|
|
})
|