Agent Zero AI framework
Git Clone Authentication for Private Repositories
keyboardstaffe2824cd6e904
diff --git a/python/api/projects.py b/python/api/projects.pyindex ba82e383c..6995d9da7 100644--- a/python/api/projects.py+++ b/python/api/projects.py@@ -57,6 +57,7 @@ class Projects(ApiHandler):if project is None:raise Exception("Project data is required")git_url = project.get("git_url", "")+ git_token = project.get("git_token", "")if not git_url:raise Exception("Git URL is required")@@ -72,7 +73,7 @@ class Projects(ApiHandler):try:data = projects.BasicProjectData(**project)- name = projects.clone_git_project(project["name"], git_url, data)+ name = projects.clone_git_project(project["name"], git_url, git_token, data)# Success notificationNotificationManager.send_notification(diff --git a/python/helpers/git.py b/python/helpers/git.pyindex f89fce5be..da0e80183 100644--- a/python/helpers/git.py+++ b/python/helpers/git.py@@ -1,8 +1,25 @@from git import Repofrom datetime import datetimeimport os+import subprocess+import base64+from urllib.parse import urlparse, urlunparsefrom python.helpers import files++def strip_auth_from_url(url: str) -> str:+ """Remove any authentication info from URL."""+ if not url:+ return url+ parsed = urlparse(url)+ if not parsed.hostname:+ return url+ clean_netloc = parsed.hostname+ if parsed.port:+ clean_netloc += f":{parsed.port}"+ return urlunparse((parsed.scheme, clean_netloc, parsed.path, '', '', ''))++def get_git_info():# Get the current working directory (assuming the repo is in the same folder as the script)repo_path = files.get_base_dir()@@ -57,13 +74,28 @@ def get_version():return "unknown"-def clone_repo(url: str, dest: str, progress_callback=None):- """Clone a git repository to destination."""- class Progress:- def __call__(self, op_code, cur_count, max_count=None, message=''):- if progress_callback and max_count:- progress_callback(cur_count, max_count, message)- return Repo.clone_from(url, dest, progress=Progress() if progress_callback else None)+def clone_repo(url: str, dest: str, token: str | None = None):+ """Clone a git repository. Uses http.extraHeader for token auth (never stored in URL/config)."""+ cmd = ['git']++ if token:+ # GitHub Git HTTP requires Basic Auth, not Bearer+ auth_string = f"x-access-token:{token}"+ auth_base64 = base64.b64encode(auth_string.encode()).decode()+ cmd.extend(['-c', f'http.extraHeader=Authorization: Basic {auth_base64}'])++ cmd.extend(['clone', '--progress', '--', url, dest])++ env = os.environ.copy()+ env['GIT_TERMINAL_PROMPT'] = '0'++ result = subprocess.run(cmd, capture_output=True, text=True, env=env)++ if result.returncode != 0:+ error_msg = result.stderr.strip() or result.stdout.strip() or 'Unknown error'+ raise Exception(f"Git clone failed: {error_msg}")++ return Repo(dest)# Files to ignore when checking dirty status (A0 project metadata)@@ -77,11 +109,11 @@ def get_repo_status(repo_path: str) -> dict:if repo.bare:return {"is_git_repo": False, "error": "Repository is bare"}- # Remote URL+ # Remote URL (always strip auth info for security)remote_url = ""try:if repo.remotes:- remote_url = repo.remotes.origin.url+ remote_url = strip_auth_from_url(repo.remotes.origin.url)except Exception:passdiff --git a/python/helpers/projects.py b/python/helpers/projects.pyindex d008f158a..d1b512112 100644--- a/python/helpers/projects.py+++ b/python/helpers/projects.py@@ -88,8 +88,8 @@ def create_project(name: str, data: BasicProjectData):return name-def clone_git_project(name: str, git_url: str, data: BasicProjectData):- """Clone a git repository as a new A0 project."""+def clone_git_project(name: str, git_url: str, git_token: str, data: BasicProjectData):+ """Clone a git repository as a new A0 project. Token is used only for cloning via http header."""from python.helpers import gitabs_path = files.create_dir_safe(@@ -98,10 +98,14 @@ def clone_git_project(name: str, git_url: str, data: BasicProjectData):actual_name = files.basename(abs_path)try:- git.clone_repo(git_url, abs_path)+ # Clone with token via http.extraHeader (token never in URL or git config)+ git.clone_repo(git_url, abs_path, token=git_token)++ # Store clean URL only (in case user provided URL with auth)+ clean_url = git.strip_auth_from_url(git_url)create_project_meta_folders(actual_name)data = _normalizeBasicData(data)- data["git_url"] = git_url+ data["git_url"] = clean_urlsave_project_header(actual_name, data)return actual_nameexcept Exception as e:diff --git a/webui/components/projects/project-create.html b/webui/components/projects/project-create.htmlindex 486dbc400..b8140cd9a 100644--- a/webui/components/projects/project-create.html+++ b/webui/components/projects/project-create.html@@ -25,6 +25,17 @@placeholder="https://github.com/user/repo.git"></div>+ <template x-if="$store.projects.selectedProject.git_url && $store.projects.selectedProject.git_url.trim()">+ <div class="projects-form-group">+ <label class="projects-form-label">Access Token (optional)</label>+ <span class="projects-form-description">For private repositories. Token is used only for cloning and will not be stored.</span>+ <input class="projects-form-input" type="password"+ x-model="$store.projects.selectedProject.git_token"+ :disabled="$store.projects.selectedProject._cloning"+ placeholder="ghp_xxxx or glpat-xxxx">+ </div>+ </template>+<div class="buttons-right"><button type="button" class="button cancel"@click="$store.projects.cancelCreate()"diff --git a/webui/components/projects/projects-store.js b/webui/components/projects/projects-store.jsindex 2e6466908..e813dd187 100644--- a/webui/components/projects/projects-store.js+++ b/webui/components/projects/projects-store.js@@ -139,6 +139,7 @@ const model = {title: project.title,color: project.color,git_url: project.git_url,+ git_token: project.git_token || "",},});@@ -386,6 +387,7 @@ const model = {description: "",color: "",git_url: "",+ git_token: "",};},