44 "cmp"
55 "fmt"
66 "io"
7+ "log/slog"
78 "net/http"
89 "net/url"
910 "os"
@@ -104,12 +105,20 @@ func (p *githubPlugin) FileContent(subpath string) ([]byte, error) {
104105 }
105106 defer res .Body .Close ()
106107 if res .StatusCode != http .StatusOK {
108+ authInfo := "No auth header was send with this request."
109+ if req .Header .Get ("Authorization" ) != "" {
110+ authInfo = fmt .Sprintf (
111+ "The auth header `%s` was send with this request." ,
112+ getRedactedAuthHeader (req ),
113+ )
114+ }
107115 return nil , 0 , usererr .New (
108- "failed to get plugin %s @ %s (Status code %d). \n Please make " +
116+ "failed to get plugin %s @ %s (Status code %d).\n %s \n Please make " +
109117 "sure a plugin.json file exists in plugin directory." ,
110118 p .LockfileKey (),
111119 req .URL .String (),
112120 res .StatusCode ,
121+ authInfo ,
113122 )
114123 }
115124 body , err := io .ReadAll (res .Body )
@@ -147,6 +156,11 @@ func (p *githubPlugin) request(contentURL string) (*http.Request, error) {
147156 if ghToken != "" {
148157 authValue := fmt .Sprintf ("token %s" , ghToken )
149158 req .Header .Add ("Authorization" , authValue )
159+ slog .Debug (
160+ "GITHUB_TOKEN env var found, adding to request's auth header" ,
161+ "headerValue" ,
162+ getRedactedAuthHeader (req ),
163+ )
150164 }
151165
152166 return req , nil
@@ -155,3 +169,22 @@ func (p *githubPlugin) request(contentURL string) (*http.Request, error) {
155169func (p * githubPlugin ) LockfileKey () string {
156170 return p .ref .String ()
157171}
172+
173+ func getRedactedAuthHeader (req * http.Request ) string {
174+ authHeader := req .Header .Get ("Authorization" )
175+ parts := strings .SplitN (authHeader , " " , 2 )
176+
177+ if len (authHeader ) < 10 || len (parts ) < 2 {
178+ // too short to safely reveal any part
179+ return strings .Repeat ("*" , len (authHeader ))
180+ }
181+
182+ authType , token := parts [0 ], parts [1 ]
183+ if len (token ) < 10 {
184+ // second word to short to reveal any, but show first word
185+ return authType + " " + strings .Repeat ("*" , len (token ))
186+ }
187+
188+ // show first 4 chars of token to help with debugging (will often be "ghp_")
189+ return authType + " " + token [:4 ] + strings .Repeat ("*" , len (token )- 4 )
190+ }
0 commit comments