Skip to content

Commit 850cf99

Browse files
timothyaaronnikhilym
authored andcommitted
Fix TimestampVerifier delta check (#118)
1 parent f33dec6 commit 850cf99

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

ask-sdk-webservice-support/ask_sdk_webservice_support/verifier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,6 @@ def verify(
432432
"""
433433
local_now = datetime.now(tz.tzutc())
434434
request_timestamp = deserialized_request_env.request.timestamp
435-
if (abs((local_now - request_timestamp).seconds) >
435+
if (abs((local_now - request_timestamp).total_seconds()) >
436436
(self._tolerance_in_millis / 1000)):
437437
raise VerificationException("Timestamp verification failed")

ask-sdk-webservice-support/tests/unit/test_verifier.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,23 @@ def test_timestamp_verification_with_expired_timestamp(self):
396396

397397
self.assertIn("Timestamp verification failed", str(exc.exception))
398398

399+
def test_timestamp_verification_with_valid_future_server_timestamp(self):
400+
valid_tolerance = int(DEFAULT_TIMESTAMP_TOLERANCE_IN_MILLIS / 2 / 1000)
401+
valid_future_datetime = datetime.now(tzutc()) + timedelta(seconds=valid_tolerance)
402+
test_request_envelope = RequestEnvelope(
403+
request=IntentRequest(
404+
timestamp=valid_future_datetime))
405+
self.timestamp_verifier = TimestampVerifier()
406+
try:
407+
self.timestamp_verifier.verify(
408+
headers={},
409+
serialized_request_env="",
410+
deserialized_request_env=test_request_envelope)
411+
except:
412+
# Should never reach here
413+
raise self.fail(
414+
"Timestamp verification failed for a valid input request")
415+
399416
def test_timestamp_verification_with_valid_timestamp(self):
400417
test_request_envelope = RequestEnvelope(
401418
request=IntentRequest(

0 commit comments

Comments
 (0)