Skip to content

Commit b46ac62

Browse files
committed
fixes
1 parent 6f8330d commit b46ac62

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

examples/webhooks.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,48 @@
1717
def mock_webhook_event() -> Tuple[Dict[str, Any], str, str]:
1818
"""Mock a webhook event."""
1919

20-
timestamp = datetime.fromisoformat("2023-01-01T00:00:00")
20+
timestamp = datetime.fromisoformat("2023-01-01T00:00:00").isoformat()
21+
2122
payload = WebhookAgentTaskStatusUpdatePayload(
2223
session_id="sess_123",
2324
task_id="task_123",
2425
status="started",
2526
metadata={"progress": 25},
2627
)
28+
2729
signature = create_webhook_signature(
28-
payload=payload.model_dump(), timestamp="2023-01-01T00:00:00", secret="your-webhook-secret-key"
30+
payload=payload.model_dump(),
31+
timestamp=timestamp,
32+
secret=SECRET,
2933
)
3034

3135
evt: Webhook = WebhookAgentTaskStatusUpdate(
3236
type="agent.task.status_update",
33-
timestamp=timestamp,
37+
timestamp=datetime.fromisoformat("2023-01-01T00:00:00"),
3438
payload=payload,
3539
)
3640

37-
return evt.model_dump(), signature, timestamp.isoformat()
41+
return evt.model_dump(), signature, timestamp
3842

3943

4044
def main() -> None:
4145
"""Demonstrate webhook functionality."""
4246

4347
# NOTE: You'd get the evt and signature from the webhook request body and headers!
4448
evt, signature, timestamp = mock_webhook_event()
49+
4550
verified_webhook = verify_webhook_event_signature(
4651
body=evt,
4752
signature=signature,
4853
timestamp=timestamp,
4954
secret=SECRET,
5055
)
5156

52-
if verified_webhook is not None:
57+
if verified_webhook is None:
58+
print("✗ Webhook signature verification failed")
59+
else:
5360
print("✓ Webhook signature verified successfully")
5461
print(f" Event type: {verified_webhook.type}")
55-
else:
56-
print("✗ Webhook signature verification failed")
5762

5863

5964
if __name__ == "__main__":

src/browser_use_sdk/lib/webhooks.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ def create_webhook_signature(payload: Any, timestamp: str, secret: str) -> str:
6565
Returns:
6666
The HMAC-SHA256 signature as a hex string
6767
"""
68-
# Sort keys and use compact JSON format (equivalent to fast-json-stable-stringify)
68+
6969
dump = json.dumps(payload, separators=(",", ":"), sort_keys=True)
7070
message = f"{timestamp}.{dump}"
7171

7272
# Create HMAC-SHA256 signature
7373
hmac_obj = hmac.new(secret.encode(), message.encode(), hashlib.sha256)
74-
return hmac_obj.hexdigest()
74+
signature = hmac_obj.hexdigest()
75+
76+
return signature
7577

7678

7779
def verify_webhook_event_signature(
@@ -94,20 +96,20 @@ def verify_webhook_event_signature(
9496
None if the signature is invalid, otherwise the parsed webhook event.
9597
"""
9698
try:
97-
# Parse body if it's a string
9899
if isinstance(body, str):
99100
json_data = json.loads(body)
100101
else:
101102
json_data = body
102103

103-
# Try to parse as each webhook type
104+
# PARSE
105+
104106
webhook_event: Optional[Webhook] = None
105107

106-
# Try test webhook first
107-
try:
108-
webhook_event = WebhookTest(**json_data)
109-
except Exception:
110-
pass
108+
if webhook_event is None:
109+
try:
110+
webhook_event = WebhookTest(**json_data)
111+
except Exception:
112+
pass
111113

112114
# Try agent task status update webhook
113115
if webhook_event is None:
@@ -119,10 +121,12 @@ def verify_webhook_event_signature(
119121
if webhook_event is None:
120122
return None
121123

122-
# Create expected signature
123-
expected_signature = create_webhook_signature(payload=webhook_event.payload, timestamp=timestamp, secret=secret)
124+
# Verify
125+
126+
expected_signature = create_webhook_signature(
127+
payload=webhook_event.payload.model_dump(), timestamp=timestamp, secret=secret
128+
)
124129

125-
# Compare signatures using timing-safe comparison
126130
if not hmac.compare_digest(signature, expected_signature):
127131
return None
128132

0 commit comments

Comments
 (0)