Class: Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/scanner.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:, formatter:, min_severity: :low) ⇒ Scanner

Returns a new instance of Scanner.



11
12
13
14
15
16
# File 'lib/scanner.rb', line 11

def initialize(client:, formatter:, min_severity: :low)
    @client = client
    @formatter = formatter
    @min_severity = min_severity
    @engine = RuleEngine.new
end

Instance Method Details

#scan(repo) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/scanner.rb', line 18

def scan(repo)
    raw_workflows = @client.fetch_workflows(repo)

    workflows = raw_workflows.map { |w|
        Workflow.new(filename: w[:filename], content: w[:content])
    }

    dependabot = @client.fetch_dependabot_config(repo)
    has_zizmor = workflows.any? { |w| w.filename.match?(/zizmor/i) }
    has_dependabot_actions = dependabot_has_actions?(dependabot)

    findings = []

    workflows.each do |wf|
        next if wf.parse_error?
        findings.concat(@engine.scan(wf))
    end

    unless has_dependabot_actions
        findings << Finding.new(
            rule: "missing-dependabot",
            severity: :low,
            file: "dependabot.yml",
            line: 0,
            code: nil,
            message: "No Dependabot configuration for github-actions ecosystem",
            fix: "Add package-ecosystem: github-actions to .github/dependabot.yml"
        )
    end

    unless has_zizmor
        findings << Finding.new(
            rule: "missing-zizmor",
            severity: :low,
            file: "(missing)",
            line: 0,
            code: nil,
            message: "No zizmor static analysis workflow found",
            fix: "Add a security_zizmor.yml workflow for GitHub Actions static analysis"
        )
    end

    findings.select! { |f| severity_passes?(f.severity) }

    output = @formatter.format(
        repo: repo,
        workflow_count: workflows.length,
        findings: findings
    )

    { output: output, findings: findings, workflow_count: workflows.length }
end

#scan_org(org) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/scanner.rb', line 71

def scan_org(org)
    repos = @client.fetch_repos(org)
    results = []

    repos.each do |repo|
        $stderr.puts "Scanning #{repo}..." if @formatter.is_a?(Formatter::Terminal)
        results << scan(repo)
    end

    results
end