|
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import json |
| 10 | +import time |
10 | 11 | from unittest.mock import AsyncMock, MagicMock, patch |
11 | 12 |
|
12 | 13 | import pytest |
@@ -92,18 +93,72 @@ async def test_template_rendering(self): |
92 | 93 |
|
93 | 94 | @pytest.mark.asyncio |
94 | 95 | async def test_hmac_signature_creation(self): |
95 | | - """Test HMAC signature creation.""" |
| 96 | + """Test HMAC signature creation includes timestamp in signed material.""" |
96 | 97 | plugin = _create_plugin() |
97 | 98 |
|
98 | 99 | payload = '{"test": "data"}' |
99 | 100 | secret = "test-secret" # pragma: allowlist secret |
100 | 101 | algorithm = "sha256" |
| 102 | + timestamp = "1700000000" |
101 | 103 |
|
102 | | - signature = plugin._create_hmac_signature(payload, secret, algorithm) |
| 104 | + signature = plugin._create_hmac_signature(payload, secret, algorithm, timestamp) |
103 | 105 |
|
104 | 106 | assert signature.startswith("sha256=") |
105 | 107 | assert len(signature) > 10 # Basic length check |
106 | 108 |
|
| 109 | + # A different timestamp must produce a different signature (timestamp is in the signed material). |
| 110 | + signature_other_ts = plugin._create_hmac_signature(payload, secret, algorithm, "1700000001") |
| 111 | + assert signature != signature_other_ts |
| 112 | + |
| 113 | + @pytest.mark.asyncio |
| 114 | + @patch("plugins.webhook_notification.webhook_notification.httpx.AsyncClient") |
| 115 | + async def test_webhook_delivery_with_hmac_auth_sends_timestamp_header(self, mock_client_class): |
| 116 | + """Test HMAC auth sends both signature and timestamp headers.""" |
| 117 | + mock_client = AsyncMock() |
| 118 | + mock_response = MagicMock() |
| 119 | + mock_response.status_code = 200 |
| 120 | + mock_client.post.return_value = mock_response |
| 121 | + mock_client_class.return_value = mock_client |
| 122 | + |
| 123 | + config = { |
| 124 | + "webhooks": [ |
| 125 | + { |
| 126 | + "url": "https://hooks.example.com/webhook", |
| 127 | + "events": ["tool_success"], |
| 128 | + "authentication": { |
| 129 | + "type": "hmac", |
| 130 | + "hmac_secret": "test-secret", # pragma: allowlist secret |
| 131 | + "hmac_algorithm": "sha256", |
| 132 | + "hmac_header": "X-Signature", |
| 133 | + "hmac_timestamp_header": "X-Timestamp", |
| 134 | + }, |
| 135 | + "retry_attempts": 0, |
| 136 | + "enabled": True, |
| 137 | + } |
| 138 | + ] |
| 139 | + } |
| 140 | + |
| 141 | + plugin = _create_plugin(config) |
| 142 | + plugin._client = mock_client |
| 143 | + |
| 144 | + before = int(time.time()) |
| 145 | + context = _create_context() |
| 146 | + webhook = plugin._cfg.webhooks[0] |
| 147 | + |
| 148 | + await plugin._send_webhook(webhook=webhook, event=EventType.TOOL_SUCCESS, context=context) |
| 149 | + |
| 150 | + after = int(time.time()) |
| 151 | + |
| 152 | + call_args = mock_client.post.call_args |
| 153 | + headers = call_args[1]["headers"] |
| 154 | + |
| 155 | + assert "X-Signature" in headers |
| 156 | + assert headers["X-Signature"].startswith("sha256=") |
| 157 | + |
| 158 | + assert "X-Timestamp" in headers |
| 159 | + ts = int(headers["X-Timestamp"]) |
| 160 | + assert before <= ts <= after |
| 161 | + |
107 | 162 | @pytest.mark.asyncio |
108 | 163 | @patch("plugins.webhook_notification.webhook_notification.httpx.AsyncClient") |
109 | 164 | async def test_webhook_delivery_success(self, mock_client_class): |
|
0 commit comments