package sources import ( "context" "encoding/json" "fmt" "net/http" "net/url" "strings" "time" "golang.org/x/time/rate" "github.com/salvacybersec/keyhunter/pkg/providers" "github.com/salvacybersec/keyhunter/pkg/recon" ) // GitHubActionsSource searches GitHub Actions workflow run logs for leaked API // keys. It queries the GitHub REST API for workflow runs matching provider // keywords (via the repository search endpoint) and emits findings for each // matching run. Requires a GitHub token (same as GitHubSource). type GitHubActionsSource struct { Token string BaseURL string Registry *providers.Registry Limiters *recon.LimiterRegistry client *Client } var _ recon.ReconSource = (*GitHubActionsSource)(nil) func (s *GitHubActionsSource) Name() string { return "github_actions" } func (s *GitHubActionsSource) RateLimit() rate.Limit { return rate.Every(2 * time.Second) } func (s *GitHubActionsSource) Burst() int { return 2 } func (s *GitHubActionsSource) RespectsRobots() bool { return false } func (s *GitHubActionsSource) Enabled(_ recon.Config) bool { return s.Token != "" } // ghActionsSearchResponse models the GitHub code search response when looking // for workflow files containing provider keywords. type ghActionsSearchResponse struct { Items []ghActionsItem `json:"items"` } type ghActionsItem struct { HTMLURL string `json:"html_url"` Repository ghActionsRepository `json:"repository"` } type ghActionsRepository struct { FullName string `json:"full_name"` } func (s *GitHubActionsSource) Sweep(ctx context.Context, _ string, out chan<- recon.Finding) error { if s.Token == "" { return nil } base := s.BaseURL if base == "" { base = "https://api.github.com" } if s.client == nil { s.client = NewClient() } queries := BuildQueries(s.Registry, "github_actions") kwIndex := ghActionsKeywordIndex(s.Registry) for _, q := range queries { if err := ctx.Err(); err != nil { return err } if s.Limiters != nil { if err := s.Limiters.Wait(ctx, s.Name(), s.RateLimit(), s.Burst(), false); err != nil { return err } } // Search for workflow YAML files referencing the keyword. endpoint := fmt.Sprintf("%s/search/code?q=%s+path:.github/workflows+extension:yml&per_page=20", base, url.QueryEscape(q)) req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) if err != nil { return fmt.Errorf("github_actions: build request: %w", err) } req.Header.Set("Authorization", "Bearer "+s.Token) req.Header.Set("Accept", "application/vnd.github+json") resp, err := s.client.Do(ctx, req) if err != nil { if strings.Contains(err.Error(), "unauthorized") { return err } continue } var result ghActionsSearchResponse decErr := json.NewDecoder(resp.Body).Decode(&result) _ = resp.Body.Close() if decErr != nil { continue } provName := kwIndex[strings.ToLower(q)] for _, item := range result.Items { f := recon.Finding{ ProviderName: provName, Confidence: "low", Source: item.HTMLURL, SourceType: "recon:github_actions", DetectedAt: time.Now(), } select { case out <- f: case <-ctx.Done(): return ctx.Err() } } } return nil } func ghActionsKeywordIndex(reg *providers.Registry) map[string]string { m := make(map[string]string) if reg == nil { return m } for _, p := range reg.List() { for _, k := range p.Keywords { kl := strings.ToLower(strings.TrimSpace(k)) if kl != "" { if _, exists := m[kl]; !exists { m[kl] = p.Name } } } } return m }