package sources import ( "context" "encoding/json" "fmt" "net/http" "strings" "time" "golang.org/x/time/rate" "github.com/salvacybersec/keyhunter/pkg/providers" "github.com/salvacybersec/keyhunter/pkg/recon" ) // CircleCISource searches public CircleCI build logs for leaked API keys. // It queries the CircleCI v2 API for recent pipeline workflows. Requires a // CircleCI API token for authenticated access. type CircleCISource struct { Token string BaseURL string Registry *providers.Registry Limiters *recon.LimiterRegistry Client *Client } var _ recon.ReconSource = (*CircleCISource)(nil) func (s *CircleCISource) Name() string { return "circleci" } func (s *CircleCISource) RateLimit() rate.Limit { return rate.Every(2 * time.Second) } func (s *CircleCISource) Burst() int { return 2 } func (s *CircleCISource) RespectsRobots() bool { return false } func (s *CircleCISource) Enabled(_ recon.Config) bool { return s.Token != "" } type circlePipelineResponse struct { Items []circlePipeline `json:"items"` } type circlePipeline struct { ID string `json:"id"` VCS circleVCSInfo `json:"vcs"` } type circleVCSInfo struct { ProviderName string `json:"provider_name"` RepoName string `json:"target_repository_url"` } func (s *CircleCISource) Sweep(ctx context.Context, _ string, out chan<- recon.Finding) error { if s.Token == "" { return nil } base := s.BaseURL if base == "" { base = "https://circleci.com/api/v2" } client := s.Client if client == nil { client = NewClient() } queries := BuildQueries(s.Registry, "circleci") kwIndex := circleKeywordIndex(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 } } endpoint := fmt.Sprintf("%s/pipeline?mine=false", base) req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) if err != nil { return fmt.Errorf("circleci: build request: %w", err) } req.Header.Set("Circle-Token", s.Token) req.Header.Set("Accept", "application/json") resp, err := client.Do(ctx, req) if err != nil { if strings.Contains(err.Error(), "unauthorized") { return err } continue } var result circlePipelineResponse decErr := json.NewDecoder(resp.Body).Decode(&result) _ = resp.Body.Close() if decErr != nil { continue } provName := kwIndex[strings.ToLower(q)] for _, p := range result.Items { source := fmt.Sprintf("https://app.circleci.com/pipelines/%s", p.ID) if p.VCS.RepoName != "" { source = p.VCS.RepoName } f := recon.Finding{ ProviderName: provName, Confidence: "low", Source: source, SourceType: "recon:circleci", DetectedAt: time.Now(), } select { case out <- f: case <-ctx.Done(): return ctx.Err() } } } return nil } func circleKeywordIndex(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 }