Class: Rules::ShellInjectionJq

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

Constant Summary collapse

ATTACKER_ENV_VARS =
%w[
    PR_TITLE PR_BODY PR_AUTHOR HEAD_REF ISSUE_TITLE ISSUE_BODY COMMENT_BODY
    PR_HEAD_REF BRANCH_NAME
].freeze
JQ_PATTERN =
/jq\s+([a-zA-Z-]+\s+)*--arg\s+\w+\s+"[^"]*\$\{/
CURL_JSON_PATTERN =
/curl\s.*-d\s+"[^"]*\$\{/

Constants included from GuardPatterns

GuardPatterns::DANGEROUS_CONTEXTS, GuardPatterns::JOB_PROPERTIES, 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



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
# File 'lib/rules/shell_injection_jq.rb', line 19

def check(workflow)
    findings = []

    return [] if safe_trigger_only?(workflow)

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

        if line.match?(JQ_PATTERN)
            var_match = line.match(/\$\{(\w+)\}/)
            next unless var_match
            var_name = var_match[1]
            next unless potentially_attacker_controlled?(var_name)

            findings << finding(workflow,
                line: line_num,
                code: line.strip,
                message: "${#{var_name}} interpolated in double-quoted jq argument — $(command) executes via bash substitution",
                fix: "Use jq --arg: jq -nc --arg #{var_name.downcase} \"$#{var_name}\" '{text: $#{var_name.downcase}}'"
            )
        end

        if line.match?(CURL_JSON_PATTERN)
            var_match = line.match(/\$\{(\w+)\}/)
            next unless var_match
            var_name = var_match[1]
            next unless potentially_attacker_controlled?(var_name)

            findings << finding(workflow,
                line: line_num,
                code: line.strip,
                message: "${#{var_name}} interpolated in double-quoted curl JSON — command substitution risk",
                fix: "Build JSON payload with jq -nc --arg instead of string interpolation"
            )
        end
    end

    findings
end

#descriptionObject



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

def description = "Shell variable interpolated in double-quoted jq/curl JSON argument"

#nameObject



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

def name = "shell-injection-jq"

#severityObject



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

def severity = :critical