Claude commited on
Commit
24d2009
·
1 Parent(s): 7d3520f

fix: validate GitHub token format before using

Browse files
Files changed (1) hide show
  1. app.py +16 -2
app.py CHANGED
@@ -574,15 +574,29 @@ class KanikoBuilder:
574
  self.state.log(f"Registry auth configured for {self.config.registry_url}")
575
  return True
576
 
 
 
 
 
 
 
 
 
577
  def clone_repo(self, build_config: BuildConfig) -> Path:
578
  target_dir = Path(tempfile.mkdtemp())
 
579
  token = build_config.github_token or self.config.github_token
580
  repo_url = build_config.repo_url
 
581
 
582
- if token and "github.com" in repo_url:
 
583
  repo_url = repo_url.replace("https://github.com", f"https://{token}@github.com")
 
584
  self.state.log(f"Cloning {build_config.repo_url} ({build_config.branch}) [authenticated]")
585
  else:
 
 
586
  self.state.log(f"Cloning {build_config.repo_url} ({build_config.branch})")
587
 
588
  try:
@@ -591,7 +605,7 @@ class KanikoBuilder:
591
  self.state.log(f"Cloned to {target_dir}")
592
  return target_dir
593
  except Exception as e:
594
- error_msg = mask_token(str(e), token)
595
  self.state.log(f"Clone failed: {error_msg}", level="error")
596
  raise RuntimeError(f"Clone failed: {error_msg}")
597
 
 
574
  self.state.log(f"Registry auth configured for {self.config.registry_url}")
575
  return True
576
 
577
+ def _is_valid_github_token(self, token: str) -> bool:
578
+ """Check if token looks like a valid GitHub token."""
579
+ if not token or len(token) < 10:
580
+ return False
581
+ # GitHub PAT formats: ghp_, gho_, ghu_, ghs_, ghr_, github_pat_
582
+ valid_prefixes = ("ghp_", "gho_", "ghu_", "ghs_", "ghr_", "github_pat_")
583
+ return token.startswith(valid_prefixes)
584
+
585
  def clone_repo(self, build_config: BuildConfig) -> Path:
586
  target_dir = Path(tempfile.mkdtemp())
587
+ # Prefer explicit token from request, fall back to config
588
  token = build_config.github_token or self.config.github_token
589
  repo_url = build_config.repo_url
590
+ use_auth = False
591
 
592
+ # Only use token if it looks valid
593
+ if token and self._is_valid_github_token(token) and "github.com" in repo_url:
594
  repo_url = repo_url.replace("https://github.com", f"https://{token}@github.com")
595
+ use_auth = True
596
  self.state.log(f"Cloning {build_config.repo_url} ({build_config.branch}) [authenticated]")
597
  else:
598
+ if token and not self._is_valid_github_token(token):
599
+ self.state.log(f"Skipping invalid token format, trying public clone")
600
  self.state.log(f"Cloning {build_config.repo_url} ({build_config.branch})")
601
 
602
  try:
 
605
  self.state.log(f"Cloned to {target_dir}")
606
  return target_dir
607
  except Exception as e:
608
+ error_msg = mask_token(str(e), token) if token else str(e)
609
  self.state.log(f"Clone failed: {error_msg}", level="error")
610
  raise RuntimeError(f"Clone failed: {error_msg}")
611