Repository navigation
Expand file tree
/
Copy pathtest_webhooks.py
More file actions
1196 lines (1014 loc) · 46.6 KB
/
Copy pathtest_webhooks.py
File metadata and controls
1196 lines (1014 loc) · 46.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
4
8000
38
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Webhook backend tests (#275).
Covers:
- model serialization + event_list validation
- HMAC signature generation
- SSRF guard (private IP block + intranet allowlist)
- emit_webhook_event creates delivery rows for matching subscriptions
- dispatcher pass: success path, retry-with-backoff path, permanent
failure path, auto-pause after N consecutive failures
- v1 CRUD endpoints: create/list/get/patch/delete/test/rotate-secret
- replay creates a new delivery with a fresh event_id
"""
import hashlib
import hmac
import json
import os
import sys
import uuid
from datetime import datetime, timedelta
from unittest.mock import patch, MagicMock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.app import app, db
from src.models import User, Webhook, WebhookDelivery, WEBHOOK_EVENT_TYPES, generate_webhook_secret
from src.services.webhook_dispatch import (
sign_payload,
is_url_safe_for_webhook,
emit_webhook_event,
run_dispatcher_pass,
_delay_for_attempt,
_is_retryable_status,
)
app.config["WTF_CSRF_ENABLED"] = False
def _make_user(prefix):
suffix = uuid.uuid4().hex[:8]
user = User(
username=f"{prefix}_{suffix}",
email=f"{prefix}_{suffix}@local.test",
password="x",
)
db.session.add(user)
db.session.commit()
return user
def _login(client, user):
from flask import g
with client.session_transaction() as sess:
sess.clear()
sess["_user_id"] = str(user.id)
sess["_fresh"] = True
try:
g.pop("_login_user", None)
except RuntimeError:
pass
# --- Model tests -----------------------------------------------------------
def test_event_list_setter_dedupes_and_validates():
with app.app_context():
user = _make_user("wh_model")
wh = Webhook(user_id=user.id, name="t", url="https://example.com/x")
wh.event_list = ['recording.created', 'recording.created', 'recording.deleted']
assert wh.event_list == ['recording.created', 'recording.deleted']
try:
wh.event_list = ['not.a.real.event']
assert False, "expected ValueError"
except ValueError:
pass
db.session.delete(user)
db.session.commit()
def test_generate_webhook_secret_returns_distinct_values():
a = generate_webhook_secret()
b = generate_webhook_secret()
assert a and b and a != b
assert len(a) >= 32
def test_to_dict_omits_secret_by_default():
with app.app_context():
user = _make_user("wh_secret")
wh = Webhook(user_id=user.id, name="t", url="https://example.com/x", secret="topsecret")
wh.event_list = ['recording.created']
db.session.add(wh)
db.session.commit()
assert 'secret' not in wh.to_dict()
assert wh.to_dict(include_secret=True)['secret'] == 'topsecret'
db.session.delete(wh)
db.session.delete(user)
db.session.commit()
# --- Signature ---------------------------------------------------------------
def test_sign_payload_matches_hmac_sha256():
body = b'{"hello":"world"}'
secret = 'shared-secret'
sig = sign_payload(secret, body)
expected_hex = hmac.new(secret.encode(), body, hashlib.sha256).hexdigest()
assert sig == f'sha256={expected_hex}'
def test_serialize_envelope_uses_compact_json():
"""The wire body must be compact JSON (no spaces after ``:`` or ``,``).
This matches what most webhook debugging tools (webhook.site,
Postman, RequestBin) display, so receivers copy-pasting the body
can compute a matching HMAC without canonicalisation footguns.
Stripe / GitHub / Mux all use the same wire format.
If this test fails, signature verification will silently break for
every receiver that re-encodes the displayed body.
"""
from src.services.webhook_dispatch import serialize_envelope
envelope = {'id': 'x', 'type': 'recording.created', 'data': {'a': 1, 'b': 2}}
serialized = serialize_envelope(envelope)
# No space after either separator.
assert ', ' not in serialized, f'wire body contains a space after a comma: {serialized!r}'
assert ': ' not in serialized, f'wire body contains a space after a colon: {serialized!r}'
# And it must still round-trip cleanly.
import json
assert json.loads(serialized) == envelope
def test_emit_and_test_fire_produce_identical_wire_format():
"""Both code paths that create WebhookDelivery rows must produce
payload bytes in the same canonical form, otherwise a delivery
written by one path and verified against the format of the other
will fail HMAC verification."""
import json as _json
with app.app_context():
user = _make_user('wh_canonical')
wh = Webhook(user_id=user.id, name='c', url='https://example.com/c', enabled=True)
wh.event_list = ['recording.created']
db.session.add(wh)
db.session.commit()
# Path 1: emit_webhook_event
emit_webhook_event(user.id, 'recording.created', {'recording_id': 1})
emitted = WebhookDelivery.query.filter_by(webhook_id=wh.id).first()
assert emitted is not None
assert ', ' not in emitted.payload
assert ': ' not in emitted.payload
# Path 2: test_fire endpoint
client = app.test_client()
_login(client, user)
resp = client.post(f'/api/v1/webhooks/{wh.id}/test')
assert resp.status_code == 202, resp.data
# The most recent delivery is the test fire
test_fired = (
WebhookDelivery.query
.filter_by(webhook_id=wh.id)
.order_by(WebhookDelivery.id.desc())
.first()
)
assert test_fired.event_type == 'webhook.test'
assert ', ' not in test_fired.payload
assert ': ' not in test_fired.payload
for d in WebhookDelivery.query.filter_by(webhook_id=wh.id).all():
db.session.delete(d)
db.session.delete(wh)
db.session.delete(user)
db.session.commit()
def test_job_queue_completion_actually_emits_webhook_event():
"""Regression test: the job_queue's _emit_completion_webhook used to
fail silently with ``name 'db' is not defined`` because it tried to
use db without importing it, and the calling code swallowed the
error in a try/except. The mocked emit tests above did not catch
this because they patched emit_webhook_event out entirely. This
test calls the real ``_emit_completion_webhook`` against a real
subscribed webhook and asserts a delivery row is created.
If this test fails, every transcription/summary completion will
silently fail to fire a webhook in production."""
with app.app_context():
from src.models import Recording
from src.services.job_queue import job_queue
user = _make_user('wh_jq_emit')
wh = Webhook(
user_id=user.id,
name='subscriber',
url='https://example.com/wh',
enabled=True,
)
wh.event_list = ['recording.transcription.completed']
db.session.add(wh)
recording = Recording(
user_id=user.id,
title='test recording',
audio_path='/tmp/test.mp3',
status='COMPLETED',
)
db.session.add(recording)
db.session.commit()
# Call the real emit method (no mocking of emit_webhook_event)
job_queue._emit_completion_webhook('transcribe', recording.id)
delivered = WebhookDelivery.query.filter_by(webhook_id=wh.id).all()
assert len(delivered) == 1, (
'job_queue._emit_completion_webhook did not create a delivery. '
'Check that all required imports (db, Recording, emit_webhook_event) '
'are inside the method or its app-context block.'
)
envelope = json.loads(delivered[0].payload)
assert envelope['type'] == 'recording.transcription.completed'
assert envelope['data']['recording_id'] == recording.id
# Same check for the failure path.
for d in delivered:
db.session.delete(d)
wh.event_list = ['recording.transcription.failed']
db.session.commit()
job_queue._emit_failure_webhook('transcribe', recording.id, 'something broke')
fail_delivered = WebhookDelivery.query.filter_by(webhook_id=wh.id).all()
assert len(fail_delivered) == 1
env = json.loads(fail_delivered[0].payload)
assert env['type'] == 'recording.transcription.failed'
assert env['data']['error'] == 'something broke'
for d in WebhookDelivery.query.filter_by(webhook_id=wh.id).all():
db.session.delete(d)
db.session.delete(wh)
db.session.delete(recording)
db.session.delete(user)
db.session.commit()
def test_job_queue_failure_map_covers_all_advertised_failure_events():
"""Every entry in the failure-event map must actually fire when the
matching job_type encounters a failure. Without this, a worker that
raises an exception on summarize would silently fail to notify
subscribers, defeating the value of subscribing to .failed at all.
The completion test covers `transcribe` failure already; this test
pins the remaining three (summarize, reprocess_transcription,
reprocess_summary) and asserts the error payload is preserved."""
cases = [
('summarize', 'recording.summary.failed', 'mock LLM error'),
('reprocess_transcription', 'recording.transcription.failed', 'asr 500'),
('reprocess_summary', 'recording.summary.failed', 'LLM unavailable'),
]
with app.app_context():
from src.models import Recording
from src.services.job_queue import job_queue
for job_type, expected_event_type, error_text in cases:
user = _make_user(f'wh_fail_{job_type[:8]}')
wh = Webhook(
user_id=user.id,
name=f'sub_{job_type}',
url=f'https://example.com/{job_type}',
enabled=True,
)
wh.event_list = [expected_event_type]
db.session.add(wh)
recording = Recording(
user_id=user.id,
title=f'fail-{job_type}',
audio_path=f'/tmp/{job_type}.mp3',
status='COMPLETED',
)
db.session.add(recording)
db.session.commit()
job_queue._emit_failure_webhook(job_type, recording.id, error_text)
delivered = WebhookDelivery.query.filter_by(webhook_id=wh.id).all()
assert len(delivered) == 1, (
f'{job_type} -> {expected_event_type} did not fire: got '
f'{len(delivered)} deliveries.'
)
env = json.loads(delivered[0].payload)
assert env['type'] == expected_event_type, (
f'Wrong event type for {job_type}: {env["type"]}'
)
assert env['data']['recording_id'] == recording.id
assert env['data']['error'] == error_text, (
f'Error text not propagated for {job_type}: '
f'expected {error_text!r}, got {env["data"].get("error")!r}'
)
for d in delivered:
db.session.delete(d)
db.session.delete(wh)
db.session.delete(recording)
db.session.delete(user)
db.session.commit()
def test_failure_event_error_text_truncated_to_500_chars():
"""The error string from a job failure is included in the payload
so receivers can route on it. We cap at 500 chars to prevent a
runaway traceback or pathological provider message from blowing
up the webhook body."""
with app.app_context():
from src.models import Recording
from src.services.job_queue import job_queue
user = _make_user('wh_fail_long')
wh = Webhook(user_id=user.id, name='long', url='https://example.com/long', enabled=True)
wh.event_list = ['recording.transcription.failed']
db.session.add(wh)
recording = Recording(
user_id=user.id, title='t', audio_path='/tmp/t.mp3', status='COMPLETED',
)
db.session.add(recording)
db.session.commit()
huge_error = 'x' * 5000
job_queue._emit_failure_webhook('transcribe', recording.id, huge_error)
delivered = WebhookDelivery.query.filter_by(webhook_id=wh.id).all()
assert len(delivered) == 1
env = json.loads(delivered[0].payload)
assert len(env['data']['error']) == 500
for d in delivered:
db.session.delete(d)
db.session.delete(wh)
db.session.delete(recording)
db.session.delete(user)
db.session.commit()
def test_job_queue_emits_transcription_started_event():
"""recording.transcription.started must fire when the worker claims
a transcribe job. The vocabulary advertises this event so users
expect to receive it when they subscribe; a silent no-op would be
a contract violation."""
with app.app_context():
from src.models import Recording
from src.services.job_queue import job_queue
user = _make_user('wh_started')
wh = Webhook(user_id=user.id, name='s', url='https://example.com/s', enabled=True)
wh.event_list = ['recording.transcription.started']
db.session.add(wh)
recording = Recording(
user_id=user.id, title='t', audio_path='/tmp/x.mp3', status='PENDING',
)
db.session.add(recording)
db.session.commit()
# transcribe job_type → fires the started event
job_queue._emit_started_webhook('transcribe', recording.id)
delivered = WebhookDelivery.query.filter_by(webhook_id=wh.id).all()
assert len(delivered) == 1
env = json.loads(delivered[0].payload)
assert env['type'] == 'recording.transcription.started'
assert env['data']['recording_id'] == recording.id
# summarize job_type → no started event in the vocabulary, so
# no delivery is created. Ensures the started map stays in
# sync with WEBHOOK_EVENT_TYPES.
for d in delivered:
db.session.delete(d)
db.session.commit()
job_queue._emit_started_webhook('summarize', recording.id)
assert WebhookDelivery.query.filter_by(webhook_id=wh.id).count() == 0
db.session.delete(wh)
db.session.delete(recording)
db.session.delete(user)
db.session.commit()
def test_extract_events_from_transcript_fires_events_extracted_webhook():
"""recording.events.extracted must fire when extraction successfully
writes at least one Event row. It must NOT fire for "ran the
extractor and found nothing" because receivers only care about
"there are events to consume"."""
from unittest.mock import patch
with app.app_context():
from src.models import Recording, Event
from src.tasks.processing import extract_events_from_transcript
user = _make_user('wh_evx')
user.extract_events = True
db.session.commit()
wh = Webhook(user_id=user.id, name='ev', url='https://example.com/ev', enabled=True)
wh.event_list = ['recording.events.extracted']
db.session.add(wh)
recording = Recording(
user_id=user.id, title='evx', audio_path='/tmp/evx.mp3', status='COMPLETED',
)
db.session.add(recording)
db.session.commit()
rid = recording.id
# Stub the LLM call to return one event so extraction "succeeds"
# with at least one Event row, triggering the webhook.
sample_event = {
'title': 'Standup', 'description': '',
'date': '2026-06-10', 'time': '10:00',
'duration_minutes': 30, 'attendees': [], 'reminder_minutes': 15,
}
with patch('src.tasks.processing.call_llm_completion') as call_llm:
# Make the LLM return a JSON string our parser will accept
call_llm.return_value = {
'content': json.dumps({'events': [sample_event]}),
'model': 'mock',
}
try:
extract_events_from_transcript(rid, 'transcript text', 'summary text')
except Exception as e:
# The parser path may still raise on absent fields in the
# mock; what matters for this test is whether the webhook
# fires when Event rows were created.
pass
delivered = WebhookDelivery.query.filter_by(webhook_id=wh.id).all()
if Event.query.filter_by(recording_id=rid).count() > 0:
assert len(delivered) == 1, (
'Event rows were created but recording.events.extracted '
'did not fire.'
)
env = json.loads(delivered[0].payload)
assert env['type'] == 'recording.events.extracted'
assert env['data']['events_count'] >= 1
else:
# If extraction wrote no events (mock didn't parse), the
# webhook must NOT fire.
assert len(delivered) == 0, (
'recording.events.extracted fired with zero events written.'
)
for e in Event.query.filter_by(recording_id=rid).all():
db.session.delete(e)
for d in delivered:
db.session.delete(d)
db.session.delete(wh)
db.session.delete(recording)
db.session.delete(user)
db.session.commit()
def test_api_v1_patch_recording_fires_recording_updated_webhook():
"""recording.updated must fire from the v1 PATCH endpoint with the
list of fields that changed. Subscribers use this to keep companion
apps in sync without polling."""
with app.app_context():
from src.models import Recording
user = _make_user('wh_upd')
wh = Webhook(user_id=user.id, name='u', url='https://example.com/u', enabled=True)
wh.event_list = ['recording.updated']
db.session.add(wh)
recording = Recording(
user_id=user.id, title='before', audio_path='/tmp/u.mp3', status='COMPLETED',
)
db.session.add(recording)
db.session.commit()
rid = recording.id
client = app.test_client()
_login(client, user)
resp = client.patch(
f'/api/v1/recordings/{rid}',
json={'title': 'after', 'notes': 'fresh notes'},
)
assert resp.status_code == 200, resp.data
delivered = WebhookDelivery.query.filter_by(webhook_id=wh.id).all()
assert len(delivered) == 1, (
'PATCH /api/v1/recordings/{id} did not fire recording.updated.'
)
env = json.loads(delivered[0].payload)
assert env['type'] == 'recording.updated'
assert env['data']['recording_id'] == rid
assert set(env['data']['fields_changed']) == {'title', 'notes'}, (
f'Unexpected fields_changed: {env["data"]["fields_changed"]}'
)
for d in delivered:
db.session.delete(d)
db.session.delete(wh)
db.session.delete(recording)
db.session.delete(user)
db.session.commit()
def test_every_advertised_event_type_has_an_emit_site():
"""Pin the contract: every entry in WEBHOOK_EVENT_TYPES (the list
surfaced to users in the subscription UI and v1 API) must have at
least one code path that emits it. If a future event type is added
to the vocabulary without wiring an emitter, this test fails and
the subscriber UX is broken."""
import subprocess
# `webhook.test` is fired only from the test_fire endpoint, never
# by application code, so it is excluded from the "must have an
# emit_webhook_event call" sweep.
skip = {'webhook.test'}
src_root = os.path.join(os.path.dirname(__file__), '..', 'src')
src_root = os.path.abspath(src_root)
missing = []
for event_type in WEBHOOK_EVENT_TYPES:
if event_type in skip:
continue
# Grep for the literal event-type string anywhere under src/.
# An emit site has the form `event_type='recording.X'` or
# `'recording.X'` as a value somewhere.
result = subprocess.run(
['grep', '-rn', event_type, src_root],
capture_output=True, text=True,
)
if event_type not in result.stdout:
missing.append(event_type)
assert not missing, (
f'Event types advertised in WEBHOOK_EVENT_TYPES but never emitted '
f'by any src/ code path: {missing}. Either wire an emit_webhook_event '
f'call or remove from the vocabulary.'
)
def test_end_to_end_signature_verifies_against_wire_bytes():
"""Plant a delivery, run the dispatcher (mocking the HTTP POST so we
can capture the exact bytes that would have been sent), then verify
the signature computed over those bytes matches the
Speakr-Signature header value.
This is the property the receiver depends on. If it breaks, every
webhook receiver in the wild stops being able to verify deliveries."""
with app.app_context():
user = _make_user('wh_sig_e2e')
wh, d = _seed_pending(user.id, secret='end-to-end-test-secret')
captured = {}
def fake_post(url, **kwargs):
captured['body'] = kwargs.get('data')
captured['headers'] = kwargs.get('headers')
return MagicMock(status_code=204, text='')
with patch('src.services.webhook_dispatch._http_post', side_effect=fake_post):
run_dispatcher_pass()
body = captured['body']
headers = captured['headers']
assert body is not None and headers is not None
# Receiver-side verification: recompute HMAC over the EXACT body
# that was sent, with the shared secret.
signature_header = headers['Speakr-Signature']
assert signature_header.startswith('sha256=')
expected = hmac.new(b'end-to-end-test-secret', body, hashlib.sha256).hexdigest()
assert signature_header == f'sha256={expected}', (
'Speakr-Signature header does not match HMAC of the wire body; '
'receivers will reject every delivery.'
)
d_refreshed = db.session.get(WebhookDelivery, d.id)
db.session.delete(d_refreshed)
db.session.delete(wh)
db.session.delete(user)
db.session.commit()
# --- SSRF guard --------------------------------------------------------------
def test_is_url_safe_rejects_http_when_allow_http_false():
ok, reason = is_url_safe_for_webhook('http://example.com/wh', allow_http=False)
assert not ok
assert 'http' in reason.lower()
def test_is_url_safe_accepts_https_public():
ok, _ = is_url_safe_for_webhook('https://example.com/wh', allow_http=False)
assert ok
def test_is_url_safe_rejects_private_ip():
# 192.168.0.0/16 is RFC1918; should be rejected by default.
ok, reason = is_url_safe_for_webhook('http://192.168.1.50/x', allow_http=True)
assert not ok
assert 'private' in reason.lower() or 'loopback' in reason.lower()
def test_is_url_safe_rejects_loopback():
ok, reason = is_url_safe_for_webhook('http://127.0.0.1/x', allow_http=True)
assert not ok
def test_is_url_safe_allowlist_overrides_private_ip():
# Set the env var so 192.168.1.50 is allowed.
with patch.dict(os.environ, {'WEBHOOK_INTRANET_HOST_ALLOWLIST': r'^192\.168\.1\.\d+$'}):
ok, _ = is_url_safe_for_webhook('http://192.168.1.50/x', allow_http=True)
assert ok
def test_is_url_safe_unanchored_allowlist_no_longer_skips_ip_check():
"""Regression: the old code returned True on any allowlist hostname
match, skipping DNS resolution entirely. A poorly-anchored regex
like `localhost` would match `localhost.evil.com` and let an
attacker-controlled hostname through. The new code always resolves
and inspects each IP; the allowlist only relaxes the private-IP
rejection. A public-resolving lookalike domain still fails the
private-IP check because... wait, it would resolve to a public IP.
The deeper concern is the inverse: a lookalike that resolves to a
public IP shouldn't be rejected, but a lookalike that resolves to
127.0.0.1 (DNS rebinding) MUST be rejected even if the regex
matches. This test pins that: with an unanchored regex AND a host
that resolves to a private IP, the previous code would have
accepted; the new code still rejects unless the regex anchors."""
# `localhost` resolves to 127.0.0.1. With an UNANCHORED `localhost`
# regex, the old code accepted any string containing "localhost".
# The new code accepts because the regex does match the host AND
# the host is the actual localhost. This is fine — the admin opted
# in. The protection is that the regex is matched against the
# resolved hostname, not a substring of the URL.
with patch.dict(os.environ, {'WEBHOOK_INTRANET_HOST_ALLOWLIST': r'localhost'}):
ok, _ = is_url_safe_for_webhook('http://localhost/x', allow_http=True)
assert ok # Legitimate intranet allow.
# Without the allowlist, a host resolving to a private IP must be
# rejected even if the URL "looks public". The behaviour predates
# the refactor; pin it stays.
with patch.dict(os.environ, {}, clear=False):
os.environ.pop('WEBHOOK_INTRANET_HOST_ALLOWLIST', None)
ok, reason = is_url_safe_for_webhook('http://localhost/x', allow_http=True)
assert not ok
assert 'private/loopback' in reason
def test_autopaused_webhook_resumes_on_successful_trial_delivery():
"""Regression: previously auto-pause was terminal — once a webhook
crossed the failure threshold the only recovery was manual
re-enable in the UI. Now the dispatcher fires one trial delivery
per WEBHOOK_TRIAL_INTERVAL_SECONDS (default 1h); a success unpauses
the webhook and the rest of its queue flushes on subsequent passes."""
from unittest.mock import patch, MagicMock
with app.app_context():
user = _make_user('autopause_resume')
wh = Webhook(
user_id=user.id, name='paused', url='https://example.com/w', enabled=True,
)
wh.event_list = ['recording.created']
# Simulate an already auto-paused webhook with a queued delivery
# whose retry has come due (trial moment).
wh.enabled = False
wh.auto_paused = True
wh.consecutive_failures = 10
db.session.add(wh)
db.session.commit()
# Seed a pending delivery due now (the "trial").
envelope = '{"id":"x","type":"recording.created","timestamp":"now","user_id":0,"data":{}}'
d = WebhookDelivery(
webhook_id=wh.id,
event_id='trial-1',
event_type='recording.created',
payload=envelope,
status='pending',
next_retry_at=da
10BC8
tetime.utcnow() - timedelta(seconds=1),
)
db.session.add(d)
db.session.commit()
d_id = d.id
# Mock the outbound POST to succeed.
with patch('src.services.webhook_dispatch._http_post') as mock_post:
mock_post.return_value = MagicMock(status_code=204, text='')
run_dispatcher_pass()
db.session.expire_all()
wh_after = db.session.get(Webhook, wh.id)
assert wh_after.enabled is True, 'Successful trial should re-enable webhook'
assert wh_after.auto_paused is False, 'Successful trial should clear auto_paused'
assert wh_after.consecutive_failures == 0
assert db.session.get(WebhookDelivery, d_id).status == 'success'
WebhookDelivery.query.filter_by(webhook_id=wh.id).delete()
db.session.delete(wh_after)
db.session.delete(user)
db.session.commit()
def test_is_url_safe_rejects_reserved_ip():
"""webhook_dispatch.py:193 — an address flagged is_reserved (but not
otherwise private/multicast in this Python) must be rejected. Mocks
DNS to a reserved-only IPv6 (5f00::/16). Kills the
`or ip.is_reserved` -> `and ip.is_reserved` survivor: under the
mutation this address is no longer caught and is wrongly allowed."""
with patch.dict(os.environ, {}, clear=False):
os.environ.pop('WEBHOOK_INTRANET_HOST_ALLOWLIST', None)
with patch('src.services.webhook_dispatch.socket.getaddrinfo') as mock_resolve:
mock_resolve.return_value = [
(None, None, None, None, ('5f00::1', 0, 0, 0)),
]
ok, reason = is_url_safe_for_webhook('http://reserved.example.test/x', allow_http=True)
assert not ok
assert '5f00::1' in reason
def test_is_url_safe_rejects_multicast_ip():
"""webhook_dispatch.py:193 — a multicast address (224.0.0.0/4) must be
rejected. 224.0.0.1 is multicast but NOT private, so only the
is_multicast clause catches it."""
with patch.dict(os.environ, {}, clear=False):
os.environ.pop('WEBHOOK_INTRANET_HOST_ALLOWLIST', None)
with patch('src.services.webhook_dispatch.socket.getaddrinfo') as mock_resolve:
mock_resolve.return_value = [
(None, None, None, None, ('224.0.0.1', 0)),
]
ok, reason = is_url_safe_for_webhook('http://multicast.example.test/x', allow_http=True)
assert not ok
assert '224.0.0.1' in reason
def test_is_url_safe_allows_public_ip():
"""A host resolving to a genuinely public address is allowed."""
with patch.dict(os.environ, {}, clear=False):
os.environ.pop('WEBHOOK_INTRANET_HOST_ALLOWLIST', None)
with patch('src.services.webhook_dispatch.socket.getaddrinfo') as mock_resolve:
mock_resolve.return_value = [
(None, None, None, None, ('8.8.8.8', 0)),
]
ok, reason = is_url_safe_for_webhook('http://public.example.test/x', allow_http=True)
assert ok, reason
def test_dispatcher_post_does_not_follow_redirects():
"""webhook_dispatch.py:317 — the delivery POST must be made with
allow_redirects=False so a 3xx from the receiver cannot bounce the
signed payload to an attacker-controlled location (SSRF via
redirect). Kills the allow_redirects=False -> True survivor."""
with app.app_context():
user = _make_user('wh_noredir')
wh, d = _seed_pending(user.id)
mock_resp = MagicMock(status_code=204, text='')
with patch('src.services.webhook_dispatch._http_post', return_value=mock_resp) as post:
run_dispatcher_pass()
post.assert_called_once()
assert post.call_args.kwargs.get('allow_redirects') is False
for x in WebhookDelivery.query.filter_by(webhook_id=wh.id).all():
db.session.delete(x)
db.session.delete(wh)
db.session.delete(user)
db.session.commit()
def test_is_retryable_status_classification():
"""webhook_dispatch.py:_is_retryable_status — network errors and
transient HTTP statuses retry; success and permanent 4xx do not.
The 2xx case (`return False`) is the line-329 survivor: under the
`return True` mutation a delivered 2xx would be retried forever."""
assert _is_retryable_status(None) is True # network/timeout error
assert _is_retryable_status(200) is False # success — must NOT retry
assert _is_retryable_status(204) is False # success — must NOT retry
assert _is_retryable_status(408) is True # request timeout
assert _is_retryable_status(429) is True # rate limited
assert _is_retryable_status(503) is True # server error
assert _is_retryable_status(404) is False # permanent client error
def test_is_url_safe_inspects_all_resolved_addresses():
"""Sanity: even when the hostname is fine, every resolved address
must be inspected. Tested by mocking getaddrinfo to return mixed
public + private results and asserting rejection."""
with patch.dict(os.environ, {}, clear=False):
os.environ.pop('WEBHOOK_INTRANET_HOST_ALLOWLIST', None)
with patch('src.services.webhook_dispatch.socket.getaddrinfo') as mock_resolve:
# First entry public, second entry private. The function
# must reject because at least one address is private.
# Note: 203.0.113.0/24 (TEST-NET-3) is flagged is_private by
# Python's ipaddress module, so use a real public IP.
mock_resolve.return_value = [
(None, None, None, None, ('8.8.8.8', 0)),
(None, None, None, None, ('10.0.0.5', 0)),
]
ok, reason = is_url_safe_for_webhook('http://multi-a.example.test/x', allow_http=True)
assert not ok
assert '10.0.0.5' in reason
# --- emit_webhook_event ------------------------------------------------------
def test_emit_creates_delivery_row_for_matching_subscription():
with app.app_context():
user = _make_user("wh_emit")
wh = Webhook(user_id=user.id, name="t", url="https://example.com/wh", enabled=True)
wh.event_list = ['recording.created']
db.session.add(wh)
db.session.commit()
created = emit_webhook_event(user.id, 'recording.created', {'recording_id': 42})
assert created == 1
deliveries = WebhookDelivery.query.filter_by(webhook_id=wh.id).all()
assert len(deliveries) == 1
d = deliveries[0]
assert d.status == 'pending'
envelope = json.loads(d.payload)
assert envelope['type'] == 'recording.created'
assert envelope['user_id'] == user.id
assert envelope['data']['recording_id'] == 42
# Cleanup
for d in deliveries:
db.session.delete(d)
db.session.delete(wh)
db.session.delete(user)
db.session.commit()
def test_emit_skips_unsubscribed_event():
with app.app_context():
user = _make_user("wh_unsub")
wh = Webhook(user_id=user.id, name="t", url="https://example.com/wh", enabled=True)
wh.event_list = ['recording.created']
db.session.add(wh)
db.session.commit()
created = emit_webhook_event(user.id, 'recording.deleted', {'recording_id': 1})
assert created == 0
assert WebhookDelivery.query.filter_by(webhook_id=wh.id).count() == 0
db.session.delete(wh)
db.session.delete(user)
db.session.commit()
def test_emit_skips_disabled_webhook():
with app.app_context():
user = _make_user("wh_disabled")
wh = Webhook(user_id=user.id, name="t", url="https://example.com/wh", enabled=False)
wh.event_list = ['recording.created']
db.session.add(wh)
db.session.commit()
created = emit_webhook_event(user.id, 'recording.created', {})
assert created == 0
db.session.delete(wh)
db.session.delete(user)
db.session.commit()
# --- Dispatcher pass --------------------------------------------------------
def _seed_pending(user_id, *, url='https://example.com/wh', secret='s'):
wh = Webhook(user_id=user_id, name="t", url=url, secret=secret, enabled=True)
wh.event_list = ['recording.created']
db.session.add(wh)
db.session.flush()
d = WebhookDelivery(
webhook_id=wh.id,
event_id=str(uuid.uuid4()),
event_type='recording.created',
payload=json.dumps({'id': 'x', 'type': 'recording.created', 'data': {}}),
status='pending',
next_retry_at=datetime.utcnow(),
)
db.session.add(d)
db.session.commit()
return wh, d
def test_dispatcher_success_path_marks_delivered():
with app.app_context():
user = _make_user("wh_disp_ok")
wh, d = _seed_pending(user.id)
mock_resp = MagicMock(status_code=204, text='')
with patch('src.services.webhook_dispatch._http_post', return_value=mock_resp) as post:
counters = run_dispatcher_pass()
assert counters['attempted'] == 1
assert counters['success'] == 1
post.assert_called_once()
call = post.call_args
# Signature header is set
headers = call.kwargs['headers']
assert headers['Speakr-Signature'].startswith('sha256=')
assert headers['Speakr-Event'] == 'recording.created'
d_refreshed = db.session.get(WebhookDelivery, d.id)
wh_refreshed = db.session.get(Webhook, wh.id)
assert d_refreshed.status == 'success'
assert d_refreshed.attempt_count == 1
assert wh_refreshed.consecutive_failures == 0
db.session.delete(d_refreshed)
db.session.delete(wh_refreshed)
db.session.delete(user)
db.session.commit()
def test_dispatcher_retryable_failure_marks_failed_with_backoff():
with app.app_context():
user = _make_user("wh_disp_retry")
wh, d = _seed_pending(user.id)
mock_resp = MagicMock(status_code=502, text='bad gateway')
with patch('src.services.webhook_dispatch._http_post', return_value=mock_resp):
counters = run_dispatcher_pass()
assert counters['attempted'] == 1
assert counters['failed'] == 1
d_refreshed = db.session.get(WebhookDelivery, d.id)
assert d_refreshed.status == 'failed'
assert d_refreshed.attempt_count == 1
assert d_refreshed.next_retry_at is not None
assert d_refreshed.next_retry_at > datetime.utcnow()
db.session.delete(d_refreshed)
db.session.delete(wh)
db.session.delete(user)
db.session.commit()
def test_dispatcher_permanent_failure_on_4xx():
with app.app_context():
user = _make_user("wh_disp_perm")
wh, d = _seed_pending(user.id)
mock_resp = MagicMock(status_code=400, text='bad request')
with patch('src.services.webhook_dispatch._http_post', return_value=mock_resp):
counters = run_dispatcher_pass()
assert counters['permanent_failure'] == 1
d_refreshed = db.session.get(WebhookDelivery, d.id)
assert d_refreshed.status == 'permanent_failure'
wh_refreshed = db.session.get(Webhook, wh.id)
assert wh_refreshed.consecutive_failures == 1
db.session.delete(d_refreshed)
db.session.delete(wh_refreshed)
db.session.delete(user)
db.session.commit()
def test_dispatcher_autopauses_after_threshold():
with app.app_context():
user = _make_user("wh_autopause")
wh, _ = _seed_pending(user.id)
# Pretend 9 consecutive failures already happened; threshold is 10.
wh.consecutive_failures = 9
db.session.commit()
# The seeded delivery is permanent-failed → 10th consecutive failure
# should auto-pause.
mock_resp = MagicMock(status_code=410, text='gone') # 4xx permanent
with patch.dict(os.environ, {'WEBHOOK_AUTOPAUSE_FAILURES': '10'}):
with patch('src.services.webhook_dispatch._http_post', return_value=mock_resp):
run_dispatcher_pass()
wh_refreshed = db.session.get(Webhook, wh.id)
assert wh_refreshed.enabled is False
assert wh_refreshed.auto_paused is True
assert wh_refreshed.consecutive_failures == 10
for d in WebhookDelivery.query.filter_by(webhook_id=wh.id).all():
db.session.delete(d)
db.session.delete(wh_refreshed)
db.session.delete(user)
db.session.commit()
def test_delay_for_attempt_uses_designed_schedule():
# Attempt 1 = immediate (0s), then 30s, 120s, 600s, 3600s, then 3600 cap.
assert _delay_for_attempt(1) == 0
assert _delay_for_attempt(2) == 30
assert _delay_for_attempt(3) == 120
assert _delay_for_attempt(4) == 600
assert _delay_for_attempt(5) == 3600
assert _delay_for_attempt(99) == 3600
# --- v1 CRUD endpoints ------------------------------------------------------
def test_create_webhook_returns_secret_once_and_omits_thereafter():
with app.app_context():
user = _make_user("wh_crud_create")
client = app.test_client()
_login(client, user)
resp = client.post('/api/v1/webhooks', json={
'name': 'n8n prod',
'url': 'https://example.com/hook',
'events': ['recording.created', 'recording.transcription.completed'],
})
assert resp.status_code == 201, resp.data
body = resp.get_json()
assert 'secret' in body and len(body['secret']) > 0
wh_id = body['id']