-
Notifications
You must be signed in to change notification settings - Fork 1
fix: harden webhook entrypoint #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| 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): | ||
| headers = headers or {} | ||
| status_headers = [] | ||
| environ = { | ||
| "REQUEST_METHOD": "POST", | ||
| "PATH_INFO": "/webhook", | ||
| "CONTENT_LENGTH": str(len(body)), | ||
| "wsgi.input": io.BytesIO(body), | ||
| } | ||
| 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): | ||
|
BunsDev marked this conversation as resolved.
|
||
| 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_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() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.