Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def async_process(
body = req.raw_body
timestamp = req.headers.get("x-slack-request-timestamp", ["0"])[0]
signature = req.headers.get("x-slack-signature", [""])[0]
if self.verifier.is_valid(body, timestamp, signature):
if self._is_valid_signature(body, timestamp, signature):
return await next()
else:
self._debug_log_error(signature, timestamp, body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ def process(
body = req.raw_body
timestamp = req.headers.get("x-slack-request-timestamp", ["0"])[0]
signature = req.headers.get("x-slack-signature", [""])[0]
if self.verifier.is_valid(body, timestamp, signature):
if self._is_valid_signature(body, timestamp, signature):
return next()
else:
self._debug_log_error(signature, timestamp, body)
return self._build_error_response()

# -----------------------------------------

def _is_valid_signature(self, body: str, timestamp: str, signature: str) -> bool:
verifier = self.verifier
try:
return verifier.is_valid(body, timestamp, signature)
except (TypeError, ValueError):
return False

@staticmethod
def _can_skip(mode: str, body: Dict[str, Any]) -> bool:
return mode == "socket_mode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,13 @@ def test_http_request_with_empty_signing_secret_raises(self):
resp = BoltResponse(status=404)
with pytest.raises(ValueError):
middleware.process(req=req, resp=resp, next=next)

def test_malformed_headers(self):
middleware = RequestVerification(signing_secret=self.signing_secret)
for headers in [
{"x-slack-request-timestamp": ["abc"], "x-slack-signature": ["v0=invalid"]},
{"x-slack-request-timestamp": [str(int(time()))], "x-slack-signature": ["v0=ñ"]},
]:
req = BoltRequest(body="payload={}", headers=headers)
resp = middleware.process(req=req, resp=BoltResponse(status=404), next=next)
assert resp.status == 401
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ async def test_http_request_with_empty_signing_secret_raises(self):
resp = BoltResponse(status=404)
with pytest.raises(ValueError):
await middleware.async_process(req=req, resp=resp, next=next)

@pytest.mark.asyncio
async def test_malformed_headers(self):
middleware = AsyncRequestVerification(signing_secret=self.signing_secret)
for headers in [
{"x-slack-request-timestamp": ["abc"], "x-slack-signature": ["v0=invalid"]},
{"x-slack-request-timestamp": [str(int(time()))], "x-slack-signature": ["v0=ñ"]},
]:
req = AsyncBoltRequest(body="payload={}", headers=headers)
resp = await middleware.async_process(req=req, resp=BoltResponse(status=404), next=next)
assert resp.status == 401