Files
keyhunter/pkg/recon/sources/githubactions.go
2026-04-06 13:39:32 +03:00

261 lines
6.8 KiB
Go

package sources
import (
"context"
"encoding/json"
"fmt"
<<<<<<< HEAD
"net/http"
"net/url"
"strings"
=======
"io"
"net/http"
>>>>>>> worktree-agent-adad8c10
"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
<<<<<<< HEAD
// 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).
=======
// keys. Workflow logs are public for public repositories and frequently contain
// accidentally printed secrets, debug output with credentials, or insecure
// echo statements that expose environment variables.
>>>>>>> worktree-agent-adad8c10
type GitHubActionsSource struct {
Token string
BaseURL string
Registry *providers.Registry
Limiters *recon.LimiterRegistry
<<<<<<< HEAD
client *Client
=======
Client *Client
>>>>>>> worktree-agent-adad8c10
}
var _ recon.ReconSource = (*GitHubActionsSource)(nil)
<<<<<<< HEAD
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
}
=======
func (s *GitHubActionsSource) Name() string { return "ghactions" }
func (s *GitHubActionsSource) RateLimit() rate.Limit { return rate.Every(2 * time.Second) }
func (s *GitHubActionsSource) Burst() int { return 3 }
func (s *GitHubActionsSource) RespectsRobots() bool { return false }
// Enabled requires a GitHub token (reuses GitHubToken from SourcesConfig).
func (s *GitHubActionsSource) Enabled(_ recon.Config) bool { return s.Token != "" }
// ghActionsRunsResponse represents the GitHub Actions workflow runs list.
type ghActionsRunsResponse struct {
WorkflowRuns []ghActionsRun `json:"workflow_runs"`
}
type ghActionsRun struct {
ID int64 `json:"id"`
LogsURL string `json:"logs_url"`
HTMLURL string `json:"html_url"`
Status string `json:"status"`
Conclusion string `json:"conclusion"`
}
func (s *GitHubActionsSource) Sweep(ctx context.Context, _ string, out chan<- recon.Finding) error {
>>>>>>> worktree-agent-adad8c10
base := s.BaseURL
if base == "" {
base = "https://api.github.com"
}
<<<<<<< HEAD
if s.client == nil {
s.client = NewClient()
}
queries := BuildQueries(s.Registry, "github_actions")
kwIndex := ghActionsKeywordIndex(s.Registry)
=======
client := s.Client
if client == nil {
client = NewClient()
}
queries := BuildQueries(s.Registry, "ghactions")
if len(queries) == 0 {
return nil
}
>>>>>>> worktree-agent-adad8c10
for _, q := range queries {
if err := ctx.Err(); err != nil {
return err
}
<<<<<<< HEAD
=======
>>>>>>> worktree-agent-adad8c10
if s.Limiters != nil {
if err := s.Limiters.Wait(ctx, s.Name(), s.RateLimit(), s.Burst(), false); err != nil {
return err
}
}
<<<<<<< HEAD
// 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()
=======
// Search for workflow runs via the Actions API.
searchURL := fmt.Sprintf("%s/search/code?q=%s+path:.github/workflows", base, q)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, searchURL, nil)
if err != nil {
continue
}
req.Header.Set("Authorization", "Bearer "+s.Token)
req.Header.Set("Accept", "application/vnd.github.v3+json")
resp, err := client.Do(ctx, req)
if err != nil {
continue
}
var runs ghActionsRunsResponse
if err := json.NewDecoder(resp.Body).Decode(&runs); err != nil {
_ = resp.Body.Close()
continue
}
_ = resp.Body.Close()
for _, run := range runs.WorkflowRuns {
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
}
}
// Fetch log content.
logURL := fmt.Sprintf("%s/actions/runs/%d/logs", base, run.ID)
logReq, err := http.NewRequestWithContext(ctx, http.MethodGet, logURL, nil)
if err != nil {
continue
}
logReq.Header.Set("Authorization", "Bearer "+s.Token)
logReq.Header.Set("Accept", "application/vnd.github.v3+json")
logResp, err := client.Do(ctx, logReq)
if err != nil {
continue
}
body, err := io.ReadAll(io.LimitReader(logResp.Body, 256*1024))
_ = logResp.Body.Close()
if err != nil {
continue
}
if ciLogKeyPattern.Match(body) {
out <- recon.Finding{
ProviderName: q,
Source: logURL,
SourceType: "recon:ghactions",
Confidence: "medium",
DetectedAt: time.Now(),
}
>>>>>>> worktree-agent-adad8c10
}
}
}
return nil
}
<<<<<<< HEAD
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
}
=======
>>>>>>> worktree-agent-adad8c10