-
Notifications
You must be signed in to change notification settings - Fork 23
/
test_reports.py
183 lines (153 loc) · 5.86 KB
/
test_reports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import unittest
from unittest.mock import MagicMock
from gatox.enumerate.reports.actions import ActionsReport
from gatox.enumerate.reports.runners import RunnersReport
PWN_DETAILS = {
"workflow_name": "pwn1.yml",
"workflow_url": "https://github.com/testOrg/testRepo/blob/main/.github/workflows/pwn1.yml",
"details": {
"candidates": {
"build": {
"confidence": "MEDIUM",
"gated": False,
"steps": [
{
"ref": "${{ github.event_name == 'pull_request_target' && format('refs/pull/{0}/merge', github.event.number) || '' }}",
"if_check": None,
"step_name": "Checkout",
}
],
"if_check": "",
}
},
"triggers": ["pull_request_target"],
},
"environments": [],
}
PWN_DETAILS_TOCTOU = {
"workflow_name": "pwn2.yml",
"workflow_url": "https://github.com/testOrg/testRepo/blob/main/.github/workflows/pwn2.yml",
"details": {
"candidates": {
"build": {
"confidence": "MEDIUM",
"gated": False,
"steps": [
{
"ref": "github.event.pull_request.head.ref",
"if_check": None,
"step_name": "Checkout",
}
],
"if_check": "",
}
},
"triggers": ["pull_request_target:labeled"],
},
"environments": [],
}
PWN_DETAILS_TOCTOU2 = {
"workflow_name": "pwn3.yml",
"workflow_url": "https://github.com/testOrg/testRepo/blob/main/.github/workflows/pwn3.yml",
"details": {
"candidates": {
"build": {
"confidence": "MEDIUM",
"gated": False,
"steps": [
{
"ref": "format('refs/pull/{0}/merge', github.event.number)",
"if_check": None,
"step_name": "Checkout",
}
],
"if_check": "",
}
},
"triggers": ["pull_request_target"],
},
"environments": ["ci"],
}
PWN_DETAILS_TOCTOU3 = {
"workflow_name": "pwn4.yml",
"workflow_url": "https://github.com/testOrg/testRepo/blob/main/.github/workflows/pwn4.yml",
"details": {
"candidates": {
"build": {
"confidence": "MEDIUM",
"gated": True,
"steps": [
{
"ref": "format('refs/pull/{0}/merge', github.event.number)",
"if_check": None,
"step_name": "Checkout",
}
],
"if_check": "",
}
},
"triggers": ["issue_comment"],
},
"environments": [],
}
INJ_DETAILS = {
"workflow_name": "inj1.yml",
"workflow_url": "https://github.com/testOrg/testRepo/blob/main/.github/workflows/inj1.yml",
"details": {
"build": {
"if_check": None,
"Set comment body": {"variables": ["steps.extract.outputs.filename"]},
},
"triggers": ["pull_request_target"],
},
"environments": [],
}
def test_report_pwn(capfd):
"""Test reporting a pwn request vulnerability."""
repository = MagicMock()
repository.pwn_req_risk = [PWN_DETAILS]
ActionsReport.report_pwn(repository)
out, err = capfd.readouterr()
assert "The workflow runs on a risky trigger and might check out the PR" in out
assert "Checkout Ref: ${{ github.event_name == 'pull_request_target' && format('refs/pull/{0}/merge', github.event.number) || '' }}"
def test_report_pwn_toctou_label(capfd):
"""Test reporting a pwn request vulnerability with TOCTOU."""
repository = MagicMock()
repository.pwn_req_risk = [PWN_DETAILS_TOCTOU]
repository.name = "testOrg/testRepo"
ActionsReport.report_pwn(repository)
out, err = capfd.readouterr()
assert " The workflow contains label-based gating but the workflow uses a" in out
assert "Checkout Ref: github.event.pull_request.head.ref" in out
assert "Trigger(s): pull_request_target:labeled" in out
def test_report_pwn_toctou_env(capfd):
"""Test reporting a pwn request vulnerability with TOCTOU, where
the gate check if a deployment environment.
"""
repository = MagicMock()
repository.pwn_req_risk = [PWN_DETAILS_TOCTOU2]
repository.name = "testOrg/testRepo"
ActionsReport.report_pwn(repository)
out, err = capfd.readouterr()
assert "Issue Type: Pwn Request with Approval TOCTOU" in out
assert "Checkout Ref: format('refs/pull/{0}/merge', github.event.number)" in out
def test_report_pwn_toctou_comment(capfd):
"""Test reporting a pwn request vulnerability with TOCTOU, where
the gate check if a deployment environment.
"""
repository = MagicMock()
repository.pwn_req_risk = [PWN_DETAILS_TOCTOU3]
repository.name = "testOrg/testRepo"
ActionsReport.report_pwn(repository)
out, err = capfd.readouterr()
assert "Issue Type: Pwn Request With Permission TOCTOU" in out
assert "Checkout Ref: format('refs/pull/{0}/merge', github.event.number)" in out
assert "The workflow contains a permission check, but uses a mutable" in out
def test_report_inj(capfd):
"""Test reporting an injection vulnerability."""
repository = MagicMock()
repository.name = "testOrg/testRepo"
repository.injection_risk = [INJ_DETAILS]
ActionsReport.report_injection(repository)
out, err = capfd.readouterr()
assert " The workflow uses variables by context expression within run or" in out