Skip to content

Commit e07f04b

Browse files
potatoknagy
andcommitted
Remove authentication headers from webhook payloads
Co-authored-by: Krisztian Nagy <krisztian.nagy@emarsys.com>
1 parent 8f507d6 commit e07f04b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

st2api/st2api/controllers/v1/webhooks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
urljoin = urlparse.urljoin
1919

2020
from st2common import log as logging
21+
from st2common.constants.auth import HEADER_API_KEY_ATTRIBUTE_NAME, HEADER_ATTRIBUTE_NAME
2122
from st2common.constants.triggers import WEBHOOK_TRIGGER_TYPES
2223
from st2common.models.api.trace import TraceContext
2324
from st2common.models.api.trigger import TriggerAPI
@@ -125,6 +126,7 @@ def post(self, hook, webhook_body_api, headers, requester_user):
125126
permission_type=permission_type)
126127

127128
headers = self._get_headers_as_dict(headers)
129+
headers = self._filter_authentication_headers(headers)
128130

129131
# If webhook contains a trace-tag use that else create create a unique trace-tag.
130132
trace_context = self._create_trace_context(trace_tag=headers.pop(TRACE_TAG_HEADER, None),
@@ -218,6 +220,10 @@ def _get_headers_as_dict(self, headers):
218220
headers_dict[key] = value
219221
return headers_dict
220222

223+
def _filter_authentication_headers(self, headers):
224+
auth_headers = [HEADER_API_KEY_ATTRIBUTE_NAME, HEADER_ATTRIBUTE_NAME, 'Cookie']
225+
return {key: value for key, value in headers.items() if key not in auth_headers}
226+
221227
def _log_request(self, msg, headers, body, log_method=LOG.debug):
222228
headers = self._get_headers_as_dict(headers)
223229
body = str(body)

st2api/tests/unit/controllers/v1/test_webhooks.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,26 @@ def get_webhook_trigger(name, url):
319319
self.assertTrue(controller._is_valid_hook('with_leading_trailing_slash'))
320320
self.assertTrue(controller._is_valid_hook('with/mixed/slash'))
321321

322+
@mock.patch.object(TriggerInstancePublisher, 'publish_trigger', mock.MagicMock(
323+
return_value=True))
324+
@mock.patch.object(WebhooksController, '_is_valid_hook', mock.MagicMock(
325+
return_value=True))
326+
@mock.patch.object(HooksHolder, 'get_triggers_for_hook', mock.MagicMock(
327+
return_value=[DUMMY_TRIGGER_DICT]))
328+
@mock.patch('st2common.transport.reactor.TriggerDispatcher.dispatch')
329+
def test_authentication_headers_should_be_removed(self, dispatch_mock):
330+
headers = {
331+
'Content-Type': 'application/x-www-form-urlencoded',
332+
'St2-Api-_Key': 'foobar',
333+
'X-Auth-Token': 'deadbeaf',
334+
'Cookie': 'foo=bar'
335+
}
336+
337+
self.app.post('/v1/webhooks/git', WEBHOOK_1, headers=headers)
338+
self.assertNotIn('St2-Api-Key', dispatch_mock.call_args[1]['payload']['headers'])
339+
self.assertNotIn('X-Auth-Token', dispatch_mock.call_args[1]['payload']['headers'])
340+
self.assertNotIn('Cookie', dispatch_mock.call_args[1]['payload']['headers'])
341+
322342
def __do_post(self, hook, webhook, expect_errors=False, headers=None):
323343
return self.app.post_json('/v1/webhooks/' + hook,
324344
params=webhook,

0 commit comments

Comments
 (0)