@@ -27,6 +27,14 @@ import (
2727
2828// ansiRegex matches ANSI escape codes (colors, formatting)
2929var ansiRegex = regexp .MustCompile (`\x1b\[[0-9;]*m` )
30+ var gitURLCredentialsRegex = regexp .MustCompile (`(https?://)[^@\s/]+@` )
31+
32+ var tokenPatterns = []* regexp.Regexp {
33+ regexp .MustCompile (`ghp_[A-Za-z0-9_]+` ),
34+ regexp .MustCompile (`github_pat_[A-Za-z0-9_]+` ),
35+ regexp .MustCompile (`glpat-[A-Za-z0-9_-]+` ),
36+ regexp .MustCompile (`ATBB[A-Za-z0-9_-]+` ),
37+ }
3038
3139// NetworkName is the shared network where Traefik lives.
3240// New containers join this network so Traefik can route traffic to them.
@@ -85,12 +93,27 @@ func (d *DockerService) CloneRepo(repoURL, branch, token string) (string, error)
8593 output , err := cmd .CombinedOutput ()
8694 if err != nil {
8795 os .RemoveAll (cloneDir ) // Cleanup failed clone attempt
88- return "" , fmt .Errorf ("git clone failed: %s - %w" , string (output ), err )
96+ safeOutput := sanitizeGitOutput (string (output ), token )
97+ if strings .TrimSpace (safeOutput ) == "" {
98+ return "" , fmt .Errorf ("git clone failed: %w" , err )
99+ }
100+ return "" , fmt .Errorf ("git clone failed: %s - %w" , safeOutput , err )
89101 }
90102
91103 return cloneDir , nil
92104}
93105
106+ func sanitizeGitOutput (output , token string ) string {
107+ sanitized := gitURLCredentialsRegex .ReplaceAllString (output , "${1}***@" )
108+ if token != "" {
109+ sanitized = strings .ReplaceAll (sanitized , token , "***" )
110+ }
111+ for _ , pattern := range tokenPatterns {
112+ sanitized = pattern .ReplaceAllString (sanitized , "***" )
113+ }
114+ return sanitized
115+ }
116+
94117// BuildImage builds a Docker image from a directory with a Dockerfile.
95118// logCallback is called for each line of build output (can be nil).
96119func (d * DockerService ) BuildImage (ctx context.Context , buildPath , dockerfilePath , imageName string , logCallback func (string )) (string , error ) {
0 commit comments