Skip to content

Commit f0ece63

Browse files
feat: Improve webhook validation checks (box/box-codegen#745) (#628)
1 parent a4ee51a commit f0ece63

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "fe0e7f5", "specHash": "83a5340", "version": "1.15.0" }
1+
{ "engineHash": "c7328b5", "specHash": "83a5340", "version": "1.15.0" }

box_sdk_gen/managers/webhooks.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,28 +450,38 @@ def validate_message(
450450
or date_time_to_epoch_seconds(delivery_timestamp) > current_epoch
451451
):
452452
return False
453-
if primary_key and compare_signatures(
453+
if (
454+
not primary_key == None and not headers.get('box-signature-primary') == None
455+
) and compare_signatures(
454456
expected_signature=compute_webhook_signature(
455457
body, headers, primary_key, escape_body=False
456458
),
457459
received_signature=headers.get('box-signature-primary'),
458460
):
459461
return True
460-
if primary_key and compare_signatures(
462+
if (
463+
not primary_key == None and not headers.get('box-signature-primary') == None
464+
) and compare_signatures(
461465
expected_signature=compute_webhook_signature(
462466
body, headers, primary_key, escape_body=True
463467
),
464468
received_signature=headers.get('box-signature-primary'),
465469
):
466470
return True
467-
if secondary_key and compare_signatures(
471+
if (
472+
not secondary_key == None
473+
and not headers.get('box-signature-secondary') == None
474+
) and compare_signatures(
468475
expected_signature=compute_webhook_signature(
469476
body, headers, secondary_key, escape_body=False
470477
),
471478
received_signature=headers.get('box-signature-secondary'),
472479
):
473480
return True
474-
if secondary_key and compare_signatures(
481+
if (
482+
not secondary_key == None
483+
and not headers.get('box-signature-secondary') == None
484+
) and compare_signatures(
475485
expected_signature=compute_webhook_signature(
476486
body, headers, secondary_key, escape_body=True
477487
),

test/webhooks.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,23 @@ def testWebhookValidation():
9999
'box-signature-secondary': 'v+1CD1Jdo3muIcbpv5lxxgPglOqMfsNHPV899xWYydo=',
100100
'box-signature-version': '1',
101101
}
102-
headers_with_japanese: Dict = {
102+
headers_with_japanese: Dict[str, str] = {
103103
**headers,
104104
'box-signature-primary': 'LV2uCu+5NJtIHrCXDYgZ0v/PP5THGRuegw3RtdnEyuE=',
105105
}
106-
headers_with_emoji: Dict = {
106+
headers_with_emoji: Dict[str, str] = {
107107
**headers,
108108
'box-signature-primary': 'xF/SDZosX4le+v4A0Qn59sZhuD1RqY5KRUKzVMSbh0E=',
109109
}
110-
headers_with_carriage_return: Dict = {
110+
headers_with_carriage_return: Dict[str, str] = {
111111
**headers,
112112
'box-signature-primary': 'SVkbKgy3dEEf2PbbzpNu2lDZS7zZ/aboU7HOZgBGrJk=',
113113
}
114-
headers_with_forward_slash: Dict = {
114+
headers_with_forward_slash: Dict[str, str] = {
115115
**headers,
116116
'box-signature-primary': 't41PWT5ZB6OcysnD6SDy9Ud+p9hdXxIdXqcdweyZv/Q=',
117117
}
118-
headers_with_back_slash: Dict = {
118+
headers_with_back_slash: Dict[str, str] = {
119119
**headers,
120120
'box-signature-primary': 'ERpMZwUQsGDTfj82ehdX6VvDZfvOhK5ULNfVmwVAGe0=',
121121
}
@@ -128,7 +128,7 @@ def testWebhookValidation():
128128
past_datetime: str = date_time_to_string(
129129
epoch_seconds_to_date_time(get_epoch_time_in_seconds() - 1200)
130130
)
131-
headers_with_correct_datetime: Dict = {
131+
headers_with_correct_datetime: Dict[str, str] = {
132132
**headers,
133133
'box-delivery-timestamp': current_datetime,
134134
'box-signature-primary': compute_webhook_signature(
@@ -144,7 +144,7 @@ def testWebhookValidation():
144144
escape_body=True,
145145
),
146146
}
147-
headers_with_japanese_with_correct_datetime: Dict = {
147+
headers_with_japanese_with_correct_datetime: Dict[str, str] = {
148148
**headers_with_japanese,
149149
'box-delivery-timestamp': current_datetime,
150150
'box-signature-primary': compute_webhook_signature(
@@ -160,7 +160,7 @@ def testWebhookValidation():
160160
escape_body=True,
161161
),
162162
}
163-
headers_with_future_datetime: Dict = {
163+
headers_with_future_datetime: Dict[str, str] = {
164164
**headers,
165165
'box-delivery-timestamp': future_datetime,
166166
'box-signature-primary': compute_webhook_signature(
@@ -176,7 +176,7 @@ def testWebhookValidation():
176176
escape_body=True,
177177
),
178178
}
179-
headers_with_past_datetime: Dict = {
179+
headers_with_past_datetime: Dict[str, str] = {
180180
**headers,
181181
'box-delivery-timestamp': past_datetime,
182182
'box-signature-primary': compute_webhook_signature(
@@ -192,11 +192,11 @@ def testWebhookValidation():
192192
escape_body=True,
193193
),
194194
}
195-
headers_with_wrong_signature_version: Dict = {
195+
headers_with_wrong_signature_version: Dict[str, str] = {
196196
**headers,
197197
'box-signature-version': '2',
198198
}
199-
headers_with_wrong_signature_algorithm: Dict = {
199+
headers_with_wrong_signature_algorithm: Dict[str, str] = {
200200
**headers,
201201
'box-signature-algorithm': 'HmacSHA1',
202202
}

0 commit comments

Comments
 (0)