Skip to content

Commit e59fed9

Browse files
author
Dan Hertz
committed
fix validation function
1 parent 9f16765 commit e59fed9

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

dev-requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ flake8
1212
autopep8
1313
pytest
1414
pytest-cov
15-
requests
15+
requests
16+
freezegun

nightfall/api.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,16 @@ def validate_webhook(self, request_signature: str, request_timestamp: str, reque
233233
"""
234234

235235
now = datetime.now()
236-
if now-timedelta(minutes=5) <= datetime.fromtimestamp(int(request_timestamp)) <= now:
237-
raise NightfallUserError("could not validate timestamp is within the last few minutes", 40000)
236+
request_datetime = datetime.fromtimestamp(int(request_timestamp))
237+
if request_datetime < now-timedelta(minutes=5) or request_datetime > now:
238+
return False
238239
computed_signature = hmac.new(
239240
self.signing_secret.encode(),
240241
msg=F"{request_timestamp}:{request_data}".encode(),
241242
digestmod=hashlib.sha256
242243
).hexdigest().lower()
243244
if computed_signature != request_signature:
244-
raise NightfallUserError("could not validate signature of inbound request!", 40000)
245+
return False
245246
return True
246247

247248

tests/test_api.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import datetime
12
import os
3+
import time
4+
5+
from freezegun import freeze_time
26
import pytest
37

48
from nightfall.api import Nightfall
@@ -47,6 +51,33 @@ def test_scan_text_detection_rules_v3(nightfall):
4751
assert redactions[0] == "491👀-👀👀👀👀-👀👀👀👀-👀👀👀👀 is my credit card number"
4852

4953

54+
@freeze_time("2021-10-04T17:30:50Z")
55+
def test_validate_webhook(nightfall):
56+
nightfall.signing_secret = "super-secret-shhhh"
57+
timestamp = 1633368645
58+
body = "hello world foo bar goodnight moon"
59+
expected = "1bb7619a9504474ffc14086d0423ad15db42606d3ca52afccb4a5b2125d7b703"
60+
assert nightfall.validate_webhook(expected, timestamp, body)
61+
62+
63+
@freeze_time("2021-10-04T19:30:50Z")
64+
def test_validate_webhook_too_old(nightfall):
65+
nightfall.signing_secret = "super-secret-shhhh"
66+
timestamp = 1633368645
67+
body = "hello world foo bar goodnight moon"
68+
expected = "1bb7619a9504474ffc14086d0423ad15db42606d3ca52afccb4a5b2125d7b703"
69+
assert not nightfall.validate_webhook(expected, timestamp, body)
70+
71+
72+
@freeze_time("2021-10-04T17:30:50Z")
73+
def test_validate_webhook_incorrect_sig(nightfall):
74+
nightfall.signing_secret = "super-secret-shhhh"
75+
timestamp = 1633368645
76+
body = "hello world foo bar goodnight moon"
77+
expected = "not matching"
78+
assert not nightfall.validate_webhook(expected, timestamp, body)
79+
80+
5081
@pytest.mark.filetest
5182
def test_scan_file_detection_rules(nightfall):
5283
file = "file.txt"

0 commit comments

Comments
 (0)