Class: Rules::GithubScriptInjection

Inherits:
Base
  • Object
show all
Includes:
GuardPatterns
Defined in:
lib/rules/github_script_injection.rb

Constant Summary collapse

DANGEROUS_EXPR_PATTERN =
/\$\{\{\s*(#{DANGEROUS_CONTEXTS.map { |c| Regexp.escape(c) }.join('|')})/
INPUT_EXPR_PATTERN =
/\$\{\{\s*((?:inputs|github\.event\.inputs)\.[^\s}]+)/

Constants included from GuardPatterns

Rules::GuardPatterns::DANGEROUS_CONTEXTS, Rules::GuardPatterns::JOB_PROPERTIES, Rules::GuardPatterns::SAFE_TRIGGERS

Instance Method Summary collapse

Methods included from GuardPatterns

#guarded_by_safe_event?, #safe_trigger_only?, #strip_inline_comment

Instance Method Details

#check(workflow) ⇒ Object



14
15
16
17
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
# File 'lib/rules/github_script_injection.rb', line 14

def check(workflow)
    findings = []
    safe_triggers = safe_trigger_only?(workflow)

    workflow.raw_lines.each_with_index do |line, idx|
        line_num = idx + 1
        next if line.strip.start_with?('#')
        next unless in_github_script_block?(workflow, line_num)

        has_dangerous = line.match?(DANGEROUS_EXPR_PATTERN)
        has_input = line.match?(INPUT_EXPR_PATTERN)
        next unless has_dangerous || has_input

        guarded = guarded_by_safe_event?(workflow, line_num)

        # INPUT expressions (inputs.*) are user-controlled even on
        # safe-trigger-only workflows, so they always fire.
        if has_input
            match = line.match(INPUT_EXPR_PATTERN)
            if match
                findings << finding(workflow,
                    line: line_num,
                    code: line.strip,
                    message: "Attacker-controllable expression \${{ #{match[1]} }} in actions/github-script — JavaScript injection risk",
                    fix: "Pass input via env: block and reference as process.env.VAR"
                )
            end
        end

        # DANGEROUS expressions respect both safe_trigger_only? and
        # event guards — they are only exploitable from unsafe triggers.
        if has_dangerous && !safe_triggers && !guarded
            match = line.match(DANGEROUS_EXPR_PATTERN)
            if match
                findings << finding(workflow,
                    line: line_num,
                    code: line.strip,
                    message: "Attacker-controllable expression \${{ #{match[1]} }} in actions/github-script — JavaScript injection risk",
                    fix: "Use context.payload instead: context.payload.pull_request.title"
                )
            end
        end
    end

    findings
end

#descriptionObject



8
# File 'lib/rules/github_script_injection.rb', line 8

def description = "Attacker-controllable ${{ }} expression in actions/github-script"

#nameObject



7
# File 'lib/rules/github_script_injection.rb', line 7

def name = "github-script-injection"

#severityObject



9
# File 'lib/rules/github_script_injection.rb', line 9

def severity = :critical