Files
opencode-manager/shared/src/utils/repo.ts
T
Chris Scott 60ed756a0b 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
2026-05-30 19:12:04 -04:00

108 lines
3.0 KiB
TypeScript

function trimTrailingChar(value: string, char: string): string {
let end = value.length
while (end > 0 && value[end - 1] === char) end--
return value.slice(0, end)
}
export function sanitizeRepoDirectoryName(input: string): string {
const collapsed = input.trim().replace(/[^a-zA-Z0-9._-]+/g, '-').replace(/^-+/, '')
const sanitized = trimTrailingChar(collapsed, '-')
return sanitized || 'repo'
}
export function getRepoDirectoryNameError(input: string): string | null {
const trimmed = input.trim()
if (!trimmed) {
return 'Directory name is required'
}
if (trimmed === '.' || trimmed.includes('..')) {
return 'Directory name cannot contain dot-dot path segments'
}
if (/^(?:[a-zA-Z]:)?[\\/]/.test(trimmed)) {
return 'Directory name must be relative'
}
if (/[\\/]/.test(trimmed)) {
return 'Directory name cannot contain path separators'
}
if (sanitizeRepoDirectoryName(trimmed) !== trimmed) {
return 'Directory name can only contain letters, numbers, dots, underscores, and hyphens'
}
return null
}
export function normalizeRepoDirectoryName(input: string): string {
const error = getRepoDirectoryNameError(input)
if (error) {
throw new Error(error)
}
return input.trim()
}
export function sanitizeBranchForDirectory(branch: string): string {
return branch.replace(/[\\/]/g, '-')
}
export function getRepoBaseDirectoryName(repo: { localPath: string; branch?: string; isWorktree?: boolean }): string {
if (repo.isWorktree && repo.branch) {
const suffix = `-${sanitizeBranchForDirectory(repo.branch)}`
if (repo.localPath.endsWith(suffix)) {
return repo.localPath.slice(0, -suffix.length)
}
}
return repo.localPath
}
export function getRepoNameFromUrl(url: string): string {
const cleaned = trimTrailingChar(url.trim().replace(/\.git$/, ''), '/')
const scpMatch = cleaned.match(/^git@[^:]+:(.+)$/)
if (scpMatch) {
const parts = scpMatch[1]?.split('/') ?? []
return parts[parts.length - 1] || ''
}
const parts = cleaned.split('/')
return parts[parts.length - 1] || ''
}
export function normalizeRepoUrlForCompare(url: string): string {
let normalized = trimTrailingChar(url.trim().replace(/\.git$/, ''), '/')
const shorthandMatch = normalized.match(/^([^/]+)\/([^/]+)$/)
if (shorthandMatch && !normalized.includes('://') && !normalized.startsWith('git@')) {
return `https://github.com/${normalized}`.toLowerCase()
}
const scpMatch = normalized.match(/^git@([^:]+):(.+)$/)
if (scpMatch) {
return `https://${scpMatch[1]}/${scpMatch[2]}`.toLowerCase()
}
if (normalized.startsWith('ssh://')) {
try {
const parsed = new URL(normalized)
const path = parsed.pathname.replace(/^\/+/, '')
const host = parsed.port ? `${parsed.hostname}:${parsed.port}` : parsed.hostname
return `https://${host}/${path}`.toLowerCase()
} catch {
return normalized.toLowerCase()
}
}
if (normalized.startsWith('http://')) {
normalized = `https://${normalized.slice(7)}`
}
return normalized.toLowerCase()
}