mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix: repo directory name collisions and custom directory support (#241)
* fix: repo directory name collisions and custom directory support Allow users to specify a custom directory name when adding a remote repo, preventing collisions when cloning a fork of an already-cloned repo. - Add shared validation and normalization utilities (shared/src/utils/repo.ts) for directory name sanitization, URL normalization, and path safety - Replace duplicated sanitizeWorkspaceAliasSegment with shared helper - Backend validates directoryName before use as filesystem path - Frontend detects directory collisions with normalized URL comparison - Move SSH credential setup after DB record creation * add backend origin validation and base-directory collision detection * fix potential ReDoS in repo url and directory name normalization
This commit is contained in:
1 parent
932d6b8d68
commit
60ed756a0b
7 files changed
+216
-29
No files matched your search
@@ -7,6 +7,7 @@ import type { Database } from 'bun:sqlite'
|
||||
import type { Repo, CreateRepoInput } from '../types/repo'
|
||||
import { logger } from '../utils/logger'
|
||||
import { getReposPath } from '@opencode-manager/shared/config/env'
|
||||
import { normalizeRepoDirectoryName, sanitizeRepoDirectoryName, sanitizeBranchForDirectory, normalizeRepoUrlForCompare } from '@opencode-manager/shared/utils'
|
||||
import type { GitAuthService } from './git-auth'
|
||||
import { isGitHubHttpsUrl, isSSHUrl, normalizeSSHUrl } from '../utils/git-auth'
|
||||
import path from 'path'
|
||||
@@ -103,19 +104,9 @@ async function isGitWorktreeRepo(targetPath: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
function sanitizeWorkspaceAliasSegment(segment: string): string {
|
||||
const sanitized = segment
|
||||
.trim()
|
||||
.replace(/[^a-zA-Z0-9._-]+/g, '-')
|
||||
.replace(/^-+/, '')
|
||||
.replace(/-+$/, '')
|
||||
|
||||
return sanitized || 'repo'
|
||||
}
|
||||
|
||||
function buildWorkspaceAliasCandidates(sourcePath: string, rootPath?: string): string[] {
|
||||
const candidates: string[] = []
|
||||
const baseName = sanitizeWorkspaceAliasSegment(path.basename(sourcePath))
|
||||
const baseName = sanitizeRepoDirectoryName(path.basename(sourcePath))
|
||||
candidates.push(baseName)
|
||||
|
||||
if (rootPath) {
|
||||
@@ -123,7 +114,7 @@ function buildWorkspaceAliasCandidates(sourcePath: string, rootPath?: string): s
|
||||
if (relativePath && !relativePath.startsWith('..')) {
|
||||
const relativeAlias = relativePath
|
||||
.split(path.sep)
|
||||
.map(sanitizeWorkspaceAliasSegment)
|
||||
.map(sanitizeRepoDirectoryName)
|
||||
.filter(Boolean)
|
||||
.join('--')
|
||||
|
||||
@@ -579,6 +570,7 @@ export async function initLocalRepo(
|
||||
|
||||
export interface CloneRepoOptions {
|
||||
branch?: string
|
||||
directoryName?: string
|
||||
useWorktree?: boolean
|
||||
skipSSHVerification?: boolean
|
||||
baseBranch?: string
|
||||
@@ -590,24 +582,22 @@ export async function cloneRepo(
|
||||
repoUrl: string,
|
||||
options: CloneRepoOptions = {}
|
||||
): Promise<Repo> {
|
||||
const { branch, useWorktree = false, skipSSHVerification = false, baseBranch } = options
|
||||
const { branch, directoryName, useWorktree = false, skipSSHVerification = false, baseBranch } = options
|
||||
const effectiveUrl = normalizeSSHUrl(repoUrl)
|
||||
const isSSH = isSSHUrl(effectiveUrl)
|
||||
const preserveSSH = isSSH
|
||||
const hasSSHCredential = await gitAuthService.setupSSHForRepoUrl(effectiveUrl, database, skipSSHVerification)
|
||||
|
||||
const { url: normalizedRepoUrl, name: repoName } = normalizeRepoUrl(effectiveUrl, preserveSSH)
|
||||
const baseRepoDirName = repoName
|
||||
const worktreeDirName = branch && useWorktree ? `${repoName}-${branch.replace(/[\\/]/g, '-')}` : repoName
|
||||
const dirName = directoryName === undefined
|
||||
? sanitizeRepoDirectoryName(repoName)
|
||||
: normalizeRepoDirectoryName(directoryName)
|
||||
const baseRepoDirName = dirName
|
||||
const worktreeDirName = branch && useWorktree ? `${dirName}-${sanitizeBranchForDirectory(branch)}` : dirName
|
||||
const localPath = worktreeDirName
|
||||
|
||||
const existing = getRepoByUrlAndBranch(database, normalizedRepoUrl, branch)
|
||||
|
||||
if (existing) {
|
||||
logger.info(`Repo branch already exists: ${normalizedRepoUrl}${branch ? `#${branch}` : ''}`)
|
||||
if (hasSSHCredential) {
|
||||
await gitAuthService.cleanupSSHKey()
|
||||
}
|
||||
return existing
|
||||
}
|
||||
|
||||
@@ -632,6 +622,8 @@ export async function cloneRepo(
|
||||
const repo = createRepo(database, createRepoInput)
|
||||
|
||||
try {
|
||||
await gitAuthService.setupSSHForRepoUrl(effectiveUrl, database, skipSSHVerification)
|
||||
|
||||
const env = {
|
||||
...gitAuthService.getGitEnvironment(),
|
||||
...(isSSH ? gitAuthService.getSSHEnvironment() : {})
|
||||
@@ -713,6 +705,17 @@ export async function cloneRepo(
|
||||
const isValidRepo = await executeCommand(['git', '-C', path.resolve(getReposPath(), baseRepoDirName), 'rev-parse', '--git-dir'], path.resolve(getReposPath())).then(() => 'valid').catch(() => 'invalid')
|
||||
|
||||
if (isValidRepo.trim() === 'valid') {
|
||||
const existingOriginUrl = await executeCommand(
|
||||
['git', '-C', path.resolve(getReposPath(), baseRepoDirName), 'remote', 'get-url', 'origin'],
|
||||
{ cwd: path.resolve(getReposPath()), silent: true }
|
||||
).then((output) => output.trim()).catch(() => '')
|
||||
|
||||
if (existingOriginUrl && normalizeRepoUrlForCompare(existingOriginUrl) !== normalizeRepoUrlForCompare(normalizedRepoUrl)) {
|
||||
const collisionError = new Error(`Directory '${baseRepoDirName}' already contains a different repository (${existingOriginUrl}). Choose a different directory name.`) as Error & { statusCode: number }
|
||||
collisionError.statusCode = 409
|
||||
throw collisionError
|
||||
}
|
||||
|
||||
logger.info(`Valid repository found: ${normalizedRepoUrl}`)
|
||||
|
||||
if (branch) {
|
||||
|
||||
Reference in new issue
Block a user