-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_webhook_adapter.py
More file actions
285 lines (235 loc) · 10.3 KB
/
Copy pathtest_webhook_adapter.py
File metadata and controls
285 lines (235 loc) · 10.3 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import hashlib
import hmac
import importlib
import io
import json
import os
import sys
import tempfile
import unittest
def import_adapter(state_dir, webhook_secret="test-webhook-secret"):
os.environ["COVEN_GITHUB_STATE_DIR"] = str(state_dir)
os.environ["COVEN_GITHUB_POLICY_PATH"] = str(state_dir / "policy.json")
os.environ.pop("WEBHOOK_SECRET", None)
os.environ["GITHUB_WEBHOOK_SECRET"] = webhook_secret
sys.modules.pop("coven_github_adapter", None)
return importlib.import_module("coven_github_adapter")
def import_adapter_with_legacy_secret(state_dir, webhook_secret="legacy-webhook-secret"):
os.environ["COVEN_GITHUB_STATE_DIR"] = str(state_dir)
os.environ["COVEN_GITHUB_POLICY_PATH"] = str(state_dir / "policy.json")
os.environ.pop("GITHUB_WEBHOOK_SECRET", None)
os.environ["WEBHOOK_SECRET"] = webhook_secret
sys.modules.pop("coven_github_adapter", None)
return importlib.import_module("coven_github_adapter")
def signature(secret, body):
digest = hmac.new(secret.encode("utf-8"), body, hashlib.sha256).hexdigest()
return "sha256=" + digest
class WebhookAdapterTests(unittest.TestCase):
def call_app(self, adapter, body, headers=None, content_length="auto"):
headers = headers or {}
status_headers = []
environ = {
"REQUEST_METHOD": "POST",
"PATH_INFO": "/webhook",
"wsgi.input": io.BytesIO(body),
}
if content_length == "auto":
environ["CONTENT_LENGTH"] = str(len(body))
elif content_length is not None:
environ["CONTENT_LENGTH"] = str(content_length)
for name, value in headers.items():
environ["HTTP_" + name.upper().replace("-", "_")] = value
def start_response(status, response_headers):
status_headers.append((status, response_headers))
response = b"".join(adapter.application(environ, start_response))
status = status_headers[0][0]
return status, json.loads(response.decode("utf-8"))
def test_webhook_rejects_missing_and_invalid_signatures(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
adapter = import_adapter(Path(tmp))
body = b'{"zen":"Keep it logically awesome."}'
missing_status, missing_payload = self.call_app(
adapter,
body,
{"X-GitHub-Event": "ping", "X-GitHub-Delivery": "delivery-1"},
)
self.assertEqual(missing_status, "401 Unauthorized")
self.assertEqual(missing_payload["error"], "missing signature")
invalid_status, invalid_payload = self.call_app(
adapter,
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-2",
"X-Hub-Signature-256": "sha256=deadbeef",
},
)
self.assertEqual(invalid_status, "401 Unauthorized")
self.assertEqual(invalid_payload["error"], "invalid signature")
def test_webhook_accepts_valid_signed_ping_without_runtime(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
secret = "valid-webhook-secret"
adapter = import_adapter(Path(tmp), secret)
body = b'{"zen":"Keep it logically awesome."}'
status, payload = self.call_app(
adapter,
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-3",
"X-Hub-Signature-256": signature(secret, body),
},
)
self.assertEqual(status, "200 OK")
self.assertTrue(payload["ok"])
self.assertEqual(payload["action"], "ignored")
self.assertEqual(payload["reason"], "no_policy_for_installation_repo")
def test_webhook_reads_body_when_content_length_is_missing(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
secret = "missing-length-secret"
adapter = import_adapter(Path(tmp), secret)
body = b'{"zen":"Keep it logically awesome."}'
status, payload = self.call_app(
adapter,
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-missing-length",
"X-Hub-Signature-256": signature(secret, body),
},
content_length=None,
)
self.assertEqual(status, "200 OK")
self.assertTrue(payload["ok"])
def test_webhook_reads_body_when_content_length_is_unparsable(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
secret = "bad-length-secret"
adapter = import_adapter(Path(tmp), secret)
body = b'{"zen":"Keep it logically awesome."}'
status, payload = self.call_app(
adapter,
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-bad-length",
"X-Hub-Signature-256": signature(secret, body),
},
content_length="not-a-number",
)
self.assertEqual(status, "200 OK")
self.assertTrue(payload["ok"])
def test_webhook_treats_zero_content_length_as_empty_body(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
secret = "zero-length-secret"
adapter = import_adapter(Path(tmp), secret)
status, payload = self.call_app(
adapter,
b'{"zen":"Keep it logically awesome."}',
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-zero-length",
"X-Hub-Signature-256": signature(secret, b""),
},
content_length=0,
)
self.assertEqual(status, "400 Bad Request")
self.assertEqual(payload["error"], "invalid json")
def test_webhook_rejects_oversized_content_length_before_signature_check(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
adapter = import_adapter(Path(tmp))
status, payload = self.call_app(
adapter,
b"",
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-large-body",
"X-Hub-Signature-256": "sha256=deadbeef",
},
content_length=10 * 1024 * 1024 + 1,
)
self.assertEqual(status, "413 Payload Too Large")
self.assertEqual(payload["error"], "payload too large")
def test_webhook_signature_allows_surrounding_whitespace(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
secret = "whitespace-secret"
adapter = import_adapter(Path(tmp), secret)
body = b'{"zen":"Keep it logically awesome."}'
status, payload = self.call_app(
adapter,
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-whitespace-signature",
"X-Hub-Signature-256": " " + signature(secret, body) + " ",
},
)
self.assertEqual(status, "200 OK")
self.assertTrue(payload["ok"])
def test_webhook_reports_missing_secret_as_server_misconfiguration(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
adapter = import_adapter(Path(tmp), "")
body = b'{"zen":"Keep it logically awesome."}'
status, payload = self.call_app(
adapter,
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-missing-secret",
"X-Hub-Signature-256": signature("ignored", body),
},
)
self.assertEqual(status, "500 Internal Server Error")
self.assertEqual(payload["error"], "webhook secret not configured")
def test_webhook_secret_supports_smoke_script_environment_name(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
secret = "legacy-webhook-secret"
adapter = import_adapter_with_legacy_secret(Path(tmp), secret)
body = b'{"zen":"Keep it logically awesome."}'
status, payload = self.call_app(
adapter,
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-legacy",
"X-Hub-Signature-256": signature(secret, body),
},
)
self.assertEqual(status, "200 OK")
self.assertTrue(payload["ok"])
def test_missing_familiar_policy_does_not_fall_back_to_hardcoded_installation(self):
with tempfile.TemporaryDirectory() as tmp:
from pathlib import Path
adapter = import_adapter(Path(tmp))
task = adapter.build_task_from_event(
"issues",
"delivery-4",
{
"action": "opened",
"installation": {"id": 111},
"repository": {
"id": 222,
"full_name": "OpenCoven/example",
"clone_url": "https://github.com/OpenCoven/example.git",
"default_branch": "main",
},
"issue": {"number": 7, "title": "Fix it", "body": "Please fix it."},
},
{
"trigger_labels": ["coven:fix"],
"bot_usernames": ["coven-github[bot]"],
"publication": {"mode": "record_only"},
},
)
self.assertEqual(task["state"], "ignored")
self.assertEqual(task["ignored_reason"], "missing_familiar_policy")
if __name__ == "__main__":
unittest.main()