fix(ui): address tracked repo review feedback

Validate tracked repo names against path traversal and surface repository listing errors in the uninstall handler.
Track in-flight dashboard repo actions with sets so loading state stays correct during overlapping clicks.
This commit is contained in:
amdoi7
2026-04-03 15:14:51 +08:00
parent 14da64f33a
commit e3ea2afb88
2 changed files with 42 additions and 22 deletions
+18 -8
View File
@@ -217,11 +217,13 @@ func (s *Server) handleUninstallRepo(w http.ResponseWriter, r *http.Request) {
defer s.mu.Unlock()
name := strings.TrimSpace(r.PathValue("name"))
if name == "" {
writeError(w, http.StatusBadRequest, "tracked repository name is required")
cleanName := filepath.Clean(filepath.FromSlash(name))
if name == "" || cleanName == "." || cleanName == ".." || filepath.IsAbs(cleanName) || strings.HasPrefix(cleanName, ".."+string(filepath.Separator)) {
writeError(w, http.StatusBadRequest, "invalid or missing tracked repository name")
return
}
sourceRoot := filepath.Clean(s.cfg.Source)
resolveRepo := func(input string) (string, string) {
candidates := []string{input}
if !strings.HasPrefix(filepath.Base(input), "_") {
@@ -232,7 +234,11 @@ func (s *Server) handleUninstallRepo(w http.ResponseWriter, r *http.Request) {
}
}
for _, candidate := range candidates {
repoPath := filepath.Join(s.cfg.Source, candidate)
repoPath := filepath.Clean(filepath.Join(sourceRoot, candidate))
relPath, relErr := filepath.Rel(sourceRoot, repoPath)
if relErr != nil || relPath == ".." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) {
continue
}
if install.IsGitRepo(repoPath) {
return candidate, repoPath
}
@@ -240,27 +246,31 @@ func (s *Server) handleUninstallRepo(w http.ResponseWriter, r *http.Request) {
return "", ""
}
repoName, repoPath := resolveRepo(name)
repoName, repoPath := resolveRepo(cleanName)
if repoPath == "" {
// Fallback: match nested tracked repos by basename.
matches := make([]string, 0, 2)
repos, _ := install.GetTrackedRepos(s.cfg.Source)
repos, err := install.GetTrackedRepos(s.cfg.Source)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to list tracked repositories: "+err.Error())
return
}
for _, repo := range repos {
base := filepath.Base(repo)
trimmed := strings.TrimPrefix(base, "_")
if base == name || trimmed == name {
if base == cleanName || trimmed == cleanName {
matches = append(matches, repo)
}
}
switch len(matches) {
case 0:
writeError(w, http.StatusBadRequest, "not a tracked repository: "+name)
writeError(w, http.StatusBadRequest, "not a tracked repository: "+cleanName)
return
case 1:
repoName = matches[0]
repoPath = filepath.Join(s.cfg.Source, repoName)
default:
writeError(w, http.StatusBadRequest, "multiple tracked repositories match: "+name+" — use the full path")
writeError(w, http.StatusBadRequest, "multiple tracked repositories match: "+cleanName+" — use the full path")
return
}
}
+24 -14
View File
@@ -417,9 +417,9 @@ export default function DashboardPage() {
function TrackedReposSection({ repos }: { repos: { name: string; skillCount: number; dirty: boolean }[] }) {
const queryClient = useQueryClient();
const { toast } = useToast();
const [updatingRepo, setUpdatingRepo] = useState<string | null>(null);
const [updatingRepos, setUpdatingRepos] = useState<Set<string>>(new Set());
const [repoToDelete, setRepoToDelete] = useState<string | null>(null);
const [deletingRepo, setDeletingRepo] = useState<string | null>(null);
const [deletingRepos, setDeletingRepos] = useState<Set<string>>(new Set());
const invalidateRepoData = async () => {
await queryClient.invalidateQueries({ queryKey: queryKeys.overview });
@@ -428,7 +428,7 @@ function TrackedReposSection({ repos }: { repos: { name: string; skillCount: num
};
const handleUpdateRepo = async (repoName: string) => {
setUpdatingRepo(repoName);
setUpdatingRepos((prev) => new Set(prev).add(repoName));
try {
const res = await api.update({ name: repoName });
const item = res.results[0];
@@ -450,24 +450,33 @@ function TrackedReposSection({ repos }: { repos: { name: string; skillCount: num
} catch (e: unknown) {
toast((e as Error).message, 'error');
} finally {
setUpdatingRepo(null);
setUpdatingRepos((prev) => {
const next = new Set(prev);
next.delete(repoName);
return next;
});
}
};
const handleDeleteRepo = async () => {
if (!repoToDelete) return;
setDeletingRepo(repoToDelete);
const targetRepo = repoToDelete;
setDeletingRepos((prev) => new Set(prev).add(targetRepo));
try {
const displayName = repoToDelete.replace(/^_/, '');
await api.deleteRepo(repoToDelete);
const displayName = targetRepo.replace(/^_/, '');
await api.deleteRepo(targetRepo);
toast(`Repository "${displayName}" uninstalled.`, 'success');
setRepoToDelete(null);
await invalidateRepoData();
} catch (e: unknown) {
toast((e as Error).message, 'error');
} finally {
setDeletingRepo(null);
setDeletingRepos((prev) => {
const next = new Set(prev);
next.delete(targetRepo);
return next;
});
}
};
@@ -485,8 +494,9 @@ function TrackedReposSection({ repos }: { repos: { name: string; skillCount: num
<div className="space-y-3">
{repos.map((repo) => {
const displayName = repo.name.replace(/^_/, '');
const isUpdating = updatingRepo === repo.name;
const isDeleting = deletingRepo === repo.name;
const isUpdating = updatingRepos.has(repo.name);
const isDeleting = deletingRepos.has(repo.name);
const hasAnyDeleteInProgress = deletingRepos.size > 0;
const isBusy = isUpdating || isDeleting;
return (
@@ -521,7 +531,7 @@ function TrackedReposSection({ repos }: { repos: { name: string; skillCount: num
size="sm"
onClick={() => handleUpdateRepo(repo.name)}
loading={isUpdating}
disabled={!!deletingRepo}
disabled={hasAnyDeleteInProgress}
>
<RefreshCw size={14} />
Update
@@ -530,7 +540,7 @@ function TrackedReposSection({ repos }: { repos: { name: string; skillCount: num
variant="danger"
size="sm"
onClick={() => setRepoToDelete(repo.name)}
disabled={isBusy || !!updatingRepo}
disabled={isBusy || repoToDelete !== null || updatingRepos.size > 0}
>
<Trash2 size={14} />
Uninstall
@@ -552,10 +562,10 @@ function TrackedReposSection({ repos }: { repos: { name: string; skillCount: num
}
confirmText="Uninstall"
variant="danger"
loading={deletingRepo !== null}
loading={repoToDelete !== null && deletingRepos.has(repoToDelete)}
onConfirm={handleDeleteRepo}
onCancel={() => {
if (!deletingRepo) setRepoToDelete(null);
if (repoToDelete === null || !deletingRepos.has(repoToDelete)) setRepoToDelete(null);
}}
/>
</>