| 209 | ) |
| 210 | |
| 211 | func attachImportAuthHeader(req *http.Request, rawURL string) { |
| 212 | parsed, err := url.Parse(rawURL) |
| 213 | if err != nil || parsed.Host == "" { |
| 214 | return |
| 215 | } |
| 216 | |
| 217 | // Never send credentials over plaintext HTTP — HTTPS is required. |
| 218 | if !strings.EqualFold(parsed.Scheme, "https") { |
| 219 | importURLFetcherLog.Printf("Skipping auth header for non-HTTPS URL: scheme=%s", parsed.Scheme) |
| 220 | return |
| 221 | } |
| 222 | |
| 223 | host := strings.ToLower(parsed.Hostname()) |
| 224 | |
| 225 | // Authoritative GitHub hosts to which the token may be sent. |
| 226 | if _, ok := defaultImportAuthHosts[host]; !ok && host != importAuthGHHost() { |
| 227 | importURLFetcherLog.Printf("Skipping auth header for non-GitHub host: %s", host) |
| 228 | return |
| 229 | } |
| 230 | |
| 231 | token, err := parser.GetGitHubToken() |
| 232 | if err != nil { |
| 233 | importURLFetcherLog.Printf("No GitHub token available: %v", err) |
| 234 | return |
| 235 | } |
| 236 | |
| 237 | importURLFetcherLog.Printf("Attaching auth header for host: %s", host) |
| 238 | req.Header.Set("Authorization", "Bearer "+token) |
| 239 | if isCopilotAutomationImportURL(parsed) { |
| 240 | req.Header.Set("Copilot-Integration-Id", copilotIntegrationHeaderValue) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // isCopilotAutomationImportURL reports whether u targets a Copilot automation API route |
| 245 | // with exactly six path segments: /agents/repos/{owner}/{repo}/automations/{id}. |